66 createConnection ,
77 TextDocuments ,
88 Diagnostic ,
9- DiagnosticSeverity ,
109 ProposedFeatures ,
1110 InitializeParams ,
1211 DidChangeConfigurationNotification ,
@@ -15,23 +14,16 @@ import {
1514 TextDocumentPositionParams ,
1615 TextDocumentSyncKind ,
1716 InitializeResult ,
18- ParameterInformation ,
19- integer ,
2017 DidChangeConfigurationParams ,
2118 DocumentFormattingRequest ,
2219 DefinitionParams ,
2320 Definition ,
24- Position ,
25- Location ,
2621 HoverParams ,
27- MarkupContent ,
28- MarkupKind ,
2922 Hover ,
3023 SemanticTokensParams ,
31- SemanticTokensRequest ,
3224 SemanticTokens ,
33- SemanticTokensBuilder ,
34- Range
25+ DidOpenTextDocumentParams ,
26+ SemanticTokensBuilder
3527} from 'vscode-languageserver/node' ;
3628
3729import {
@@ -46,13 +38,13 @@ import { MapIniLexer } from './utils/antlr/MapIniLexer';
4638import { MapIniParser } from './utils/antlr/MapIniParser' ;
4739import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker' ;
4840
49- import * as formatter from './utils/formatter'
50- import { SymbolVisitor } from './utils/symbols/SymbolVisitor' ;
41+ import { formatDocument } from './utils/formatter' ;
5142import { computeSymbolTable , SymbolTable } from './utils/symbols/SymbolTable' ;
52- import { findDefinition , getWordAtPosition } from './utils/definitions' ;
43+ import { findDefinition } from './utils/definitions' ;
5344import { getHoverInformation } from './utils/hover' ;
5445import { tokenModifiers , tokenTypes } from './utils/tokenTypes'
5546import { getSemanticTokens } from './utils/semanticTokens' ;
47+ import { computeDiagnostics } from './utils/diagnostics' ;
5648
5749// Create a connection for the server, using Node's IPC as a transport.
5850// Also include all preview / proposed LSP features.
@@ -71,7 +63,7 @@ let hasDiagnosticRelatedInformationCapability = false;
7163
7264let forceAddModule : boolean = true
7365
74- let symbolTable = new SymbolTable ( )
66+ let symbolTable : Map < string , SymbolTable > = new Map < string , SymbolTable > ( ) ;
7567
7668connection . onInitialize ( ( params : InitializeParams ) => {
7769
@@ -144,7 +136,7 @@ connection.onInitialized(() => {
144136 return null
145137 }
146138
147- return formatter . formatDocument ( document , _edits . options . tabSize )
139+ return formatDocument ( document , _edits . options . tabSize )
148140 } )
149141
150142 connection . client . register ( DidChangeConfigurationNotification . type )
@@ -157,82 +149,70 @@ connection.onInitialized(() => {
157149 forceAddModule = settings . forceAddModule
158150 connection . console . log ( `Updated forceAddmodule to: ${ forceAddModule } ` )
159151 } else {
160- // If setting is not null set forceAddmoule to setting else set forceAddmodule to true
152+ // If setting is not null set forceAddmoule to setting else default to true
161153 change . settings . forceAddModule !== null ? forceAddModule = change . settings . forceAddModule : forceAddModule = true
162154 }
163155 } )
164156 }
165157} ) ;
166158
159+ connection . onDidOpenTextDocument ( ( params : DidOpenTextDocumentParams ) => {
160+ const textDocument = documents . get ( params . textDocument . uri )
161+ symbolTable . set ( textDocument ! . uri , computeSymbolTable ( textDocument ! ) )
162+ } ) ;
163+
167164connection . onHover ( ( params : HoverParams ) : Hover | null => {
168- return getHoverInformation ( params , documents , symbolTable )
165+ return getHoverInformation ( params , documents , symbolTable . get ( params . textDocument . uri ) ! )
169166} ) ;
170167
171168connection . onDefinition ( ( params : DefinitionParams ) : Definition | null => {
172- return findDefinition ( params , documents , symbolTable )
169+ return findDefinition ( params , documents , symbolTable . get ( params . textDocument . uri ) ! )
173170} ) ;
174171
175172connection . onRequest ( "textDocument/semanticTokens/full" , ( params : SemanticTokensParams ) : SemanticTokens => {
176- return getSemanticTokens ( documents , params , symbolTable )
173+ try {
174+ return getSemanticTokens ( documents , params , symbolTable . get ( params . textDocument . uri ) ! )
175+ } catch {
176+ console . log ( 'Failed to generate SemanticTokens, rerun on next change.' )
177+ let tokenBuilder = new SemanticTokensBuilder ( )
178+ return tokenBuilder . build ( )
179+ }
177180} ) ;
178181
179182connection . onRequest ( "textDocument/semanticTokens/range" , ( params : SemanticTokensParams ) : SemanticTokens => {
180- return getSemanticTokens ( documents , params , symbolTable )
183+ try {
184+ return getSemanticTokens ( documents , params , symbolTable . get ( params . textDocument . uri ) ! )
185+ } catch {
186+ console . log ( 'Failed to generate SemanticTokens, rerun on next change.' )
187+ let tokenBuilder = new SemanticTokensBuilder ( )
188+ return tokenBuilder . build ( )
189+ }
181190} ) ;
182191
183192// The content of a text document has changed. This event is emitted
184193// when the text document first opened or when its content has changed.
185194documents . onDidChangeContent ( change => {
186195
187-
188196 if ( parseTimer ) {
189197 clearTimeout ( parseTimer ) ;
190198 }
191199
192200 parseTimer = setTimeout ( async ( ) => {
193201 const textDocument = change . document ;
202+ // Update SymbolTable
203+ symbolTable . set ( textDocument . uri , computeSymbolTable ( textDocument ) )
204+
194205 // Create a listener for tree traversal
195206 const treeListener = new TreeListener ( forceAddModule ) ;
196-
197207 // Parse the document to compute diagnostics
198208 const diagnostics = await computeDiagnostics ( treeListener , textDocument ) ;
209+ treeListener . resetDiagnostics ( ) ;
199210
200211 // Send the updated diagnostics to the client
201212 connection . sendDiagnostics ( { uri : textDocument . uri , diagnostics } ) ;
202-
203- // Reset the tree listener diagnostics for the next run
204- treeListener . resetDiagnostics ( ) ;
205-
206-
207- // Update SymbolTable
208- symbolTable = computeSymbolTable ( textDocument )
209- // connection.console.log(`All Symbols: ${symbolTable.getAllSymbols()}`)
210-
211213 } , parseDelay ) ;
212214} ) ;
213215
214- async function computeDiagnostics ( listener : TreeListener , textDocument : TextDocument ) : Promise < Diagnostic [ ] > {
215- try {
216-
217- const inputStream = CharStreams . fromString ( textDocument . getText ( ) ) ;
218- const lexer = new MapIniLexer ( inputStream ) ;
219- const tokenStream = new CommonTokenStream ( lexer ) ;
220- const parser = new MapIniParser ( tokenStream ) ;
221- parser . removeErrorListeners ( ) ;
222- parser . addErrorListener ( new CustomErrorListener ( textDocument , listener ) ) ;
223-
224- const walker = new ParseTreeWalker ( ) ;
225- const root = parser . program ( ) ;
226- walker . walk ( listener , root ) ;
227-
228- return listener . getDiagnostics ( ) ;
229- } catch ( error ) {
230- // Handle any parsing errors here
231- connection . console . error ( `Error computing diagnostics: ${ error } ` ) ;
232- return [ ] ;
233- }
234- }
235-
236216connection . onDidCloseTextDocument ( e => {
237217 connection . console . log ( `Closed document` )
238218} ) ;
@@ -318,10 +298,6 @@ connection.onCompletionResolve(
318298 }
319299) ;
320300
321-
322-
323-
324-
325301// Make the text document manager listen on the connection
326302// for open, change and close text document events
327303documents . listen ( connection ) ;
0 commit comments