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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The Go extension is ready to use on the get go. If you want to customize the fea

The Go extension uses a host of Go tools to provide the various language features. An alternative is to use a single language server that provides the same feature.

Set `go.useLanguageServer` to `true` to use the Go language server from [Sourcegraph](https://github.com/sourcegraph/go-langserver) for features like Hover, Definition, Find All References, Signature Help, Go to Symbol in File and Workspace.
Set `go.useLanguageServer` to `true` to use the Go language server from [Sourcegraph](https://github.com/sourcegraph/go-langserver) for features like Code completion, Hover, Definition, Find All References, Signature Help, Go to Symbol in File and Workspace.
* This is an experimental feature and is not available in Windows yet.
* If set to true, you will be prompted to install the Go language server. Once installed, you will have to reload VS Code window. The language server will then be run by the Go extension in the background to provide services needed for the above mentioned features.
* Everytime you change the value of the setting `go.useLanguageServer`, you need to reload the VS Code window for it to take effect.
Expand Down
26 changes: 19 additions & 7 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function installTools(goVersion: SemVersion, missing?: string[]) {
outputChannel.appendLine(''); // Blank line for spacing
let failures = res.filter(x => x != null);
if (failures.length === 0) {
if (missing.indexOf('langserver-go') > -1) {
if (missing.indexOf('go-langserver') > -1) {
outputChannel.appendLine('Reload VS Code window to use the Go language server');
}
outputChannel.appendLine('All tools successfully installed. You\'re ready to Go :).');
Expand Down Expand Up @@ -383,8 +383,25 @@ export function checkLanguageServer(): boolean {
if (!langServerAvailable) {
promptForMissingTool('go-langserver');
vscode.window.showInformationMessage('Reload VS Code window after installing the Go language server');
return langServerAvailable;
}
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

// print all the available flags
let helpText = '';
try {
helpText = cp.execFileSync(getBinPath('go-langserver'), ['-help']).toString();
} catch (err) {
helpText = (<cp.SpawnSyncReturns<Buffer>>err).stderr.toString();
}
let langserverSupportsCompletion = helpText.includes('-gocodecompletion');

if (!langserverSupportsCompletion) {
promptForUpdatingTool('go-langserver');
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?

}

function allFoldersHaveSameGopath(): boolean {
Expand All @@ -395,8 +412,3 @@ function allFoldersHaveSameGopath(): boolean {
let tempGopath = getCurrentGoPath(vscode.workspace.workspaceFolders[0].uri);
return vscode.workspace.workspaceFolders.find(x => tempGopath !== getCurrentGoPath(x.uri)) ? false : true;
}





4 changes: 2 additions & 2 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

},
{
documentSelector: ['go'],
Expand All @@ -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(), '.', '\"'));
				}
			});

} else {
ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
ctx.subscriptions.push(vscode.languages.registerHoverProvider(GO_MODE, new GoHoverProvider()));
ctx.subscriptions.push(vscode.languages.registerDefinitionProvider(GO_MODE, new GoDefinitionProvider()));
ctx.subscriptions.push(vscode.languages.registerReferenceProvider(GO_MODE, new GoReferenceProvider()));
Expand All @@ -130,7 +131,6 @@ export function activate(ctx: vscode.ExtensionContext): void {
let testCodeLensProvider = new GoRunTestCodeLensProvider();
let referencesCodeLensProvider = new GoReferencesCodeLensProvider();

ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(GO_MODE, new GoDocumentFormattingEditProvider()));
ctx.subscriptions.push(vscode.languages.registerRenameProvider(GO_MODE, new GoRenameProvider()));
ctx.subscriptions.push(vscode.languages.registerCodeActionsProvider(GO_MODE, new GoCodeActionProvider()));
Expand Down