import * as vscode from 'vscode' import * as path from 'path' import { WINGED_DIRS } from '../consts' import { fm } from '../helpers/FileManage' import { FileSuffix } from '../type' import { tc } from '../helpers/TsComplier' import { store } from '../Store' export class SubviewDefinition { /** * 根据单词和提示组获取Html跳转的位置,成功返回 Location实例 否则 返回 null * @param word 发生跳转的单词 */ public async getHtmlLocation(word: string) { if (fm.validateCompName(word)) { return await this.basicGetLocation(word, '.html') } } /** * 根据单词和提示组获取Ts跳转的位置,成功返回 Location实例 否则 返回 null * @param word 发生跳转的单词 */ public async getTsLocation(word: string) { return await this.basicGetLocation(word, '.ts') } /** * 校验跳转上下文 * @param word 单词 */ public validateContext(word: string) { return fm.validateCompName(word) } public async getHtmlReference(word: string) { const rootPath = fm.getRootPath() const references: vscode.Location[] = [] if (rootPath) { const compsDir = path.join(rootPath, WINGED_DIRS.TPL_COMP) const fileName = fm.getFileName(word, '.html') const filePath = fm.getFilePathByName(fileName, compsDir) const viewDependencies = store.viewDependencies const keys = Object.keys(viewDependencies) await Promise.all(keys.map(async (referencePath) => { const dependencies = viewDependencies[referencePath] if (dependencies.some((absPath) => absPath === filePath)) { const absPath = path.join(rootPath, WINGED_DIRS.VIEW_TPLS, referencePath) const res = await vscode.workspace.openTextDocument(absPath) for (let i = 0; i < res.lineCount; i++) { const lineText = res.lineAt(i) if (!lineText.isEmptyOrWhitespace) { if (lineText.text.includes(`<${word}`)) { references.push( new vscode.Location(vscode.Uri.file(absPath), new vscode.Position(i, 0)), ) } } } } })) } return references } private async basicGetLocation(word: string, suffix: FileSuffix) { const rootPath = fm.getRootPath() if (rootPath) { let targetDir = '' switch (suffix) { case '.html': targetDir = WINGED_DIRS.TPL_COMP break case '.ts': targetDir = WINGED_DIRS.TS_COMP break } const compsDir = path.join(rootPath, targetDir) const filePath = fm.getFilePathByName(fm.getFileName(word, suffix), compsDir) if (filePath !== null) { if (suffix === '.ts') { const res = await vscode.workspace.openTextDocument(filePath) let i = 0 const classFragments = tc.getClassFragments(word) while (i <= res.lineCount) { const lineText = res.lineAt(i) if (!lineText.isEmptyOrWhitespace) { if (lineText.text.match(new RegExp(classFragments))) { break } } i++ } if (i >= res.lineCount) { i = 0 } return new vscode.Location(vscode.Uri.file(filePath), new vscode.Position(i, 0)) } return new vscode.Location(vscode.Uri.file(filePath), new vscode.Position(0, 0)) } } return null } }