From 11ced4f108259b88fa47ba6764b84fd301a39695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20=C3=96stlund?= Date: Sun, 18 Dec 2016 13:37:32 +0100 Subject: [PATCH 1/2] Update the config object on change Add a listener for updates to the config file which updates the configuration object attached to `this`. --- src/extension.ts | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index fc218f7..954c01b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,7 +1,7 @@ // The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below -import {window, workspace, commands, Disposable, - ExtensionContext, StatusBarAlignment, StatusBarItem, +import {window, workspace, commands, Disposable, + ExtensionContext, StatusBarAlignment, StatusBarItem, TextDocument, QuickPickItem, FileSystemWatcher, Uri, TextEditorEdit, TextEditor, Position} from 'vscode'; import * as path from "path"; @@ -43,12 +43,13 @@ class RelativePath { this.initializeWatcher(); this.searchWorkspace(); + this.initializeConfigWatcher(); } // When a file is added or deleted, we need to update cache private initializeWatcher() { // Watch for file system changes - as we're caching the searched files - this._watcher = workspace.createFileSystemWatcher("**/*.*", false, true, false); + this._watcher = workspace.createFileSystemWatcher("**/*.*", false, true, false); // Add a file on creation this._watcher.onDidCreate((e: Uri) => { @@ -66,7 +67,7 @@ class RelativePath { } // Go through workspace to cache files - private searchWorkspace() { + private searchWorkspace(skipOpen = false) { let emptyItem: QuickPickItem = { label: "", description: "No files found" }; // Show loading info box @@ -105,7 +106,9 @@ class RelativePath { } this._items = files; - this.findRelativePath(); + if (!skipOpen) { + this.findRelativePath(); + } }); this._myGlob.on("end", () => { this._pausedSearch = false; @@ -113,6 +116,30 @@ class RelativePath { } } + // Compares the ignore property of _configuration to lastConfig + private ignoreWasUpdated(currentIgnore: Array, lastIgnore: Array): boolean { + if (currentIgnore.length !== lastIgnore.length) { + return true; + } else if (currentIgnore.some(glob => lastIgnore.indexOf(glob) < 0)) { + return true; + } + + return false; + } + + // Listen for changes in the config files and update the config object + private initializeConfigWatcher(): void { + workspace.onDidChangeConfiguration((e) => { + const lastConfig = this._configuration; + this._configuration = workspace.getConfiguration("relativePath"); + + // Handle updates to the ignored property if there's one + if (this.ignoreWasUpdated(this._configuration.ignore, lastConfig.ignore)) { + this.searchWorkspace(true); + } + }, this); + } + // Show dropdown editor private showQuickPick(items: string[], editor: TextEditor): void { if (items) { From 291669d829176edad7d4ce5558dd47d357eaf56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20=C3=96stlund?= Date: Sun, 18 Dec 2016 13:51:50 +0100 Subject: [PATCH 2/2] Move file updates to own function This allows the config watcher to not open the input window --- src/extension.ts | 53 ++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 954c01b..4743088 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -66,6 +66,33 @@ class RelativePath { }); } + // Purely updates the files + private updateFiles(skipOpen = false): void { + // Search for files + if (this._pausedSearch) { + this._pausedSearch = false; + if (this._myGlob) { + this._myGlob.resume(); + } + } else { + this._myGlob = new Glob(this._workspacePath + "/**/*.*", + { ignore: this._configuration.get("ignore") }, + (err, files) => { + if (err) { + return; + } + + this._items = files; + if (!skipOpen) { + this.findRelativePath(); + } + }); + this._myGlob.on("end", () => { + this._pausedSearch = false; + }); + } + } + // Go through workspace to cache files private searchWorkspace(skipOpen = false) { let emptyItem: QuickPickItem = { label: "", description: "No files found" }; @@ -91,29 +118,7 @@ class RelativePath { } ); - // Search for files - if (this._pausedSearch) { - this._pausedSearch = false; - if (this._myGlob) { - this._myGlob.resume(); - } - } else { - this._myGlob = new Glob(this._workspacePath + "/**/*.*", - { ignore: this._configuration.get("ignore") }, - (err, files) => { - if (err) { - return; - } - - this._items = files; - if (!skipOpen) { - this.findRelativePath(); - } - }); - this._myGlob.on("end", () => { - this._pausedSearch = false; - }); - } + this.updateFiles(skipOpen); } // Compares the ignore property of _configuration to lastConfig @@ -135,7 +140,7 @@ class RelativePath { // Handle updates to the ignored property if there's one if (this.ignoreWasUpdated(this._configuration.ignore, lastConfig.ignore)) { - this.searchWorkspace(true); + this.updateFiles(true); } }, this); }