Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Add 'file' option for lintOnSave setting #1965

Merged
merged 6 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,13 @@
"go.lintOnSave": {
"type": "string",
"enum": [
"file",
"package",
"workspace",
"off"
],
"default": "package",
"description": "Lints code on file save using the configured Lint tool. Options are 'workspace', 'package' or 'off'.",
"description": "Lints code on file save using the configured Lint tool. Options are 'file', 'package', 'workspace' or 'off'.",
"scope": "resource"
},
"go.lintTool": {
Expand Down
2 changes: 1 addition & 1 deletion src/goCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function check(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurati
}

if (!!goConfig['lintOnSave'] && goConfig['lintOnSave'] !== 'off') {
runningToolsPromises.push(goLint(fileUri, goConfig, goConfig['lintOnSave'] === 'workspace'));
runningToolsPromises.push(goLint(fileUri, goConfig, (goConfig['lintOnSave'])));
}

if (!!goConfig['vetOnSave'] && goConfig['vetOnSave'] !== 'off') {
Expand Down
24 changes: 15 additions & 9 deletions src/goLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { getToolsEnvVars, resolvePath, runTool, ICheckResult, handleDiagnosticEr
import { outputChannel } from './goStatus';
import { diagnosticsStatusBarItem } from './goStatus';
/**
* Runs linter in the current package or workspace.
* Runs linter on the current file, package or workspace.
*/
export function lintCode(lintWorkspace?: boolean) {
export function lintCode(scope?: string) {
let editor = vscode.window.activeTextEditor;
if (!editor && !lintWorkspace) {
if (!editor && scope !== 'workspace') {
vscode.window.showInformationMessage('No editor is active, cannot find current package to lint');
return;
}
if (editor.document.languageId !== 'go' && !lintWorkspace) {
if (editor.document.languageId !== 'go' && scope !== 'workspace') {
vscode.window.showInformationMessage('File in the active editor is not a Go file, cannot find current package to lint');
return;
}
Expand All @@ -24,7 +24,7 @@ export function lintCode(lintWorkspace?: boolean) {
diagnosticsStatusBarItem.show();
diagnosticsStatusBarItem.text = 'Linting...';

goLint(documentUri, goConfig, lintWorkspace)
goLint(documentUri, goConfig, scope)
.then(warnings => {
handleDiagnosticErrors(editor ? editor.document : null, warnings, vscode.DiagnosticSeverity.Warning);
diagnosticsStatusBarItem.hide();
Expand All @@ -40,9 +40,9 @@ export function lintCode(lintWorkspace?: boolean) {
*
* @param fileUri Document uri.
* @param goConfig Configuration for the Go extension.
* @param lintWorkspace If true runs linter in all workspace.
* @param scope Scope in which to run the linter.
*/
export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, lintWorkspace?: boolean): Promise<ICheckResult[]> {
export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, scope?: string): Promise<ICheckResult[]> {
epoch++;
let closureEpoch = epoch;
if (tokenSource) {
Expand All @@ -54,7 +54,9 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat
tokenSource = new vscode.CancellationTokenSource();

const currentWorkspace = getWorkspaceFolderPath(fileUri);
const cwd = (lintWorkspace && currentWorkspace) ? currentWorkspace : path.dirname(fileUri.fsPath);

const cwd = (scope === 'workspace' && currentWorkspace) ? currentWorkspace : path.dirname(fileUri.fsPath);

if (!path.isAbsolute(cwd)) {
return Promise.resolve([]);
}
Expand Down Expand Up @@ -100,10 +102,14 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat
}
}

if (lintWorkspace && currentWorkspace) {
if (scope === 'workspace' && currentWorkspace) {
args.push('./...');
}

if (scope === 'file') {
args.push(fileUri);
}

running = true;
const lintPromise = runTool(
args,
Expand Down
6 changes: 4 additions & 2 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,11 @@ 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', lintCode));
ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.package', () => lintCode('package')));

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

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

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

Expand Down