11import { IChannel , IConduit , IPlugin } from "@sourceacademy/conductor/conduit" ;
22import { 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" ;
44import type { SyntaxHighlightMessage , SyntaxHighlightData } from "./types/syntax" ;
55
66export 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