@@ -6,6 +6,7 @@ import { Range, TextDocument } from 'vscode-languageserver-textdocument';
66import { SyntaxParser } from './parser/vbaSyntaxParser' ;
77import { FoldingRange } from '../capabilities/folding' ;
88import { SemanticTokensManager } from '../capabilities/semanticTokens' ;
9+ import { ParseCancellationException } from 'antlr4ng' ;
910
1011export interface DocumentSettings {
1112 maxDocumentLines : number ;
@@ -134,13 +135,28 @@ export abstract class BaseProjectDocument {
134135 }
135136
136137 async parseAsync ( token : CancellationToken ) : Promise < void > {
138+ // Handle already cancelled.
139+ if ( token . isCancellationRequested ) {
140+ throw new ParseCancellationException ( Error ( 'Parse operation cancelled before it started.' ) ) ;
141+ }
142+
143+ // Listen for cancellation event.
144+ token . onCancellationRequested ( ( ) => {
145+ throw new ParseCancellationException ( new Error ( 'Parse operation cancelled during parse.' ) ) ;
146+ } )
147+
148+ // Don't parse oversize documents.
137149 if ( await this . isOversize ) {
138150 console . log ( `Document oversize: ${ this . textDocument . lineCount } lines.` ) ;
139151 console . warn ( `Syntax parsing has been disabled to prevent crashing.` ) ;
140152 this . _isBusy = false ;
141153 return ;
142154 }
155+
156+ // Parse the document.
143157 await ( new SyntaxParser ( ) ) . parseAsync ( this , token )
158+
159+ // Evaluate the diagnostics.
144160 this . _hasDiagnosticElements . forEach ( element => {
145161 element . evaluateDiagnostics ;
146162 this . _diagnostics . concat ( element . diagnostics ) ;
0 commit comments