@@ -17,7 +17,6 @@ import {
17
17
DocumentSymbol ,
18
18
HoverParams ,
19
19
Hover ,
20
- DocumentHighlight ,
21
20
} from 'vscode-languageserver/node' ;
22
21
23
22
import { TextDocument } from 'vscode-languageserver-textdocument' ;
@@ -27,13 +26,6 @@ import { sourceLocToRange } from './utils';
27
26
import { AST } from './ast' ;
28
27
import { Chapter , Context } from './types' ;
29
28
30
- const SECTION = "\u00A7" ;
31
- const chapter_names = {
32
- [ `Source ${ SECTION } 1` ] : Chapter . SOURCE_1 ,
33
- [ `Source ${ SECTION } 2` ] : Chapter . SOURCE_2 ,
34
- [ `Source ${ SECTION } 3` ] : Chapter . SOURCE_3 ,
35
- [ `Source ${ SECTION } 4` ] : Chapter . SOURCE_4
36
- } ;
37
29
38
30
// Create a connection for the server, using Node's IPC as a transport.
39
31
// Also include all preview / proposed LSP features.
@@ -45,19 +37,21 @@ let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
45
37
let hasConfigurationCapability : boolean = false ;
46
38
let hasWorkspaceFolderCapability : boolean = false ;
47
39
48
- // let context: Context = { chapter: Chapter.SOURCE_1 };
40
+ // Map between file uri string and context object
49
41
let contextCache : Map < string , Context > = new Map ( ) ;
50
42
let astCache : Map < string , AST > = new Map ( ) ;
43
+ const DEFAULT_CONTEXT : Context = { chapter : Chapter . SOURCE_1 } ;
51
44
52
45
function getAST ( uri : string ) : AST {
53
46
if ( astCache . has ( uri ) ) return astCache . get ( uri ) ! ;
54
47
55
- let context = contextCache . get ( uri )
48
+ let context = contextCache . get ( uri ) ;
56
49
if ( ! context ) {
57
- context = { chapter : Chapter . SOURCE_1 } ;
58
- console . log ( `No context found for ${ uri } , using default context ${ JSON . stringify ( context ) } ` ) ;
50
+ context = DEFAULT_CONTEXT ;
51
+ contextCache . set ( uri , DEFAULT_CONTEXT ) ;
52
+ console . log ( `No context found for ${ uri } , using default context ${ JSON . stringify ( DEFAULT_CONTEXT ) } ` ) ;
59
53
}
60
- console . log ( `Creating AST for ${ uri } with context ${ JSON . stringify ( context ) } ` ) ;
54
+ // console.log(`Creating AST for ${uri} with context ${JSON.stringify(context)}`);
61
55
62
56
// const ast = new AST(documents.get(uri)!.getText(), context, uri, 4);
63
57
const ast = new AST ( documents . get ( uri ) ! . getText ( ) , context , uri ) ;
@@ -66,7 +60,7 @@ function getAST(uri: string): AST {
66
60
}
67
61
68
62
connection . onInitialize ( ( params : InitializeParams ) => {
69
- connection . console . log ( 'LSP INIT' ) ;
63
+ connection . console . log ( 'LSP INIT' ) ;
70
64
let capabilities = params . capabilities ;
71
65
72
66
// Does the client support the `workspace/configuration` request?
@@ -193,7 +187,7 @@ connection.onDocumentHighlight((params: DocumentHighlightParams) => {
193
187
const document = documents . get ( params . textDocument . uri ) ;
194
188
if ( ! document ) return null ;
195
189
196
- return getAST ( params . textDocument . uri ) . getOccurences ( params . position ) . map ( x => ( { range : x } ) ) ;
190
+ return getAST ( params . textDocument . uri ) . getOccurences ( params . position ) . map ( x => ( { range : x } ) ) ;
197
191
} )
198
192
199
193
connection . onDocumentSymbol ( async ( params : DocumentSymbolParams ) : Promise < DocumentSymbol [ ] | null > => {
@@ -218,19 +212,34 @@ connection.onHover((params: HoverParams): Hover | null => {
218
212
return getAST ( params . textDocument . uri ) . onHover ( position ) ;
219
213
} )
220
214
221
- connection . onNotification ( "source/publishInfo" , ( info ) => {
215
+ connection . onRequest ( "source/publishInfo" , ( info : { [ uri : string ] : Context } ) => {
222
216
connection . console . log ( "Info" ) ;
223
- connection . console . log ( info ) ;
217
+ connection . console . log ( JSON . stringify ( info ) ) ;
218
+
219
+ for ( const [ uri , context ] of Object . entries ( info ) ) {
220
+ const document = documents . get ( uri ) ;
221
+ if ( document ) {
222
+ let oldContext = contextCache . get ( uri ) ;
223
+ if ( ! oldContext ) {
224
+ oldContext = context ;
225
+ contextCache . set ( uri , context ) ;
226
+ astCache . delete ( uri ) ;
227
+ validateTextDocument ( document )
228
+ }
229
+ // Check if context changed
230
+ else if ( context . chapter !== oldContext . chapter || context . prelude !== oldContext . prelude ) {
231
+ contextCache . set ( uri , context ) ;
232
+ astCache . delete ( uri ) ;
233
+ validateTextDocument ( document ) ;
234
+ }
235
+ }
236
+ }
224
237
225
- contextCache = new Map ( Object . entries ( info ) ) ;
226
- // TODO: We only need to revalidate the documents that have changed
227
- astCache . clear ( ) ;
228
- documents . all ( ) . forEach ( validateTextDocument ) ;
229
238
return { success : true } ;
230
239
} )
231
240
232
241
// Make the text document manager listen on the connection
233
242
documents . listen ( connection ) ;
234
243
235
244
// Listen on the connection
236
- connection . listen ( ) ;
245
+ connection . listen ( ) ;
0 commit comments