Skip to content

Commit 74526ee

Browse files
authored
Merge pull request #21 from SSlinky/dev
Better error handling and parse cancellation
2 parents a0a4f8d + 049ddac commit 74526ee

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"icon": "images/vba-lsp-icon.png",
77
"author": "SSlinky",
88
"license": "MIT",
9-
"version": "1.4.3",
9+
"version": "1.4.4",
1010
"repository": {
1111
"type": "git",
1212
"url": "https://github.com/SSlinky/VBA-LanguageServer"

server/src/project/document.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Range, TextDocument } from 'vscode-languageserver-textdocument';
66
import { SyntaxParser } from './parser/vbaSyntaxParser';
77
import { FoldingRange } from '../capabilities/folding';
88
import { SemanticTokensManager } from '../capabilities/semanticTokens';
9+
import { ParseCancellationException } from 'antlr4ng';
910

1011
export 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);

server/src/project/workspace.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ export class Workspace {
4848
this.activateDocument(document);
4949
this._parseCancellationTokenSource?.cancel();
5050
this._parseCancellationTokenSource = new CancellationTokenSource();
51-
await this._activeDocument?.parseAsync(this._parseCancellationTokenSource.token);
51+
52+
// Exceptions thrown by the parser should be ignored.
53+
try {
54+
await this._activeDocument?.parseAsync(this._parseCancellationTokenSource.token);
55+
} catch (error) {
56+
this.connection.console.log(`Parser error: ${error}`)
57+
}
58+
5259
this._parseCancellationTokenSource = undefined;
5360
this.connection.sendDiagnostics(this._activeDocument?.languageServerDiagnostics() ?? {uri: "", diagnostics: []});
5461
}

snippets/vba.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"prefix": "func",
7070
"description": "Function",
7171
"body": [
72-
"${1:Public }Function ${2:Identifier}($3) As $4",
72+
"${1:Public }Function ${2:Identifier}($3) As ${4:Variant}",
7373
"Attribute $2.VB_Description = \"${5:Dosctring}.\"",
7474
"' $5.",
7575
"'",

0 commit comments

Comments
 (0)