Skip to content

Commit ac78af2

Browse files
authored
fix(File tree): Only allow markdown files in the tree (#27)
* 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
1 parent 0a2182c commit ac78af2

File tree

4 files changed

+74
-73
lines changed

4 files changed

+74
-73
lines changed

.circleci/CHANGELOG.md

Whitespace-only changes.

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"watch": "tsc -watch -p ./"
2727
},
2828
"devDependencies": {
29+
"@semantic-release/changelog": "^3.0.2",
2930
"@semantic-release/commit-analyzer": "^6.1.0",
3031
"@semantic-release/exec": "^3.3.2",
3132
"@semantic-release/git": "^7.0.8",
@@ -34,6 +35,7 @@
3435
"@types/debounce": "^1.2.0",
3536
"@types/jest": "^23.3.12",
3637
"@types/node": "^10.12.18",
38+
"@types/recursive-readdir": "2.2.0",
3739
"commitizen": "^3.0.5",
3840
"cz-conventional-changelog": "^2.1.0",
3941
"husky": "^1.3.1",
@@ -67,10 +69,10 @@
6769
}
6870
},
6971
"icon": "images/icon.png",
70-
"galleryBanner": {
71-
"color": "#073642",
72-
"theme": "dark"
73-
},
72+
"galleryBanner": {
73+
"color": "#073642",
74+
"theme": "dark"
75+
},
7476
"publisher": "vscode-nested-tags",
7577
"config": {
7678
"loglevel": "verbose",
@@ -91,20 +93,15 @@
9193
]
9294
},
9395
"release": {
94-
"verifyConditions": [
95-
"semantic-release-vsce",
96-
"@semantic-release/github"
97-
],
98-
"prepare": {
99-
"path": "semantic-release-vsce",
100-
"packageVsix": "vscode-nested-tags.vsix"
101-
},
102-
"publish": [
103-
"semantic-release-vsce",
104-
{
105-
"path": "@semantic-release/github",
106-
"assets": "vscode-nested-tags.vsix"
107-
}
96+
"plugins": [
97+
["semantic-release-vsce", {
98+
"path": "@semantic-release/github",
99+
"assets": "vscode-nested-tags.vsix"
100+
}],
101+
"@semantic-release/github",
102+
["@semantic-release/changelog", {
103+
"changelogFile": "./CHANGELOG.md"
104+
}]
108105
]
109106
}
110107
}

src/tag-tree-data-provider.ts

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { debounce } from "debounce";
22
import * as fs from "fs";
3-
import * as path from "path";
3+
import * as recursiveReadDir from "recursive-readdir";
44
import * as vscode from "vscode";
55
import { setsAreEqual } from "./sets";
66
import { FileNode, fileNodeSort } from "./tag-tree/file-node";
@@ -25,35 +25,33 @@ class TagTreeDataProvider
2525
readonly onDidChangeTreeData: vscode.Event< TagNode | FileNode | null> = this._onDidChangeTreeData.event;
2626

2727
constructor() {
28-
// vscode.window.onDidChangeActiveTextEditor(() => this.onActiveEditorChanged());
29-
// vscode.workspace.onDidSaveTextDocument((e) => {
30-
// console.log(e);
31-
// });
32-
33-
// Register the extension to events of interest
34-
// Debounce to improve performance. Otherwise a file read would occur during each of the user's change to the document.
28+
/* Register the extension to events of interest
29+
* Debounce to improve performance. Otherwise a file read would occur during each of the user's change to the document.
30+
*/
3531
vscode.workspace.onDidChangeTextDocument(debounce((e: vscode.TextDocumentChangeEvent) => this.onDocumentChanged(e), 500));
3632
vscode.workspace.onWillSaveTextDocument((e) => {
3733
this.onWillSaveTextDocument(e);
3834
});
3935

4036
this.tagTree = new TagTree();
4137

42-
// Add all files in the current workspace folder to the tag tree
43-
// @ts-ignore
44-
const workspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath;
45-
const files = [];
46-
47-
// TODO: (bdietz) - this is probably going to be pretty slow
48-
for(const filePath of this.walkFileSystemSync(workspaceFolder)) {
49-
const fileInfo = this.getTagsFromFileOnFileSystem(filePath);
50-
if (fileInfo.tags.size > 0) {
51-
files.push(fileInfo);
52-
}
53-
}
54-
55-
for (const fileInfo of files) {
56-
this.tagTree.addFile(fileInfo.filePath, [...fileInfo.tags], fileInfo.filePath);
38+
/* Add all files in the current workspace folder to the tag tree
39+
* @ts-ignore
40+
*/
41+
if (vscode.workspace.workspaceFolders!.length > 0) {
42+
vscode.workspace.workspaceFolders!.forEach(workspaceFolder => {
43+
const { fsPath } = workspaceFolder.uri;
44+
recursiveReadDir(fsPath, ["!*.md"], (error: any, files: any) => {
45+
for (const filePath of files) {
46+
const fileInfo = this.getTagsFromFileOnFileSystem(filePath);
47+
if (fileInfo.tags.size > 0) {
48+
this.tagTree.addFile(fileInfo.filePath, [...fileInfo.tags], fileInfo.filePath);
49+
}
50+
}
51+
52+
this._onDidChangeTreeData.fire();
53+
});
54+
});
5755
}
5856
}
5957

@@ -114,7 +112,7 @@ class TagTreeDataProvider
114112
* @param changeEvent
115113
*/
116114
private onWillSaveTextDocument(changeEvent: vscode.TextDocumentWillSaveEvent) {
117-
if (changeEvent.document.isDirty) {
115+
if (changeEvent.document.isDirty && changeEvent.document.languageId === "markdown") {
118116
const filePath = changeEvent.document.fileName;
119117
const fileInfo = this.getTagsFromFileOnFileSystem(filePath);
120118
const tagsInTreeForFile = this.tagTree.getTagsForFile(filePath);
@@ -132,37 +130,21 @@ class TagTreeDataProvider
132130
*/
133131
private onDocumentChanged(changeEvent: vscode.TextDocumentChangeEvent): void {
134132
const filePath = changeEvent.document.fileName;
135-
const fileInfo = this.getTagsFromFileText(changeEvent.document.getText(), filePath);
136-
const tagsInTreeForFile = this.tagTree.getTagsForFile(filePath);
137-
const isUpdateNeeded = !setsAreEqual(fileInfo.tags, tagsInTreeForFile);
138-
/*
139-
* This could be potentially performance intensive due to the number of changes that could
140-
* be made to a document and how large the document is. There will definitely need to be some
141-
* work done around TagTree to make sure that the code
142-
*/
143-
if (isUpdateNeeded) {
144-
this.tagTree.deleteFile(filePath);
145-
this.tagTree.addFile(filePath, [...fileInfo.tags.values()], filePath);
146-
// TODO: (bdietz) - this._onDidChangeTreeData.fire(specificNode?)
147-
this._onDidChangeTreeData.fire();
148-
}
149-
}
150-
151-
/**
152-
* NOTE: Stole this from https://gist.github.com/luciopaiva/4ba78a124704007c702d0293e7ff58dd.
153-
*
154-
* Recursively walk through the file system synchronously.
155-
*/
156-
private *walkFileSystemSync(dir: string): IterableIterator<string> {
157-
const files = fs.readdirSync(dir);
158-
159-
for (const file of files) {
160-
const pathToFile = path.join(dir, file);
161-
const isDirectory = fs.statSync(pathToFile).isDirectory();
162-
if (isDirectory) {
163-
yield* this.walkFileSystemSync(pathToFile);
164-
} else {
165-
yield pathToFile;
133+
// If the file has been saved and the file is a markdown file allow for making changes to the tag tree
134+
if (filePath !== undefined && changeEvent.document.languageId === "markdown") {
135+
const fileInfo = this.getTagsFromFileText(changeEvent.document.getText(), filePath);
136+
const tagsInTreeForFile = this.tagTree.getTagsForFile(filePath);
137+
const isUpdateNeeded = !setsAreEqual(fileInfo.tags, tagsInTreeForFile);
138+
/*
139+
* This could be potentially performance intensive due to the number of changes that could
140+
* be made to a document and how large the document is. There will definitely need to be some
141+
* work done around TagTree to make sure that the code
142+
*/
143+
if (isUpdateNeeded) {
144+
this.tagTree.deleteFile(filePath);
145+
this.tagTree.addFile(filePath, [...fileInfo.tags.values()], filePath);
146+
// TODO: (bdietz) - this._onDidChangeTreeData.fire(specificNode?)
147+
this._onDidChangeTreeData.fire();
166148
}
167149
}
168150
}
@@ -210,6 +192,7 @@ class TagTreeDataProvider
210192
.split(',');
211193
return {...accumulator, tags: new Set([...accumulator.tags,...tagsToAdd])};
212194
}
195+
213196
return accumulator;
214197
}, { tags: new Set(), filePath });
215198
}

0 commit comments

Comments
 (0)