import { Completion } from './Completion' import * as vscode from 'vscode' import { tc } from '../helpers/TsComplier' import { completionRefreshDelay } from '../readConfig' import { utils } from '../utils' export class SubviewPropsCompletion extends Completion { public completionArr: vscode.CompletionItem[] = [] private timerCache: number = 0 private compName!: string public validateType: 'comp' | 'other' | undefined public validateContext(document: vscode.TextDocument, position: vscode.Position): boolean { const res = tc.validateTagRange(document, position) if (res && res.type === 'comp') { this.compName = res.name this.validateType = 'comp' return true } else if (res && res.type === 'other') { this.validateType = 'other' return true } this.validateType = undefined return false } protected getCompletionItem(label: string, desp: string): vscode.CompletionItem { const compleItem = new vscode.CompletionItem(label) compleItem.kind = vscode.CompletionItemKind.Field compleItem.documentation = `propsType.${label}` compleItem.detail = desp label = utils.initLabel(label) compleItem.insertText = desp === 'boolean' ? label : new vscode.SnippetString(`${label}="\${1}"`) return compleItem } /** * 获取子视图props补全数组,复用缓存中的PropsDesp */ public async getCompletionArr() { if (this.compName) { const now = Date.now() if (now - this.timerCache > completionRefreshDelay) { const compName = this.compName if (!compName) return [] this.timerCache = now const desp = await tc.getPropsDespByCompName(compName) if (desp) { this.completionArr = Object.keys(desp).map((key) => { return this.getCompletionItem(key, utils.marshalIndent(desp[key])) }) return this.completionArr } } else { return this.completionArr } } } }