-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
69 lines (58 loc) · 2.46 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
const vscode = require('vscode');
const vnu = require('vnu-jar');
const { execFile } = require('child_process');
const diagnosticCollection = vscode.languages.createDiagnosticCollection('html-validator');
const validate = /** @param {vscode.TextDocument} document @param {boolean} fromSave */ (document, fromSave) => {
if (!vscode.workspace.getConfiguration('html-validator').get('validate-on-save') && fromSave)
return;
if (vscode.workspace.getConfiguration('html-validator').get('file-types').includes(document.languageId)) {
vscode.window.setStatusBarMessage('Checking HTML...');
const process = execFile('java', ['-jar', '"' + vnu + '"', '--html', '--format json', '-'], { shell: true }, (error, stdout, stderr) => {
const data = JSON.parse(stderr).messages;
diagnosticCollection.clear();
const diagnostics = [];
let errorLevel = false;
for (const err of data) {
if (err.type === 'error')
errorLevel = true;
diagnostics.push(new vscode.Diagnostic(new vscode.Range((err.firstLine || err.lastLine) - 1, err.firstColumn || err.lastColumn - err.hiliteLength, err.lastLine - 1, err.lastColumn), err.message, err.type === 'error' ? vscode.DiagnosticSeverity.Error : vscode.DiagnosticSeverity.Warning));
}
diagnosticCollection.set(document.uri, diagnostics);
if (!fromSave) {
const message = 'HTML Validator: ' + data.length + ' error' + (data.length == 1 ? '' : 's') + ' found';
if (data.length == 0)
vscode.window.showInformationMessage(message);
else if (errorLevel)
vscode.window.showErrorMessage(message);
else
vscode.window.showWarningMessage(message);
}
vscode.window.setStatusBarMessage('');
});
let text = document.getText().replaceAll(/{%.*%}|{{.*}}/gm, '');
if (text.indexOf('---') == 0)
text = text.replace(/---(.|\n)*---/gm, '');
process.stdin.write(text);
process.stdin.end();
}
else if (!fromSave)
vscode.window.showErrorMessage('Please open an HTML file');
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand('html-validator.validate', function () {
if (vscode.window.activeTextEditor)
validate(vscode.window.activeTextEditor.document, false);
else
vscode.window.showErrorMessage('No active text editor');
});
vscode.workspace.onDidSaveTextDocument(document => validate(document, true));
context.subscriptions.push(disposable);
}
function deactivate() { }
module.exports = {
activate,
deactivate
}