Skip to content

Commit

Permalink
src/goMain: export a small API
Browse files Browse the repository at this point in the history
Exports a function for use by other extensions. When I'm creating a
Go-related VSCode extension, being able to reuse this extension's
configuration and certain helper functions would be very useful.
Additionally, users expect Go-related extensions to respect `go.*`
configuration options, and have reported issues when they don't. This
change exports `getBinPath`. With this function, Go-related
extensions don't have to reinvent the wheel when it comes to
installing and configuring Go tools.

Updates #233

Change-Id: I9edf7f87437492182e8562aa107b643ca01a1202
GitHub-Last-Rev: 0171541
GitHub-Pull-Request: #1642
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/336509
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
Trust: Hyang-Ah Hana Kim <[email protected]>
Trust: Suzy Mueller <[email protected]>
Run-TryBot: Hyang-Ah Hana Kim <[email protected]>
  • Loading branch information
firelizzard18 authored and hyangah committed Aug 10, 2021
1 parent 8a4731f commit 4e7f6f5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/export.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*---------------------------------------------------------
* Copyright 2021 The Go Authors. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/

import { Uri } from 'vscode';
export { ToolAtVersion } from './goTools';

export interface CommandInvocation {
binPath: string;
args?: string[];
env?: Object;
cwd?: string;
}

export interface ExtensionAPI {
settings: {
/**
* Returns the execution command corresponding to the specified resource, taking into account
* any workspace-specific settings for the workspace to which this resource belongs.
*/
getExecutionCommand(toolName: string, resource?: Uri): CommandInvocation | undefined;
};
}
19 changes: 19 additions & 0 deletions src/extensionAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*---------------------------------------------------------
* Copyright 2021 The Go Authors. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/

import { Uri } from 'vscode';
import { CommandInvocation, ExtensionAPI } from './export';
import { getBinPathWithExplanation } from './util';

const api: ExtensionAPI = {
settings: {
getExecutionCommand(toolName: string, resource?: Uri): CommandInvocation | undefined {
const { binPath } = getBinPathWithExplanation(toolName, true, resource);
return { binPath };
}
}
};

export default api;
6 changes: 5 additions & 1 deletion src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ import semver = require('semver');
import vscode = require('vscode');
import { getFormatTool } from './goFormat';
import { resetSurveyConfig, showSurveyConfig, timeMinute } from './goSurvey';
import { ExtensionAPI } from './export';
import extensionAPI from './extensionAPI';

export let buildDiagnosticCollection: vscode.DiagnosticCollection;
export let lintDiagnosticCollection: vscode.DiagnosticCollection;
Expand All @@ -124,7 +126,7 @@ export let restartLanguageServer = () => {
return;
};

export async function activate(ctx: vscode.ExtensionContext) {
export async function activate(ctx: vscode.ExtensionContext): Promise<ExtensionAPI> {
if (process.env['VSCODE_GO_IN_TEST'] === '1') {
// Make sure this does not run when running in test.
return;
Expand Down Expand Up @@ -705,6 +707,8 @@ If you would like additional configuration for diagnostics from gopls, please se
vscode.languages.setLanguageConfiguration(GO_MODE.language, {
wordPattern: /(-?\d*\.\d\w*)|([^`~!@#%^&*()\-=+[{\]}\\|;:'",.<>/?\s]+)/g
});

return extensionAPI;
}

function showGoWelcomePage(ctx: vscode.ExtensionContext) {
Expand Down
8 changes: 6 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,12 @@ export function getBinPath(tool: string, useCache = true): string {

// getBinPathWithExplanation returns the path to the tool, and the explanation on why
// the path was chosen. See getBinPathWithPreferredGopathGorootWithExplanation for details.
export function getBinPathWithExplanation(tool: string, useCache = true): { binPath: string; why?: string } {
const cfg = getGoConfig();
export function getBinPathWithExplanation(
tool: string,
useCache = true,
uri?: vscode.Uri
): { binPath: string; why?: string } {
const cfg = getGoConfig(uri);
const alternateTools: { [key: string]: string } = cfg.get('alternateTools');
const alternateToolPath: string = alternateTools[tool];

Expand Down

0 comments on commit 4e7f6f5

Please sign in to comment.