From ffb64f81d8f5919a61f0f10604382b5b7968c49f Mon Sep 17 00:00:00 2001 From: jorgeacortes Date: Sun, 2 Feb 2020 12:09:04 +0100 Subject: [PATCH] Added on-demand command for users that doesn't want that linter is automatically executed when opening or saving files. Added clear lints command for clearing the file squiggles and problems. Added support for custom .py files by checking cpplintPath for ends in ".py". Fixed missing license warning. --- README.md | 2 +- package.json | 22 ++++++++++++++++++---- src/configuration.ts | 17 ++++++++++++++++- src/extension.ts | 31 +++++++++++++++++++++++++++---- src/runner.ts | 16 +++++++++++++--- 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 16d9cfd..ba65500 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ dir c:\ProgramData\Anaconda2\Scripts\cpplint.exe ## Extension Settings -* `cpplint.cpplintPath`: set cpplint executable path, path on windows should like `c:\\ProgramData\\Anaconda2\\Scripts\\cpplint.exe` +* `cpplint.cpplintPath`: set cpplint executable path, path on windows should like `c:\\ProgramData\\Anaconda2\\Scripts\\cpplint.exe`. Python files are allowed like `cpplint.py`. * `cpplint.lintMode`: set cpplint mode, avialable value are single and workspace * `cpplint.lineLength`: set line length strict, default is 80 characters * `cpplint.excludes`: set exclude rules, which is related path and shell globbing is preforming, abosluted path is supported right now, diff --git a/package.json b/package.json index 0248861..50ba4b6 100644 --- a/package.json +++ b/package.json @@ -2,12 +2,13 @@ "name": "cpplint", "displayName": "cpplint", "description": "code style check tool extension for cpplint", - "version": "0.1.3", + "version": "0.1.4", "publisher": "mine", "repository": { "type": "Git", "url": "https://github.com/secularbird/cpplint-extension" }, + "license": "MIT", "engines": { "vscode": "^1.21.0" }, @@ -21,7 +22,9 @@ "onLanguage:cpp", "onLanguage:c", "onCommand:cpplint.runAnalysis", - "onCommand:cpplint.runWholeAnalysis" + "onCommand:cpplint.runWholeAnalysis", + "onCommand:cpplint.lintCurrentFile", + "onCommand:cpplint.clearLints" ], "main": "./out/src/extension", "contributes": { @@ -35,6 +38,16 @@ "command": "cpplint.runWholeAnalysis", "title": "Analyze current workspace", "category": "cpplinter" + }, + { + "command": "cpplint.lintCurrentFile", + "title": "Lint current file", + "category": "cpplinter" + }, + { + "command": "cpplint.clearLints", + "title": "Clear cpplint problems and squiggles", + "category": "cpplinter" } ], "configuration": { @@ -51,9 +64,10 @@ "default": "single", "enum": [ "single", - "workspace" + "workspace", + "single-demand" ], - "description": "single is fast, only provides information of current active file, workspace is slow, provides informations of the whole workspace" + "description": "single is fast, only provides information of current active file, workspace is slow, provides informations of the whole workspace, single-demand works only when manually invoking the command for a single file." }, "cpplint.lineLength": { "type": "number", diff --git a/src/configuration.ts b/src/configuration.ts index 88b023d..62a7a4c 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -57,6 +57,14 @@ export class ConfigManager { } } + public isWorkspaceMode(): boolean { + if (this.config['lintMode'] == 'workspace') { + return true; + } else { + return false; + } + } + public isSupportLanguage(language: string): boolean { if (this.config["languages"].indexOf(language) >= 0) { return true; @@ -72,8 +80,15 @@ export class ConfigManager { if (settings) { var cpplintPath = this.findCpplintPath(settings); + let threeLastChars = cpplintPath.substring(cpplintPath.length - 3); + if (threeLastChars === ".py"){ + this.config['usePyFile'] = true; + } else { + this.config['usePyFile'] = false; + } + if (!existsSync(cpplintPath)) { - vscode.window.showErrorMessage('Cpplint: Could not find cpplint executable'); + vscode.window.showErrorMessage('Cpplint: Could not find cpplint executable.'); } this.config['cpplintPath'] = cpplintPath; diff --git a/src/extension.ts b/src/extension.ts index c3f1cbb..2f58ac4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -35,6 +35,12 @@ export function activate(context: vscode.ExtensionContext) { let whole = vscode.commands.registerCommand('cpplint.runWholeAnalysis', runWholeAnalysis); context.subscriptions.push(whole); + let demand = vscode.commands.registerCommand('cpplint.lintCurrentFile', lintCurrentFile); + context.subscriptions.push(demand);clearLints + + let clear = vscode.commands.registerCommand('cpplint.clearLints', clearLints); + context.subscriptions.push(clear); + vscode.workspace.onDidChangeConfiguration((()=>loadConfigure()).bind(this)); } @@ -87,10 +93,10 @@ function doLint() { if (vscode.window.activeTextEditor) { let language = vscode.window.activeTextEditor.document.languageId if (ConfigManager.getInstance().isSupportLanguage(language)) { - if (ConfigManager.getInstance().isSingleMode()) { - Lint(diagnosticCollection, false); - } else { + if (ConfigManager.getInstance().isWorkspaceMode()) { Lint(diagnosticCollection, true); + } else { + Lint(diagnosticCollection, false); } } } @@ -111,9 +117,26 @@ function loadConfigure() { startLint2(); vscode.window.onDidChangeActiveTextEditor((() => startLint2()).bind(this)); vscode.workspace.onDidSaveTextDocument((() => startLint2()).bind(this)); - } else { + } else if (ConfigManager.getInstance().isWorkspaceMode()) { // start timer to do workspace lint startLint(); vscode.workspace.onDidSaveTextDocument((() => startLint()).bind(this)); + } else { + // Do nothing in demand mode. + } +} + +// Lints current file on demand. If the file is dirty, saves it to avoid wrong +// linting as cpplint parses saved file. +function lintCurrentFile() { + if (vscode.window.activeTextEditor.document.isDirty){ + vscode.window.activeTextEditor.document.save(); } + startLint2(); } + +// Clears all problems and squiggles from the views. +function clearLints() { + diagnosticCollection.clear(); +} + diff --git a/src/runner.ts b/src/runner.ts index 5805d4d..c787c95 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -36,7 +36,17 @@ export function runCppLint(filename: string, workspaces: string[], enableworkspa let config = ConfigManager.getInstance().getConfig(); let cpplint = config["cpplintPath"]; let linelength = "--linelength=" + config['lineLength']; - let param: string[] = ['--output=eclipse', linelength]; + let param: string[] = []; + let exec: string; + // If using a custom python script modify the args and exec to call spawn. + if (config['usePyFile'] == true ){ + exec = "python"; + param = [cpplint, '--output=eclipse', linelength]; + } + else{ + exec = cpplint; + param = ['--output=eclipse', linelength]; + } if (config['excludes'].length != 0) { config['excludes'].forEach(element => { @@ -84,7 +94,7 @@ export function runCppLint(filename: string, workspaces: string[], enableworkspa } workspaceparam = workspaceparam.concat(["--recursive", workspace]); - let output = lint(cpplint, workspaceparam); + let output = lint(exec, workspaceparam); out = output; } return out.join('\n'); @@ -110,7 +120,7 @@ export function runCppLint(filename: string, workspaces: string[], enableworkspa } param.push(filename); - let output = lint(cpplint, param); + let output = lint(exec, param); let end = 'CppLint ended: ' + new Date().toString(); let out = output; return out.join('\n');