From f4eb2d6f424e747d1daef1e0ac09959ab4382f9f Mon Sep 17 00:00:00 2001 From: Blake Dietz Date: Tue, 5 Mar 2019 14:23:58 -0700 Subject: [PATCH] feat(File node): Add support for opening file from file node (#36) * docs(vscode-logo): Adds logo for extension (#20) * docs(logo): Add logo * build(config.yml): Add release branch * feat(Sorting): Sort the tag tree by tag and file (#21) Sort the tag tree alphbetically first by tags, then by file. Resolves #11 * style(tag-tree-data-provider.ts): Add some space in the import statement * ci(config.yml): Add develop branch to trigger ci * fix(icon.png): Renames nested-tags-logo.png to icon.png * perf(tag-tree-data-provider.ts): Scan files asynchronously (#24) * Scan files asynchronously * Only include markdown files * docs(CHANGELOG.md): Automatically publish changes to CHANGELOG.md (#25) Automatically publish changes to CHANGELOG.md * feat(go to file): add file-open command to file nodes (#34) - make treeDataProvider use vscode the findFiles function for searching - read files and scan for tags asynchronously * build(recursive-readdir): Remove recursive-readdir as a dependency * ci(package.json): Change run order of the plugins for semantic release --- package.json | 21 +++++++++------ src/tag-tree-data-provider.ts | 50 +++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index d4b27cc..112e27d 100644 --- a/package.json +++ b/package.json @@ -52,8 +52,7 @@ "vscode": "^1.1.25" }, "dependencies": { - "debounce": "^1.2.0", - "recursive-readdir": "2.2.2" + "debounce": "^1.2.0" }, "activationEvents": [ "onView:tagTreeView" @@ -94,14 +93,20 @@ }, "release": { "plugins": [ - ["semantic-release-vsce", { + [ + "semantic-release-vsce", + { "path": "@semantic-release/github", "assets": "vscode-nested-tags.vsix" - }], - ["@semantic-release/changelog", { - "changelogFile": "./CHANGELOG.md" - }], - "@semantic-release/github" + } + ], + "@semantic-release/github", + [ + "@semantic-release/changelog", + { + "changelogFile": "./CHANGELOG.md" + } + ] ] } } diff --git a/src/tag-tree-data-provider.ts b/src/tag-tree-data-provider.ts index 6b2dc02..ec00e13 100644 --- a/src/tag-tree-data-provider.ts +++ b/src/tag-tree-data-provider.ts @@ -1,6 +1,5 @@ import { debounce } from "debounce"; import * as fs from "fs"; -import * as recursiveReadDir from "recursive-readdir"; import * as vscode from "vscode"; import { setsAreEqual } from "./sets"; import { FileNode, fileNodeSort } from "./tag-tree/file-node"; @@ -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(); + })(); } /** @@ -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; } /** @@ -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 { 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); } @@ -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 { + const buffer = await fs.promises.readFile(filePath); + return this.getTagsFromFileText(buffer.toString(), filePath); } }