Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

When using go-languageserver, also use code completion feature #1607

Merged
merged 11 commits into from
May 27, 2018
Merged

When using go-languageserver, also use code completion feature #1607

merged 11 commits into from
May 27, 2018

Conversation

m90
Copy link
Contributor

@m90 m90 commented Apr 5, 2018

Instead of using go-code, use go-languageserver for providing
code completion results when enabled. Solves #1593

Instead of using go-code, use go-languageserver for providing
code completion results when enabled. Solves #1593
@m90
Copy link
Contributor Author

m90 commented Apr 5, 2018

I wonder if there is some way for the extension to know if the installed version of go-languageserver is recent enough to support the flag?

In case executing the binary with an unknown flag does not yield
the -gocodecompletion flag in the help output, we assume the version
installed is out of date and prompt for updating
}
return langServerAvailable;

// we execute the languageserver using -help so that it will fail and
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty terrible, but it's the best I could come up with in case we want to make sure an up-to-date version of the language server is installed.

Alternatively we could call go-langserver -version, but I don't really know what do to with the output as it would currently just print sth like v2-dev: https://github.com/sourcegraph/go-langserver/blob/dd4c619b53ae9d18c94e372d23d8710e59f0766b/main.go#L41

@OneOfOne
Copy link
Contributor

OneOfOne commented May 1, 2018

This doesn't seem to work at all, I can see go-langserver -mode=stdio -gocodecompletion running but there are no completions and nothing in the console.

@OneOfOne
Copy link
Contributor

OneOfOne commented May 1, 2018

Ignore my previous comment, this is related to nsf/gocode#510, I opened a ticket sourcegraph/go-langserver#268.

I can confirm this works perfectly with go 1.10.

@ramya-rao-a ping

@primalmotion
Copy link
Contributor

works like a charm for me

@m90
Copy link
Contributor Author

m90 commented May 14, 2018

@ramya-rao-a Any input on this one?

@ramya-rao-a
Copy link
Contributor

@m90 Do you know the reason why completions are not supported by default by the language server?
Is it because of the memory burden? The help for the -gocodecompletion flag has the comment enable completion (extra memory burden).

If this indeed is the case, then I'd prefer for users to opt-in to use the completions from the language server instead of us providing as a default.

cc @keegancsmith and @slimsag for input on why completions are not supported by default by the language server.

vscode.window.showInformationMessage('Reload VS Code window after updating the Go language server');
}

return langserverSupportsCompletion;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, checkLanguageServer will return false for users with older version of the language server stopping them from using the language server altogether.

Instead, create a new function getLanguageServerFlags that returns all supported flags. Then in goMain, you can use the result to pass the appropriate flags. This will help in the future when we have other flags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, create a new function getLanguageServerFlags that returns all supported flags.

Just to double check: for getLanguageServerFlags you envision sth that reads the output from calling the naked command just above? Or do we already do sth similar elsewhere?

src/goMain.ts Outdated
@@ -96,7 +96,7 @@ export function activate(ctx: vscode.ExtensionContext): void {
'go-langserver',
{
command: getBinPath('go-langserver'),
args: ['-mode=stdio', ...langServerFlags],
args: ['-mode=stdio', '-gocodecompletion', ...langServerFlags],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass the func-snippet-enabled flag as per the setting go.useCodeSnippetsOnFunctionSuggest

src/goMain.ts Outdated
@@ -110,6 +110,7 @@ export function activate(ctx: vscode.ExtensionContext): void {

ctx.subscriptions.push(c.start());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since its possible for users to run the older versions of the language server where completions are not supported, add the below to register the completion provider from the extension

c.onReady().then(() => {
				if (c.initializeResult && c.initializeResult.capabilities && !c.initializeResult.capabilities.completionProvider) {
					ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
				}
			});

@m90
Copy link
Contributor Author

m90 commented May 15, 2018

@ramya-rao-a That's a good question and a valid concern. I do have no idea why completion is behind a flag though. All I know is that when running locally it feels faster and more lightweight, but then again that's very likely to be very subjective.

Maybe the lovely Sourcegraph people know why it is the way it is?

@m90
Copy link
Contributor Author

m90 commented May 15, 2018

@ramya-rao-a I think this should reflect the requested changes now, feel free to check and let me know if you feel it'd be nicer to put this behind a preference.

src/goMain.ts Outdated
];

let applicableFlags = configuredLangServerFlags.filter(f => {
return supportedLangServerFlags.some(supported => f.startsWith(supported));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why startsWith and not === ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because for example the supported flags will contain -func-snippet-enabled but we also pass a value like -func-snippet-enabled=false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Makes sense

@ramya-rao-a
Copy link
Contributor

I've logged a proposal for the language server to use the initialziationOptions instead of flags to drive features. See sourcegraph/go-langserver#280

Let's wait on what our friends at sourcegraph have to say there before moving forward here

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented May 22, 2018

@m90 Can you update this PR as per the below points?

  • Merge from master. There has been quite some changes there.
  • Use the InitializationOptions to enable the completion feature instead of the flags. See Use InitializationOptions instead of flags to drive capabilities sourcegraph/go-langserver#280
  • Include auto-complete as an experimental feature in the go.languageServerExperimentalFeatures setting such that users can disable this feature but still use the language server. This is helpful as completions for unimported packages and symbols from unimported packages are not yet supported by the language server

@m90
Copy link
Contributor Author

m90 commented May 22, 2018

@ramya-rao-a Sure thing. When are you planning to do the next release of the extension? I'm a little swamped right now, so it might take until the weekend or so.

@ramya-rao-a
Copy link
Contributor

@m90 In that case, no worries. I can make the changes in your branch and create a vsix file. You can then install that vsix and help with testing?

@primalmotion
Copy link
Contributor

I can test if you like. I’m on that branch anyway

@m90
Copy link
Contributor Author

m90 commented May 22, 2018

@ramya-rao-a Sounds good, thanks. Let me know when I can give things a test drive.

@m90
Copy link
Contributor Author

m90 commented May 25, 2018

@ramya-rao-a So I have been able to unswamp myself and would be happy to still do the requested updates in the coming days. Does that sound like a plan to you or have you already started updating things?

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented May 25, 2018

@m90 I havent started :) so go ahead make the updates

@m90
Copy link
Contributor Author

m90 commented May 26, 2018

@ramya-rao-a I pushed the requested changes. Tbh I am not entirely sure how the entire graceful fallback mechanism works now when someone has an older version of go-langserver installed, so in case I missed something here, let me know and I will update accordingly. With an up-to-date language server it seems to work like a charm for me.

Copy link
Contributor

@ramya-rao-a ramya-rao-a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main comment in the PR is that we can revert the changes made to support the flag and assume that the user has the latest version of the language server if they have enabled the auto-complete feature.

Other than that, just small changes needed. Thats all.

Thanks for all your work on this!

export function checkLanguageServer(): boolean {
// If langserver needs to be used, and is installed, this will return the list of supported flags
// Returns null in all other cases
export function checkLanguageServer(): string[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep things simple, lets go with the assumption that the auto-completion feature from language server is available only if the user has the latest language server i.e if the user's version of the language server respects the initalizationOptions sent.

Towards that end, we can revert the changes made to checkLanguageServer here

src/goMain.ts Outdated
@@ -109,7 +117,7 @@ export function activate(ctx: vscode.ExtensionContext): void {
revealOutputChannelOn: RevealOutputChannelOn.Never,
middleware: {
provideDocumentFormattingEdits: (document: vscode.TextDocument, options: FormattingOptions, token: vscode.CancellationToken, next: ProvideDocumentFormattingEditsSignature) => {
if (languageServerExperimentalFeatures['format'] === true) {
if (languageServerExperimentalFeatures['format']) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I specifically check for true here as user can provide anything in the settings and that value will make it through.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't allowed to use a non-boolean value when testing, which is why thought this was possible, but maybe that's only a warning? Undid this in any case.

src/goMain.ts Outdated
@@ -118,6 +126,12 @@ export function activate(ctx: vscode.ExtensionContext): void {
}
);

c.onReady().then(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a c.onReady() below, can you combine this with that?

src/goMain.ts Outdated
@@ -118,6 +126,12 @@ export function activate(ctx: vscode.ExtensionContext): void {
}
);

c.onReady().then(() => {
if (c.initializeResult && c.initializeResult.capabilities && !c.initializeResult.capabilities.completionProvider) {
ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would get inside this if block when the user had enabled the languageServerExperimentalFeatures['autoComplete'] setting, but the language server is old as well. In this case, we should notify the user that their version of the language server is old and does not support the auto-complete feature.

Same for the format feature.

src/goMain.ts Outdated
const capabilities = c.initializeResult && c.initializeResult.capabilities;
if (capabilities && !capabilities.completionProvider) {
ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
vscode.window.showInformationMessage('Your installed version of `go-langserver` is out of date and does not support code completion. Falling back to default behavior.');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ramya-rao-a Is this an ok phrasing or would you also prompt the user to upgrade? It might also make sense to show a single generic message in case any fallback condition inside this function body was triggered.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m90 I'll look into the update option later. I've pushed a commit to use generic message

src/goMain.ts Outdated
ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
vscode.window.showInformationMessage('Your installed version of `go-langserver` is out of date and does not support code completion. Falling back to default behavior.');
}

if (!languageServerExperimentalFeatures['format'] ||
Copy link
Contributor Author

@m90 m90 May 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this check for !== true then, too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've pushed a commit to fix it

@ramya-rao-a ramya-rao-a merged commit 372b0fe into microsoft:master May 27, 2018
@m90 m90 deleted the server-completion branch May 27, 2018 18:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants