From 5651828aa584b252d29f36b3f51eaa3cd6aaa58e Mon Sep 17 00:00:00 2001 From: Yesterday17 Date: Thu, 25 Jun 2020 00:36:15 +0800 Subject: [PATCH] feat: better unknown bracketHandler strategy #27, #29 --- server/api/index.d.ts | 2 ++ server/parser/index.d.ts | 5 ++++ server/parser/zsInterpreter.ts | 17 ++++++----- server/services/zsDocumentContentChange.ts | 35 ++++++++++++++-------- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/server/api/index.d.ts b/server/api/index.d.ts index dd279d5..3160042 100644 --- a/server/api/index.d.ts +++ b/server/api/index.d.ts @@ -19,6 +19,8 @@ export interface ZenScriptSettings { showIsProjectWarn: boolean; autoshowLTCompletion: boolean; + + // Whether to show modid autocompletion with bracketHandlers. modIdItemCompletion: boolean; } diff --git a/server/parser/index.d.ts b/server/parser/index.d.ts index 1deb818..9bd3584 100644 --- a/server/parser/index.d.ts +++ b/server/parser/index.d.ts @@ -17,6 +17,11 @@ export interface ASTError { detail: string; } +export interface ASTBracketHandlerError extends ASTError { + reason: 'BracketHandler error'; + isItem: boolean; +} + export interface ASTBody extends ASTNode { body: ASTNode[]; } diff --git a/server/parser/zsInterpreter.ts b/server/parser/zsInterpreter.ts index fcfbb09..422b55f 100644 --- a/server/parser/zsInterpreter.ts +++ b/server/parser/zsInterpreter.ts @@ -26,6 +26,7 @@ import { ASTNodeUnaryExpression, ASTNodeXorExpression, NodeContext, + ASTBracketHandlerError, } from '.'; import { ERROR_BRACKET_HANDLER } from '../api/constants'; import { zGlobal } from '../api/global'; @@ -931,14 +932,14 @@ class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() { exist = BracketHandlerMap.get('item').check(['item', ...items]); } if (!exist) { - node.errors = [ - { - start: node.start, - end: node.end, - reason: ERROR_BRACKET_HANDLER, - detail: `BracketHandler <${node.items.join(':')}> does not exist.`, - }, - ]; + const error: ASTBracketHandlerError = { + start: node.start, + end: node.end, + reason: ERROR_BRACKET_HANDLER, + detail: `BracketHandler <${node.items.join(':')}> does not exist.`, + isItem: items[0] === 'item', + }; + node.errors.push(error); } return node; } diff --git a/server/services/zsDocumentContentChange.ts b/server/services/zsDocumentContentChange.ts index d02e2ac..59689f7 100644 --- a/server/services/zsDocumentContentChange.ts +++ b/server/services/zsDocumentContentChange.ts @@ -8,6 +8,8 @@ import { URI } from 'vscode-uri'; import { ERROR_BRACKET_HANDLER } from '../api/constants'; import { zGlobal } from '../api/global'; import { ZenParsedFile } from '../api/zenParsedFile'; +import { ASTBracketHandlerError } from '../parser'; +import { getdocumentSettings } from '../utils/setting'; import { ZenScriptActiveService } from './zsService'; export class ZenScriptDocumentContentChange extends ZenScriptActiveService { @@ -69,21 +71,28 @@ export class ZenScriptDocumentContentChange extends ZenScriptActiveService { }); if (file.ast) { + const setting = await getdocumentSettings(textDocument.uri); file.ast.errors.forEach((error) => { - if ( - !file.ignoreBracketErrors || - error.reason !== ERROR_BRACKET_HANDLER - ) { - const diagnotic: Diagnostic = { - severity: DiagnosticSeverity.Error, - range: { - start: textDocument.positionAt(error.start), - end: textDocument.positionAt(error.end), - }, - message: error.detail, - }; - diagnostics.push(diagnotic); + if (error.reason === ERROR_BRACKET_HANDLER) { + if (file.ignoreBracketErrors) { + return; + } + const e = error as ASTBracketHandlerError; + if (!e.isItem && !setting.modIdItemCompletion) { + return; + } } + + // not ignored + const diagnotic: Diagnostic = { + severity: DiagnosticSeverity.Error, + range: { + start: textDocument.positionAt(error.start), + end: textDocument.positionAt(error.end), + }, + message: error.detail, + }; + diagnostics.push(diagnotic); }); }