Skip to content

Commit

Permalink
Added dfmt checks (fix #20, fix #21)
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Jan 29, 2016
1 parent 24d1246 commit bbd4f73
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 26 deletions.
40 changes: 40 additions & 0 deletions src/dfmt-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as vscode from "vscode"

function getMatchIndices(regex: RegExp, str: string) {
let result: number[] = [];
let match: RegExpExecArray;
while (match = regex.exec(str))
result.push(match.index);
return result;
}

let validDfmt = /\/\/dfmt (off|on)(\n|\s+|$)/;

export function lintDfmt(doc: vscode.TextDocument, code: string) {
let locations = getMatchIndices(/\/\/dfmt/g, code);
let issues: vscode.Diagnostic[] = [];
let isOn: boolean = true;
locations.forEach(location => {
let part = code.substr(location, 11);
let match = validDfmt.exec(part);
if (!match) {
let pos = doc ? doc.positionAt(location) : new vscode.Position(0, 0);
issues.push(new vscode.Diagnostic(new vscode.Range(pos, pos.translate(0, 100)), "Not a valid dfmt command (try //dfmt off or //dfmt on instead)", vscode.DiagnosticSeverity.Warning));
} else {
if (match[1] == "off") {
if (!isOn) {
let pos = doc ? doc.positionAt(location) : new vscode.Position(0, 0);
issues.push(new vscode.Diagnostic(new vscode.Range(pos, pos.translate(0, 10)), "Redundant //dfmt off", vscode.DiagnosticSeverity.Information));
}
isOn = false;
} else {
if (isOn) {
let pos = doc ? doc.positionAt(location) : new vscode.Position(0, 0);
issues.push(new vscode.Diagnostic(new vscode.Range(pos, pos.translate(0, 9)), "Redundant //dfmt on", vscode.DiagnosticSeverity.Information));
}
isOn = true;
}
}
});
return issues;
}
38 changes: 34 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { uploadCode } from "./util"
import * as statusbar from "./statusbar"
import * as path from "path"
import { DlangUIHandler } from "./dlangui"
import { lintDfmt } from "./dfmt-check"

let diagnosticCollection: vscode.DiagnosticCollection;

Expand All @@ -21,7 +22,7 @@ export function activate(context: vscode.ExtensionContext) {

let workspaced = new WorkspaceD(vscode.workspace.rootPath);
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(DML_MODE, workspaced.getDlangUI()));

context.subscriptions.push(vscode.languages.registerCompletionItemProvider(D_MODE, workspaced, "."));
context.subscriptions.push(vscode.languages.registerSignatureHelpProvider(D_MODE, workspaced, "(", ","));
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(D_MODE, workspaced));
Expand Down Expand Up @@ -79,7 +80,7 @@ export function activate(context: vscode.ExtensionContext) {
decreaseIndentPattern: /\}/,
increaseIndentPattern: /\{/
},

wordPattern: /[a-zA-Z_][a-zA-Z0-9_]*/g,

brackets: [
Expand All @@ -95,7 +96,34 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(diagnosticCollection);

let version;
let oldLint = [[], []];
let writeTimeout;
let oldLint: [vscode.Uri, vscode.Diagnostic[]][][] = [[], [], []];
context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(event => {
clearTimeout(writeTimeout);
writeTimeout = setTimeout(function() {
let document = event.document;
if (document.languageId != "d")
return;
version = document.version;
let target = version;
if (config().get("enableLinting", true)) {
let allErrors: [vscode.Uri, vscode.Diagnostic[]][] = [];

let fresh = true;
let buildErrors = () => {
allErrors = [];
oldLint.forEach(errors => {
allErrors.push.apply(allErrors, errors);
});
diagnosticCollection.set(allErrors);
};

oldLint[2] = [[document.uri, lintDfmt(document, document.getText())]];
buildErrors();
}
}, 500);
}));

context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(document => {
if (document.languageId != "d")
return;
Expand All @@ -113,6 +141,8 @@ export function activate(context: vscode.ExtensionContext) {
diagnosticCollection.set(allErrors);
};

oldLint[2] = [[document.uri, lintDfmt(document, document.getText())]];
buildErrors();
workspaced.lint(document).then((errors: [vscode.Uri, vscode.Diagnostic[]][]) => {
if (target == version) {
oldLint[0] = errors;
Expand Down Expand Up @@ -196,7 +226,7 @@ export function activate(context: vscode.ExtensionContext) {
vscode.window.showErrorMessage("Could not update imports. dub might not be initialized yet!");
});
}));

context.subscriptions.push(vscode.commands.registerCommand("code-d.insertDscanner", () => {
vscode.window.activeTextEditor.edit((bld) => {
bld.insert(vscode.window.activeTextEditor.selection.start, `; Configurue which static analysis checks are enabled
Expand Down
30 changes: 30 additions & 0 deletions test/dfmt-check.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as assert from 'assert';
import * as vscode from 'vscode';
import { lintDfmt } from '../src/dfmt-check';

// Defines a Mocha test suite to group tests of similar kind together
suite("dfmt lint", () => {
test("misspelling on/off", () => {
let linted = lintDfmt(undefined, `void foo() {
//dfmt offs
int i = 5;
//dfmt onf
}`);
assert.strictEqual(linted.length, 2);
assert.strictEqual(linted[0].severity, vscode.DiagnosticSeverity.Warning);
assert.strictEqual(linted[1].severity, vscode.DiagnosticSeverity.Warning);
});
test("redundant on/off", () => {
let linted = lintDfmt(undefined, `void foo() {
//dfmt on
//dfmt off
int i = 5;
//dfmt off
//dfmt ons
}`);
assert.strictEqual(linted.length, 3);
assert.strictEqual(linted[0].severity, vscode.DiagnosticSeverity.Information);
assert.strictEqual(linted[1].severity, vscode.DiagnosticSeverity.Information);
assert.strictEqual(linted[2].severity, vscode.DiagnosticSeverity.Warning);
});
});
22 changes: 0 additions & 22 deletions test/extension.test.ts

This file was deleted.

0 comments on commit bbd4f73

Please sign in to comment.