Skip to content

Commit 6c7dd5a

Browse files
kristofferrembackJakob Werner
authored andcommitted
Update config on change (#15)
* Update the config object on change Add a listener for updates to the config file which updates the configuration object attached to `this`. * Move file updates to own function This allows the config watcher to not open the input window
1 parent 35f7e49 commit 6c7dd5a

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

src/extension.ts

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ class RelativePath {
4343

4444
this.initializeWatcher();
4545
this.searchWorkspace();
46+
this.initializeConfigWatcher();
4647
}
4748

4849
// When a file is added or deleted, we need to update cache
4950
private initializeWatcher() {
5051
// Watch for file system changes - as we're caching the searched files
51-
this._watcher = workspace.createFileSystemWatcher("**/*.*", false, true, false);
52+
this._watcher = workspace.createFileSystemWatcher("**/*.*", false, true, false);
5253

5354
// Add a file on creation
5455
this._watcher.onDidCreate((e: Uri) => {
@@ -65,8 +66,35 @@ class RelativePath {
6566
});
6667
}
6768

69+
// Purely updates the files
70+
private updateFiles(skipOpen = false): void {
71+
// Search for files
72+
if (this._pausedSearch) {
73+
this._pausedSearch = false;
74+
if (this._myGlob) {
75+
this._myGlob.resume();
76+
}
77+
} else {
78+
this._myGlob = new Glob(this._workspacePath + "/**/*.*",
79+
{ ignore: this._configuration.get("ignore") },
80+
(err, files) => {
81+
if (err) {
82+
return;
83+
}
84+
85+
this._items = files;
86+
if (!skipOpen) {
87+
this.findRelativePath();
88+
}
89+
});
90+
this._myGlob.on("end", () => {
91+
this._pausedSearch = false;
92+
});
93+
}
94+
}
95+
6896
// Go through workspace to cache files
69-
private searchWorkspace() {
97+
private searchWorkspace(skipOpen = false) {
7098
let emptyItem: QuickPickItem = { label: "", description: "No files found" };
7199

72100
// Show loading info box
@@ -90,27 +118,31 @@ class RelativePath {
90118
}
91119
);
92120

93-
// Search for files
94-
if (this._pausedSearch) {
95-
this._pausedSearch = false;
96-
if (this._myGlob) {
97-
this._myGlob.resume();
98-
}
99-
} else {
100-
this._myGlob = new Glob(this._workspacePath + "/**/*.*",
101-
{ ignore: this._configuration.get("ignore") },
102-
(err, files) => {
103-
if (err) {
104-
return;
105-
}
121+
this.updateFiles(skipOpen);
122+
}
106123

107-
this._items = files;
108-
this.findRelativePath();
109-
});
110-
this._myGlob.on("end", () => {
111-
this._pausedSearch = false;
112-
});
124+
// Compares the ignore property of _configuration to lastConfig
125+
private ignoreWasUpdated(currentIgnore: Array<string>, lastIgnore: Array<string>): boolean {
126+
if (currentIgnore.length !== lastIgnore.length) {
127+
return true;
128+
} else if (currentIgnore.some(glob => lastIgnore.indexOf(glob) < 0)) {
129+
return true;
113130
}
131+
132+
return false;
133+
}
134+
135+
// Listen for changes in the config files and update the config object
136+
private initializeConfigWatcher(): void {
137+
workspace.onDidChangeConfiguration((e) => {
138+
const lastConfig = this._configuration;
139+
this._configuration = workspace.getConfiguration("relativePath");
140+
141+
// Handle updates to the ignored property if there's one
142+
if (this.ignoreWasUpdated(this._configuration.ignore, lastConfig.ignore)) {
143+
this.updateFiles(true);
144+
}
145+
}, this);
114146
}
115147

116148
// Show dropdown editor

0 commit comments

Comments
 (0)