Skip to content

Commit

Permalink
fix(File tree): Only allow markdown files in the tree (#27)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
blakedietz authored Feb 14, 2019
1 parent 0a2182c commit ac78af2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 73 deletions.
Empty file added .circleci/CHANGELOG.md
Empty file.
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 15 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"watch": "tsc -watch -p ./"
},
"devDependencies": {
"@semantic-release/changelog": "^3.0.2",
"@semantic-release/commit-analyzer": "^6.1.0",
"@semantic-release/exec": "^3.3.2",
"@semantic-release/git": "^7.0.8",
Expand All @@ -34,6 +35,7 @@
"@types/debounce": "^1.2.0",
"@types/jest": "^23.3.12",
"@types/node": "^10.12.18",
"@types/recursive-readdir": "2.2.0",
"commitizen": "^3.0.5",
"cz-conventional-changelog": "^2.1.0",
"husky": "^1.3.1",
Expand Down Expand Up @@ -67,10 +69,10 @@
}
},
"icon": "images/icon.png",
"galleryBanner": {
"color": "#073642",
"theme": "dark"
},
"galleryBanner": {
"color": "#073642",
"theme": "dark"
},
"publisher": "vscode-nested-tags",
"config": {
"loglevel": "verbose",
Expand All @@ -91,20 +93,15 @@
]
},
"release": {
"verifyConditions": [
"semantic-release-vsce",
"@semantic-release/github"
],
"prepare": {
"path": "semantic-release-vsce",
"packageVsix": "vscode-nested-tags.vsix"
},
"publish": [
"semantic-release-vsce",
{
"path": "@semantic-release/github",
"assets": "vscode-nested-tags.vsix"
}
"plugins": [
["semantic-release-vsce", {
"path": "@semantic-release/github",
"assets": "vscode-nested-tags.vsix"
}],
"@semantic-release/github",
["@semantic-release/changelog", {
"changelogFile": "./CHANGELOG.md"
}]
]
}
}
93 changes: 38 additions & 55 deletions src/tag-tree-data-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { debounce } from "debounce";
import * as fs from "fs";
import * as path from "path";
import * as recursiveReadDir from "recursive-readdir";
import * as vscode from "vscode";
import { setsAreEqual } from "./sets";
import { FileNode, fileNodeSort } from "./tag-tree/file-node";
Expand All @@ -25,35 +25,33 @@ class TagTreeDataProvider
readonly onDidChangeTreeData: vscode.Event< TagNode | FileNode | null> = this._onDidChangeTreeData.event;

constructor() {
// vscode.window.onDidChangeActiveTextEditor(() => this.onActiveEditorChanged());
// vscode.workspace.onDidSaveTextDocument((e) => {
// console.log(e);
// });

// Register the extension to events of interest
// Debounce to improve performance. Otherwise a file read would occur during each of the user's change to the document.
/* Register the extension to events of interest
* Debounce to improve performance. Otherwise a file read would occur during each of the user's change to the document.
*/
vscode.workspace.onDidChangeTextDocument(debounce((e: vscode.TextDocumentChangeEvent) => this.onDocumentChanged(e), 500));
vscode.workspace.onWillSaveTextDocument((e) => {
this.onWillSaveTextDocument(e);
});

this.tagTree = new TagTree();

// Add all files in the current workspace folder to the tag tree
// @ts-ignore
const workspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath;
const files = [];

// TODO: (bdietz) - this is probably going to be pretty slow
for(const filePath of this.walkFileSystemSync(workspaceFolder)) {
const fileInfo = this.getTagsFromFileOnFileSystem(filePath);
if (fileInfo.tags.size > 0) {
files.push(fileInfo);
}
}

for (const fileInfo of files) {
this.tagTree.addFile(fileInfo.filePath, [...fileInfo.tags], fileInfo.filePath);
/* Add all files in the current workspace folder to the tag tree
* @ts-ignore
*/
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();
});
});
}
}

Expand Down Expand Up @@ -114,7 +112,7 @@ class TagTreeDataProvider
* @param changeEvent
*/
private onWillSaveTextDocument(changeEvent: vscode.TextDocumentWillSaveEvent) {
if (changeEvent.document.isDirty) {
if (changeEvent.document.isDirty && changeEvent.document.languageId === "markdown") {
const filePath = changeEvent.document.fileName;
const fileInfo = this.getTagsFromFileOnFileSystem(filePath);
const tagsInTreeForFile = this.tagTree.getTagsForFile(filePath);
Expand All @@ -132,37 +130,21 @@ class TagTreeDataProvider
*/
private onDocumentChanged(changeEvent: vscode.TextDocumentChangeEvent): void {
const filePath = changeEvent.document.fileName;
const fileInfo = this.getTagsFromFileText(changeEvent.document.getText(), filePath);
const tagsInTreeForFile = this.tagTree.getTagsForFile(filePath);
const isUpdateNeeded = !setsAreEqual(fileInfo.tags, tagsInTreeForFile);
/*
* This could be potentially performance intensive due to the number of changes that could
* be made to a document and how large the document is. There will definitely need to be some
* work done around TagTree to make sure that the code
*/
if (isUpdateNeeded) {
this.tagTree.deleteFile(filePath);
this.tagTree.addFile(filePath, [...fileInfo.tags.values()], filePath);
// TODO: (bdietz) - this._onDidChangeTreeData.fire(specificNode?)
this._onDidChangeTreeData.fire();
}
}

/**
* NOTE: Stole this from https://gist.github.com/luciopaiva/4ba78a124704007c702d0293e7ff58dd.
*
* Recursively walk through the file system synchronously.
*/
private *walkFileSystemSync(dir: string): IterableIterator<string> {
const files = fs.readdirSync(dir);

for (const file of files) {
const pathToFile = path.join(dir, file);
const isDirectory = fs.statSync(pathToFile).isDirectory();
if (isDirectory) {
yield* this.walkFileSystemSync(pathToFile);
} else {
yield pathToFile;
// If the file has been saved and the file is a markdown file allow for making changes to the tag tree
if (filePath !== undefined && changeEvent.document.languageId === "markdown") {
const fileInfo = this.getTagsFromFileText(changeEvent.document.getText(), filePath);
const tagsInTreeForFile = this.tagTree.getTagsForFile(filePath);
const isUpdateNeeded = !setsAreEqual(fileInfo.tags, tagsInTreeForFile);
/*
* This could be potentially performance intensive due to the number of changes that could
* be made to a document and how large the document is. There will definitely need to be some
* work done around TagTree to make sure that the code
*/
if (isUpdateNeeded) {
this.tagTree.deleteFile(filePath);
this.tagTree.addFile(filePath, [...fileInfo.tags.values()], filePath);
// TODO: (bdietz) - this._onDidChangeTreeData.fire(specificNode?)
this._onDidChangeTreeData.fire();
}
}
}
Expand Down Expand Up @@ -210,6 +192,7 @@ class TagTreeDataProvider
.split(',');
return {...accumulator, tags: new Set([...accumulator.tags,...tagsToAdd])};
}

return accumulator;
}, { tags: new Set(), filePath });
}
Expand Down

0 comments on commit ac78af2

Please sign in to comment.