Skip to content

Commit 370ff84

Browse files
committed
fix(vscode): stabilized recreation of test explorer items if folders are deleted
1 parent 8abcb67 commit 370ff84

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

vscode-client/testcontrollermanager.ts

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { red, yellow, blue } from "ansi-colors";
55
import * as vscode from "vscode";
66
import { DebugManager } from "./debugmanager";
7+
import * as fs from "fs";
78

89
import {
910
ClientState,
@@ -142,7 +143,7 @@ export class TestControllerManager {
142143
private readonly refreshMutex = new Mutex();
143144
private readonly debugSessions = new Set<vscode.DebugSession>();
144145
private readonly didChangedTimer = new Map<string, DidChangeEntry>();
145-
private fileChangeTimer: DidChangeEntry | undefined;
146+
private refreshWorkspaceChangeTimer: DidChangeEntry | undefined;
146147
private diagnosticCollection = vscode.languages.createDiagnosticCollection("robotCode discovery");
147148

148149
constructor(
@@ -166,12 +167,17 @@ export class TestControllerManager {
166167
(_) => undefined,
167168
);
168169

169-
const fileWatcher = vscode.workspace.createFileSystemWatcher(
170-
`**/*.{${this.languageClientsManager.fileExtensions.join(",")}}`,
171-
);
172-
fileWatcher.onDidCreate((uri) => this.refreshUri(uri, "create"));
173-
fileWatcher.onDidDelete((uri) => this.refreshUri(uri, "delete"));
174-
fileWatcher.onDidChange((uri) => this.refreshUri(uri, "change"));
170+
const fileWatcher = vscode.workspace.createFileSystemWatcher(`**/[!._]*`);
171+
172+
fileWatcher.onDidCreate((uri) => {
173+
this.refreshUri(uri, "create");
174+
});
175+
fileWatcher.onDidDelete((uri) => {
176+
this.refreshUri(uri, "delete");
177+
});
178+
fileWatcher.onDidChange((uri) => {
179+
this.refreshUri(uri, "change");
180+
});
175181

176182
this._disposables = vscode.Disposable.from(
177183
this.diagnosticCollection,
@@ -730,7 +736,7 @@ export class TestControllerManager {
730736

731737
const result = await this.discoverTests(
732738
folder,
733-
["discover", "--no-diagnostics", "--read-from-stdin", "all"],
739+
["discover", "--read-from-stdin", "all"],
734740
[],
735741
JSON.stringify(o),
736742
true,
@@ -792,7 +798,7 @@ export class TestControllerManager {
792798

793799
const result = await this.discoverTests(
794800
folder,
795-
["discover", "--no-diagnostics", "--read-from-stdin", "tests"],
801+
["discover", "--read-from-stdin", "tests"],
796802
[
797803
...(robotWorkspaceItem?.needs_parse_include && testItem.rel_source
798804
? ["--parse-include", testItem.rel_source]
@@ -1023,33 +1029,46 @@ export class TestControllerManager {
10231029
}
10241030

10251031
private refreshUri(uri?: vscode.Uri, reason?: string) {
1026-
if (vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri?.toString())) return;
1032+
if (uri) {
1033+
if (uri?.scheme !== "file") return;
10271034

1028-
this.outputChannel.appendLine(`refresh uri ${uri ? uri.toString() : "unknown"} because: ${reason ?? "unknown"}`);
1035+
const exists = fs.existsSync(uri.fsPath);
1036+
const isFileAndExists = exists && fs.statSync(uri.fsPath).isFile();
10291037

1030-
if (uri) {
1031-
const testItem = this.findTestItemByUri(uri.toString());
1032-
if (testItem) {
1033-
this.refreshItem(testItem).then(
1034-
(_) => undefined,
1035-
(_) => undefined,
1036-
);
1038+
if (
1039+
isFileAndExists &&
1040+
!this.languageClientsManager.fileExtensions.some((ext) => uri?.path.toLowerCase().endsWith(`.${ext}`))
1041+
)
10371042
return;
1043+
1044+
if (isFileAndExists && vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri?.toString())) return;
1045+
1046+
this.outputChannel.appendLine(`refresh uri ${uri ? uri.toString() : "unknown"} because: ${reason ?? "unknown"}`);
1047+
1048+
if (exists) {
1049+
const testItem = this.findTestItemByUri(uri.toString());
1050+
if (testItem) {
1051+
this.refreshItem(testItem).then(
1052+
(_) => undefined,
1053+
(_) => undefined,
1054+
);
1055+
return;
1056+
}
10381057
}
10391058

10401059
const workspace = vscode.workspace.getWorkspaceFolder(uri);
10411060
if (workspace === undefined) return;
10421061

10431062
if (this.didChangedTimer.has(uri.toString())) return;
10441063

1045-
if (this.fileChangeTimer) {
1046-
this.fileChangeTimer.cancel();
1047-
this.fileChangeTimer = undefined;
1064+
if (this.refreshWorkspaceChangeTimer) {
1065+
this.refreshWorkspaceChangeTimer.cancel();
1066+
this.refreshWorkspaceChangeTimer = undefined;
10481067
}
10491068

10501069
const cancelationTokenSource = new vscode.CancellationTokenSource();
10511070

1052-
this.fileChangeTimer = new DidChangeEntry(
1071+
this.refreshWorkspaceChangeTimer = new DidChangeEntry(
10531072
setTimeout(() => {
10541073
this.refreshWorkspace(workspace, reason, cancelationTokenSource.token).then(
10551074
() => undefined,
@@ -1059,9 +1078,9 @@ export class TestControllerManager {
10591078
cancelationTokenSource,
10601079
);
10611080
} else {
1062-
if (this.fileChangeTimer) {
1063-
this.fileChangeTimer.cancel();
1064-
this.fileChangeTimer = undefined;
1081+
if (this.refreshWorkspaceChangeTimer) {
1082+
this.refreshWorkspaceChangeTimer.cancel();
1083+
this.refreshWorkspaceChangeTimer = undefined;
10651084
}
10661085

10671086
this.refresh().then(

0 commit comments

Comments
 (0)