Skip to content

Commit cc124db

Browse files
committed
Dynamically update token counts in QuickPick placeholders based on selection changes
1 parent 5ab6135 commit cc124db

3 files changed

Lines changed: 29 additions & 26 deletions

File tree

apps/editor/src/commands/apply-context-command/helpers/applying/select-context-paths.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,26 @@ export const select_context_paths = async (
7474

7575
const quick_pick_items = await create_quick_pick_items(resolved_paths)
7676

77-
const total_tokens = quick_pick_items.reduce(
78-
(acc, item) => acc + item.token_count,
79-
0
80-
)
81-
const formatted_total = display_token_count(total_tokens)
82-
8377
const list_quick_pick = vscode.window.createQuickPick<
8478
vscode.QuickPickItem & { file_path: string; token_count: number }
8579
>()
8680
list_quick_pick.title = context.name
87-
list_quick_pick.placeholder = `Select files to apply to context (totalling ${formatted_total} tokens)`
8881
list_quick_pick.canSelectMany = true
8982
list_quick_pick.ignoreFocusOut = true
9083
list_quick_pick.items = quick_pick_items
9184
list_quick_pick.selectedItems = quick_pick_items.filter((i) => i.picked)
85+
86+
const update_placeholder = () => {
87+
const total = list_quick_pick.selectedItems.reduce(
88+
(sum, item) => sum + item.token_count,
89+
0
90+
)
91+
const total_text =
92+
total > 0 ? ` (totalling ${display_token_count(total)} tokens)` : ''
93+
list_quick_pick.placeholder = `Select files to apply to context${total_text}`
94+
}
95+
update_placeholder()
96+
list_quick_pick.onDidChangeSelection(update_placeholder)
9297
list_quick_pick.buttons = [vscode.QuickInputButtons.Back]
9398

9499
const list_selection = await new Promise<
@@ -144,12 +149,7 @@ export const select_context_paths = async (
144149
(i) => i.picked
145150
)
146151

147-
const new_total = list_quick_pick.items.reduce(
148-
(acc, item) => acc + item.token_count,
149-
0
150-
)
151-
list_quick_pick.placeholder = `Select files to apply to context (totalling ${display_token_count(new_total)} tokens)`
152-
152+
update_placeholder()
153153
const choice = await vscode.window.showInformationMessage(
154154
'Removed from context.',
155155
'Undo'
@@ -180,11 +180,7 @@ export const select_context_paths = async (
180180
(i) => i.picked
181181
)
182182

183-
const undo_total = list_quick_pick.items.reduce(
184-
(acc, item) => acc + item.token_count,
185-
0
186-
)
187-
list_quick_pick.placeholder = `Select files to apply to context (totalling ${display_token_count(undo_total)} tokens)`
183+
update_placeholder()
188184
}
189185
}
190186
})

apps/editor/src/commands/apply-context-command/sources/commit-files-source.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,6 @@ export const handle_commit_files_source = async (
176176
})
177177
)
178178

179-
const total_tokens = file_items.reduce(
180-
(acc, item) => acc + item.token_count,
181-
0
182-
)
183-
const formatted_total = display_token_count(total_tokens)
184-
185179
const quick_pick_files = vscode.window.createQuickPick<any>()
186180
quick_pick_files.canSelectMany = true
187181
quick_pick_files.items = file_items
@@ -192,7 +186,18 @@ export const handle_commit_files_source = async (
192186
hash: selected_commit.hash.substring(0, 7)
193187
}
194188
)
195-
quick_pick_files.placeholder = `${t('command.apply-context.commit.select-files')} (totalling ${formatted_total} tokens)`
189+
190+
const update_placeholder = () => {
191+
const total = quick_pick_files.selectedItems.reduce(
192+
(sum: number, item: any) => sum + item.token_count,
193+
0
194+
)
195+
const total_text =
196+
total > 0 ? ` (totalling ${display_token_count(total)} tokens)` : ''
197+
quick_pick_files.placeholder = `${t('command.apply-context.commit.select-files')}${total_text}`
198+
}
199+
update_placeholder()
200+
quick_pick_files.onDidChangeSelection(update_placeholder)
196201
quick_pick_files.buttons = [vscode.QuickInputButtons.Back]
197202
quick_pick_files.ignoreFocusOut = true
198203

apps/editor/src/commands/select-imported-files-command.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ export const select_imported_files_command = (
450450
(sum, item) => sum + (item.tokens || 0),
451451
0
452452
)
453-
quick_pick.placeholder = `Select imported files (totalling ${display_token_count(total)} tokens)`
453+
const total_text =
454+
total > 0 ? ` (totalling ${display_token_count(total)} tokens)` : ''
455+
quick_pick.placeholder = `Select imported files${total_text}`
454456
}
455457
update_placeholder()
456458
quick_pick.onDidChangeSelection(update_placeholder)

0 commit comments

Comments
 (0)