import * as vscode from 'vscode' import { fm } from '../helpers/FileManage' import { FileSuffix } from '../type' import * as path from 'path' interface StatusBarItemConfigs { command?: string color?: string text: string } export class FindFiles { constructor(statusBarItemConfigs?: StatusBarItemConfigs) { if (statusBarItemConfigs) { this.createStatusBarItem(statusBarItemConfigs) } } public async handleCommand(filePath: string) { const currentFileType = fm.getFileType(filePath) const initPickArr = this.initPickArr(currentFileType) const targetPathMap: { [key: string]: string } = {} const pickArr: Array = [] initPickArr!.forEach((item) => { const { targetPath, relativePath, fileName } = this.getTargetPath(filePath, item) if (targetPath) { pickArr.push({ label: `[ ${item.toLocaleUpperCase()} ] ${fileName}`, description: relativePath, payLoad: item }) targetPathMap[item] = targetPath! } }) const res = await vscode.window.showQuickPick(pickArr) if (res) { this.jumpFile(targetPathMap[res.payLoad]) } } private createStatusBarItem(configs: StatusBarItemConfigs) { const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right) statusBarItem.command = configs.command statusBarItem.color = configs.color statusBarItem.text = configs.text statusBarItem.show() } private initPickArr(currentFileType: string) { switch (currentFileType) { case 'html': return ['ts', 'less', 'v'] case 'ts': return ['html', 'less', 'v'] case 'less': return ['html', 'ts', 'v'] case 'v': return ['html', 'ts', 'less'] default: vscode.window.showErrorMessage( `文件类型解析错误,获取了错误的类型${currentFileType},解析机制为split('.')请确保路径中无异常干扰` ) break } } private getSuffix(targetType: string): FileSuffix | undefined { switch (targetType) { case 'html': return '.html' case 'ts': return '.ts' case 'less': return '.less' case 'v': return '.v.ts' } } private jumpFile(targetPath: string) { if (targetPath) { vscode.workspace.openTextDocument(vscode.Uri.file(targetPath)).then( (document) => { vscode.window.showTextDocument(document) }, (error) => { vscode.window.showErrorMessage(`文件打开失败${error}`) } ) } else { vscode.window.showErrorMessage(`跳转失败,文件路径[ ${targetPath} ]不存在`) } } private getTargetPath(filePath: string, targetType: string) { const compName = fm.getCompNameByPath(filePath) const targetFileSuffix = this.getSuffix(targetType) const targetFileName = fm.getFileName(compName, targetFileSuffix!) let dir = fm.getDirByFileType(targetType as 'ts') const absDir = path.join(fm.getRootPath()!, dir) const targetPath = fm.getFilePathByName(targetFileName, absDir) return { targetPath, relativePath: targetPath?.replace(fm.getRootPath()!, ''), fileName: targetFileName } } }