11import { CandidatesCollection } from "antlr4-c3" ;
22import { ParserRuleContext , TerminalNode , Token } from "antlr4ng" ;
33import { MapIniParser } from "../utils/antlr4ng/MapIniParser" ;
4- import { CompletionItem , CompletionItemKind } from "vscode-languageserver" ;
4+ import { CompletionItem , CompletionItemKind , InsertTextFormat } from "vscode-languageserver" ;
55import * as list from '../utils/lists'
66import { RBTree } from "bintrees" ;
77
88
9+
910export function findTokenIndex ( tokens : Token [ ] , offset : number ) : number {
1011 for ( let i = 0 ; i < tokens . length ; i ++ ) {
1112 const token = tokens [ i ] ;
@@ -29,16 +30,36 @@ export function generateCompletionItems(candidates: CandidatesCollection, parser
2930 }
3031
3132 // Clean up the token name
32- const label = tokenName . startsWith ( "'" ) && tokenName . endsWith ( "'" )
33+ let label = tokenName . startsWith ( "'" ) && tokenName . endsWith ( "'" )
3334 ? tokenName . substring ( 1 , tokenName . length - 1 )
3435 : tokenName ;
3536
36- completionItems . push ( {
37- label,
38- kind : CompletionItemKind . Field ,
39- data : tokenType ,
40- documentation : `Keyword: ${ label } ` ,
41- } ) ;
37+ switch ( label ) {
38+
39+ case 'NEWLINE' :
40+ case 'COMMENT' :
41+ break ;
42+
43+ case 'Coords' :
44+ completionItems . push ( {
45+ label,
46+ kind : CompletionItemKind . Snippet ,
47+ insertTextFormat : InsertTextFormat . Snippet ,
48+ detail : 'Insert Left, Top, Right & Bottom' ,
49+ insertText : 'Coords = Left:${1:0} Top:${2:0} Right:${3:0} Bottom:${0:0}' ,
50+ documentation : `Snippet: ${ label } `
51+ } )
52+ break ;
53+
54+ default :
55+ completionItems . push ( {
56+ label,
57+ kind : CompletionItemKind . Field ,
58+ data : tokenType ,
59+ documentation : `Keyword: ${ label } ` ,
60+ } ) ;
61+ break ;
62+ }
4263 }
4364
4465 // Process rule candidates (snippets, templates)
@@ -63,8 +84,6 @@ export function findContextAtPosition(tree: ParserRuleContext, position: number)
6384
6485 console . log ( `Start: ${ tree . start . start } , End: ${ tree . stop . stop } , Position: ${ position } ` )
6586
66- position -= 1 ;
67-
6887 const start = tree . start . start ;
6988 const stop = tree . stop . stop + 1 ;
7089
@@ -96,6 +115,33 @@ export function getContextSpecificCompletions(ruleName: string): CompletionItem[
96115
97116 //TODO: Add retrievels here
98117 switch ( ruleName ) {
118+ case 'cb_command_property' :
119+ completionItems . push ( ...getCompletionItemsFromStringArray ( list . CommandButtonCommandValues ) )
120+ break ;
121+
122+ case 'cb_options_property' :
123+ completionItems . push ( ...getCompletionItemsFromStringArray ( list . CommandButtonOptionValues ) )
124+ break ;
125+
126+ case 'cb_buttonbordertype_property' :
127+ completionItems . push ( ...getCompletionItemsFromStringArray ( list . CommandButtonBorderTypeValues ) )
128+ break ;
129+
130+ case 'object_property' :
131+ completionItems . push ( ...getCompletionItemsFromRBTree ( list . objects ) )
132+ completionItems . push ( ...getCompletionItemsFromRBTree ( list . customObjects ) )
133+ break ;
134+
135+ case 'science_property' :
136+ completionItems . push ( ...getCompletionItemsFromRBTree ( list . science ) )
137+ completionItems . push ( ...getCompletionItemsFromRBTree ( list . customScience ) )
138+ break ;
139+
140+ case 'specialpower_property' :
141+ completionItems . push ( ...getCompletionItemsFromRBTree ( list . specialPower ) )
142+ completionItems . push ( ...getCompletionItemsFromRBTree ( list . customSpecialPower ) )
143+ break ;
144+
99145 case 'upgrade_property' :
100146 // Add completion items relevant to function declarations
101147
@@ -127,5 +173,20 @@ function getCompletionItemsFromRBTree(tree: RBTree<string>): CompletionItem[] {
127173 } ) ;
128174 } ) ;
129175
176+ return completionItems ;
177+ }
178+
179+ function getCompletionItemsFromStringArray ( stringArray : string [ ] ) : CompletionItem [ ] {
180+ const completionItems : CompletionItem [ ] = [ ] ;
181+
182+ stringArray . forEach ( string => {
183+ completionItems . push ( {
184+ label : string ,
185+ kind : CompletionItemKind . Text ,
186+ data : string ,
187+ documentation : `Value: ${ string } ` ,
188+ } ) ;
189+ } ) ;
190+
130191 return completionItems ;
131192}
0 commit comments