Skip to content

Commit e4d435e

Browse files
committed
Extract parent folder quick pick logic into a reusable utility and integrate it into search result workflows
- in 'search files': each file result should have 'add parent folder to context' action - copy these newly referenced translations to 'search files' [Changes] - going back from parent folders doesn't work properly - file results are not shown - in search files: going back from 'parent folder' quick pick should hightlight the item that was clicked - once parent folder gets selected, instead of closing the flow, reopen the list of files - extract parent folder selection to `apps/editor/src/utils` (put translations in `apps/editor/src/i18n/translations/utils` - give a better name for `apps/editor/src/utils/prompt-for-parent-folder.ts` (also rename translations), e.g. select parent folder quick pick - rename 'add parent folder to context' to 'select parent folder...'
1 parent 9db37b0 commit e4d435e

8 files changed

Lines changed: 337 additions & 191 deletions

File tree

‎apps/editor/src/commands/select-workspace-file-command.ts‎

Lines changed: 32 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import * as vscode from 'vscode'
22
import * as path from 'path'
3-
import {
4-
WorkspaceProvider,
5-
FileItem
6-
} from '../context/providers/workspace/workspace-provider'
3+
import { WorkspaceProvider } from '../context/providers/workspace/workspace-provider'
74
import { natural_sort } from '../utils/natural-sort'
85
import { t } from '../i18n'
6+
import { show_parent_folder_quick_pick } from '../utils/show-parent-folder-quick-pick'
97

108
interface FileQuickPickItem extends vscode.QuickPickItem {
119
full_path: string
@@ -110,125 +108,43 @@ export const select_workspace_file_command = (
110108
const item = e.item
111109
last_interacted_path = item.full_path
112110

113-
if (
114-
e.button.tooltip ==
115-
t('command.select-workspace-file.add-parent-folder')
116-
) {
117-
const workspace_root = workspace_provider.get_workspace_root_for_file(
118-
item.full_path
119-
)
111+
if (e.button.tooltip == t('common.select-parent-folder')) {
112+
is_showing_folder_quick_pick = true
113+
quick_pick.hide()
120114

121-
if (!workspace_root) return
115+
const result = await show_parent_folder_quick_pick({
116+
file_path: item.full_path,
117+
workspace_provider
118+
})
122119

123-
const folders: { label: string; full_path: string }[] = []
124-
let current_dir = path.dirname(item.full_path)
120+
is_showing_folder_quick_pick = false
125121

126-
while (
127-
current_dir.startsWith(workspace_root) &&
128-
current_dir != workspace_root
122+
if (
123+
result === 'added' ||
124+
result === 'back' ||
125+
result === 'no_folders' ||
126+
result === 'no_workspace_root'
129127
) {
130-
const relative = path.relative(workspace_root, current_dir)
131-
folders.push({
132-
label: relative.replace(/\\/g, '/'),
133-
full_path: current_dir
134-
})
135-
current_dir = path.dirname(current_dir)
136-
}
137-
138-
if (folders.length == 0) {
139-
vscode.window.showInformationMessage(
140-
t('command.select-workspace-file.no-parent-folders')
141-
)
142-
return
143-
}
144-
145-
const folder_quick_pick = vscode.window.createQuickPick<{
146-
label: string
147-
full_path: string
148-
}>()
149-
folder_quick_pick.title = t(
150-
'command.select-workspace-file.parent-folders'
151-
)
152-
folder_quick_pick.placeholder = t(
153-
'command.select-workspace-file.select-folder'
154-
)
155-
folder_quick_pick.items = folders.map((f) => ({
156-
label: f.label,
157-
full_path: f.full_path
158-
}))
159-
folder_quick_pick.ignoreFocusOut = false
160-
folder_quick_pick.buttons = [vscode.QuickInputButtons.Back]
161-
162-
let folder_accepted = false
163-
let go_back = false
164-
165-
folder_quick_pick.onDidTriggerButton((button) => {
166-
if (button === vscode.QuickInputButtons.Back) {
167-
go_back = true
168-
folder_quick_pick.hide()
128+
if (file_items_cache.length > 0) {
129+
quick_pick.items = file_items_cache
169130
}
170-
})
131+
quick_pick.value = last_search_query
132+
quick_pick.show()
171133

172-
folder_quick_pick.onDidAccept(async () => {
173-
const selected = folder_quick_pick.selectedItems[0]
174-
if (selected) {
175-
folder_accepted = true
176-
const file_item = new FileItem(
177-
path.basename(selected.full_path),
178-
vscode.Uri.file(selected.full_path),
179-
vscode.TreeItemCollapsibleState.Collapsed,
180-
true,
181-
vscode.TreeItemCheckboxState.Unchecked,
182-
false,
183-
false,
184-
undefined,
185-
undefined,
186-
undefined,
187-
undefined,
188-
undefined
189-
)
190-
191-
await workspace_provider.update_check_state(
192-
file_item,
193-
vscode.TreeItemCheckboxState.Checked
194-
)
195-
folder_quick_pick.hide()
196-
quick_pick.hide()
197-
}
198-
})
134+
const source_item = last_interacted_path
135+
? file_items_cache.find(
136+
(i) => i.full_path === last_interacted_path
137+
)
138+
: undefined
199139

200-
folder_quick_pick.onDidHide(() => {
201-
folder_quick_pick.dispose()
202-
is_showing_folder_quick_pick = false
203-
204-
if (!folder_accepted) {
205-
if (go_back) {
206-
if (file_items_cache.length > 0) {
207-
quick_pick.items = file_items_cache
208-
}
209-
quick_pick.value = last_search_query
210-
quick_pick.show()
211-
212-
const source_item = last_interacted_path
213-
? file_items_cache.find(
214-
(i) => i.full_path === last_interacted_path
215-
)
216-
: undefined
217-
218-
if (source_item) {
219-
setTimeout(() => {
220-
quick_pick.activeItems = [source_item]
221-
}, 0)
222-
}
223-
} else {
224-
quick_pick.dispose()
225-
}
140+
if (source_item) {
141+
setTimeout(() => {
142+
quick_pick.activeItems = [source_item]
143+
}, 0)
226144
}
227-
})
228-
229-
is_showing_folder_quick_pick = true
230-
quick_pick.hide()
231-
folder_quick_pick.show()
145+
} else if (result === 'cancel') {
146+
quick_pick.dispose()
147+
}
232148
} else if (e.button.iconPath instanceof vscode.ThemeIcon) {
233149
if (e.button.iconPath.id == 'go-to-file') {
234150
quick_pick.activeItems = [item]
@@ -276,7 +192,7 @@ export const select_workspace_file_command = (
276192
if (has_parent_folder) {
277193
buttons.push({
278194
iconPath: new vscode.ThemeIcon('folder'),
279-
tooltip: t('command.select-workspace-file.add-parent-folder')
195+
tooltip: t('common.select-parent-folder')
280196
})
281197
}
282198

‎apps/editor/src/features/search-files/utils/prompt-for-intelligent-search-results.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provi
44
import { display_token_count } from '@/utils/display-token-count'
55
import { FileAnalysisResult } from './analyze-files'
66
import { t } from '@/i18n'
7+
import { show_parent_folder_quick_pick } from '@/utils/show-parent-folder-quick-pick'
78

89
export const prompt_for_intelligent_search_results = async (params: {
910
extracted_files: string[]
@@ -43,6 +44,10 @@ export const prompt_for_intelligent_search_results = async (params: {
4344
iconPath: new vscode.ThemeIcon('close'),
4445
tooltip: t('common.close')
4546
}
47+
const add_parent_folder_button = {
48+
iconPath: new vscode.ThemeIcon('folder'),
49+
tooltip: t('common.select-parent-folder')
50+
}
4651
const open_file_button = {
4752
iconPath: new vscode.ThemeIcon('go-to-file'),
4853
tooltip: t('common.go-to-file')
@@ -60,6 +65,7 @@ export const prompt_for_intelligent_search_results = async (params: {
6065
? path.relative(workspace_root, file_path)
6166
: file_path
6267
const dir_name = path.dirname(relative_path)
68+
const has_parent_folder = dir_name != '.'
6369
let display_dir = dir_name == '.' ? '' : dir_name
6470

6571
if (
@@ -77,13 +83,19 @@ export const prompt_for_intelligent_search_results = async (params: {
7783
await params.workspace_provider.calculate_file_tokens(file_path)
7884
const formatted_token_count = display_token_count(token_count.total)
7985

86+
const buttons: vscode.QuickInputButton[] = []
87+
if (has_parent_folder) {
88+
buttons.push(add_parent_folder_button)
89+
}
90+
buttons.push(open_file_button)
91+
8092
return {
8193
label: path.basename(file_path),
8294
description: display_dir
8395
? `${formatted_token_count} · ${display_dir}`
8496
: formatted_token_count,
8597
file_path,
86-
buttons: [open_file_button]
98+
buttons
8799
}
88100
})
89101
)
@@ -109,6 +121,8 @@ export const prompt_for_intelligent_search_results = async (params: {
109121
close_button
110122
]
111123

124+
let is_showing_folder_quick_pick = false
125+
112126
const list_selection = await new Promise<
113127
| { selected_paths: string[]; matched_paths: string[] }
114128
| { action: 'search_in_results'; matched_paths: string[] }
@@ -154,10 +168,53 @@ export const prompt_for_intelligent_search_results = async (params: {
154168
})
155169
)
156170
}
171+
} else if (e.button === add_parent_folder_button) {
172+
is_showing_folder_quick_pick = true
173+
quick_pick.hide()
174+
175+
const result = await show_parent_folder_quick_pick({
176+
file_path: e.item.file_path,
177+
workspace_provider: params.workspace_provider
178+
})
179+
180+
is_showing_folder_quick_pick = false
181+
182+
if (
183+
result === 'added' ||
184+
result === 'back' ||
185+
result === 'no_folders' ||
186+
result === 'no_workspace_root'
187+
) {
188+
const current_items = quick_pick.items
189+
let current_selected = quick_pick.selectedItems
190+
191+
if (result === 'added') {
192+
const currently_checked =
193+
params.workspace_provider.get_checked_files()
194+
current_selected = current_items.filter(
195+
(item) =>
196+
currently_checked.includes(item.file_path) ||
197+
current_selected.includes(item)
198+
)
199+
}
200+
201+
quick_pick.items = [...current_items]
202+
quick_pick.selectedItems = current_selected
203+
quick_pick.show()
204+
205+
setTimeout(() => {
206+
quick_pick.activeItems = [e.item]
207+
}, 0)
208+
} else {
209+
is_resolved = true
210+
resolve('cancel')
211+
quick_pick.dispose()
212+
}
157213
}
158214
})
159215

160216
quick_pick.onDidHide(() => {
217+
if (is_showing_folder_quick_pick) return
161218
if (!is_resolved) {
162219
resolve('back')
163220
}

0 commit comments

Comments
 (0)