Skip to content

Commit a1b67e1

Browse files
Add watchers for examples directory
Signed-off-by: Sanjula Ganepola <[email protected]>
1 parent 429f93f commit a1b67e1

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/views/examples/exampleBrowser.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event, EventEmitter, ExtensionContext, MarkdownString, TextDocument, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState, Uri, commands, window, workspace } from "vscode";
1+
import { Event, EventEmitter, ExtensionContext, FileSystemWatcher, MarkdownString, RelativePattern, TextDocument, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState, Uri, commands, window, workspace } from "vscode";
22
import { Examples, SQLExample, ServiceInfoLabel, getMergedExamples } from ".";
33
import { notebookFromStatements } from "../../notebooks/logic/openAsNotebook";
44
import { osDetail } from "../../config";
@@ -11,9 +11,12 @@ export class ExampleBrowser implements TreeDataProvider<any> {
1111
private _onDidChangeTreeData: EventEmitter<TreeItem | undefined | null | void> = new EventEmitter<TreeItem | undefined | null | void>();
1212
readonly onDidChangeTreeData: Event<TreeItem | undefined | null | void> = this._onDidChangeTreeData.event;
1313

14+
private watchers: FileSystemWatcher[] = [];
1415
private currentFilter: string | undefined;
1516

1617
constructor(context: ExtensionContext) {
18+
this.setupWatchers();
19+
1720
context.subscriptions.push(
1821
commands.registerCommand(openExampleCommand, (example: SQLExample) => {
1922
if (example) {
@@ -153,6 +156,7 @@ export class ExampleBrowser implements TreeDataProvider<any> {
153156

154157
workspace.onDidChangeConfiguration(e => {
155158
if (e.affectsConfiguration('vscode-db2i.examples.customExampleDirectories')) {
159+
this.setupWatchers();
156160
this.refresh();
157161
}
158162
})
@@ -190,6 +194,32 @@ export class ExampleBrowser implements TreeDataProvider<any> {
190194
}
191195
}
192196
}
197+
198+
setupWatchers() {
199+
if (this.watchers) {
200+
for (const watcher of this.watchers) {
201+
watcher.dispose();
202+
}
203+
this.watchers = [];
204+
}
205+
206+
const existingDirectories = Configuration.get<string[]>(`examples.customExampleDirectories`) || [];
207+
for (const directory of existingDirectories) {
208+
const directoryUri = Uri.file(directory);
209+
const relativePattern = new RelativePattern(directoryUri, '**/*');
210+
const watcher = workspace.createFileSystemWatcher(relativePattern);
211+
watcher.onDidCreate(async (uri) => {
212+
this.refresh();
213+
});
214+
watcher.onDidChange(async (uri) => {
215+
this.refresh();
216+
});
217+
watcher.onDidDelete(async (uri) => {
218+
this.refresh();
219+
});
220+
this.watchers.push(watcher);
221+
}
222+
}
193223
}
194224

195225
class ExampleGroupItem extends TreeItem {

0 commit comments

Comments
 (0)