11import { debounce } from "debounce" ;
22import * as fs from "fs" ;
3- import * as path from "path " ;
3+ import * as recursiveReadDir from "recursive-readdir " ;
44import * as vscode from "vscode" ;
55import { setsAreEqual } from "./sets" ;
66import { 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