77import type { CustomPatternMatcherFunc , TokenType , IToken , IMultiModeLexerDefinition , TokenVocabulary } from 'chevrotain' ;
88import type { Grammar , TerminalRule } from '../languages/generated/ast.js' ;
99import type { LexingReport , TokenBuilderOptions } from './token-builder.js' ;
10- import type { LexerResult } from './lexer.js' ;
10+ import type { LexerResult , TokenizeOptions } from './lexer.js' ;
1111import type { LangiumCoreServices } from '../services.js' ;
1212import { createToken , createTokenInstance , Lexer } from 'chevrotain' ;
1313import { DefaultTokenBuilder } from './token-builder.js' ;
14- import { DefaultLexer , isTokenTypeArray } from './lexer.js' ;
14+ import { DEFAULT_TOKENIZE_OPTIONS , DefaultLexer , isTokenTypeArray } from './lexer.js' ;
1515
1616type IndentationAwareDelimiter < TokenName extends string > = [ begin : TokenName , end : TokenName ] ;
1717
@@ -179,11 +179,11 @@ export class IndentationAwareTokenBuilder<Terminals extends string = string, Key
179179 }
180180 }
181181
182- override popLexingReport ( text : string ) : IndentationLexingReport {
183- const result = super . popLexingReport ( text ) ;
182+ override flushLexingReport ( text : string ) : IndentationLexingReport {
183+ const result = super . flushLexingReport ( text ) ;
184184 return {
185185 ...result ,
186- remainingDedents : this . popRemainingDedents ( text ) ,
186+ remainingDedents : this . flushRemainingDedents ( text ) ,
187187 } ;
188188 }
189189
@@ -203,9 +203,12 @@ export class IndentationAwareTokenBuilder<Terminals extends string = string, Key
203203 *
204204 * @param text The full input string.
205205 * @param offset The current position at which to attempt a match
206+ * @param tokens Previously scanned tokens
207+ * @param groups Token Groups
206208 * @returns The current and previous indentation levels and the matched whitespace
207209 */
208- protected matchWhitespace ( text : string , offset : number , _tokens : IToken [ ] , _groups : Record < string , IToken [ ] > ) : { currIndentLevel : number , prevIndentLevel : number , match : RegExpExecArray | null } {
210+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
211+ protected matchWhitespace ( text : string , offset : number , tokens : IToken [ ] , groups : Record < string , IToken [ ] > ) : { currIndentLevel : number , prevIndentLevel : number , match : RegExpExecArray | null } {
209212 this . whitespaceRegExp . lastIndex = offset ;
210213 const match = this . whitespaceRegExp . exec ( text ) ;
211214 return {
@@ -251,12 +254,10 @@ export class IndentationAwareTokenBuilder<Terminals extends string = string, Key
251254 *
252255 * @param text The full input string.
253256 * @param offset The offset at which to attempt a match
254- * @param tokens Previously scanned Tokens
257+ * @param tokens Previously scanned tokens
255258 * @param groups Token Groups
256259 */
257260 protected indentMatcher ( text : string , offset : number , tokens : IToken [ ] , groups : Record < string , IToken [ ] > ) : ReturnType < CustomPatternMatcherFunc > {
258- const { indentTokenName } = this . options ;
259-
260261 if ( ! this . isStartOfLine ( text , offset ) ) {
261262 return null ;
262263 }
@@ -274,7 +275,7 @@ export class IndentationAwareTokenBuilder<Terminals extends string = string, Key
274275 const indentToken = this . createIndentationTokenInstance (
275276 this . indentTokenType ,
276277 text ,
277- match ?. [ 0 ] ?? indentTokenName ,
278+ match ?. [ 0 ] ?? '' ,
278279 offset ,
279280 ) ;
280281 tokens . push ( indentToken ) ;
@@ -288,12 +289,10 @@ export class IndentationAwareTokenBuilder<Terminals extends string = string, Key
288289 *
289290 * @param text The full input string.
290291 * @param offset The offset at which to attempt a match
291- * @param tokens Previously scanned Tokens
292+ * @param tokens Previously scanned tokens
292293 * @param groups Token Groups
293294 */
294295 protected dedentMatcher ( text : string , offset : number , tokens : IToken [ ] , groups : Record < string , IToken [ ] > ) : ReturnType < CustomPatternMatcherFunc > {
295- const { dedentTokenName } = this . options ;
296-
297296 if ( ! this . isStartOfLine ( text , offset ) ) {
298297 return null ;
299298 }
@@ -316,7 +315,7 @@ export class IndentationAwareTokenBuilder<Terminals extends string = string, Key
316315 offset,
317316 length : match ?. [ 0 ] ?. length ?? 0 ,
318317 line : this . getLineNumber ( text , offset ) ,
319- column : 0
318+ column : 1
320319 } ) ;
321320 return null ;
322321 }
@@ -327,7 +326,7 @@ export class IndentationAwareTokenBuilder<Terminals extends string = string, Key
327326 const token = this . createIndentationTokenInstance (
328327 this . dedentTokenType ,
329328 text ,
330- match ?. [ 0 ] ?? dedentTokenName ,
329+ match ?. [ 0 ] ?? '' ,
331330 offset ,
332331 ) ;
333332 tokens . push ( token ) ;
@@ -362,7 +361,7 @@ export class IndentationAwareTokenBuilder<Terminals extends string = string, Key
362361 * @param text Full text that was tokenized
363362 * @returns Remaining dedent tokens to match all previous indents at the end of the file
364363 */
365- popRemainingDedents ( text : string ) : IToken [ ] {
364+ flushRemainingDedents ( text : string ) : IToken [ ] {
366365 const remainingDedents : IToken [ ] = [ ] ;
367366 while ( this . indentationStack . length > 1 ) {
368367 remainingDedents . push (
@@ -402,13 +401,15 @@ export class IndentationAwareLexer extends DefaultLexer {
402401 }
403402 }
404403
405- override tokenize ( text : string ) : LexerResult {
404+ override tokenize ( text : string , options : TokenizeOptions = DEFAULT_TOKENIZE_OPTIONS ) : LexerResult {
406405 const result = super . tokenize ( text ) ;
407406
408407 // consuming all remaining dedents and remove them as they might not be serializable
409408 const report = result . report as IndentationLexingReport ;
410- const remainingDedents = report . remainingDedents ;
411- result . tokens . push ( ...remainingDedents ) ;
409+ if ( options ?. mode === 'full' ) {
410+ // auto-complete document with remaining dedents
411+ result . tokens . push ( ...report . remainingDedents ) ;
412+ }
412413 report . remainingDedents = [ ] ;
413414
414415 // remove any "indent-dedent" pair with an empty body as these are typically
0 commit comments