Skip to content

Commit e1e2429

Browse files
authored
Merge pull request #2 from source-academy/hz/info
support for tracking source chapters per file
2 parents c4ff02b + ed6d6ec commit e1e2429

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

client/src/extension.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ export function activate(context: ExtensionContext) {
5454
clientOptions
5555
);
5656

57-
// Start the client. This will also launch the server
58-
client.start();
57+
// Start the client. This will also launch the server
58+
(async function () {
59+
console.log("Going to call client.start()");
60+
await client.start();
61+
console.log("client.start() called");
62+
})();
5963

6064
context.subscriptions.push(
6165
commands.registerCommand("source.setLanguageVersion", async () => {

server/src/server.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,29 @@ let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
4444

4545
let hasConfigurationCapability: boolean = false;
4646
let hasWorkspaceFolderCapability: boolean = false;
47-
let context: Context = { chapter: Chapter.SOURCE_1 };
4847

48+
// let context: Context = { chapter: Chapter.SOURCE_1 };
49+
let contextCache: Map<string, Context> = new Map();
4950
let astCache: Map<string, AST> = new Map();
5051

5152
function getAST(uri: string): AST {
5253
if (astCache.has(uri)) return astCache.get(uri)!;
5354

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+
5462
// const ast = new AST(documents.get(uri)!.getText(), context, uri, 4);
5563
const ast = new AST(documents.get(uri)!.getText(), context, uri);
5664
astCache.set(uri, ast);
5765
return ast;
5866
}
5967

6068
connection.onInitialize((params: InitializeParams) => {
69+
connection.console.log('LSP INIT');
6170
let capabilities = params.capabilities;
6271

6372
// Does the client support the `workspace/configuration` request?
@@ -105,16 +114,16 @@ connection.onInitialized(() => {
105114
}
106115
});
107116

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+
// });
118127

119128
// The content of a text document has changed. This event is emitted
120129
// when the text document first opened or when its content has changed.
@@ -131,7 +140,7 @@ async function validateTextDocument(document: TextDocument): Promise<void> {
131140
connection.sendDiagnostics({ uri: document.uri, diagnostics: getAST(document.uri).getDiagnostics() });
132141
}
133142

134-
// TODO: handle file deletion and creation
143+
// TODO: handle file deletion and creation
135144
connection.onDidChangeWatchedFiles(_change => {
136145
// Monitored files have change in VS Code
137146
connection.console.log('We received a file change event');
@@ -209,6 +218,17 @@ connection.onHover((params: HoverParams): Hover | null => {
209218
return getAST(params.textDocument.uri).onHover(position);
210219
})
211220

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+
212232
// Make the text document manager listen on the connection
213233
documents.listen(connection);
214234

0 commit comments

Comments
 (0)