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