Skip to content

Commit d521766

Browse files
committed
Refactor reorder handlers into a unified configuration reordering function
1 parent 75aa9e6 commit d521766

8 files changed

Lines changed: 95 additions & 184 deletions

‎packages/vscode/src/views/settings/backend/message-handlers/handle-reorder-code-completions-configurations.ts‎

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎packages/vscode/src/views/settings/backend/message-handlers/handle-reorder-commit-messages-configurations.ts‎

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { SettingsProvider } from '@/views/settings/backend/settings-provider'
2+
import {
3+
ModelProvidersManager,
4+
ToolConfig,
5+
get_tool_config_id
6+
} from '@/services/model-providers-manager'
7+
8+
export const handle_reorder_configuration = async (
9+
provider: SettingsProvider,
10+
configurations: { id: string }[],
11+
type:
12+
| 'code-completions'
13+
| 'edit-context'
14+
| 'intelligent-update'
15+
| 'commit-messages'
16+
| 'prune-context'
17+
): Promise<void> => {
18+
const providers_manager = new ModelProvidersManager(provider.context)
19+
20+
let get_configs: () => Promise<ToolConfig[]>
21+
let save_configs: (configs: ToolConfig[]) => Promise<void>
22+
23+
switch (type) {
24+
case 'code-completions':
25+
get_configs = () => providers_manager.get_code_completions_tool_configs()
26+
save_configs = (c) =>
27+
providers_manager.save_code_completions_tool_configs(c)
28+
break
29+
case 'edit-context':
30+
get_configs = () => providers_manager.get_edit_context_tool_configs()
31+
save_configs = (c) => providers_manager.save_edit_context_tool_configs(c)
32+
break
33+
case 'intelligent-update':
34+
get_configs = () =>
35+
providers_manager.get_intelligent_update_tool_configs()
36+
save_configs = (c) =>
37+
providers_manager.save_intelligent_update_tool_configs(c)
38+
break
39+
case 'commit-messages':
40+
get_configs = () => providers_manager.get_commit_messages_tool_configs()
41+
save_configs = (c) =>
42+
providers_manager.save_commit_messages_tool_configs(c)
43+
break
44+
case 'prune-context':
45+
get_configs = () => providers_manager.get_prune_context_tool_configs()
46+
save_configs = (c) => providers_manager.save_prune_context_tool_configs(c)
47+
break
48+
}
49+
50+
const current_configs = await get_configs()
51+
52+
const reordered_ids = configurations.map((p) => p.id)
53+
54+
const reordered_configs = reordered_ids
55+
.map((id) => {
56+
const found = current_configs.find((p) => get_tool_config_id(p) == id)
57+
if (!found) {
58+
console.error(`Config with id ${id} not found during reorder.`)
59+
return null
60+
}
61+
return found
62+
})
63+
.filter((p): p is ToolConfig => p !== null)
64+
65+
if (reordered_configs.length == current_configs.length) {
66+
await save_configs(reordered_configs)
67+
}
68+
}

‎packages/vscode/src/views/settings/backend/message-handlers/handle-reorder-edit-context-configurations.ts‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎packages/vscode/src/views/settings/backend/message-handlers/handle-reorder-intelligent-update-configurations.ts‎

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎packages/vscode/src/views/settings/backend/message-handlers/handle-reorder-prune-context-configurations.ts‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎packages/vscode/src/views/settings/backend/message-handlers/index.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ export * from './handle-add-model-provider'
44
export * from './handle-delete-model-provider'
55
export * from './handle-edit-custom-model-provider'
66
export * from './handle-edit-model-provider-key'
7-
export * from './handle-reorder-code-completions-configurations'
7+
export * from './handle-reorder-configuration'
88
export * from './handle-set-default-code-completions-configuration'
9-
export * from './handle-reorder-commit-messages-configurations'
109
export * from './handle-set-default-commit-messages-configuration'
11-
export * from './handle-reorder-edit-context-configurations'
1210
export * from './handle-get-edit-context-system-instructions'
1311
export * from './handle-update-edit-context-system-instructions'
1412
export * from './handle-get-edit-format-instructions'
1513
export * from './handle-update-edit-format-instructions'
16-
export * from './handle-reorder-intelligent-update-configurations'
1714
export * from './handle-set-default-intelligent-update-configuration'
18-
export * from './handle-reorder-prune-context-configurations'
1915
export * from './handle-get-commit-message-instructions'
2016
export * from './handle-update-commit-message-instructions'
2117
export * from './handle-get-commit-message-auto-accept-after'

‎packages/vscode/src/views/settings/backend/settings-provider.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ import {
2323
handle_get_model_providers,
2424
handle_get_send_with_shift_enter,
2525
handle_get_checkpoint_lifespan,
26-
handle_reorder_code_completions_configurations,
27-
handle_reorder_commit_messages_configurations,
28-
handle_reorder_edit_context_configurations,
29-
handle_reorder_intelligent_update_configurations,
26+
handle_reorder_configuration,
3027
handle_reorder_model_providers,
31-
handle_reorder_prune_context_configurations,
3228
handle_set_default_code_completions_configuration,
3329
handle_set_default_commit_messages_configuration,
3430
handle_set_default_intelligent_update_configuration,
@@ -123,7 +119,11 @@ export class SettingsProvider {
123119
} else if (
124120
message.command == 'REORDER_CODE_COMPLETIONS_CONFIGURATIONS'
125121
) {
126-
await handle_reorder_code_completions_configurations(this, message)
122+
await handle_reorder_configuration(
123+
this,
124+
message.configurations,
125+
'code-completions'
126+
)
127127
} else if (message.command == 'DELETE_CODE_COMPLETIONS_CONFIGURATION') {
128128
await handle_delete_configuration(
129129
this,
@@ -137,7 +137,11 @@ export class SettingsProvider {
137137
} else if (message.command == 'GET_EDIT_CONTEXT_CONFIGURATIONS') {
138138
await handle_get_configurations(this, 'edit-context')
139139
} else if (message.command == 'REORDER_EDIT_CONTEXT_CONFIGURATIONS') {
140-
await handle_reorder_edit_context_configurations(this, message)
140+
await handle_reorder_configuration(
141+
this,
142+
message.configurations,
143+
'edit-context'
144+
)
141145
} else if (message.command == 'DELETE_EDIT_CONTEXT_CONFIGURATION') {
142146
await handle_delete_configuration(
143147
this,
@@ -159,7 +163,11 @@ export class SettingsProvider {
159163
} else if (
160164
message.command == 'REORDER_INTELLIGENT_UPDATE_CONFIGURATIONS'
161165
) {
162-
await handle_reorder_intelligent_update_configurations(this, message)
166+
await handle_reorder_configuration(
167+
this,
168+
message.configurations,
169+
'intelligent-update'
170+
)
163171
} else if (
164172
message.command == 'DELETE_INTELLIGENT_UPDATE_CONFIGURATION'
165173
) {
@@ -178,7 +186,11 @@ export class SettingsProvider {
178186
} else if (message.command == 'GET_PRUNE_CONTEXT_CONFIGURATIONS') {
179187
await handle_get_configurations(this, 'prune-context')
180188
} else if (message.command == 'REORDER_PRUNE_CONTEXT_CONFIGURATIONS') {
181-
await handle_reorder_prune_context_configurations(this, message)
189+
await handle_reorder_configuration(
190+
this,
191+
message.configurations,
192+
'prune-context'
193+
)
182194
} else if (message.command == 'DELETE_PRUNE_CONTEXT_CONFIGURATION') {
183195
await handle_delete_configuration(
184196
this,
@@ -190,7 +202,11 @@ export class SettingsProvider {
190202
} else if (
191203
message.command == 'REORDER_COMMIT_MESSAGES_CONFIGURATIONS'
192204
) {
193-
await handle_reorder_commit_messages_configurations(this, message)
205+
await handle_reorder_configuration(
206+
this,
207+
message.configurations,
208+
'commit-messages'
209+
)
194210
} else if (message.command == 'DELETE_COMMIT_MESSAGES_CONFIGURATION') {
195211
await handle_delete_configuration(
196212
this,

0 commit comments

Comments
 (0)