Skip to content

Commit f5d409e

Browse files
committed
Refactor workspace root detection to properly handle nested workspaces and exclude files from other workspaces
1 parent ea9a607 commit f5d409e

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎apps/editor/src/context/providers/open-editors/open-editors-provider.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ export class OpenEditorsProvider
9191
}
9292

9393
private _is_file_in_any_workspace(file_path: string): boolean {
94-
return this._workspace_roots.some((root) => file_path.startsWith(root))
94+
return !!this._workspace_provider.get_workspace_root_for_file(file_path)
9595
}
9696

9797
private _get_containing_workspace_root(
9898
file_path: string
9999
): string | undefined {
100-
return this._workspace_roots.find((root) => file_path.startsWith(root))
100+
return this._workspace_provider.get_workspace_root_for_file(file_path)
101101
}
102102

103103
private _uncheck_ignored_files() {

‎apps/editor/src/context/providers/workspace/modules/token-calculator.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,13 @@ export class TokenCalculator implements vscode.Disposable {
418418

419419
for (const entry of entries) {
420420
const full_path = path.join(dir_path, entry.name)
421+
422+
const file_workspace_root =
423+
this._provider.get_workspace_root_for_file(full_path)
424+
if (file_workspace_root && file_workspace_root !== workspace_root) {
425+
continue
426+
}
427+
421428
const relative_path = path.relative(workspace_root, full_path)
422429

423430
if (
@@ -523,6 +530,13 @@ export class TokenCalculator implements vscode.Disposable {
523530
})
524531
for (const entry of entries) {
525532
const full_path = path.join(dir_path, entry.name)
533+
534+
const file_workspace_root =
535+
this._provider.get_workspace_root_for_file(full_path)
536+
if (file_workspace_root && file_workspace_root !== workspace_root) {
537+
continue
538+
}
539+
526540
const relative_path = path.relative(workspace_root, full_path)
527541

528542
if (

‎apps/editor/src/context/providers/workspace/workspace-provider.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,17 @@ export class WorkspaceProvider
191191
let matching_root: string | undefined
192192

193193
for (const root of this._workspace_roots) {
194-
if (file_path.startsWith(root)) {
194+
const is_exact_match = file_path === root
195+
const is_child =
196+
file_path.startsWith(root) &&
197+
(root.endsWith('/') ||
198+
root.endsWith('\\') ||
199+
root.endsWith(path.sep) ||
200+
file_path[root.length] === '/' ||
201+
file_path[root.length] === '\\' ||
202+
file_path[root.length] === path.sep)
203+
204+
if (is_exact_match || is_child) {
195205
// If we found a match, or if this root is longer than the current match
196206
// (to handle nested workspace folders)
197207
if (!matching_root || root.length > matching_root.length) {
@@ -907,6 +917,13 @@ export class WorkspaceProvider
907917

908918
for (const entry of dir_entries) {
909919
const full_path = path.join(dir_path, entry.name)
920+
921+
// Exclude nested workspace folders from appearing in parent workspace trees
922+
const file_workspace_root = this.get_workspace_root_for_file(full_path)
923+
if (file_workspace_root && file_workspace_root !== workspace_root) {
924+
continue
925+
}
926+
910927
const relative_path = path.relative(workspace_root, full_path)
911928

912929
if (context_view) {
@@ -1103,6 +1120,13 @@ export class WorkspaceProvider
11031120

11041121
for (const entry of dir_entries) {
11051122
const sibling_path = path.join(dir_path, entry.name)
1123+
1124+
const file_workspace_root =
1125+
this.get_workspace_root_for_file(sibling_path)
1126+
if (file_workspace_root && file_workspace_root !== workspace_root) {
1127+
continue
1128+
}
1129+
11061130
const relative_path = path.relative(workspace_root, sibling_path)
11071131

11081132
if (
@@ -1205,6 +1229,12 @@ export class WorkspaceProvider
12051229

12061230
for (const entry of dir_entries) {
12071231
const full_path = path.join(dir_path, entry.name)
1232+
1233+
const file_workspace_root = this.get_workspace_root_for_file(full_path)
1234+
if (file_workspace_root && file_workspace_root !== workspace_root) {
1235+
continue
1236+
}
1237+
12081238
const relative_path = path.relative(workspace_root, full_path)
12091239

12101240
if (
@@ -1532,6 +1562,13 @@ export class WorkspaceProvider
15321562

15331563
for (const entry of entries) {
15341564
const full_path = path.join(dir_path, entry.name)
1565+
1566+
const file_workspace_root =
1567+
this.get_workspace_root_for_file(full_path)
1568+
if (file_workspace_root && file_workspace_root !== workspace_root) {
1569+
continue
1570+
}
1571+
15351572
const relative_path = path.relative(workspace_root, full_path)
15361573

15371574
if (

0 commit comments

Comments
 (0)