@@ -13,10 +13,11 @@ export interface ValidationError {
1313 endColumn : number ;
1414}
1515
16- interface Token {
16+ export interface Token {
1717 text : string ;
1818 line : number ;
1919 column : number ;
20+ whiteSpaceAfter ?: string ;
2021}
2122
2223export type WordCompletion = RequiredCompletionParts & Partial < monaco . languages . CompletionItem > ;
@@ -96,24 +97,29 @@ export class NegatableWord implements AbstractWord {
9697}
9798
9899export class AutoCompleteTree {
99- constructor ( private roots : AutoCompleteNode [ ] ) { }
100+ constructor ( protected roots : AutoCompleteNode < AbstractWord > [ ] ) { }
100101
101- private tokenize ( text : string [ ] ) : Token [ ] {
102+ protected tokenize ( text : string [ ] ) : Token [ ] {
102103 if ( ! text || text . length == 0 ) {
103104 return [ ] ;
104105 }
105106
106107 const tokens : Token [ ] = [ ] ;
107108 for ( const [ lineNumber , line ] of text . entries ( ) ) {
108- const lineTokens = line . split ( / \s + / ) . filter ( ( t ) => t . length > 0 ) ;
109+ const lineTokens = line . split ( / ( \s + ) / ) ;
109110 let column = 0 ;
110- for ( const token of lineTokens ) {
111- column = line . indexOf ( token , column ) ;
112- tokens . push ( {
113- text : token ,
114- line : lineNumber + 1 ,
115- column : column + 1 ,
116- } ) ;
111+ for ( let i = 0 ; i < lineTokens . length ; i += 2 ) {
112+ const token = lineTokens [ i ] ;
113+ if ( token . length > 0 ) {
114+ tokens . push ( {
115+ text : token ,
116+ line : lineNumber + 1 ,
117+ column : column + 1 ,
118+ whiteSpaceAfter : lineTokens [ i + 1 ] ,
119+ } ) ;
120+ }
121+ column += token . length ;
122+ column += lineTokens [ i + 1 ] ? lineTokens [ i + 1 ] . length : 0 ; // Add whitespace length
117123 }
118124 }
119125
@@ -199,6 +205,7 @@ export class AutoCompleteTree {
199205 column : lines [ lines . length - 1 ] . length + 1 ,
200206 } ) ;
201207 }
208+
202209 let result : WordCompletion [ ] = [ ] ;
203210 if ( tokens . length == 0 ) {
204211 for ( const r of this . roots ) {
@@ -214,14 +221,16 @@ export class AutoCompleteTree {
214221 nodes : AutoCompleteNode [ ] ,
215222 tokens : Token [ ] ,
216223 index : number ,
224+ cameFromFinal = false ,
217225 skipStartCheck = false ,
218226 ) : WordCompletion [ ] {
219227 // check for new start
220-
221228 if ( ! skipStartCheck && tokens [ index ] . column == 1 ) {
222229 const matchesAnyRoot = this . roots . some ( ( n ) => n . word . verifyWord ( tokens [ index ] . text ) . length === 0 ) ;
223230 if ( matchesAnyRoot ) {
224- return this . completeNode ( this . roots , tokens , index , true ) ;
231+ return this . completeNode ( this . roots , tokens , index , cameFromFinal , true ) ;
232+ } else if ( cameFromFinal || nodes . length == 0 ) {
233+ return this . completeNode ( [ ...this . roots , ...nodes ] , tokens , index , cameFromFinal , true ) ;
225234 }
226235 }
227236
@@ -233,10 +242,10 @@ export class AutoCompleteTree {
233242 return result ;
234243 }
235244 for ( const n of nodes ) {
236- if ( ! n . word . verifyWord ( tokens [ index ] . text ) ) {
245+ if ( n . word . verifyWord ( tokens [ index ] . text ) . length > 0 ) {
237246 continue ;
238247 }
239- result = result . concat ( this . completeNode ( n . children , tokens , index + 1 ) ) ;
248+ result = result . concat ( this . completeNode ( n . children , tokens , index + 1 , n . canBeFinal || false ) ) ;
240249 }
241250 return result ;
242251 }
@@ -283,9 +292,9 @@ function deduplicateErrors(errors: ValidationError[]): ValidationError[] {
283292 } ) ;
284293}
285294
286- export interface AutoCompleteNode {
287- word : AbstractWord ;
288- children : AutoCompleteNode [ ] ;
295+ export interface AutoCompleteNode < W extends AbstractWord = AbstractWord > {
296+ word : W ;
297+ children : AutoCompleteNode < W > [ ] ;
289298 canBeFinal ?: boolean ;
290299 viewAsLeaf ?: boolean ;
291300}
0 commit comments