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 4 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'] === 'workspace'), (goConfig['lintOnSave'] === 'file')));
}

if (!!goConfig['vetOnSave'] && goConfig['vetOnSave'] !== 'off') {
Expand Down
19 changes: 13 additions & 6 deletions src/goLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ 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(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');
Expand All @@ -24,7 +24,7 @@ export function lintCode(lintWorkspace?: boolean) {
diagnosticsStatusBarItem.show();
diagnosticsStatusBarItem.text = 'Linting...';

goLint(documentUri, goConfig, lintWorkspace)
goLint(documentUri, goConfig, lintWorkspace, lintFile)
.then(warnings => {
handleDiagnosticErrors(editor ? editor.document : null, warnings, vscode.DiagnosticSeverity.Warning);
diagnosticsStatusBarItem.hide();
Expand All @@ -40,9 +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 all workspace.
* @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): Promise<ICheckResult[]> {
export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, lintWorkspace?:boolean, lintFile?: boolean): Promise<ICheckResult[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using 2 separate variables to manage scope feels excessive. Why not use a single variable, say scope? This can have one of the 4 values file, package, workspace, off.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I was wondering about this. I took your advice and passed a string value around. It cleans up the signatures.

I appreciate the feedback 😄

epoch++;
let closureEpoch = epoch;
if (tokenSource) {
Expand All @@ -54,7 +55,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);

if (!path.isAbsolute(cwd)) {
return Promise.resolve([]);
}
Expand Down Expand Up @@ -103,7 +106,11 @@ export function goLint(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurat
if (lintWorkspace && currentWorkspace) {
args.push('./...');
}


if (lintFile) {
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(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));

Expand Down