From 5ae9d2db126a9c08140408164b8f424cf2fc9ec4 Mon Sep 17 00:00:00 2001 From: Logan Date: Sun, 30 Sep 2018 15:53:25 -0700 Subject: [PATCH 1/6] Add file as option for lintOnSave --- package.json | 3 ++- src/goCheck.ts | 2 +- src/goLint.ts | 15 +++++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 083d137a0..56d4126a0 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/goCheck.ts b/src/goCheck.ts index 06a648800..59876ffdb 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -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)); } if (!!goConfig['vetOnSave'] && goConfig['vetOnSave'] !== 'off') { diff --git a/src/goLint.ts b/src/goLint.ts index bd5fadfe6..e4f869b31 100644 --- a/src/goLint.ts +++ b/src/goLint.ts @@ -4,7 +4,7 @@ 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) { let editor = vscode.window.activeTextEditor; @@ -40,9 +40,8 @@ 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. */ -export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, lintWorkspace?: boolean): Promise { +export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration): Promise { epoch++; let closureEpoch = epoch; if (tokenSource) { @@ -54,7 +53,15 @@ 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 lintFile = goConfig['lintOnSave'] === 'file'; + const lintPackage = goConfig['lintOnSave'] === 'package'; + const lintWorkspace = goConfig['lintOnSave'] === 'workspace'; + + const cwd = (lintWorkspace && currentWorkspace) ? currentWorkspace : + (lintFile) ? fileUri.fsPath : + (lintPackage) ? path.dirname(fileUri.fsPath) : path.dirname(fileUri.fsPath); + if (!path.isAbsolute(cwd)) { return Promise.resolve([]); } From ddc8ebe74eac8980bb94b1847f2b38fa6806d6c4 Mon Sep 17 00:00:00 2001 From: Logan Date: Mon, 1 Oct 2018 12:53:02 -0700 Subject: [PATCH 2/6] Remove unused param to goLint --- src/goLint.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/goLint.ts b/src/goLint.ts index e4f869b31..34306364b 100644 --- a/src/goLint.ts +++ b/src/goLint.ts @@ -24,7 +24,7 @@ export function lintCode(lintWorkspace?: boolean) { diagnosticsStatusBarItem.show(); diagnosticsStatusBarItem.text = 'Linting...'; - goLint(documentUri, goConfig, lintWorkspace) + goLint(documentUri, goConfig) .then(warnings => { handleDiagnosticErrors(editor ? editor.document : null, warnings, vscode.DiagnosticSeverity.Warning); diagnosticsStatusBarItem.hide(); @@ -111,6 +111,8 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat args.push('./...'); } + console.log(cwd); + running = true; const lintPromise = runTool( args, From 9f82c34601b5bc69207cc22361e718da4de7bccd Mon Sep 17 00:00:00 2001 From: Logan Date: Wed, 3 Oct 2018 18:01:57 -0700 Subject: [PATCH 3/6] Implement lint on save for a single file --- src/goCheck.ts | 2 +- src/goLint.ts | 18 ++++++------------ src/goMain.ts | 6 ++++-- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/goCheck.ts b/src/goCheck.ts index 59876ffdb..a1e491cf7 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -103,7 +103,7 @@ export function check(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurati } if (!!goConfig['lintOnSave'] && goConfig['lintOnSave'] !== 'off') { - runningToolsPromises.push(goLint(fileUri, goConfig)); + runningToolsPromises.push(goLint(fileUri, goConfig, (goConfig['lintOnSave'] === 'workspace'), (goConfig['lintOnSave'] === 'file'))); } if (!!goConfig['vetOnSave'] && goConfig['vetOnSave'] !== 'off') { diff --git a/src/goLint.ts b/src/goLint.ts index 34306364b..be4f040d4 100644 --- a/src/goLint.ts +++ b/src/goLint.ts @@ -6,7 +6,7 @@ import { diagnosticsStatusBarItem } from './goStatus'; /** * Runs linter on the current file, package or workspace. */ -export function lintCode(lintWorkspace?: boolean) { +export function lintCode(lintWorkspace?: boolean, lintFile?: boolean) { let editor = vscode.window.activeTextEditor; if (!editor && !lintWorkspace) { vscode.window.showInformationMessage('No editor is active, cannot find current package to lint'); @@ -24,7 +24,7 @@ export function lintCode(lintWorkspace?: boolean) { diagnosticsStatusBarItem.show(); diagnosticsStatusBarItem.text = 'Linting...'; - goLint(documentUri, goConfig) + goLint(documentUri, goConfig, lintWorkspace, lintFile) .then(warnings => { handleDiagnosticErrors(editor ? editor.document : null, warnings, vscode.DiagnosticSeverity.Warning); diagnosticsStatusBarItem.hide(); @@ -40,8 +40,10 @@ export function lintCode(lintWorkspace?: boolean) { * * @param fileUri Document uri. * @param goConfig Configuration for the Go extension. + * @param lintWorkspace If true, runs linter in the entire workspace. + * @param lintFile If true, runs linter on a single file. */ -export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration): Promise { +export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, lintWorkspace?:boolean, lintFile?: boolean): Promise { epoch++; let closureEpoch = epoch; if (tokenSource) { @@ -54,13 +56,7 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat const currentWorkspace = getWorkspaceFolderPath(fileUri); - const lintFile = goConfig['lintOnSave'] === 'file'; - const lintPackage = goConfig['lintOnSave'] === 'package'; - const lintWorkspace = goConfig['lintOnSave'] === 'workspace'; - - const cwd = (lintWorkspace && currentWorkspace) ? currentWorkspace : - (lintFile) ? fileUri.fsPath : - (lintPackage) ? path.dirname(fileUri.fsPath) : path.dirname(fileUri.fsPath); + const cwd = (lintWorkspace && currentWorkspace) ? currentWorkspace : path.dirname(fileUri.fsPath); if (!path.isAbsolute(cwd)) { return Promise.resolve([]); @@ -110,8 +106,6 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat if (lintWorkspace && currentWorkspace) { args.push('./...'); } - - console.log(cwd); running = true; const lintPromise = runTool( diff --git a/src/goMain.ts b/src/goMain.ts index ab118d4cf..dc884d226 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -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(false, false))); - ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.workspace', () => lintCode(true))); + ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.workspace', () => lintCode(true, false))); + + ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.file', () => lintCode(false, true))); ctx.subscriptions.push(vscode.commands.registerCommand('go.vet.package', vetCode)); From 95e9c2c2256649b9082651a0cd7556dadabb8232 Mon Sep 17 00:00:00 2001 From: Logan Date: Wed, 3 Oct 2018 20:30:52 -0700 Subject: [PATCH 4/6] Add file to lint args --- src/goLint.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/goLint.ts b/src/goLint.ts index be4f040d4..7d94637c3 100644 --- a/src/goLint.ts +++ b/src/goLint.ts @@ -107,6 +107,10 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat args.push('./...'); } + if (lintFile) { + args.push(fileUri) + } + running = true; const lintPromise = runTool( args, From 80006d56290b3a8df54f8a5efa792df9c22cd397 Mon Sep 17 00:00:00 2001 From: Logan Date: Wed, 10 Oct 2018 17:37:35 -0700 Subject: [PATCH 5/6] Address npm lint suggestions --- src/goLint.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/goLint.ts b/src/goLint.ts index 7d94637c3..2b3c20eb6 100644 --- a/src/goLint.ts +++ b/src/goLint.ts @@ -43,7 +43,7 @@ export function lintCode(lintWorkspace?: boolean, lintFile?: boolean) { * @param lintWorkspace If true, runs linter in the entire workspace. * @param lintFile If true, runs linter on a single file. */ -export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, lintWorkspace?:boolean, lintFile?: boolean): Promise { +export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, lintWorkspace?: boolean, lintFile?: boolean): Promise { epoch++; let closureEpoch = epoch; if (tokenSource) { @@ -106,11 +106,11 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat if (lintWorkspace && currentWorkspace) { args.push('./...'); } - + if (lintFile) { - args.push(fileUri) + args.push(fileUri); } - + running = true; const lintPromise = runTool( args, From 1d40ff655760f37834d8281f3818fb9b0ade502d Mon Sep 17 00:00:00 2001 From: Logan Date: Wed, 10 Oct 2018 18:11:30 -0700 Subject: [PATCH 6/6] Update method signature for lint scoping --- src/goCheck.ts | 2 +- src/goLint.ts | 19 +++++++++---------- src/goMain.ts | 6 +++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/goCheck.ts b/src/goCheck.ts index a1e491cf7..6add85151 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -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'), (goConfig['lintOnSave'] === 'file'))); + runningToolsPromises.push(goLint(fileUri, goConfig, (goConfig['lintOnSave']))); } if (!!goConfig['vetOnSave'] && goConfig['vetOnSave'] !== 'off') { diff --git a/src/goLint.ts b/src/goLint.ts index 2b3c20eb6..bdad8bf4b 100644 --- a/src/goLint.ts +++ b/src/goLint.ts @@ -6,13 +6,13 @@ import { diagnosticsStatusBarItem } from './goStatus'; /** * Runs linter on the current file, package or workspace. */ -export function lintCode(lintWorkspace?: boolean, lintFile?: 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; } @@ -24,7 +24,7 @@ export function lintCode(lintWorkspace?: boolean, lintFile?: boolean) { diagnosticsStatusBarItem.show(); diagnosticsStatusBarItem.text = 'Linting...'; - goLint(documentUri, goConfig, lintWorkspace, lintFile) + goLint(documentUri, goConfig, scope) .then(warnings => { handleDiagnosticErrors(editor ? editor.document : null, warnings, vscode.DiagnosticSeverity.Warning); diagnosticsStatusBarItem.hide(); @@ -40,10 +40,9 @@ export function lintCode(lintWorkspace?: boolean, lintFile?: boolean) { * * @param fileUri Document uri. * @param goConfig Configuration for the Go extension. - * @param lintWorkspace If true, runs linter in the entire workspace. - * @param lintFile If true, runs linter on a single file. + * @param scope Scope in which to run the linter. */ -export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, lintWorkspace?: boolean, lintFile?: boolean): Promise { +export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, scope?: string): Promise { epoch++; let closureEpoch = epoch; if (tokenSource) { @@ -56,7 +55,7 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat 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([]); @@ -103,11 +102,11 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat } } - if (lintWorkspace && currentWorkspace) { + if (scope === 'workspace' && currentWorkspace) { args.push('./...'); } - if (lintFile) { + if (scope === 'file') { args.push(fileUri); } diff --git a/src/goMain.ts b/src/goMain.ts index dc884d226..c56c64a52 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -387,11 +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(false, false))); + ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.package', () => lintCode('package'))); - ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.workspace', () => lintCode(true, false))); + ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.workspace', () => lintCode('workspace'))); - ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.file', () => lintCode(false, true))); + ctx.subscriptions.push(vscode.commands.registerCommand('go.lint.file', () => lintCode('file'))); ctx.subscriptions.push(vscode.commands.registerCommand('go.vet.package', vetCode));