Skip to content

Support for custom .py files. Added on-demand mode and clear lints command. #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 18 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -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",
Expand Down
17 changes: 16 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
31 changes: 27 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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();
}

16 changes: 13 additions & 3 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down