Skip to content

Commit

Permalink
feat(tag tree): Add relative file path (#43)
Browse files Browse the repository at this point in the history
Adds support for relative file path from tag tree.
  • Loading branch information
blakedietz authored Apr 23, 2019
1 parent a5de2c3 commit 811fbe7
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/tag-tree-data-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
Expand Down Expand Up @@ -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();
})();
Expand Down Expand Up @@ -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();
}
Expand All @@ -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.
Expand Down Expand Up @@ -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 };

0 comments on commit 811fbe7

Please sign in to comment.