Skip to content

Commit 0c896a7

Browse files
committed
Improve implementation of autocomplete in web plugin
1 parent db3ba73 commit 0c896a7

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

src/web.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IChannel, IConduit, IPlugin } from "@sourceacademy/conductor/conduit";
22
import { AUTOCOMPLETE_CHANNEL_ID, SYNTAX_CHANNEL_ID, WEB_PLUGIN_ID } from "./constants";
3-
import type { AutoCompleteEntry, AutoCompleteMessage } from "./types/autocomplete";
3+
import type { AutoCompleteMessage, AutoCompleteResponse } from "./types/autocomplete";
44
import type { SyntaxHighlightMessage, SyntaxHighlightData } from "./types/syntax";
55

66
export abstract class BaseAutoCompleteWebPlugin implements IPlugin {
@@ -15,9 +15,28 @@ export abstract class BaseAutoCompleteWebPlugin implements IPlugin {
1515
* @param code The current code in the editor.
1616
* @param row The current row of the cursor in the editor (1-indexed).
1717
* @param column The current column of the cursor in the editor (1-indexed).
18-
* @returns A list of autocomplete entries relevant to the current cursor position in the code.
18+
* @param callback The callback function to handle the autocomplete suggestions received from the runner plugin.
1919
*/
20-
abstract autocomplete(code: string, row: number, column: number): AutoCompleteEntry[];
20+
autocomplete(
21+
code: string,
22+
row: number,
23+
column: number,
24+
callback: (suggestions: AutoCompleteResponse) => void,
25+
) {
26+
const handler = (message: AutoCompleteMessage) => {
27+
if (message.type === "response") {
28+
this.__autoCompleteChannel.unsubscribe(handler);
29+
callback(message);
30+
}
31+
};
32+
this.__autoCompleteChannel.subscribe(handler);
33+
this.__autoCompleteChannel.send({
34+
type: "request",
35+
code,
36+
row,
37+
column,
38+
});
39+
}
2140

2241
/**
2342
* The `loadMode` method is called when the web plugin receives the syntax highlighting data from the runner plugin.

0 commit comments

Comments
 (0)