11import * as vscode from "vscode" ;
2- import { WitSyntaxValidator } from "./witValidator .js" ;
2+ import { WitSyntaxValidator } from "./validator .js" ;
33
44const 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-
311138export function deactivate ( ) {
312139 // Extension cleanup is handled by context.subscriptions
313140}
0 commit comments