fix(vscode): repair WRN language diagnostics and activation
This commit is contained in:
@@ -76,11 +76,35 @@ function stripComments(source) {
|
||||
return source.replace(/<!--[\s\S]*?-->/g, (comment) => comment.replace(/[^\n]/g, " "));
|
||||
}
|
||||
|
||||
function maskLeadingTrivia(source) {
|
||||
const masked = [...source];
|
||||
let offset = 0;
|
||||
|
||||
while (offset < source.length) {
|
||||
if (/\s/u.test(source[offset])) {
|
||||
offset += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!source.startsWith("//", offset)) break;
|
||||
|
||||
while (offset < source.length && source[offset] !== "\n") {
|
||||
masked[offset] = " ";
|
||||
offset += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return masked.join("");
|
||||
}
|
||||
|
||||
function findTopLevelDeclaration(document, source) {
|
||||
const match = TOP_LEVEL_PATTERN.exec(source);
|
||||
const sourceWithoutLeadingTrivia = maskLeadingTrivia(source);
|
||||
const match = TOP_LEVEL_PATTERN.exec(sourceWithoutLeadingTrivia);
|
||||
|
||||
if (!match) {
|
||||
const firstMeaningfulLine = source.split(/\r?\n/).findIndex((line) => line.trim().length > 0);
|
||||
const firstMeaningfulLine = sourceWithoutLeadingTrivia
|
||||
.split(/\r?\n/)
|
||||
.findIndex((line) => line.trim().length > 0);
|
||||
|
||||
return {
|
||||
diagnostic: lineDiagnostic(
|
||||
@@ -134,6 +158,13 @@ function validateBalancedCharacters(document, source) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (source.startsWith("//", index)) {
|
||||
const lineEnd = source.indexOf("\n", index + 2);
|
||||
|
||||
index = lineEnd === -1 ? source.length : lineEnd;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (character === '"' || character === "'") {
|
||||
quote = character;
|
||||
continue;
|
||||
@@ -1063,6 +1094,14 @@ function registerDiagnostics(context) {
|
||||
|
||||
if (previousTimer) {
|
||||
clearTimeout(previousTimer);
|
||||
timers.delete(key);
|
||||
}
|
||||
|
||||
const configuration = vscode.workspace.getConfiguration("wrnexus", document.uri);
|
||||
|
||||
if (!configuration.get("diagnostics.enable", true)) {
|
||||
collection.delete(document.uri);
|
||||
return;
|
||||
}
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
@@ -1089,6 +1128,16 @@ function registerDiagnostics(context) {
|
||||
|
||||
vscode.workspace.onDidSaveTextDocument(update),
|
||||
|
||||
vscode.workspace.onDidChangeConfiguration((event) => {
|
||||
if (!event.affectsConfiguration("wrnexus.diagnostics.enable")) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const document of vscode.workspace.textDocuments) {
|
||||
update(document);
|
||||
}
|
||||
}),
|
||||
|
||||
vscode.workspace.onDidCloseTextDocument((document) => {
|
||||
const key = document.uri.toString();
|
||||
const timer = timers.get(key);
|
||||
@@ -1114,6 +1163,8 @@ function registerDiagnostics(context) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
findTopLevelDeclaration,
|
||||
maskLeadingTrivia,
|
||||
registerDiagnostics,
|
||||
validateBalancedCharacters,
|
||||
validateDocument,
|
||||
|
||||
Reference in New Issue
Block a user