Skip to content

Commit 607e39b

Browse files
committed
Add a folder button to file quick pick items to allow selecting and referencing parent directories in the prompt field
1 parent db21380 commit 607e39b

1 file changed

Lines changed: 138 additions & 12 deletions

File tree

‎apps/editor/src/views/panel/backend/message-handlers/handle-at-sign-quick-pick/handle-at-sign-quick-pick.ts‎

Lines changed: 138 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,75 @@ import * as path from 'path'
55
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
66
import { natural_sort } from '@/utils/natural-sort'
77

8-
type QuickPickItem = {
8+
type QuickPickItem = vscode.QuickPickItem & {
99
label: string
1010
description: string
1111
full_path: string
1212
absolute_path: string
13+
is_folder?: boolean
14+
}
15+
16+
const FOLDER_BUTTON: vscode.QuickInputButton = {
17+
iconPath: new vscode.ThemeIcon('folder'),
18+
tooltip: 'Reference parent folder'
19+
}
20+
21+
const select_parent_folder = async (
22+
workspace_provider: WorkspaceProvider,
23+
file_path: string,
24+
format_path: (p: string) => string
25+
): Promise<QuickPickItem | 'back' | undefined> => {
26+
const workspace_root =
27+
workspace_provider.get_workspace_root_for_file(file_path)
28+
if (!workspace_root) return undefined
29+
30+
const folders: QuickPickItem[] = []
31+
let current_dir = path.dirname(file_path)
32+
33+
while (
34+
current_dir.startsWith(workspace_root) &&
35+
current_dir !== workspace_root
36+
) {
37+
const normalized_path = format_path(current_dir)
38+
39+
folders.push({
40+
label: normalized_path,
41+
description: '',
42+
full_path: normalized_path,
43+
absolute_path: current_dir,
44+
is_folder: true
45+
})
46+
47+
current_dir = path.dirname(current_dir)
48+
}
49+
50+
if (folders.length == 0) {
51+
vscode.window.showInformationMessage('No parent folders found.')
52+
return undefined
53+
}
54+
55+
const quick_pick = vscode.window.createQuickPick<QuickPickItem>()
56+
quick_pick.items = folders
57+
quick_pick.placeholder = 'Reference a folder in the prompt field'
58+
quick_pick.title = 'Reference Parent Folders'
59+
quick_pick.buttons = [vscode.QuickInputButtons.Back]
60+
quick_pick.matchOnDescription = true
61+
62+
return new Promise<QuickPickItem | 'back' | undefined>((resolve) => {
63+
quick_pick.onDidTriggerButton((button) => {
64+
if (button === vscode.QuickInputButtons.Back) resolve('back')
65+
quick_pick.hide()
66+
})
67+
quick_pick.onDidAccept(() => {
68+
resolve(quick_pick.selectedItems[0])
69+
quick_pick.hide()
70+
})
71+
quick_pick.onDidHide(() => {
72+
resolve(undefined)
73+
quick_pick.dispose()
74+
})
75+
quick_pick.show()
76+
})
1377
}
1478

1579
const add_to_context_if_needed = async (
@@ -58,7 +122,8 @@ const browse_all_files = async (
58122
label: filename,
59123
description: dir_path == '.' ? '' : dir_path,
60124
full_path: normalized_path,
61-
absolute_path: p
125+
absolute_path: p,
126+
buttons: [FOLDER_BUTTON]
62127
}
63128
})
64129

@@ -75,20 +140,46 @@ const browse_all_files = async (
75140
}
76141

77142
return new Promise<QuickPickItem | 'back' | undefined>((resolve) => {
143+
let is_navigating = false
144+
78145
quick_pick.onDidTriggerButton((button) => {
79146
if (button === vscode.QuickInputButtons.Back) {
80147
resolve('back')
81148
}
82149
quick_pick.hide()
83150
})
84151

152+
quick_pick.onDidTriggerItemButton(async (e) => {
153+
if (e.button === FOLDER_BUTTON) {
154+
is_navigating = true
155+
quick_pick.hide()
156+
const selected = await select_parent_folder(
157+
workspace_provider,
158+
e.item.absolute_path,
159+
format_path
160+
)
161+
if (selected && selected !== 'back') {
162+
resolve(selected)
163+
quick_pick.dispose()
164+
} else if (selected === 'back') {
165+
is_navigating = false
166+
quick_pick.show()
167+
} else {
168+
resolve('back')
169+
quick_pick.dispose()
170+
}
171+
}
172+
})
173+
85174
quick_pick.onDidAccept(() => {
86175
resolve(quick_pick.selectedItems[0])
87176
quick_pick.hide()
88177
})
89178
quick_pick.onDidHide(() => {
90-
resolve('back')
91-
quick_pick.dispose()
179+
if (!is_navigating) {
180+
resolve('back')
181+
quick_pick.dispose()
182+
}
92183
})
93184
})
94185
}
@@ -122,10 +213,12 @@ const at_sign_quick_pick = async (params: {
122213
false
123214
)
124215
if (browsed_item && browsed_item != 'back') {
125-
await add_to_context_if_needed(
126-
params.workspace_provider,
127-
browsed_item.absolute_path
128-
)
216+
if (!browsed_item.is_folder) {
217+
await add_to_context_if_needed(
218+
params.workspace_provider,
219+
browsed_item.absolute_path
220+
)
221+
}
129222
return `\`${browsed_item.full_path}\``
130223
}
131224
return
@@ -151,7 +244,8 @@ const at_sign_quick_pick = async (params: {
151244
label: filename,
152245
description: dir_path == '.' ? '' : dir_path,
153246
full_path: normalized_path,
154-
absolute_path: p
247+
absolute_path: p,
248+
buttons: [FOLDER_BUTTON]
155249
}
156250
})
157251

@@ -187,12 +281,36 @@ const at_sign_quick_pick = async (params: {
187281
const choice = await new Promise<QuickPickItem | undefined>((resolve) => {
188282
let is_accepted = false
189283
let is_browse_visible = true
284+
let is_navigating = false
190285
const disposables: vscode.Disposable[] = []
191286

192287
disposables.push(
193288
quick_pick.onDidTriggerButton((_button) => {
194289
quick_pick.hide()
195290
}),
291+
quick_pick.onDidTriggerItemButton(async (e) => {
292+
if (e.button === FOLDER_BUTTON) {
293+
is_navigating = true
294+
quick_pick.hide()
295+
const selected = await select_parent_folder(
296+
params.workspace_provider,
297+
e.item.absolute_path,
298+
format_path
299+
)
300+
if (selected && selected !== 'back') {
301+
is_accepted = true
302+
resolve(selected)
303+
quick_pick.dispose()
304+
} else if (selected === 'back') {
305+
is_navigating = false
306+
quick_pick.show()
307+
} else {
308+
resolve(undefined)
309+
disposables.forEach((d) => d.dispose())
310+
quick_pick.dispose()
311+
}
312+
}
313+
}),
196314
quick_pick.onDidChangeValue((value) => {
197315
if (value && is_browse_visible) {
198316
quick_pick.items = all_quick_pick_items
@@ -209,11 +327,13 @@ const at_sign_quick_pick = async (params: {
209327
quick_pick.hide()
210328
}),
211329
quick_pick.onDidHide(() => {
212-
if (!is_accepted) {
330+
if (!is_accepted && !is_navigating) {
213331
resolve(undefined)
214332
}
215-
disposables.forEach((d) => d.dispose())
216-
quick_pick.dispose()
333+
if (!is_navigating) {
334+
disposables.forEach((d) => d.dispose())
335+
quick_pick.dispose()
336+
}
217337
})
218338
)
219339
quick_pick.show()
@@ -251,6 +371,12 @@ const at_sign_quick_pick = async (params: {
251371
}
252372

253373
if (selected_path_item) {
374+
if (!selected_path_item.is_folder) {
375+
await add_to_context_if_needed(
376+
params.workspace_provider,
377+
selected_path_item.absolute_path
378+
)
379+
}
254380
return `\`${selected_path_item.full_path}\``
255381
}
256382
return

0 commit comments

Comments
 (0)