From 43fbc1650f1113cb0ccf3c07915dac043dad0976 Mon Sep 17 00:00:00 2001 From: bbbboom Date: Sun, 15 May 2022 12:06:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=9B=B8=E5=AF=B9=E4=BA=8E?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E7=A9=BA=E9=97=B4=E7=9A=84=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E7=94=9F=E6=88=90=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 举个例子: **当前工作空间**:/code/extensions/obkoro1.korofileheader **当前文件路径**:/code/extensions/obkoro1.korofileheader/src/logic/filePath.js **路径生成结果**: >"relative path": /obkoro1.korofileheader/src/logic/filePath.js >"relative path without workspace": /src/logic/filePath.js --- src/logic/filePath.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/logic/filePath.js b/src/logic/filePath.js index c6fb0c7..7b037ed 100644 --- a/src/logic/filePath.js +++ b/src/logic/filePath.js @@ -19,6 +19,27 @@ const createFilePath = (FilePath) => { let res = `${path.sep}${itemName}${fileItemPath}` // 拼接项目名称和相对于项目的路径 if (FilePath === 'no item name') { res = `${fileItemPath}` + } else if (FilePath === 'relative path') { + if (vscode.workspace.workspaceFolders.length > 0) { + // 获取工作空间路径 + let folder = vscode.workspace.workspaceFolders[0]; + let root = path.resolve(folder.uri.fsPath, '..'); + if (fileItemPath.indexOf(root) === 0) { + res = path.sep + path.relative(root, fileItemPath); + } + } + } else if (FilePath === 'relative path without workspace') { + if (vscode.workspace.workspaceFolders.length > 0) { + // 遍历工作空间路径 + for (const workspaceFolder of vscode.workspace.workspaceFolders) { + let root = workspaceFolder.uri.fsPath; + if (fileItemPath.indexOf(root) === 0) { + // 存在于工作空间的路径才进行转换 + res = path.sep + path.relative(root, fileItemPath); + break; + } + } + } } else if (FilePath === 'only file name') { const arr = fileItemPath.split(path.sep) res = arr[arr.length - 1]