Skip to content

Commit

Permalink
Merge branch 'master' into quoct/remote_test
Browse files Browse the repository at this point in the history
  • Loading branch information
quoctruong committed Oct 28, 2020
2 parents a0d0b06 + 5eaf73c commit 3f1965d
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 75 deletions.
9 changes: 7 additions & 2 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ RUN /scratch/build/all.bash setup_env

FROM node:latest

# Prepare a home directory under workspace.
RUN mkdir -p /workspace/vscodego
ENV HOME=/workspace/vscodego

# GO111MODULE=auto
RUN mkdir /go
COPY --from=gobuilder /gobin /go/bin
COPY --from=gobuilder /usr/local/go /usr/local/go

ENV GOPATH /go
ENV PATH ${GOPATH}/bin:/usr/local/go/bin:${PATH}
# Add the default GOPATH/bin to the PATH.
# Add the directories of the go tool chains to PATH.
ENV PATH /workspace/vscodego/go/bin:/go/bin:/usr/local/go/bin:${PATH}
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y libnss3 libgtk-3-dev libxss1 libasound2 xvfb libsecret-1-0
Expand Down
28 changes: 14 additions & 14 deletions build/all.bash
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ prepare_nightly() {

# setup dependencies required for tests.
install_dependencies() {
GO111MODULE=on go get -x golang.org/x/tools/gopls
GO111MODULE=on go get -x github.com/acroca/go-symbols
GO111MODULE=on go get -x github.com/cweill/gotests/...
GO111MODULE=on go get -x github.com/davidrjenni/reftools/cmd/fillstruct
GO111MODULE=on go get -x github.com/haya14busa/goplay/cmd/goplay
GO111MODULE=on go get -x github.com/mdempsky/gocode
GO111MODULE=on go get -x github.com/ramya-rao-a/go-outline
GO111MODULE=on go get -x github.com/rogpeppe/godef
GO111MODULE=on go get -x github.com/sqs/goreturns
GO111MODULE=on go get -x github.com/uudashr/gopkgs/v2/cmd/gopkgs
GO111MODULE=on go get -x github.com/zmb3/gogetdoc
GO111MODULE=on go get -x golang.org/x/lint/golint
GO111MODULE=on go get -x golang.org/x/tools/cmd/gorename
GO111MODULE=on go get -x github.com/go-delve/delve/cmd/dlv
GO111MODULE=on go get golang.org/x/tools/gopls
GO111MODULE=on go get github.com/acroca/go-symbols
GO111MODULE=on go get github.com/cweill/gotests/...
GO111MODULE=on go get github.com/davidrjenni/reftools/cmd/fillstruct
GO111MODULE=on go get github.com/haya14busa/goplay/cmd/goplay
GO111MODULE=on go get github.com/mdempsky/gocode
GO111MODULE=on go get github.com/ramya-rao-a/go-outline
GO111MODULE=on go get github.com/rogpeppe/godef
GO111MODULE=on go get github.com/sqs/goreturns
GO111MODULE=on go get github.com/uudashr/gopkgs/v2/cmd/gopkgs
GO111MODULE=on go get github.com/zmb3/gogetdoc
GO111MODULE=on go get golang.org/x/lint/golint
GO111MODULE=on go get golang.org/x/tools/cmd/gorename
GO111MODULE=on go get github.com/go-delve/delve/cmd/dlv
}

main() {
Expand Down
4 changes: 4 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Generates unit tests for the selected function in the current file

Generates method stub for implementing the provided interface and inserts at the cursor.

### `Go: extract server output channel to editor`

Show the gopls (server) output channel in the editor.

### `Go: Toggle gc details`

Toggle the display of compiler optimization choices
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@
"title": "Go: Generate Interface Stubs",
"description": "Generates method stub for implementing the provided interface and inserts at the cursor."
},
{
"command": "go.extractServerChannel",
"title": "Go: extract server output channel to editor",
"description": "Show the gopls (server) output channel in the editor."
},
{
"command": "go.toggle.gc_details",
"title": "Go: Toggle gc details",
Expand Down
4 changes: 2 additions & 2 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { addGoRuntimeBaseToPATH, clearGoRuntimeBaseFromPATH } from './goEnvironm
import { getLanguageServerToolPath } from './goLanguageServer';
import { logVerbose } from './goLogging';
import { restartLanguageServer } from './goMain';
import { addGoStatus, initGoEnvStatusBar, outputChannel, removeGoStatus } from './goStatus';
import { addGoStatus, initGoStatusBar, outputChannel, removeGoStatus } from './goStatus';
import {
containsTool,
disableModulesForWildcard,
Expand Down Expand Up @@ -413,7 +413,7 @@ export function updateGoVarsFromConfig(): Promise<void> {
// clear pre-existing terminal PATH mutation logic set up by this extension.
clearGoRuntimeBaseFromPATH();
}
initGoEnvStatusBar();
initGoStatusBar();
// TODO: restart language server or synchronize with language server update.

return resolve();
Expand Down
109 changes: 94 additions & 15 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 Expand Up @@ -1147,7 +1198,7 @@ function daysBetween(a: Date, b: Date): number {
return msBetween(a, b) / timeDay;
}

// minutesBetween returns the number of days between a and b.
// minutesBetween returns the number of minutes between a and b.
function minutesBetween(a: Date, b: Date): number {
return msBetween(a, b) / timeMinute;
}
Expand All @@ -1156,6 +1207,34 @@ function msBetween(a: Date, b: Date): number {
return Math.abs(a.getTime() - b.getTime());
}

export function showServerOutputChannel() {
if (!languageServerIsRunning) {
vscode.window.showInformationMessage(`gopls is not running`);
return;
}
// likely show() is asynchronous, despite the documentation
serverOutputChannel.show();
let found: vscode.TextDocument;
for (const doc of vscode.workspace.textDocuments) {
if (doc.fileName.indexOf('extension-output-') !== -1) {
// despite show() above, this might not get the output we want, so check
const contents = doc.getText();
if (contents.indexOf('[Info - ') === -1) {
continue;
}
if (found !== undefined) {
vscode.window.showInformationMessage('multiple docs named extension-output-...');
}
found = doc;
// .log, as some decoration is better than none
vscode.workspace.openTextDocument({language: 'log', content: contents});
}
}
if (found === undefined) {
vscode.window.showErrorMessage('make sure "gopls (server)" output is showing');
}
}

function collectGoplsLog(): string {
serverOutputChannel.show();
// Find the logs in the output channel. There is no way to read
Expand Down
15 changes: 9 additions & 6 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ import {
} from './goInstallTools';
import {
languageServerIsRunning,
promptForLanguageServerDefaultChange,
resetSurveyConfig,
showSurveyConfig,
startLanguageServerWithFallback, watchLanguageServerConfiguration
promptForLanguageServerDefaultChange, resetSurveyConfig, showServerOutputChannel,
showSurveyConfig, startLanguageServerWithFallback,
watchLanguageServerConfiguration
} from './goLanguageServer';
import { lintCode } from './goLint';
import { logVerbose, setLogConfig } from './goLogging';
Expand All @@ -46,7 +45,7 @@ import { GO111MODULE, isModSupported } from './goModules';
import { playgroundCommand } from './goPlayground';
import { GoReferencesCodeLensProvider } from './goReferencesCodelens';
import { GoRunTestCodeLensProvider } from './goRunTestCodelens';
import { disposeGoStatusBar, expandGoStatusBar, outputChannel, showHideStatus } from './goStatus';
import { disposeGoStatusBar, expandGoStatusBar, outputChannel, updateGoStatusBar } from './goStatus';
import { subTestAtCursor, testAtCursor, testCurrentFile, testCurrentPackage, testPrevious, testWorkspace } from './goTest';
import { getConfiguredTools } from './goTools';
import { vetCode } from './goVet';
Expand Down Expand Up @@ -452,6 +451,10 @@ export function activate(ctx: vscode.ExtensionContext) {

ctx.subscriptions.push(vscode.commands.registerCommand('go.install.package', installCurrentPackage));

ctx.subscriptions.push(vscode.commands.registerCommand('go.extractServerChannel', () => {
showServerOutputChannel();
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.toggle.gc_details', () => {
if (!languageServerIsRunning) {
vscode.window.showErrorMessage('"Go: Toggle gc details" command is available only when the language server is running');
Expand Down Expand Up @@ -586,7 +589,7 @@ function addOnChangeTextDocumentListeners(ctx: vscode.ExtensionContext) {
}

function addOnChangeActiveTextEditorListeners(ctx: vscode.ExtensionContext) {
[showHideStatus, applyCodeCoverage].forEach((listener) => {
[updateGoStatusBar, applyCodeCoverage].forEach((listener) => {
// Call the listeners on initilization for current active text editor
if (!!vscode.window.activeTextEditor) {
listener(vscode.window.activeTextEditor);
Expand Down
Loading

0 comments on commit 3f1965d

Please sign in to comment.