-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrorChecker.ts
More file actions
51 lines (41 loc) · 1.98 KB
/
errorChecker.ts
File metadata and controls
51 lines (41 loc) · 1.98 KB
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
//@ts-ignore
import * as vscode from 'vscode';
// Function to update diagnostics for the document
export function updateDiagnostics(doc: vscode.TextDocument, declaredVariables: Set<string>, collection: vscode.DiagnosticCollection): void {
const diagnostics: vscode.Diagnostic[] = [];
const text = doc.getText();
const lines = text.split(/\r?\n/);
lines.forEach((line, i) => {
const lineText = line.trim();
// 1. Detect invalid `;;` at end of line (example)
if (lineText.endsWith(';')) {
const range = new vscode.Range(i, line.length - 2, i, line.length);
diagnostics.push(new vscode.Diagnostic(range, "Unexpected ';' at end of line", vscode.DiagnosticSeverity.Error));
}
const trimmedLine = line.trim();
// Direct string matching (like Python)
if (trimmedLine.startsWith("} else") ||
trimmedLine.startsWith("} elif") ||
trimmedLine.startsWith("} except") ||
trimmedLine.startsWith("} def") ||
trimmedLine.startsWith("} class") ||
trimmedLine.startsWith("} case")) {
const range = new vscode.Range(i, 0, i, line.length);
diagnostics.push(new vscode.Diagnostic(range, `Misplaced '${trimmedLine}' statement.`, vscode.DiagnosticSeverity.Error));
}
});
collection.set(doc.uri, diagnostics);
}
// Function to detect and collect declared variables in the document
export function getDeclaredVariables(document: vscode.TextDocument): Set<string> {
const declaredVariables = new Set<string>();
const text = document.getText();
// Regular expression to match variable declarations (e.g., `x = 10`, `my_var = "hello"`)
const varRegex = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*=/g;
let match;
while ((match = varRegex.exec(text)) !== null) {
// Add variable names to the set (Set automatically avoids duplicates)
declaredVariables.add(match[1]);
}
return declaredVariables;
}