import { Completion } from './Completion' import type { EventResponseCompletion } from '../completion/EventResponseCompletion' import * as vscode from 'vscode' import * as json from '../html-event.json' import { tc } from '../helpers/TsComplier' import { utils } from '../utils' export class EventNameCompletion extends Completion { public completionArr!: vscode.CompletionItem[] protected triggerRangeMatcher: RegExp = /@(bind|catch):/ constructor() { super() this.init() } /** * 载入写好的eventName json文件,生成补全数组 */ private init() { this.completionArr = json.map((el) => { const label = el.name const desp = el.description?.value return this.getCompletionItem(label, desp || '') }) } public validateContext(document: vscode.TextDocument, position: vscode.Position): boolean { if (document.getWordRangeAtPosition(position, this.triggerRangeMatcher)) { return true } return false } protected getCompletionItem(label: string, desp: string): vscode.CompletionItem { return { label, kind: vscode.CompletionItemKind.Enum, insertText: new vscode.SnippetString(`${label}="\${1:handle${utils.camelToPascal(label)}}"`), documentation: desp, } } public async getCompletionArr( eventResComplet: EventResponseCompletion, document: vscode.TextDocument, position: vscode.Position ) { const res = tc.validateTagRange(document, position, '@bind:') if (res) { if (res.type === 'comp') { return await eventResComplet.getCompletionArr(res.name, true) } else { return this.completionArr } } else { const res2 = tc.validateTagRange(document, position, '@catch:') if (res2) { if (res2.type === 'comp') { return await eventResComplet.getCompletionArr(res2.name, true) } else { return this.completionArr } } } return null } }