Skip to content

Commit dd3a5ee

Browse files
committed
Add workspace folder selection before file browsing in multi-root workspaces for both add and remove context commands
1 parent e8c70bf commit dd3a5ee

2 files changed

Lines changed: 122 additions & 6 deletions

File tree

packages/vscode/src/commands/add-file-to-context-command.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,54 @@ export const add_file_to_context_command = (
2121
return
2222
}
2323

24+
let roots_to_index = workspace_roots
25+
26+
if (workspace_roots.length > 1) {
27+
const items: vscode.QuickPickItem[] = workspace_roots.map((root) => ({
28+
label: workspace_provider.get_workspace_name(root),
29+
description: root
30+
}))
31+
32+
const selected = await new Promise<vscode.QuickPickItem | undefined>(
33+
(resolve) => {
34+
const quick_pick = vscode.window.createQuickPick()
35+
quick_pick.items = items
36+
quick_pick.placeholder = 'Select a workspace folder to browse files'
37+
quick_pick.title = 'Workspace Folders'
38+
quick_pick.buttons = [
39+
{
40+
iconPath: new vscode.ThemeIcon('close'),
41+
tooltip: 'Close'
42+
}
43+
]
44+
45+
quick_pick.onDidTriggerButton((button) => {
46+
if (button.tooltip == 'Close') {
47+
quick_pick.hide()
48+
}
49+
})
50+
51+
quick_pick.onDidAccept(() => {
52+
resolve(quick_pick.selectedItems[0])
53+
quick_pick.hide()
54+
})
55+
56+
quick_pick.onDidHide(() => {
57+
resolve(undefined)
58+
quick_pick.dispose()
59+
})
60+
61+
quick_pick.show()
62+
}
63+
)
64+
65+
if (!selected || !selected.description) {
66+
return
67+
}
68+
69+
roots_to_index = [selected.description]
70+
}
71+
2472
const quick_pick = vscode.window.createQuickPick<FileQuickPickItem>()
2573
quick_pick.title = 'Workspace Files'
2674
quick_pick.placeholder = 'Select a file to add to context'
@@ -156,7 +204,7 @@ export const add_file_to_context_command = (
156204

157205
try {
158206
const all_files: string[] = []
159-
for (const root of workspace_roots) {
207+
for (const root of roots_to_index) {
160208
const files = await workspace_provider.find_all_files(root)
161209
all_files.push(...files)
162210
}
@@ -177,7 +225,7 @@ export const add_file_to_context_command = (
177225
directory = ''
178226
}
179227

180-
if (workspace_roots.length > 1 && workspace_root) {
228+
if (roots_to_index.length > 1 && workspace_root) {
181229
const workspace_name =
182230
workspace_provider.get_workspace_name(workspace_root)
183231
directory = directory

packages/vscode/src/commands/remove-file-from-context-command.ts

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,76 @@ export const remove_file_from_context_command = (
2323
return
2424
}
2525

26+
const workspace_roots = workspace_provider.getWorkspaceRoots()
27+
let files_to_show = current_checked
28+
let show_workspace_prefix = workspace_roots.length > 1
29+
30+
if (workspace_roots.length > 1) {
31+
const roots_with_checked_files = new Map<string, string[]>()
32+
33+
for (const file_path of current_checked) {
34+
const root = workspace_provider.get_workspace_root_for_file(file_path)
35+
if (root) {
36+
if (!roots_with_checked_files.has(root)) {
37+
roots_with_checked_files.set(root, [])
38+
}
39+
roots_with_checked_files.get(root)!.push(file_path)
40+
}
41+
}
42+
43+
const eligible_roots = Array.from(roots_with_checked_files.keys())
44+
45+
if (eligible_roots.length > 1) {
46+
const items: vscode.QuickPickItem[] = eligible_roots
47+
.map((root) => ({
48+
label: workspace_provider.get_workspace_name(root),
49+
description: root
50+
}))
51+
.sort((a, b) => natural_sort(a.label, b.label))
52+
53+
const selected = await new Promise<vscode.QuickPickItem | undefined>(
54+
(resolve) => {
55+
const quick_pick = vscode.window.createQuickPick()
56+
quick_pick.items = items
57+
quick_pick.placeholder =
58+
'Select a workspace folder to remove files from'
59+
quick_pick.title = 'Workspace Folders'
60+
quick_pick.buttons = [
61+
{
62+
iconPath: new vscode.ThemeIcon('close'),
63+
tooltip: 'Close'
64+
}
65+
]
66+
67+
quick_pick.onDidTriggerButton((button) => {
68+
if (button.tooltip == 'Close') {
69+
quick_pick.hide()
70+
}
71+
})
72+
73+
quick_pick.onDidAccept(() => {
74+
resolve(quick_pick.selectedItems[0])
75+
quick_pick.hide()
76+
})
77+
78+
quick_pick.onDidHide(() => {
79+
resolve(undefined)
80+
quick_pick.dispose()
81+
})
82+
83+
quick_pick.show()
84+
}
85+
)
86+
87+
if (!selected || !selected.description) {
88+
return
89+
}
90+
91+
files_to_show = roots_with_checked_files.get(selected.description)!
92+
show_workspace_prefix = false
93+
}
94+
}
95+
2696
const quick_pick = vscode.window.createQuickPick<FileQuickPickItem>()
2797
quick_pick.title = 'Context Files'
2898
quick_pick.placeholder = 'Select a file to remove from context'
@@ -156,9 +226,7 @@ export const remove_file_from_context_command = (
156226
}
157227
})
158228

159-
const workspace_roots = workspace_provider.getWorkspaceRoots()
160-
161-
const items: FileQuickPickItem[] = current_checked.map((file_path) => {
229+
const items: FileQuickPickItem[] = files_to_show.map((file_path) => {
162230
const workspace_root =
163231
workspace_provider.get_workspace_root_for_file(file_path)
164232

@@ -174,7 +242,7 @@ export const remove_file_from_context_command = (
174242
directory = ''
175243
}
176244

177-
if (workspace_roots.length > 1 && workspace_root) {
245+
if (show_workspace_prefix && workspace_root) {
178246
const workspace_name =
179247
workspace_provider.get_workspace_name(workspace_root)
180248
directory = directory

0 commit comments

Comments
 (0)