Skip to content

Commit 802aee4

Browse files
author
WebFreak001
committed
Added support for dub build as linter
1 parent 62d2dc2 commit 802aee4

3 files changed

Lines changed: 80 additions & 8 deletions

File tree

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
"default": true,
8181
"description": "If code-d should watch for file saves and report static analysis. Might interfere with other lint plugins or settings."
8282
},
83+
"d.enableDubLinting": {
84+
"type": "boolean",
85+
"default": true,
86+
"description": "If code-d should build on save to check for compile errors."
87+
},
8388
"d.enableAutoComplete": {
8489
"type": "boolean",
8590
"default": true,

src/extension.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,39 @@ export function activate(context: vscode.ExtensionContext) {
5151
diagnosticCollection = vscode.languages.createDiagnosticCollection("d");
5252
context.subscriptions.push(diagnosticCollection);
5353

54+
let version;
55+
let oldLint = [[], []];
5456
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(document => {
5557
if (document.languageId != "d")
5658
return;
57-
if (config().get("enableLinting", true))
58-
workspaced.lint(document).then(errors => {
59-
diagnosticCollection.delete(document.uri);
60-
diagnosticCollection.set(document.uri, errors);
59+
version = document.version;
60+
let target = version;
61+
if (config().get("enableLinting", true)) {
62+
let allErrors: [vscode.Uri, vscode.Diagnostic[]][] = [];
63+
64+
let fresh = true;
65+
let buildErrors = () => {
66+
allErrors = [];
67+
oldLint.forEach(errors => {
68+
allErrors.push.apply(allErrors, errors);
69+
});
70+
diagnosticCollection.set(allErrors);
71+
};
72+
73+
workspaced.lint(document).then((errors: [vscode.Uri, vscode.Diagnostic[]][]) => {
74+
if (target == version) {
75+
oldLint[0] = errors;
76+
buildErrors();
77+
}
6178
});
79+
if (config().get("enableDubLinting", true))
80+
workspaced.dubBuild(document).then((errors: [vscode.Uri, vscode.Diagnostic[]][]) => {
81+
if (target == version) {
82+
oldLint[1] = errors;
83+
buildErrors();
84+
}
85+
});
86+
}
6287
}));
6388

6489
vscode.commands.registerCommand("code-d.switchConfiguration", () => {

src/workspace-d.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as ChildProcess from "child_process"
22
import * as vscode from "vscode"
3+
import * as path from "path"
34
import { EventEmitter } from "events"
45

56
function config() {
67
return vscode.workspace.getConfiguration("d");
78
}
89

9-
const TARGET_VERSION = [2, 1, 0];
10+
const TARGET_VERSION = [2, 2, 0];
1011

1112
export class WorkspaceD extends EventEmitter implements
1213
vscode.CompletionItemProvider,
@@ -47,7 +48,7 @@ export class WorkspaceD extends EventEmitter implements
4748
console.log("WorkspaceD ended with an error:");
4849
console.log(err);
4950
if (err && (<any>err).code == "ENOENT") {
50-
vscode.window.showErrorMessage("'" + path + "' is not a valid executable. Please check your D config!", "Retry").then(s => {
51+
vscode.window.showErrorMessage("'" + path + "' is not a valid executable. Please check your user settings and make sure workspace-d is installed!", "Retry").then(s => {
5152
if (s == "Retry")
5253
self.startWorkspaceD.call(self);
5354
});
@@ -243,7 +244,7 @@ export class WorkspaceD extends EventEmitter implements
243244
});
244245
}
245246

246-
lint(document: vscode.TextDocument): Thenable<vscode.Diagnostic[]> {
247+
lint(document: vscode.TextDocument): Thenable<[vscode.Uri, vscode.Diagnostic[]][]> {
247248
let self = this;
248249
console.log("lint");
249250
return new Promise((resolve, reject) => {
@@ -261,9 +262,38 @@ export class WorkspaceD extends EventEmitter implements
261262
diagnostics.push(new vscode.Diagnostic(range, element.description, self.mapLintType(element.type)));
262263
});
263264
console.log("Resolve");
265+
console.log([[document.uri, diagnostics]]);
266+
resolve([[document.uri, diagnostics]]);
267+
}, reject);
268+
});
269+
}
270+
271+
dubBuild(document: vscode.TextDocument): Thenable<[vscode.Uri, vscode.Diagnostic[]][]> {
272+
console.log("dubBuild");
273+
return new Promise((resolve, reject) => {
274+
if (!this.dubReady)
275+
return resolve(null);
276+
this.request({ cmd: "dub", subcmd: "build" }).then((issues: { line: number, column: number, file: string, type: number, text: string }[]) => {
277+
let diagnostics: [vscode.Uri, vscode.Diagnostic[]][] = [];
278+
if (issues && issues.length)
279+
issues.forEach(element => {
280+
let range = new vscode.Range(Math.max(0, element.line - 1), element.column - 1, Math.max(0, element.line - 1), element.column + 500);
281+
let uri = vscode.Uri.file(path.join(vscode.workspace.rootPath, element.file));
282+
let error = new vscode.Diagnostic(range, element.text, this.mapDubLintType(element.type));
283+
let found = false;
284+
diagnostics.forEach(element => {
285+
if (element[0].fsPath == uri.fsPath) {
286+
found = true;
287+
element[1].push(error);
288+
}
289+
});
290+
if (!found)
291+
diagnostics.push([uri, [error]]);
292+
});
293+
console.log("Resolve");
264294
console.log(diagnostics);
265295
resolve(diagnostics);
266-
}, reject);
296+
});
267297
});
268298
}
269299

@@ -364,6 +394,18 @@ export class WorkspaceD extends EventEmitter implements
364394
}
365395
}
366396

397+
private mapDubLintType(type: number): vscode.DiagnosticSeverity {
398+
switch (type) {
399+
case 2:
400+
return vscode.DiagnosticSeverity.Information;
401+
case 1:
402+
return vscode.DiagnosticSeverity.Warning;
403+
case 0:
404+
default:
405+
return vscode.DiagnosticSeverity.Error;
406+
}
407+
}
408+
367409
private checkVersion() {
368410
this.request({ cmd: "version" }).then(version => {
369411
if (version.major < TARGET_VERSION[0])

0 commit comments

Comments
 (0)