Skip to content

Commit

Permalink
Add vet package and workspace cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Nov 10, 2017
1 parent b03d33b commit 7adc180
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 70 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@
"command": "go.lint.workspace",
"title": "Go: Lint Workspace",
"description": "Run linter in the current workspace."
},
{
"command": "go.vet.package",
"title": "Go: Vet Package",
"description": "Run go vet in the package of the current file."
},
{
"command": "go.vet.workspace",
"title": "Go: Vet Workspace",
"description": "Run go vet in the current workspace."
}
],
"debuggers": [
Expand Down
15 changes: 3 additions & 12 deletions src/goCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getBinPath, parseFilePrelude, getCurrentGoPath, getToolsEnvVars, resolv
import { getNonVendorPackages } from './goPackages';
import { getTestFlags } from './testUtils';
import { goLint } from './goLint';
import { goVet } from './goVet';

let statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
statusBarItem.command = 'go.test.showOutput';
Expand Down Expand Up @@ -142,18 +143,8 @@ export function check(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurati
}

if (!!goConfig['vetOnSave'] && goConfig['vetOnSave'] !== 'off') {
let vetFlags = goConfig['vetFlags'] || [];
let vetArgs = vetFlags.length ? ['tool', 'vet', ...vetFlags, '.'] : ['vet', './...'];
let vetWorkDir = (goConfig['vetOnSave'] === 'workspace' && currentWorkspace) ? currentWorkspace : cwd;

runningToolsPromises.push(runTool(
vetArgs,
vetWorkDir,
'warning',
true,
null,
env
));
let vetWorkspace = goConfig['vetOnSave'] === 'workspace';
runningToolsPromises.push(goVet(fileUri, goConfig, vetWorkspace));
}

if (!!goConfig['coverOnSave']) {
Expand Down
63 changes: 9 additions & 54 deletions src/goLint.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,22 @@
import path = require('path');
import vscode = require('vscode');
import os = require('os');
import cp = require('child_process');
import { getToolsEnvVars, resolvePath, getBinPath, runTool, ICheckResult, handleDiagnosticErrors } from './util';
import { getToolsEnvVars, resolvePath, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath } from './util';
import { outputChannel } from './goStatus';
import { getGoRuntimePath } from './goPath';

/**
* Runs linter in the current package.
* Runs linter in the current package or workspace.
*/
export function lintCurrentPackage(): Promise<ICheckResult[]> {
export function lintCode(lintWorkspace?: boolean) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
if (!editor && !lintWorkspace) {
vscode.window.showInformationMessage('No editor is active, cant find current package to lint');
return;
}

let documentUri = editor ? editor.document.uri : null;
let goConfig = vscode.workspace.getConfiguration('go', documentUri);
outputChannel.clear();
goLint(documentUri, goConfig)
.then(warnings => handleDiagnosticErrors(editor ? editor.document : null, warnings, vscode.DiagnosticSeverity.Warning))
.catch(err => {
vscode.window.showInformationMessage('Error: ' + err);
});
}

/**
* Runs linter in all packages in the current workspace.
*/
export function lintWorkspace() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}

let documentUri = editor ? editor.document.uri : null;
let goConfig = vscode.workspace.getConfiguration('go', documentUri);
outputChannel.clear();
goLint(documentUri, goConfig, true)
goLint(documentUri, goConfig, lintWorkspace)
.then(warnings => handleDiagnosticErrors(editor ? editor.document : null, warnings, vscode.DiagnosticSeverity.Warning))
.catch(err => {
vscode.window.showInformationMessage('Error: ' + err);
Expand All @@ -59,6 +36,7 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat
let lintEnv = Object.assign({}, getToolsEnvVars());
let args = [];
let configFlag = '--config=';
let currentWorkspace = getWorkspaceFolderPath(fileUri);
lintFlags.forEach(flag => {
// --json is not a valid flag for golint and in gometalinter, it is used to print output in json which we dont want
if (flag === '--json') {
Expand All @@ -83,36 +61,13 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat
}
}

let lintWorkDir: string;

if (lintWorkspace) {
let currentWorkspace: string;
if (fileUri) {
let workspace = vscode.workspace.getWorkspaceFolder(fileUri);
if (workspace) {
currentWorkspace = workspace.uri.fsPath;
}
}

if (!currentWorkspace) {
// finding workspace root path
let folders = vscode.workspace.workspaceFolders;
if (folders && folders.length) {
currentWorkspace = folders[0].uri.fsPath;
} else {
return Promise.resolve([]);
}
}

lintWorkDir = currentWorkspace;
if (lintWorkspace && currentWorkspace) {
args.push('./...');
} else {
lintWorkDir = path.dirname(fileUri.fsPath);
}

return runTool(
args,
lintWorkDir,
(lintWorkspace && currentWorkspace) ? currentWorkspace : path.dirname(fileUri.fsPath),
'warning',
false,
lintTool,
Expand Down
11 changes: 8 additions & 3 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import { browsePackages } from './goBrowsePackage';
import { goGetPackage } from './goGetPackage';
import { GoDebugConfigurationProvider } from './goDebugConfiguration';
import { playgroundCommand } from './goPlayground';
import { lintCurrentPackage, lintWorkspace } from './goLint';
import { lintCode } from './goLint';
import { vetCode } from './goVet';

export let errorDiagnosticCollection: vscode.DiagnosticCollection;
export let warningDiagnosticCollection: vscode.DiagnosticCollection;
Expand Down Expand Up @@ -290,9 +291,13 @@ export function activate(ctx: vscode.ExtensionContext): void {

ctx.subscriptions.push(vscode.commands.registerCommand('go.playground', playgroundCommand));

ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.package', lintCurrentPackage));
ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.package', lintCode));

ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.workspace', lintWorkspace));
ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.workspace', () => lintCode(true)));

ctx.subscriptions.push(vscode.commands.registerCommand('go.vet.package', vetCode));

ctx.subscriptions.push(vscode.commands.registerCommand('go.vet.workspace', () => vetCode(true)));

vscode.languages.setLanguageConfiguration(GO_MODE.language, {
indentationRules: {
Expand Down
47 changes: 47 additions & 0 deletions src/goVet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import path = require('path');
import vscode = require('vscode');
import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath } from './util';
import { outputChannel } from './goStatus';

/**
* Runs go vet in the current package or workspace.
*/
export function vetCode(vetWorkspace?: boolean) {
let editor = vscode.window.activeTextEditor;
if (!editor && !vetWorkspace) {
vscode.window.showInformationMessage('No editor is active, cant find current package to vet');
return;
}

let documentUri = editor ? editor.document.uri : null;
let goConfig = vscode.workspace.getConfiguration('go', documentUri);
outputChannel.clear();
goVet(documentUri, goConfig, vetWorkspace)
.then(warnings => handleDiagnosticErrors(editor ? editor.document : null, warnings, vscode.DiagnosticSeverity.Warning))
.catch(err => {
vscode.window.showInformationMessage('Error: ' + err);
});
}

/**
* Runs go vet or go tool vet and presents the output in the 'Go' channel and in the diagnostic collections.
*
* @param fileUri Document uri.
* @param goConfig Configuration for the Go extension.
* @param vetWorkspace If true vets code in all workspace.
*/
export function goVet(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, vetWorkspace?: boolean): Promise<ICheckResult[]> {
let vetFlags = goConfig['vetFlags'] || [];
let vetEnv = Object.assign({}, getToolsEnvVars());
let vetArgs = vetFlags.length ? ['tool', 'vet', ...vetFlags, '.'] : ['vet', './...'];
let currentWorkspace = getWorkspaceFolderPath(fileUri);

return runTool(
vetArgs,
(vetWorkspace && currentWorkspace) ? currentWorkspace : path.dirname(fileUri.fsPath),
'warning',
true,
null,
vetEnv
);
}
16 changes: 15 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ export function sendTelemetryEvent(eventName: string, properties?: {
[key: string]: number;
}): void {

let temp = vscode.extensions.getExtension(extensionId).packageJSON.contributes;
telemtryReporter = telemtryReporter ? telemtryReporter : new TelemetryReporter(extensionId, extensionVersion, aiKey);
telemtryReporter.sendTelemetryEvent(eventName, properties, measures);
}
Expand Down Expand Up @@ -620,4 +619,19 @@ function mapSeverityToVSCodeSeverity(sev: string): vscode.DiagnosticSeverity {
case 'warning': return vscode.DiagnosticSeverity.Warning;
default: return vscode.DiagnosticSeverity.Error;
}
}

export function getWorkspaceFolderPath(fileUri: vscode.Uri): string {
if (fileUri) {
let workspace = vscode.workspace.getWorkspaceFolder(fileUri);
if (workspace) {
return workspace.uri.fsPath;
}
}

// fall back to the first workspace
let folders = vscode.workspace.workspaceFolders;
if (folders && folders.length) {
return folders[0].uri.fsPath;
}
}

0 comments on commit 7adc180

Please sign in to comment.