From 811fbe7d0f2fd162747cd04b216fd726a5ab3771 Mon Sep 17 00:00:00 2001 From: Blake Dietz Date: Tue, 23 Apr 2019 12:43:42 -0600 Subject: [PATCH] feat(tag tree): Add relative file path (#43) Adds support for relative file path from tag tree. --- src/tag-tree-data-provider.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/tag-tree-data-provider.ts b/src/tag-tree-data-provider.ts index 59b1300..3f9052a 100644 --- a/src/tag-tree-data-provider.ts +++ b/src/tag-tree-data-provider.ts @@ -6,6 +6,8 @@ import { FileNode, fileNodeSort } from "./tag-tree/file-node"; import { TagNode, tagNodeSort } from "./tag-tree/tag-node"; import { TagTree } from "./tag-tree/tag-tree"; import * as grayMatter from "gray-matter"; +import { Uri } from "vscode"; +import * as path from "path"; interface IFileInfo { tags: Set; @@ -46,7 +48,11 @@ class TagTreeDataProvider ); infos .filter(info => info.tags.size > 0) - .forEach(info => this.tagTree.addFile(info.filePath, [...info.tags], info.filePath)); + .forEach(info => { + const displayName = this.getPathRelativeToWorkspaceFolder(Uri.file(info.filePath)); + + this.tagTree.addFile(info.filePath, [...info.tags], displayName); + }); this._onDidChangeTreeData.fire(); })(); @@ -147,7 +153,8 @@ class TagTreeDataProvider */ if (isUpdateNeeded) { this.tagTree.deleteFile(filePath); - this.tagTree.addFile(filePath, [...fileInfo.tags.values()], filePath); + const displayName = this.getPathRelativeToWorkspaceFolder(Uri.file(filePath)); + this.tagTree.addFile(filePath, [...fileInfo.tags.values()], displayName); // TODO: (bdietz) - this._onDidChangeTreeData.fire(specificNode?) this._onDidChangeTreeData.fire(); } @@ -164,7 +171,8 @@ class TagTreeDataProvider const isUpdateNeeded = !setsAreEqual(tagsBefore, tagsAfter); if (isUpdateNeeded) { this.tagTree.deleteFile(filePath); - this.tagTree.addFile(filePath, [...tagsAfter.values()], filePath); + const displayName = this.getPathRelativeToWorkspaceFolder(Uri.file(filePath)); + this.tagTree.addFile(filePath, [...tagsAfter.values()], displayName); /* * TODO (bdietz) - this._onDidChangeTreeData.fire(specificNode?) * specifying the specific node would help to improve the efficiency of the tree refresh. @@ -219,6 +227,19 @@ class TagTreeDataProvider const buffer = await fs.promises.readFile(filePath); return this.getTagsFromFileText(buffer.toString(), filePath); } + + /** + * + * @param uri + */ + private getPathRelativeToWorkspaceFolder(uri: Uri): string { + const currentWorkspaceFolder = vscode.workspace.getWorkspaceFolder(uri); + const relativePath = typeof currentWorkspaceFolder !== "undefined" + ? path.relative(currentWorkspaceFolder.uri.path, uri.path) + : uri.path; + + return relativePath; + } } export { TagTreeDataProvider };