Skip to content

Commit

Permalink
goLanguageServer: turn on all experiments in the Nightly
Browse files Browse the repository at this point in the history
For users of the Go Nightly extension and gopls/v0.5.2 and greater,
enable "allExperiments" by default. Users can still provide their own
overrides, and they can still set the "allExperiments" flag to false
manually.

Fixes #818

Change-Id: Ife0db71e6a1cb7472cfe2f18ca5a3d4aa2987697
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/264317
Run-TryBot: Rebecca Stambler <[email protected]>
TryBot-Result: kokoro <[email protected]>
Trust: Rebecca Stambler <[email protected]>
Trust: Hyang-Ah Hana Kim <[email protected]>
Reviewed-by: Suzy Mueller <[email protected]>
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
  • Loading branch information
stamblerre committed Oct 28, 2020
1 parent 6f8699c commit 408d199
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
79 changes: 65 additions & 14 deletions src/goLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ import semver = require('semver');
import util = require('util');
import vscode = require('vscode');
import {
CancellationToken,
CloseAction,
CompletionItemKind,
ConfigurationParams,
ConfigurationRequest,
ErrorAction,
HandleDiagnosticsSignature,
InitializeError,
Message,
ProvideCodeLensesSignature,
ProvideCompletionItemsSignature,
ProvideDocumentLinksSignature,
ResponseError,
RevealOutputChannelOn
} from 'vscode-languageclient';
import {
Expand Down Expand Up @@ -57,6 +61,7 @@ import { getToolFromToolPath } from './utils/pathUtils';
interface LanguageServerConfig {
serverName: string;
path: string;
version: string;
modtime: Date;
enabled: boolean;
flags: string[];
Expand Down Expand Up @@ -171,7 +176,7 @@ async function startLanguageServer(ctx: vscode.ExtensionContext, config: Languag
// Track the latest config used to start the language server,
// and rebuild the language client.
latestConfig = config;
languageClient = buildLanguageClient(config);
languageClient = await buildLanguageClient(config);
crashCount = 0;
}

Expand Down Expand Up @@ -201,27 +206,28 @@ async function startLanguageServer(ctx: vscode.ExtensionContext, config: Languag
return true;
}

function buildLanguageClient(config: LanguageServerConfig): LanguageClient {
async function buildLanguageClient(cfg: LanguageServerConfig): Promise<LanguageClient> {
// Reuse the same output channel for each instance of the server.
if (config.enabled) {
if (cfg.enabled) {
if (!serverOutputChannel) {
serverOutputChannel = vscode.window.createOutputChannel(config.serverName + ' (server)');
serverOutputChannel = vscode.window.createOutputChannel(cfg.serverName + ' (server)');
}
if (!serverTraceChannel) {
serverTraceChannel = vscode.window.createOutputChannel(config.serverName);
serverTraceChannel = vscode.window.createOutputChannel(cfg.serverName);
}
}
const goplsConfig = getGoplsConfig();
let goplsWorkspaceConfig = getGoplsConfig();
goplsWorkspaceConfig = await adjustGoplsWorkspaceConfiguration(cfg, goplsWorkspaceConfig);
const c = new LanguageClient(
'go', // id
config.serverName, // name
cfg.serverName, // name
{
command: config.path,
args: ['-mode=stdio', ...config.flags],
options: { env: config.env },
command: cfg.path,
args: ['-mode=stdio', ...cfg.flags],
options: { env: cfg.env },
},
{
initializationOptions: goplsConfig,
initializationOptions: goplsWorkspaceConfig,
documentSelector: ['go', 'go.mod', 'go.sum'],
uriConverters: {
// Apply file:/// scheme to all file paths.
Expand Down Expand Up @@ -298,7 +304,7 @@ function buildLanguageClient(config: LanguageServerConfig): LanguageClient {
diagnostics: vscode.Diagnostic[],
next: HandleDiagnosticsSignature
) => {
if (!config.features.diagnostics) {
if (!cfg.features.diagnostics) {
return null;
}
return next(uri, diagnostics);
Expand All @@ -308,7 +314,7 @@ function buildLanguageClient(config: LanguageServerConfig): LanguageClient {
token: vscode.CancellationToken,
next: ProvideDocumentLinksSignature
) => {
if (!config.features.documentLink) {
if (!cfg.features.documentLink) {
return null;
}
return next(document, token);
Expand Down Expand Up @@ -399,12 +405,50 @@ function buildLanguageClient(config: LanguageServerConfig): LanguageClient {
lastUserAction = new Date();
next(e);
},
workspace: {
configuration: async (params: ConfigurationParams, token: CancellationToken, next: ConfigurationRequest.HandlerSignature): Promise<any[] | ResponseError<void>> => {
const configs = await next(params, token);
if (!Array.isArray(configs)) {
return configs;
}
for (let workspaceConfig of configs) {
workspaceConfig = await adjustGoplsWorkspaceConfiguration(cfg, workspaceConfig);
}
return configs;
},
},
}
}
);
return c;
}

// adjustGoplsWorkspaceConfiguration adds any extra options to the gopls
// config. Right now, the only extra option is enabling experiments for the
// Nightly extension.
async function adjustGoplsWorkspaceConfiguration(cfg: LanguageServerConfig, config: any): Promise<any> {
if (!config) {
return config;
}
// Only modify the user's configurations for the Nightly.
if (extensionId !== 'golang.go-nightly') {
return config;
}
// allExperiments is only available with gopls/v0.5.2 and above.
const version = await getLocalGoplsVersion(cfg);
if (!version) {
return config;
}
const sv = semver.parse(version, true);
if (!sv || semver.lt(sv, 'v0.5.2')) {
return config;
}
if (!config['allExperiments']) {
config['allExperiments'] = true;
}
return config;
}

// createTestCodeLens adds the go.test.cursor and go.debug.cursor code lens
function createTestCodeLens(lens: vscode.CodeLens): vscode.CodeLens[] {
// CodeLens argument signature in gopls is [fileName: string, testFunctions: string[], benchFunctions: string[]],
Expand Down Expand Up @@ -510,6 +554,7 @@ export function buildLanguageServerConfig(): LanguageServerConfig {
const cfg: LanguageServerConfig = {
serverName: '',
path: '',
version: '', // compute version lazily
modtime: null,
enabled: goConfig['useLanguageServer'] === true,
flags: goConfig['languageServerFlags'] || [],
Expand Down Expand Up @@ -748,7 +793,12 @@ export const getLatestGoplsVersion = async (tool: Tool) => {
// getLocalGoplsVersion returns the version of gopls that is currently
// installed on the user's machine. This is determined by running the
// `gopls version` command.
//
// If this command has already been executed, it returns the saved result.
export const getLocalGoplsVersion = async (cfg: LanguageServerConfig) => {
if (cfg.version !== '') {
return cfg.version;
}
const execFile = util.promisify(cp.execFile);
let output: any;
try {
Expand Down Expand Up @@ -802,7 +852,8 @@ export const getLocalGoplsVersion = async (cfg: LanguageServerConfig) => {
//
// v0.1.3
//
return split[1];
cfg.version = split[1];
return cfg.version;
};

async function goProxyRequest(tool: Tool, endpoint: string): Promise<any> {
Expand Down
1 change: 1 addition & 0 deletions test/gopls/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ suite('gopls update tests', () => {
const got = await lsp.shouldUpdateLanguageServer(tool, {
enabled: true,
path: 'bad/path/to/gopls',
version: '',
checkForUpdates: true,
env: {},
features: {
Expand Down

0 comments on commit 408d199

Please sign in to comment.