-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
WebFreak001
committed
Jan 29, 2016
1 parent
24d1246
commit bbd4f73
Showing
4 changed files
with
104 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.