@@ -43,19 +43,28 @@ let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
43
43
44
44
let hasConfigurationCapability : boolean = false ;
45
45
let hasWorkspaceFolderCapability : boolean = false ;
46
- let context : Context = { chapter : Chapter . SOURCE_1 } ;
47
46
47
+ // let context: Context = { chapter: Chapter.SOURCE_1 };
48
+ let contextCache : Map < string , Context > = new Map ( ) ;
48
49
let astCache : Map < string , AST > = new Map ( ) ;
49
50
50
51
function getAST ( uri : string ) : AST {
51
52
if ( astCache . has ( uri ) ) return astCache . get ( uri ) ! ;
52
53
54
+ let context = contextCache . get ( uri )
55
+ if ( ! context ) {
56
+ context = { chapter : Chapter . SOURCE_1 } ;
57
+ console . log ( `No context found for ${ uri } , using default context ${ JSON . stringify ( context ) } ` ) ;
58
+ }
59
+ console . log ( `Creating AST for ${ uri } with context ${ JSON . stringify ( context ) } ` ) ;
60
+
53
61
const ast = new AST ( documents . get ( uri ) ! . getText ( ) , context , uri ) ;
54
62
astCache . set ( uri , ast ) ;
55
63
return ast ;
56
64
}
57
65
58
66
connection . onInitialize ( ( params : InitializeParams ) => {
67
+ connection . console . log ( 'LSP INIT' ) ;
59
68
let capabilities = params . capabilities ;
60
69
61
70
// Does the client support the `workspace/configuration` request?
@@ -103,16 +112,16 @@ connection.onInitialized(() => {
103
112
}
104
113
} ) ;
105
114
106
- // Custom request to set the language version
107
- connection . onRequest ( "setLanguageVersion" , ( params : { version : string } ) => {
108
- if ( Object . keys ( chapter_names ) . includes ( params . version ) ) {
109
- context = { chapter : chapter_names [ params . version as keyof typeof chapter_names ] }
110
- astCache . clear ( ) ;
111
- documents . all ( ) . forEach ( validateTextDocument ) ;
112
- return { success : true } ;
113
- }
114
- else return { success : false } ;
115
- } ) ;
115
+ // // Custom request to set the language version
116
+ // connection.onRequest("setLanguageVersion", (params: { version: string }) => {
117
+ // if (Object.keys(chapter_names).includes(params.version)) {
118
+ // context = { chapter: chapter_names[params.version as keyof typeof chapter_names] }
119
+ // astCache.clear();
120
+ // documents.all().forEach(validateTextDocument);
121
+ // return { success: true };
122
+ // }
123
+ // else return { success: false };
124
+ // });
116
125
117
126
// The content of a text document has changed. This event is emitted
118
127
// when the text document first opened or when its content has changed.
@@ -129,7 +138,7 @@ async function validateTextDocument(document: TextDocument): Promise<void> {
129
138
connection . sendDiagnostics ( { uri : document . uri , diagnostics : getAST ( document . uri ) . getDiagnostics ( ) } ) ;
130
139
}
131
140
132
- // TODO: handle file deletion and creation
141
+ // TODO: handle file deletion and creation
133
142
connection . onDidChangeWatchedFiles ( _change => {
134
143
// Monitored files have change in VS Code
135
144
connection . console . log ( 'We received a file change event' ) ;
@@ -207,6 +216,16 @@ connection.onHover((params: HoverParams): Hover | null => {
207
216
return getAST ( params . textDocument . uri ) . onHover ( position ) ;
208
217
} )
209
218
219
+ connection . onNotification ( "source/publishInfo" , ( info ) => {
220
+ connection . console . log ( "Info" ) ;
221
+ connection . console . log ( info ) ;
222
+
223
+ contextCache = new Map ( Object . entries ( info ) ) ;
224
+ // TODO: We only need to revalidate the documents that have changed
225
+ astCache . clear ( ) ;
226
+ documents . all ( ) . forEach ( validateTextDocument ) ;
227
+ return { success : true } ;
228
+ } )
210
229
211
230
// Make the text document manager listen on the connection
212
231
documents . listen ( connection ) ;
0 commit comments