src/goLanguageServer: replace [] foldingRange response with undefined

In the presence of parse errors, gopls may fail to compute
foldingRanges correctly and return only partial results.
Partial results can confuse editors and cause to drop still
valid, previously known folding ranges information.

Currently, LSP does not allow a way for the server to tell
clients the server is not ready for answering to the request.
VSCode foldingRange API allows registered foldingRange
provider to opt out of the folding range computation by
providing undefined.

https://code.visualstudio.com/api/references/vscode-api#FoldingRangeProvider
"Returns a list of folding ranges or null and undefined
if the provider does not want to participate or was cancelled."

But, LSP currently does not disginguish undefined/null and
empty results. Raising an error may be an option, but some
LSP clients may not handle errors in user-friendly ways. Thus
gopls devs want to avoid raising errors here.

With https://go-review.googlesource.com/c/tools/+/291569, gopls
will return an empty foldingRange response when encountering
parse errors instead of returning incomplete results.
This CL adds provideFoldingRanges in the middleware, that
converts an empty foldingRange response with 'undefined'
if the document is not empty.

Manually tested.
Testing it in the integration test setup is currently tricky
until gopls with the change is released.

Updates golang/vscode-go#1224
Updates golang/go#41281

Change-Id: I32cfb30d7e6a9874b9d1cb6c7e5309f19ee081e5
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/299569
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/src/goLanguageServer.ts b/src/goLanguageServer.ts
index b534127..1950974 100644
--- a/src/goLanguageServer.ts
+++ b/src/goLanguageServer.ts
@@ -69,6 +69,8 @@
 import { Mutex } from './utils/mutex';
 import { getToolFromToolPath } from './utils/pathUtils';
 import WebRequest = require('web-request');
+import { FoldingContext } from 'vscode';
+import { ProvideFoldingRangeSignature } from 'vscode-languageclient/lib/common/foldingRange';
 
 export interface LanguageServerConfig {
 	serverName: string;
@@ -495,6 +497,18 @@
 				}
 			},
 			middleware: {
+				provideFoldingRanges: async (
+					doc: vscode.TextDocument,
+					context: FoldingContext,
+					token: CancellationToken,
+					next: ProvideFoldingRangeSignature
+				) => {
+					const ranges = await next(doc, context, token);
+					if ((!ranges || ranges.length === 0) && doc.lineCount > 0) {
+						return undefined;
+					}
+					return ranges;
+				},
 				provideCodeLenses: async (
 					doc: vscode.TextDocument,
 					token: vscode.CancellationToken,