-
Notifications
You must be signed in to change notification settings - Fork 5
/
ameba.ts
129 lines (112 loc) · 4.34 KB
/
ameba.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { exec } from 'child_process';
import * as path from 'path';
import {
commands,
Diagnostic,
DiagnosticCollection,
DiagnosticSeverity,
Range,
TextDocument,
Uri,
window,
workspace
} from 'vscode';
import { AmebaOutput } from './amebaOutput';
import { AmebaConfig, getConfig } from './configuration';
import { Task, TaskQueue } from './taskQueue';
export class Ameba {
private diag: DiagnosticCollection;
private taskQueue: TaskQueue;
public config: AmebaConfig;
constructor(diagnostics: DiagnosticCollection) {
this.diag = diagnostics;
this.taskQueue = new TaskQueue();
this.config = getConfig();
}
public execute(document: TextDocument): void {
if (document.languageId !== 'crystal' || document.isUntitled || document.uri.scheme !== 'file') {
return;
}
const args = [this.config.command, document.fileName, '--format', 'json'];
if (this.config.configFileName.length) {
const dir = workspace.getWorkspaceFolder(document.uri)!.uri.fsPath;
args.push('--config', path.join(dir, this.config.configFileName));
}
const task = new Task(document.uri, token => {
const proc = exec(args.join(' '), (err, stdout, stderr) => {
if (token.isCanceled) return;
this.diag.delete(document.uri);
if (err && stderr.length) {
if ((process.platform == 'win32' && err.code === 1) || err.code === 127) {
window.showErrorMessage(
`Could not execute Ameba file${args[0] === 'ameba' ? '.' : ` at ${args[0]}`}`,
'Disable (workspace)'
).then(
disable => disable && commands.executeCommand('crystal.ameba.disable'),
_ => {}
);
} else {
window.showErrorMessage(stderr);
}
return;
}
let results: AmebaOutput;
try {
results = JSON.parse(stdout);
} catch (err) {
console.error(`Ameba: failed parsing JSON: ${err}`);
window.showErrorMessage('Ameba: failed to parse JSON response.');
return;
}
if (!results.summary.issues_count) return;
const diagnostics: [Uri, Diagnostic[]][] = [];
for (let source of results.sources) {
if (!source.issues.length) continue;
let parsed: Diagnostic[] = [];
source.issues.forEach(issue => {
let start = issue.location;
let end = issue.end_location;
if (!end.line || !end.column) {
end = start;
}
const range = new Range(
start.line - 1,
start.column - 1,
end.line - 1,
end.column
);
const diag = new Diagnostic(
range,
`[${issue.rule_name}] ${issue.message}`,
this.parseSeverity(issue.severity)
);
parsed.push(diag);
});
diagnostics.push([document.uri, parsed]);
}
this.diag.set(diagnostics);
});
return () => proc.kill();
});
this.taskQueue.enqueue(task);
}
private parseSeverity(severity: string): DiagnosticSeverity {
switch(severity) {
case 'Error':
return DiagnosticSeverity.Error;
case 'Warning':
return DiagnosticSeverity.Warning;
case 'Refactoring':
return DiagnosticSeverity.Hint;
default:
return DiagnosticSeverity.Information;
}
}
public clear(document: TextDocument): void {
let uri = document.uri;
if (uri.scheme === 'file') {
this.taskQueue.cancel(uri);
this.diag.delete(uri);
}
}
}