@@ -8,6 +8,7 @@ import { get_git_repository } from '@/utils/git-repository-utils'
88import { Logger } from '@shared/utils/logger'
99import { dictionary } from '@shared/constants/dictionary'
1010import { t } from '@/i18n'
11+ import { display_token_count } from '@/utils/display-token-count'
1112
1213export 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
0 commit comments