Skip to content

Commit

Permalink
src/goLanguageServer: replace [] foldingRange response with undefined
Browse files Browse the repository at this point in the history
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 #1224
Updates golang/go#41281

Change-Id: I32cfb30d7e6a9874b9d1cb6c7e5309f19ee081e5
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/299569
Trust: Hyang-Ah Hana Kim <[email protected]>
Run-TryBot: Hyang-Ah Hana Kim <[email protected]>
TryBot-Result: kokoro <[email protected]>
Reviewed-by: Rebecca Stambler <[email protected]>
  • Loading branch information
hyangah committed Mar 8, 2021
1 parent 80f450f commit f6391a2
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/goLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ import {
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;
Expand Down Expand Up @@ -495,6 +497,18 @@ export async function buildLanguageClient(cfg: BuildLanguageClientOption): Promi
}
},
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,
Expand Down

0 comments on commit f6391a2

Please sign in to comment.