@@ -10,7 +10,10 @@ import {
1010import { Logger } from '@shared/utils/logger'
1111import he from 'he'
1212import { PROVIDERS } from '@shared/constants/providers'
13- import { LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY } from '@/constants/state-keys'
13+ import {
14+ LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY ,
15+ RECENTLY_USED_CODE_COMPLETION_CONFIG_IDS_STATE_KEY
16+ } from '@/constants/state-keys'
1417import { ToolConfig } from '@/services/model-providers-manager'
1518import { PanelProvider } from '@/views/panel/backend/panel-provider'
1619import { dictionary } from '@shared/constants/dictionary'
@@ -38,10 +41,6 @@ const show_inline_completion = async (params: {
3841 const change_listener = vscode . workspace . onDidChangeTextDocument (
3942 async ( e ) => {
4043 if ( e . document === document ) {
41- await vscode . commands . executeCommand (
42- 'editor.action.formatDocument' ,
43- document . uri
44- )
4544 controller . dispose ( )
4645 change_listener . dispose ( )
4746 }
@@ -88,13 +87,9 @@ const get_code_completion_config = async (
8887 if ( default_config ) {
8988 selected_config = default_config
9089 } else {
91- const last_selected_id =
92- context . workspaceState . get < string > (
93- LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY
94- ) ??
95- context . globalState . get < string > (
96- LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY
97- )
90+ const last_selected_id = context . workspaceState . get < string > (
91+ LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY
92+ )
9893 if ( last_selected_id ) {
9994 selected_config =
10095 code_completions_configs . find (
@@ -109,7 +104,33 @@ const get_code_completion_config = async (
109104
110105 if ( ! selected_config || show_quick_pick ) {
111106 const create_items = ( ) => {
112- return code_completions_configs . map ( ( config : ToolConfig , index ) => {
107+ const recent_ids =
108+ context . workspaceState . get < string [ ] > (
109+ RECENTLY_USED_CODE_COMPLETION_CONFIG_IDS_STATE_KEY
110+ ) || [ ]
111+
112+ const matched_recent_configs : ToolConfig [ ] = [ ]
113+ const remaining_configs : ToolConfig [ ] = [ ]
114+
115+ code_completions_configs . forEach ( ( config ) => {
116+ const id = get_tool_config_id ( config )
117+ if ( recent_ids . includes ( id ) ) {
118+ matched_recent_configs . push ( config )
119+ } else {
120+ remaining_configs . push ( config )
121+ }
122+ } )
123+
124+ matched_recent_configs . sort ( ( a , b ) => {
125+ const id_a = get_tool_config_id ( a )
126+ const id_b = get_tool_config_id ( b )
127+ return recent_ids . indexOf ( id_a ) - recent_ids . indexOf ( id_b )
128+ } )
129+
130+ const recent_configs = matched_recent_configs
131+ const other_configs = remaining_configs
132+
133+ const map_config_to_item = ( config : ToolConfig ) => {
113134 const description_parts = [ config . provider_name ]
114135 if ( config . temperature != null ) {
115136 description_parts . push ( `${ config . temperature } ` )
@@ -118,38 +139,64 @@ const get_code_completion_config = async (
118139 description_parts . push ( `${ config . reasoning_effort } ` )
119140 }
120141
142+ const buttons : vscode . QuickInputButton [ ] = [ ]
143+
121144 return {
122145 label : config . model ,
123146 description : description_parts . join ( ' · ' ) ,
147+ buttons,
124148 config,
125- index,
126149 id : get_tool_config_id ( config )
127150 }
128- } )
151+ }
152+
153+ const items : ( vscode . QuickPickItem & {
154+ config ?: ToolConfig
155+ id ?: string
156+ } ) [ ] = [ ]
157+
158+ if ( recent_configs . length > 0 ) {
159+ items . push ( {
160+ label : 'recently used' ,
161+ kind : vscode . QuickPickItemKind . Separator
162+ } )
163+ items . push ( ...recent_configs . map ( map_config_to_item ) )
164+ }
165+
166+ if ( other_configs . length > 0 ) {
167+ if ( recent_configs . length > 0 ) {
168+ items . push ( {
169+ label : 'other configurations' ,
170+ kind : vscode . QuickPickItemKind . Separator
171+ } )
172+ }
173+ items . push ( ...other_configs . map ( map_config_to_item ) )
174+ }
175+
176+ return items
129177 }
130178
131179 const quick_pick = vscode . window . createQuickPick ( )
132180 quick_pick . items = create_items ( )
133181 quick_pick . placeholder = 'Select code completions configuration'
134182 quick_pick . matchOnDescription = true
135183
136- const last_selected_id =
137- context . workspaceState . get < string > (
138- LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY
139- ) ??
140- context . globalState . get < string > (
141- LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY
142- )
184+ const last_selected_id = context . workspaceState . get < string > (
185+ LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY
186+ )
143187
144188 const items = quick_pick . items as ( vscode . QuickPickItem & { id : string } ) [ ]
145- const last_selected_item = items . find (
146- ( item ) => item . id === last_selected_id
147- )
189+ const last_selected_item = items . find ( ( item ) => item . id == last_selected_id )
148190
149191 if ( last_selected_item ) {
150192 quick_pick . activeItems = [ last_selected_item ]
151193 } else if ( items . length > 0 ) {
152- quick_pick . activeItems = [ items [ 0 ] ]
194+ const first_selectable = items . find (
195+ ( i ) => i . kind !== vscode . QuickPickItemKind . Separator
196+ )
197+ if ( first_selectable ) {
198+ quick_pick . activeItems = [ first_selectable ]
199+ }
153200 }
154201
155202 return new Promise < { provider : any ; config : any } | undefined > (
@@ -158,7 +205,7 @@ const get_code_completion_config = async (
158205 const selected = quick_pick . selectedItems [ 0 ] as any
159206 quick_pick . hide ( )
160207
161- if ( ! selected ) {
208+ if ( ! selected || ! selected . config ) {
162209 resolve ( undefined )
163210 return
164211 }
@@ -167,9 +214,15 @@ const get_code_completion_config = async (
167214 LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY ,
168215 selected . id
169216 )
170- context . globalState . update (
171- LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY ,
172- selected . id
217+
218+ let recents =
219+ context . workspaceState . get < string [ ] > (
220+ RECENTLY_USED_CODE_COMPLETION_CONFIG_IDS_STATE_KEY
221+ ) || [ ]
222+ recents = [ selected . id , ...recents . filter ( ( id ) => id != selected . id ) ]
223+ context . workspaceState . update (
224+ RECENTLY_USED_CODE_COMPLETION_CONFIG_IDS_STATE_KEY ,
225+ recents
173226 )
174227
175228 if ( panel_provider ) {
0 commit comments