import { Completion } from './Completion' import * as vscode from 'vscode' import { tc } from '../helpers/TsComplier' import { utils } from '../utils' import { completionRefreshDelay } from '../readConfig' export class EventResponseCompletion extends Completion { public completionArr: vscode.CompletionItem[] = [] private timerCache = 0 private compName!: string public validateContext(document: vscode.TextDocument, position: vscode.Position): boolean { const res = tc.validateTagRange(document, position, ':') if (res !== undefined && res.type === 'comp') { this.compName = res.name return true } return false } protected getCompletionItem(label: string, desp: string): vscode.CompletionItem { return { label, kind: vscode.CompletionItemKind.Function, insertText: new vscode.SnippetString(`${label}="\${1:handle${utils.camelToPascal(label)}}"`), documentation: `eventsType.${label}`, detail: desp, } } /** * 获取 事件响应补全信息 * @param linePrefix 前缀 or 组件名 * @param flag 如果flag为true linePrefix 为组件名跳过parse,否则需要 parse */ public async getCompletionArr(linePrefix: string, flag?: boolean) { let compName: string | undefined if (flag) { compName = linePrefix } else { if (this.compName) { compName = this.compName } } if (!compName) return null const now = Date.now() if (now - this.timerCache > completionRefreshDelay) { this.timerCache = now const desp = await tc.getEventsTypeDespByCompName(compName) if (desp) { this.completionArr = Object.keys(desp).map((item) => { return this.getCompletionItem(item, utils.marshalIndent(desp[item])) }) return this.completionArr } } return this.completionArr } }