Skip to content

Commit

Permalink
feat: show decorations with different colors for all errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatWizard committed Jul 11, 2021
1 parent 48604a7 commit 87321da
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,33 @@ const vscode = require("vscode");
const { spawn } = require("child_process");
const path = require("path");

function _transformErrorInDecorator(line, error) {
console.log(line, error.nStart, error.nEnd, error.sMessage);
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]);
}

/**
Expand All @@ -23,13 +48,22 @@ function activate(context) {
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) {
_transformErrorInDecorator(paragraph.iParagraph, grammarError);
_transformErrorInDecoration(paragraph.iParagraph, grammarError);
}
for (let spellingError of paragraph.lSpellingErrors) {
_transformErrorInDecorator(paragraph.iParagraph, spellingError);
_transformErrorInDecoration(paragraph.iParagraph, spellingError);
}
}
for (let typeName in _decorations) {
if (_decorations[typeName].length > 0) {
editor.setDecorations(
_decorationTypes[typeName],
_decorations[typeName]
);
}
}
});
Expand All @@ -41,7 +75,10 @@ function activate(context) {
}
exports.activate = activate;

function deactivate() {}
function deactivate() {
_decorations = {};
_decorationTypes = {};
}

module.exports = {
activate,
Expand Down

0 comments on commit 87321da

Please sign in to comment.