-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
86 lines (79 loc) · 2.38 KB
/
extension.js
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
const vscode = require("vscode");
const { spawn } = require("child_process");
const path = require("path");
const _typesToString = {
WORD: "Mot inconnu",
};
let _decorations = {};
let _decorationTypes = {};
function _transformErrorInDecoration(line, error) {
const typeName = error.aColor
? `color-${error.aColor.join("-")}`
: `color-${error.sType.toLowerCase()}`;
const color = error.aColor ? `rgb(${error.aColor.join(",")})` : `grey`;
if (!_decorationTypes[typeName]) {
_decorationTypes[typeName] = vscode.window.createTextEditorDecorationType({
backgroundColor: color,
});
}
const decoration = {
range: new vscode.Range(
new vscode.Position(line - 1, error.nStart),
new vscode.Position(line - 1, error.nEnd)
),
hoverMessage: error.sMessage || _typesToString[error.sType],
};
_decorations[typeName] !== undefined
? _decorations[typeName].push(decoration)
: (_decorations[typeName] = [decoration]);
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand(
"grammalecte.run",
function () {
const editor = vscode.window.activeTextEditor;
if (editor) {
const file = editor.document.uri.path;
const grammalecte = spawn("python3", [
context.asAbsolutePath("grammalecte-cli.py"),
"--json",
"--file",
file,
]);
grammalecte.stdout.on("data", (data) => {
_decorations = {};
const json = JSON.parse(data.toString());
for (let paragraph of json.data) {
for (let grammarError of paragraph.lGrammarErrors) {
_transformErrorInDecoration(paragraph.iParagraph, grammarError);
}
for (let spellingError of paragraph.lSpellingErrors) {
_transformErrorInDecoration(paragraph.iParagraph, spellingError);
}
}
for (let typeName in _decorations) {
if (_decorations[typeName].length > 0) {
editor.setDecorations(
_decorationTypes[typeName],
_decorations[typeName]
);
}
}
});
}
}
);
context.subscriptions.push(disposable);
}
exports.activate = activate;
function deactivate() {
_decorations = {};
_decorationTypes = {};
}
module.exports = {
activate,
deactivate,
};