Skip to content

Commit 934911a

Browse files
committed
src/goLanguageServer: cache language server info
LSP initialize response includes useful info about the gopls server. Cache the gopls name, version, go version, and implemented commands list. Change-Id: Ibe4f63fb93eca0b27a927f9a446d11698bec09dd Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/385674 Trust: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Suzy Mueller <[email protected]> TryBot-Result: kokoro <[email protected]>
1 parent 28136c8 commit 934911a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/goLanguageServer.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
ExecuteCommandSignature,
2626
HandleDiagnosticsSignature,
2727
InitializeError,
28+
InitializeResult,
2829
Message,
2930
ProvideCodeLensesSignature,
3031
ProvideCompletionItemsSignature,
@@ -94,12 +95,24 @@ export interface LanguageServerConfig {
9495
// Global variables used for management of the language client.
9596
// They are global so that the server can be easily restarted with
9697
// new configurations.
98+
// TODO: refactor it. These can be encapsulated in a single LanguageServer class
99+
// that keeps track of the state of the active language server instance.
97100
export let languageClient: LanguageClient;
98101
let languageServerDisposable: vscode.Disposable;
99102
export let latestConfig: LanguageServerConfig;
100103
export let serverOutputChannel: vscode.OutputChannel;
101104
export let languageServerIsRunning = false;
102105

106+
// serverInfo is the information from the server received during initialization.
107+
export let serverInfo: ServerInfo = undefined;
108+
109+
interface ServerInfo {
110+
Name: string;
111+
Version?: string;
112+
GoVersion?: string;
113+
Commands?: string[];
114+
}
115+
103116
const languageServerStartMutex = new Mutex();
104117

105118
let serverTraceChannel: vscode.OutputChannel;
@@ -451,9 +464,36 @@ async function startLanguageServer(ctx: vscode.ExtensionContext, config: Languag
451464
languageServerDisposable = languageClient.start();
452465
ctx.subscriptions.push(languageServerDisposable);
453466
await languageClient.onReady();
467+
serverInfo = toServerInfo(languageClient.initializeResult);
468+
469+
console.log(`Server: ${JSON.stringify(serverInfo, null, 2)}`);
454470
return true;
455471
}
456472

473+
function toServerInfo(res?: InitializeResult): ServerInfo | undefined {
474+
if (!res) return undefined;
475+
476+
const info: ServerInfo = {
477+
Commands: res.capabilities?.executeCommandProvider?.commands || [],
478+
Name: res.serverInfo?.name || 'unknown'
479+
};
480+
481+
try {
482+
interface serverVersionJSON {
483+
GoVersion?: string;
484+
Version?: string;
485+
// before gopls 0.8.0
486+
version?: string;
487+
}
488+
const v = <serverVersionJSON>(res.serverInfo?.version ? JSON.parse(res.serverInfo.version) : {});
489+
info.Version = v.Version || v.version;
490+
info.GoVersion = v.GoVersion;
491+
} catch (e) {
492+
// gopls is not providing any info, that's ok.
493+
}
494+
return info;
495+
}
496+
457497
export interface BuildLanguageClientOption extends LanguageServerConfig {
458498
outputChannel?: vscode.OutputChannel;
459499
traceOutputChannel?: vscode.OutputChannel;

0 commit comments

Comments
 (0)