-
Notifications
You must be signed in to change notification settings - Fork 646
When using go-languageserver, also use code completion feature #1607
Changes from 6 commits
ad7b9f6
6554ee6
55901d3
f2d7910
f901c06
4216a5a
02ba529
00aae74
5bba166
e59e7c1
c2df1f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -267,7 +267,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 :).'); | ||
|
@@ -370,27 +370,42 @@ function getMissingTools(goVersion: SemVersion): Promise<string[]> { | |
} | ||
|
||
// If langserver needs to be used, but is not installed, this will prompt user to install and Reload | ||
// If langserver needs to be used, and is installed, this will return true | ||
// Returns false in all other cases | ||
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[] { | ||
let latestGoConfig = vscode.workspace.getConfiguration('go'); | ||
if (!latestGoConfig['useLanguageServer']) return false; | ||
if (!latestGoConfig['useLanguageServer']) return null; | ||
|
||
if (process.platform === 'win32') { | ||
vscode.window.showInformationMessage('The Go language server is not supported on Windows yet.'); | ||
return false; | ||
return null; | ||
} | ||
if (!allFoldersHaveSameGopath()) { | ||
vscode.window.showInformationMessage('The Go language server is not supported in a multi root set up with different GOPATHs.'); | ||
return false; | ||
return null; | ||
} | ||
|
||
let langServerAvailable = getBinPath('go-langserver') !== 'go-langserver'; | ||
if (!langServerAvailable) { | ||
promptForMissingTool('go-langserver'); | ||
vscode.window.showInformationMessage('Reload VS Code window after installing the Go language server'); | ||
return null; | ||
} | ||
return langServerAvailable; | ||
|
||
// we execute the languageserver using -help so that it will fail and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
||
// return the list of supported flags so consumers know what features | ||
// can be supported | ||
return helpText.split('\n') | ||
.filter(line => line.trim().startsWith('-')) | ||
.map(flag => flag.split(' ')[0]); | ||
} | ||
|
||
function allFoldersHaveSameGopath(): boolean { | ||
|
@@ -401,8 +416,3 @@ function allFoldersHaveSameGopath(): boolean { | |
let tempGopath = getCurrentGoPath(vscode.workspace.workspaceFolders[0].uri); | ||
return vscode.workspace.workspaceFolders.find(x => tempGopath !== getCurrentGoPath(x.uri)) ? false : true; | ||
} | ||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,14 +87,24 @@ export function activate(ctx: vscode.ExtensionContext): void { | |
ctx.globalState.update('goroot', currentGoroot); | ||
|
||
offerToInstallTools(); | ||
let langServerAvailable = checkLanguageServer(); | ||
if (langServerAvailable) { | ||
let langServerFlags: string[] = vscode.workspace.getConfiguration('go')['languageServerFlags'] || []; | ||
let supportedLangServerFlags = checkLanguageServer(); | ||
if (supportedLangServerFlags) { | ||
let configuredLangServerFlags: string[] = vscode.workspace.getConfiguration('go')['languageServerFlags'] || []; | ||
configuredLangServerFlags = [ | ||
...configuredLangServerFlags, | ||
'-gocodecompletion', | ||
`-func-snippet-enabled=${vscode.workspace.getConfiguration('go')['useCodeSnippetsOnFunctionSuggest']}` | ||
]; | ||
|
||
let applicableFlags = configuredLangServerFlags.filter(f => { | ||
return supportedLangServerFlags.some(supported => f.startsWith(supported)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because for example the supported flags will contain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! Makes sense |
||
}); | ||
|
||
const c = new LanguageClient( | ||
'go-langserver', | ||
{ | ||
command: getBinPath('go-langserver'), | ||
args: ['-mode=stdio', ...langServerFlags], | ||
args: ['-mode=stdio', ...applicableFlags], | ||
options: { | ||
env: getToolsEnvVars() | ||
} | ||
|
@@ -110,8 +120,15 @@ export function activate(ctx: vscode.ExtensionContext): void { | |
} | ||
); | ||
|
||
c.onReady().then(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already a |
||
if (c.initializeResult && c.initializeResult.capabilities && !c.initializeResult.capabilities.completionProvider) { | ||
ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would get inside this Same for the format feature. |
||
} | ||
}); | ||
|
||
ctx.subscriptions.push(c.start()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
} 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())); | ||
|
@@ -133,7 +150,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())); | ||
|
There was a problem hiding this comment.
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