Skip to content

Commit dbf1fd3

Browse files
committed
fix: missing world error failing to parse
bump test wit files to newer versions Refactor validator Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent be62473 commit dbf1fd3

File tree

168 files changed

+8752
-3362
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+8752
-3362
lines changed

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"editor.formatOnSave": true,
33
"editor.defaultFormatter": "vscode.typescript-language-features",
4+
"files.insertFinalNewline": true,
45
"editor.codeActionsOnSave": {
56
"source.fixAll.eslint": "explicit"
67
},
@@ -13,11 +14,11 @@
1314
"editor.formatOnSave": true
1415
},
1516
"[json]": {
16-
"editor.defaultFormatter": "vscode.json-language-features",
17+
"editor.defaultFormatter": "esbenp.prettier-vscode",
1718
"editor.formatOnSave": true
1819
},
1920
"[jsonc]": {
20-
"editor.defaultFormatter": "vscode.json-language-features",
21+
"editor.defaultFormatter": "esbenp.prettier-vscode",
2122
"editor.formatOnSave": true
2223
}
2324
}

prettier.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export default {
1212
semi: true,
1313
singleQuote: false,
1414
printWidth: 120,
15+
endOfLine: "auto",
1516
};

src/errorParser.ts

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
*/
44

55
/**
6-
* Regex pattern to extract error information from WIT validation errors
7-
* Groups:
8-
* 1. Main error message (after "Error: ")
9-
* 2. Detailed error message (after "Caused by:")
10-
* 3. File path (from the arrow line)
11-
* 4. Row number
12-
* 5. Column number
6+
* Regex patterns to extract error information from WIT validation errors
137
*/
14-
const witErrorRegex =
8+
9+
/**
10+
* Pattern for errors with file location information
11+
* Groups: 1=main error, 2=detailed error, 3=file path, 4=row, 5=column
12+
*/
13+
const witErrorWithLocationRegex =
1514
/Error:\s*([^\n]+)(?:\n\nCaused by:\s*\n\s*([^\n]+(?:\n[^\n-]+)*?))?[\s\S]*?-->\s*([^:]+):(\d+):(\d+)/;
1615

16+
/**
17+
* Pattern for errors without file location information
18+
* Groups: 1=main error
19+
*/
20+
const witErrorWithoutLocationRegex = /(?:Error|RuntimeError):\s*([^\n]+)/;
21+
1722
/**
1823
* Extracts error information from a WIT validation error stack trace
1924
* @param errorStack - The error stack trace string
@@ -26,19 +31,33 @@ export function extractErrorInfo(errorStack: string): {
2631
row?: number;
2732
column?: number;
2833
} | null {
29-
const match = errorStack.match(witErrorRegex);
34+
// First try to match errors with file location
35+
let match = errorStack.match(witErrorWithLocationRegex);
3036

31-
if (!match) {
32-
return null;
37+
if (match) {
38+
const [, mainError, detailedError, filePath, rowStr, columnStr] = match;
39+
return {
40+
mainError: mainError.trim(),
41+
detailedError: detailedError?.trim(),
42+
filePath: filePath.trim(),
43+
row: parseInt(rowStr, 10),
44+
column: parseInt(columnStr, 10),
45+
};
3346
}
3447

35-
const [, mainError, detailedError, filePath, rowStr, columnStr] = match;
48+
// If no location match, try simple error format
49+
match = errorStack.match(witErrorWithoutLocationRegex);
50+
51+
if (match) {
52+
const [, mainError] = match;
53+
return {
54+
mainError: mainError.trim(),
55+
detailedError: undefined,
56+
filePath: undefined,
57+
row: undefined,
58+
column: undefined,
59+
};
60+
}
3661

37-
return {
38-
mainError: mainError.trim(),
39-
detailedError: detailedError?.trim(),
40-
filePath: filePath.trim(),
41-
row: parseInt(rowStr, 10),
42-
column: parseInt(columnStr, 10),
43-
};
62+
return null;
4463
}

src/extension.ts

Lines changed: 5 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from "vscode";
2-
import { WitSyntaxValidator } from "./witValidator.js";
2+
import { WitSyntaxValidator } from "./validator.js";
33

44
const builtinTypes = [
55
"u8",
@@ -89,20 +89,20 @@ export function activate(context: vscode.ExtensionContext) {
8989
return;
9090
}
9191

92-
await syntaxCheckCurrentFile(activeEditor, validator);
92+
await validator.syntaxCheckCurrentFile(activeEditor);
9393
});
9494

9595
// Register syntax check for workspace command
9696
const syntaxCheckWorkspaceCommand = vscode.commands.registerCommand("wit-idl.syntaxCheckWorkspace", async () => {
97-
await syntaxCheckWorkspace(validator);
97+
await validator.syntaxCheckWorkspace();
9898
});
9999

100100
// Auto-validate on file save
101101
const onSaveListener = vscode.workspace.onDidSaveTextDocument(async (document) => {
102102
if (document.languageId === "wit") {
103103
const editor = vscode.window.visibleTextEditors.find((e) => e.document === document);
104104
if (editor) {
105-
await syntaxCheckCurrentFile(editor, validator);
105+
await validator.syntaxCheckCurrentFile(editor);
106106
}
107107
}
108108
});
@@ -112,7 +112,7 @@ export function activate(context: vscode.ExtensionContext) {
112112
if (document.languageId === "wit") {
113113
const editor = vscode.window.visibleTextEditors.find((e) => e.document === document);
114114
if (editor) {
115-
await syntaxCheckCurrentFile(editor, validator);
115+
await validator.syntaxCheckCurrentFile(editor);
116116
}
117117
}
118118
});
@@ -135,179 +135,6 @@ export function activate(context: vscode.ExtensionContext) {
135135
);
136136
}
137137

138-
/**
139-
* Perform syntax check on the current active WIT file
140-
* @param editor - The active text editor
141-
* @param validator - The WIT syntax validator instance
142-
*/
143-
async function syntaxCheckCurrentFile(editor: vscode.TextEditor, validator: WitSyntaxValidator): Promise<void> {
144-
const document = editor.document;
145-
const content = document.getText();
146-
147-
try {
148-
// Clear existing diagnostics for this file
149-
validator.clearDiagnostics(document.uri);
150-
151-
// Validate the file
152-
const errorInfo = await validator.validate(document.uri.fsPath, content);
153-
154-
if (errorInfo) {
155-
// Create a diagnostic from the error information
156-
const diagnostic = createDiagnosticFromError(errorInfo, document);
157-
158-
// Set the diagnostic for this file
159-
validator.getDiagnosticCollection().set(document.uri, [diagnostic]);
160-
161-
// Show error message
162-
vscode.window.showErrorMessage(
163-
`WIT Syntax Error in ${vscode.workspace.asRelativePath(document.uri)}: ${errorInfo.mainError}`
164-
);
165-
} else {
166-
// Validation succeeded - show success message
167-
vscode.window.showInformationMessage(`WIT file ${vscode.workspace.asRelativePath(document.uri)} is valid`);
168-
}
169-
} catch (error) {
170-
// Handle unexpected errors
171-
console.error("Error during WIT validation:", error);
172-
vscode.window.showErrorMessage(
173-
`Failed to validate WIT file: ${error instanceof Error ? error.message : String(error)}`
174-
);
175-
}
176-
}
177-
178-
/**
179-
* Create a VS Code diagnostic from WIT error information
180-
* @param errorInfo - The parsed error information
181-
* @param document - The text document
182-
* @returns A VS Code diagnostic
183-
*/
184-
function createDiagnosticFromError(
185-
errorInfo: NonNullable<ReturnType<typeof import("./errorParser.js").extractErrorInfo>>,
186-
document: vscode.TextDocument
187-
): vscode.Diagnostic {
188-
// Convert 1-based row/column to 0-based for VS Code
189-
const line = Math.max(0, (errorInfo.row || 1) - 1);
190-
const character = Math.max(0, (errorInfo.column || 1) - 1);
191-
192-
// Try to get the actual line length for better range highlighting
193-
const documentLine = document.lineAt(Math.min(line, document.lineCount - 1));
194-
const endCharacter = Math.min(character + 10, documentLine.text.length); // Highlight ~10 characters or to end of line
195-
196-
const range = new vscode.Range(new vscode.Position(line, character), new vscode.Position(line, endCharacter));
197-
198-
const message = errorInfo.detailedError || errorInfo.mainError || "Unknown WIT syntax error";
199-
200-
const diagnostic = new vscode.Diagnostic(range, message, vscode.DiagnosticSeverity.Error);
201-
202-
// Add additional information
203-
diagnostic.source = "wit-syntax";
204-
diagnostic.code = "wit-parse-error";
205-
206-
if (errorInfo.mainError && errorInfo.detailedError) {
207-
diagnostic.relatedInformation = [
208-
new vscode.DiagnosticRelatedInformation(
209-
new vscode.Location(document.uri, range),
210-
`Context: ${errorInfo.mainError}`
211-
),
212-
];
213-
}
214-
215-
return diagnostic;
216-
}
217-
218-
/**
219-
* Perform syntax check on all WIT files in the workspace
220-
* @param validator - The WIT syntax validator instance
221-
*/
222-
async function syntaxCheckWorkspace(validator: WitSyntaxValidator): Promise<void> {
223-
const witFiles = await vscode.workspace.findFiles("**/*.wit", "**/node_modules/**");
224-
225-
if (witFiles.length === 0) {
226-
vscode.window.showInformationMessage("No WIT files found in workspace");
227-
return;
228-
}
229-
230-
// Clear all existing diagnostics
231-
validator.clearAllDiagnostics();
232-
233-
let totalErrors = 0;
234-
let totalFiles = 0;
235-
const validFiles: string[] = [];
236-
const errorFiles: string[] = [];
237-
238-
await vscode.window.withProgress(
239-
{
240-
location: vscode.ProgressLocation.Notification,
241-
title: "Checking WIT files syntax...",
242-
cancellable: false,
243-
},
244-
async (progress) => {
245-
for (let i = 0; i < witFiles.length; i++) {
246-
const file = witFiles[i];
247-
const fileName = vscode.workspace.asRelativePath(file);
248-
249-
progress.report({
250-
message: `Checking ${fileName} (${i + 1}/${witFiles.length})`,
251-
increment: 100 / witFiles.length,
252-
});
253-
254-
try {
255-
const document = await vscode.workspace.openTextDocument(file);
256-
const content = document.getText();
257-
const errorInfo = await validator.validate(document.uri.fsPath, content);
258-
259-
totalFiles++;
260-
261-
if (errorInfo) {
262-
// Create and set diagnostic
263-
const diagnostic = createDiagnosticFromError(errorInfo, document);
264-
validator.getDiagnosticCollection().set(document.uri, [diagnostic]);
265-
266-
totalErrors++;
267-
errorFiles.push(fileName);
268-
} else {
269-
// File is valid
270-
validFiles.push(fileName);
271-
}
272-
} catch (error) {
273-
console.error(`Error checking ${fileName}:`, error);
274-
totalErrors++;
275-
errorFiles.push(fileName);
276-
}
277-
}
278-
}
279-
);
280-
281-
// Show summary
282-
const validCount = totalFiles - totalErrors;
283-
const message = `WIT Syntax Check Complete: ${validCount}/${totalFiles} files valid, ${totalErrors} error(s)`;
284-
285-
if (totalErrors > 0) {
286-
vscode.window.showErrorMessage(message);
287-
288-
// Show details in output channel
289-
const outputChannel = vscode.window.createOutputChannel("WIT Syntax Check");
290-
outputChannel.appendLine("WIT Syntax Check Results:");
291-
outputChannel.appendLine(`Total files checked: ${totalFiles}`);
292-
outputChannel.appendLine(`Valid files: ${validCount}`);
293-
outputChannel.appendLine(`Files with errors: ${totalErrors}`);
294-
295-
if (errorFiles.length > 0) {
296-
outputChannel.appendLine("\nFiles with errors:");
297-
errorFiles.forEach((file) => outputChannel.appendLine(` - ${file}`));
298-
}
299-
300-
if (validFiles.length > 0) {
301-
outputChannel.appendLine("\nValid files:");
302-
validFiles.forEach((file) => outputChannel.appendLine(` - ${file}`));
303-
}
304-
305-
outputChannel.show();
306-
} else {
307-
vscode.window.showInformationMessage(message);
308-
}
309-
}
310-
311138
export function deactivate() {
312139
// Extension cleanup is handled by context.subscriptions
313140
}

0 commit comments

Comments
 (0)