Skip to content

Commit e58c687

Browse files
committed
Display token counts in context selection menus and Quick Pick placeholders across context source commands
1 parent 361fad3 commit e58c687

5 files changed

Lines changed: 163 additions & 21 deletions

File tree

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,37 @@ export const select_context_paths = async (
6565
: formatted_token_count,
6666
picked: currently_checked_set.has(file_path),
6767
file_path,
68-
buttons
68+
buttons,
69+
token_count: token_count.total
6970
}
7071
})
7172
)
7273
}
7374

7475
const quick_pick_items = await create_quick_pick_items(resolved_paths)
7576

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+
7683
const list_quick_pick = vscode.window.createQuickPick<
77-
vscode.QuickPickItem & { file_path: string }
84+
vscode.QuickPickItem & { file_path: string; token_count: number }
7885
>()
7986
list_quick_pick.title = context.name
80-
list_quick_pick.placeholder = 'Select files to apply to context'
87+
list_quick_pick.placeholder = `Select files to apply to context (totalling ${formatted_total} tokens)`
8188
list_quick_pick.canSelectMany = true
8289
list_quick_pick.ignoreFocusOut = true
8390
list_quick_pick.items = quick_pick_items
8491
list_quick_pick.selectedItems = quick_pick_items.filter((i) => i.picked)
8592
list_quick_pick.buttons = [vscode.QuickInputButtons.Back]
8693

8794
const list_selection = await new Promise<
88-
| readonly (vscode.QuickPickItem & { file_path: string })[]
95+
| readonly (vscode.QuickPickItem & {
96+
file_path: string
97+
token_count: number
98+
})[]
8999
| 'back'
90100
| undefined
91101
>((resolve) => {
@@ -134,6 +144,12 @@ export const select_context_paths = async (
134144
(i) => i.picked
135145
)
136146

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+
137153
const choice = await vscode.window.showInformationMessage(
138154
'Removed from context.',
139155
'Undo'
@@ -163,6 +179,12 @@ export const select_context_paths = async (
163179
list_quick_pick.selectedItems = list_quick_pick.items.filter(
164180
(i) => i.picked
165181
)
182+
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)`
166188
}
167189
}
168190
})

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

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { get_git_repository } from '@/utils/git-repository-utils'
88
import { Logger } from '@shared/utils/logger'
99
import { dictionary } from '@shared/constants/dictionary'
1010
import { t } from '@/i18n'
11+
import { display_token_count } from '@/utils/display-token-count'
1112

1213
export const handle_commit_files_source = async (
1314
workspace_provider: WorkspaceProvider,
@@ -19,6 +20,8 @@ export const handle_commit_files_source = async (
1920
return
2021
}
2122

23+
let last_selected_commit_hash: string | undefined
24+
2225
while (true) {
2326
const log_output = execSync(
2427
'git log --since="6 months ago" --pretty=format:"%H|%s|%ar"',
@@ -50,11 +53,21 @@ export const handle_commit_files_source = async (
5053
const quick_pick = vscode.window.createQuickPick<
5154
vscode.QuickPickItem & { hash: string }
5255
>()
56+
quick_pick.title = t('command.apply-context.commit.title')
5357
quick_pick.items = commits
5458
quick_pick.placeholder = t('command.apply-context.commit.select')
5559
quick_pick.matchOnDetail = true
5660
quick_pick.buttons = [vscode.QuickInputButtons.Back]
5761

62+
if (last_selected_commit_hash) {
63+
const active_item = commits.find(
64+
(c) => c.hash === last_selected_commit_hash
65+
)
66+
if (active_item) {
67+
quick_pick.activeItems = [active_item]
68+
}
69+
}
70+
5871
const selected_commit = await new Promise<
5972
(vscode.QuickPickItem & { hash: string }) | 'back' | undefined
6073
>((resolve) => {
@@ -92,6 +105,8 @@ export const handle_commit_files_source = async (
92105
return
93106
}
94107

108+
last_selected_commit_hash = selected_commit.hash
109+
95110
const files_output = execSync(
96111
`git show --name-only --pretty="" ${selected_commit.hash}`,
97112
{
@@ -136,26 +151,87 @@ export const handle_commit_files_source = async (
136151

137152
// Inner loop for file selection
138153
while (true) {
139-
const file_items = valid_files.map((f) => ({
140-
label: path.basename(f.relative_path),
141-
description: path.dirname(f.relative_path),
142-
picked: true,
143-
file_path: f.absolute_path
144-
}))
145-
146-
const selected_files = await vscode.window.showQuickPick(file_items, {
147-
canPickMany: true,
148-
placeHolder: t('command.apply-context.commit.select-files'),
149-
title: t('command.apply-context.commit.files-modified', {
150-
hash: selected_commit.hash.substring(0, 7)
154+
const file_items = await Promise.all(
155+
valid_files.map(async (f) => {
156+
const token_count = await workspace_provider.calculate_file_tokens(
157+
f.absolute_path
158+
)
159+
const formatted_token_count = display_token_count(token_count.total)
160+
const dir_name = path.dirname(f.relative_path)
161+
const display_dir = dir_name == '.' ? '' : dir_name
162+
163+
return {
164+
label: path.basename(f.relative_path),
165+
description: display_dir
166+
? `${formatted_token_count} · ${display_dir}`
167+
: formatted_token_count,
168+
picked: true,
169+
file_path: f.absolute_path,
170+
token_count: token_count.total
171+
}
151172
})
152-
})
173+
)
153174

154-
if (!selected_files || selected_files.length == 0) {
155-
// User cancelled file selection (pressed ESC), go back to commit selection
175+
const total_tokens = file_items.reduce(
176+
(acc, item) => acc + item.token_count,
177+
0
178+
)
179+
const formatted_total = display_token_count(total_tokens)
180+
181+
const quick_pick_files = vscode.window.createQuickPick<any>()
182+
quick_pick_files.canSelectMany = true
183+
quick_pick_files.items = file_items
184+
quick_pick_files.selectedItems = file_items.filter((i) => i.picked)
185+
quick_pick_files.title = t(
186+
'command.apply-context.commit.files-modified',
187+
{
188+
hash: selected_commit.hash.substring(0, 7)
189+
}
190+
)
191+
quick_pick_files.placeholder = `${t('command.apply-context.commit.select-files')} (totalling ${formatted_total} tokens)`
192+
quick_pick_files.buttons = [vscode.QuickInputButtons.Back]
193+
quick_pick_files.ignoreFocusOut = true
194+
195+
const selected_files = await new Promise<any[] | 'back' | undefined>(
196+
(resolve) => {
197+
let is_accepted = false
198+
let did_trigger_back = false
199+
const disposables: vscode.Disposable[] = []
200+
201+
disposables.push(
202+
quick_pick_files.onDidTriggerButton((button) => {
203+
if (button === vscode.QuickInputButtons.Back) {
204+
did_trigger_back = true
205+
resolve('back')
206+
quick_pick_files.hide()
207+
}
208+
}),
209+
quick_pick_files.onDidAccept(() => {
210+
is_accepted = true
211+
resolve(Array.from(quick_pick_files.selectedItems))
212+
quick_pick_files.hide()
213+
}),
214+
quick_pick_files.onDidHide(() => {
215+
if (!is_accepted && !did_trigger_back) {
216+
resolve('back')
217+
}
218+
disposables.forEach((d) => d.dispose())
219+
quick_pick_files.dispose()
220+
})
221+
)
222+
223+
quick_pick_files.show()
224+
}
225+
)
226+
227+
if (selected_files == 'back') {
156228
break
157229
}
158230

231+
if (!selected_files || selected_files.length == 0) {
232+
return
233+
}
234+
159235
const selected_paths = selected_files.map((item) => item.file_path)
160236
const currently_checked = workspace_provider.get_checked_files()
161237
let paths_to_apply = selected_paths

‎apps/editor/src/commands/apply-context-command/sources/json-file-source.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
load_contexts_from_file,
1919
resolve_unique_context_name
2020
} from '../helpers/saving'
21+
import { display_token_count } from '../../../utils/display-token-count'
2122
import { t } from '@/i18n'
2223

2324
export const load_and_merge_file_contexts = async (): Promise<{
@@ -124,8 +125,20 @@ export const handle_json_file_source = async (
124125
primary_workspace_root,
125126
workspace_provider
126127
)
128+
129+
const token_counts = await Promise.all(
130+
resolved_paths.map((p) =>
131+
workspace_provider.calculate_file_tokens(p)
132+
)
133+
)
134+
const total_tokens = token_counts.reduce(
135+
(acc, tc) => acc + tc.total,
136+
0
137+
)
138+
const formatted_tokens = display_token_count(total_tokens)
139+
127140
const roots = context_to_roots.get(context.name) || []
128-
let description = `${resolved_paths.length} file${
141+
let description = `${formatted_tokens} · ${resolved_paths.length} file${
129142
resolved_paths.length == 1 ? '' : 's'
130143
}`
131144

‎apps/editor/src/commands/apply-context-command/sources/workspace-state-source.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
save_contexts_for_workspace,
1717
load_and_merge_global_contexts
1818
} from '../helpers/saving'
19+
import { display_token_count } from '../../../utils/display-token-count'
1920
import { t } from '@/i18n'
2021

2122
let active_deletion_timestamp: number | undefined
@@ -78,8 +79,20 @@ export const handle_workspace_state_source = async (
7879
primary_workspace_root,
7980
workspace_provider
8081
)
82+
83+
const token_counts = await Promise.all(
84+
resolved_paths.map((p) =>
85+
workspace_provider.calculate_file_tokens(p)
86+
)
87+
)
88+
const total_tokens = token_counts.reduce(
89+
(acc, tc) => acc + tc.total,
90+
0
91+
)
92+
const formatted_tokens = display_token_count(total_tokens)
93+
8194
const roots = context_to_roots.get(context.name) || []
82-
let description = `${resolved_paths.length} file${
95+
let description = `${formatted_tokens} · ${resolved_paths.length} file${
8396
resolved_paths.length == 1 ? '' : 's'
8497
}`
8598

‎apps/editor/src/i18n/translations/commands/apply-context.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,24 @@ export const apply_context = {
521521
hu: 'Nem találhatók commitok a tárolóban.',
522522
bg: 'Няма намерени къмити в хранилището.'
523523
},
524+
'command.apply-context.commit.title': {
525+
en: 'Commits',
526+
pl: 'Commity',
527+
'zh-cn': '提交',
528+
ja: 'コミット',
529+
'zh-tw': '提交',
530+
de: 'Commits',
531+
es: 'Commits',
532+
fr: 'Commits',
533+
'pt-br': 'Commits',
534+
ru: 'Коммиты',
535+
ko: '커밋',
536+
it: 'Commit',
537+
tr: 'İşlemeler',
538+
cs: 'Commity',
539+
hu: 'Commitok',
540+
bg: 'Къмити'
541+
},
524542
'command.apply-context.commit.select': {
525543
en: 'Select a commit to load modified files from',
526544
pl: 'Wybierz commit, z którego załadować zmodyfikowane pliki',

0 commit comments

Comments
 (0)