Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions src/tag-tree-data-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { debounce } from "debounce";
import * as fs from "fs";
import * as recursiveReadDir from "recursive-readdir";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to pull this out of package.json but I'm not going to block your pr because of that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I missed that.

import * as vscode from "vscode";
import { setsAreEqual } from "./sets";
import { FileNode, fileNodeSort } from "./tag-tree/file-node";
Expand Down Expand Up @@ -35,24 +34,20 @@ class TagTreeDataProvider

this.tagTree = new TagTree();

/* Add all files in the current workspace folder to the tag tree
* @ts-ignore
/**
* Add all files in the current workspace folder to the tag tree
*/
if (vscode.workspace.workspaceFolders!.length > 0) {
vscode.workspace.workspaceFolders!.forEach(workspaceFolder => {
const { fsPath } = workspaceFolder.uri;
recursiveReadDir(fsPath, ["!*.md"], (error: any, files: any) => {
for (const filePath of files) {
const fileInfo = this.getTagsFromFileOnFileSystem(filePath);
if (fileInfo.tags.size > 0) {
this.tagTree.addFile(fileInfo.filePath, [...fileInfo.tags], fileInfo.filePath);
}
}

this._onDidChangeTreeData.fire();
});
});
}
(async () => {
const uris = await vscode.workspace.findFiles("**/*.md");
const infos = await Promise.all(
uris.map(uri => this.getTagsFromFileOnFileSystem(uri.fsPath))
);
infos
.filter(info => info.tags.size > 0)
.forEach(info => this.tagTree.addFile(info.filePath, [...info.tags], info.filePath));

this._onDidChangeTreeData.fire();
})();
}

/**
Expand Down Expand Up @@ -102,7 +97,15 @@ class TagTreeDataProvider
? vscode.TreeItemCollapsibleState.None
: vscode.TreeItemCollapsibleState.Collapsed;

return new vscode.TreeItem(displayName, collapsibleState);
const result = new vscode.TreeItem(displayName, collapsibleState);
if (isFile) {
result.command = {
arguments: [ vscode.Uri.file(tagTreeNode.filePath) ],
command: "vscode.open",
title: "Jump to tag reference"
};
}
return result;
}

/**
Expand All @@ -111,10 +114,10 @@ class TagTreeDataProvider
* any changes to tags for a document before saving.
* @param changeEvent
*/
private onWillSaveTextDocument(changeEvent: vscode.TextDocumentWillSaveEvent) {
private async onWillSaveTextDocument(changeEvent: vscode.TextDocumentWillSaveEvent): Promise<void> {
if (changeEvent.document.isDirty && changeEvent.document.languageId === "markdown") {
const filePath = changeEvent.document.fileName;
const fileInfo = this.getTagsFromFileOnFileSystem(filePath);
const fileInfo = await this.getTagsFromFileOnFileSystem(filePath);
const tagsInTreeForFile = this.tagTree.getTagsForFile(filePath);
this.updateTreeForFile(filePath, tagsInTreeForFile, fileInfo.tags);
}
Expand Down Expand Up @@ -202,8 +205,9 @@ class TagTreeDataProvider
*
* @param filePath The local filesystem path
*/
private getTagsFromFileOnFileSystem(filePath: string): IFileInfo {
return this.getTagsFromFileText(fs.readFileSync(filePath).toString(), filePath);
private async getTagsFromFileOnFileSystem(filePath: string): Promise<IFileInfo> {
const buffer = await fs.promises.readFile(filePath);
return this.getTagsFromFileText(buffer.toString(), filePath);
}
}

Expand Down