Skip to content

Commit 62b5ae5

Browse files
committed
Refactor command registration functions to accept a single params object and introduce a dedicated command for generating Git commit messages
1 parent 01bde87 commit 62b5ae5

6 files changed

Lines changed: 98 additions & 83 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/apply-chat-response-command.ts‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ let in_progress = false
2424
let placeholder_created_at_for_update: number | undefined
2525

2626
export const apply_chat_response_command = (
27-
context: vscode.ExtensionContext,
28-
panel_provider: PanelProvider,
29-
workspace_provider: WorkspaceProvider
27+
params: {
28+
context: vscode.ExtensionContext
29+
panel_provider: PanelProvider
30+
workspace_provider: WorkspaceProvider
31+
}
3032
) => {
3133
return vscode.commands.registerCommand(
3234
'codeWebChat.applyChatResponse',
@@ -61,7 +63,7 @@ export const apply_chat_response_command = (
6163

6264
if (response_preview_promise_resolve) {
6365
// Previewing response...
64-
const history = panel_provider.response_history
66+
const history = params.panel_provider.response_history
6567

6668
const new_item: ResponseHistoryItem = {
6769
response: chat_response,
@@ -70,7 +72,7 @@ export const apply_chat_response_command = (
7072
}
7173

7274
history.push(new_item)
73-
panel_provider.send_message({
75+
params.panel_provider.send_message({
7476
command: 'RESPONSE_HISTORY',
7577
history
7678
})
@@ -95,10 +97,10 @@ export const apply_chat_response_command = (
9597
let before_checkpoint: Checkpoint | undefined
9698
try {
9799
before_checkpoint = await create_checkpoint(
98-
workspace_provider,
99-
context,
100-
panel_provider,
101-
panel_provider.websites_provider,
100+
params.workspace_provider,
101+
params.context,
102+
params.panel_provider,
103+
params.panel_provider.websites_provider,
102104
'Before response previewed',
103105
args?.raw_instructions
104106
)
@@ -109,8 +111,8 @@ export const apply_chat_response_command = (
109111
const review_data = await process_chat_response(
110112
args,
111113
chat_response,
112-
context,
113-
panel_provider
114+
params.context,
115+
params.panel_provider
114116
)
115117

116118
if (review_data) {
@@ -179,7 +181,7 @@ export const apply_chat_response_command = (
179181
is_checked: true
180182
})
181183
}
182-
const history = panel_provider.response_history
184+
const history = params.panel_provider.response_history
183185

184186
if (placeholder_created_at_for_update) {
185187
const item_to_update = history.find(
@@ -216,18 +218,18 @@ export const apply_chat_response_command = (
216218
}
217219
}
218220

219-
panel_provider.send_message({
221+
params.panel_provider.send_message({
220222
command: 'RESPONSE_HISTORY',
221223
history
222224
})
223225
}
224226

225-
const history_for_checkpoint = [...panel_provider.response_history]
227+
const history_for_checkpoint = [...params.panel_provider.response_history]
226228
const changes_accepted = await preview_handler({
227229
original_states: review_data.original_states,
228230
chat_response: review_data.chat_response,
229-
panel_provider,
230-
context,
231+
panel_provider: params.panel_provider,
232+
context: params.context,
231233
original_editor_state: args?.original_editor_state,
232234
raw_instructions: args?.raw_instructions,
233235
created_at: created_at_for_preview
@@ -236,7 +238,7 @@ export const apply_chat_response_command = (
236238
if (changes_accepted) {
237239
if (before_checkpoint) {
238240
const checkpoints =
239-
context.workspaceState.get<Checkpoint[]>(
241+
params.context.workspaceState.get<Checkpoint[]>(
240242
CHECKPOINTS_STATE_KEY,
241243
[]
242244
) ?? []
@@ -248,11 +250,11 @@ export const apply_chat_response_command = (
248250
checkpoint_to_update.response_history = history_for_checkpoint
249251
checkpoint_to_update.response_preview_item_created_at =
250252
created_at_for_preview
251-
await context.workspaceState.update(
253+
await params.context.workspaceState.update(
252254
CHECKPOINTS_STATE_KEY,
253255
checkpoints
254256
)
255-
await panel_provider.send_checkpoints()
257+
await params.panel_provider.send_checkpoints()
256258
}
257259
}
258260

@@ -267,8 +269,8 @@ export const apply_chat_response_command = (
267269
in_progress = false
268270
if (before_checkpoint) {
269271
delete_checkpoint({
270-
context,
271-
panel_provider,
272+
context: params.context,
273+
panel_provider: params.panel_provider,
272274
checkpoint_to_delete: before_checkpoint
273275
})
274276
}

‎packages/vscode/src/commands/code-completion-commands.ts‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -485,17 +485,19 @@ const perform_code_completion = async (params: {
485485
}
486486

487487
export const code_completion_commands = (
488-
file_tree_provider: any,
489-
open_editors_provider: any,
490-
context: vscode.ExtensionContext,
491-
panel_provider: PanelProvider
488+
params: {
489+
file_tree_provider: any
490+
open_editors_provider: any
491+
context: vscode.ExtensionContext
492+
panel_provider: PanelProvider
493+
}
492494
) => {
493495
return [
494496
vscode.commands.registerCommand('codeWebChat.codeCompletion', async () =>
495497
perform_code_completion({
496-
file_tree_provider,
497-
open_editors_provider,
498-
context,
498+
file_tree_provider: params.file_tree_provider,
499+
open_editors_provider: params.open_editors_provider,
500+
context: params.context,
499501
with_completion_instructions: false,
500502
show_quick_pick: false
501503
})
@@ -504,9 +506,9 @@ export const code_completion_commands = (
504506
'codeWebChat.codeCompletionWithInstructions',
505507
async () =>
506508
perform_code_completion({
507-
file_tree_provider,
508-
open_editors_provider,
509-
context,
509+
file_tree_provider: params.file_tree_provider,
510+
open_editors_provider: params.open_editors_provider,
511+
context: params.context,
510512
with_completion_instructions: true,
511513
show_quick_pick: false
512514
})
@@ -515,24 +517,24 @@ export const code_completion_commands = (
515517
'codeWebChat.codeCompletionUsing',
516518
async () =>
517519
perform_code_completion({
518-
file_tree_provider,
519-
open_editors_provider,
520-
context,
520+
file_tree_provider: params.file_tree_provider,
521+
open_editors_provider: params.open_editors_provider,
522+
context: params.context,
521523
with_completion_instructions: false,
522524
show_quick_pick: true,
523-
panel_provider
525+
panel_provider: params.panel_provider
524526
})
525527
),
526528
vscode.commands.registerCommand(
527529
'codeWebChat.codeCompletionWithInstructionsUsing',
528530
async () =>
529531
perform_code_completion({
530-
file_tree_provider,
531-
open_editors_provider,
532-
context,
532+
file_tree_provider: params.file_tree_provider,
533+
open_editors_provider: params.open_editors_provider,
534+
context: params.context,
533535
with_completion_instructions: true,
534536
show_quick_pick: true,
535-
panel_provider
537+
panel_provider: params.panel_provider
536538
})
537539
)
538540
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as vscode from 'vscode'
2+
import {
3+
get_git_repository,
4+
prepare_staged_changes
5+
} from '../utils/git-repository-utils'
6+
import { generate_commit_message_from_diff } from '../views/panel/backend/message-handlers/handle-commit-changes/utils'
7+
8+
export function generate_commit_message_command(
9+
context: vscode.ExtensionContext
10+
) {
11+
return vscode.commands.registerCommand(
12+
'codeWebChat.generateCommitMessage',
13+
async (source_control?: vscode.SourceControl) => {
14+
const repository = get_git_repository(source_control)
15+
if (!repository) return
16+
17+
const diff = await prepare_staged_changes(repository)
18+
19+
if (!diff) return
20+
21+
const commit_message = await generate_commit_message_from_diff({
22+
context,
23+
repository,
24+
diff
25+
})
26+
if (commit_message) {
27+
repository.inputBox.value = commit_message
28+
}
29+
}
30+
)
31+
}

‎packages/vscode/src/commands/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './close-editor-command'
55
export * from './duplicate-workspace-command'
66
export * from './code-completion-commands'
77
export * from './delete-command'
8+
export * from './generate-commit-message-command'
89
export * from './new-file-command'
910
export * from './new-folder-command'
1011
export * from './open-file-from-workspace-command'

‎packages/vscode/src/commands/reference-in-prompt-command.ts‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import { FileItem } from '../context/providers/workspace-provider'
55
import { dictionary } from '@shared/constants/dictionary'
66
import { PanelProvider } from '@/views/panel/backend/panel-provider'
77

8-
export const reference_in_prompt_command = (
9-
panel_provider: PanelProvider | undefined,
8+
export const reference_in_prompt_command = (params: {
9+
panel_provider: PanelProvider | undefined
1010
workspace_provider: WorkspaceProvider | undefined
11-
) => {
11+
}) => {
1212
return vscode.commands.registerCommand(
1313
'codeWebChat.referenceInPrompt',
1414
async (uri: FileItem) => {
15-
if (!panel_provider || !workspace_provider) {
15+
if (!params.panel_provider || !params.workspace_provider) {
1616
return
1717
}
1818

1919
const file_path = uri.resourceUri.fsPath
2020

2121
const workspace_root =
22-
workspace_provider.get_workspace_root_for_file(file_path)
22+
params.workspace_provider.get_workspace_root_for_file(file_path)
2323

2424
if (!workspace_root) {
2525
vscode.window.showWarningMessage(
@@ -28,15 +28,16 @@ export const reference_in_prompt_command = (
2828
return
2929
}
3030

31-
const is_checked = workspace_provider
31+
const is_checked = params.workspace_provider
3232
.get_all_checked_paths()
3333
.includes(file_path)
3434

3535
const is_partially_checked =
36-
uri.isDirectory && workspace_provider.is_partially_checked(file_path)
36+
uri.isDirectory &&
37+
params.workspace_provider.is_partially_checked(file_path)
3738

3839
if (!is_checked && !is_partially_checked) {
39-
await workspace_provider.update_check_state(
40+
await params.workspace_provider.update_check_state(
4041
uri,
4142
vscode.TreeItemCheckboxState.Checked
4243
)
@@ -45,7 +46,7 @@ export const reference_in_prompt_command = (
4546
const relative_path = path.relative(workspace_root, file_path)
4647
const reference_text = `\`${relative_path}\``
4748

48-
panel_provider.add_text_at_cursor_position(reference_text)
49+
params.panel_provider.add_text_at_cursor_position(reference_text)
4950
}
5051
)
5152
}

‎packages/vscode/src/extension.ts‎

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import * as vscode from 'vscode'
22
import { context_initialization } from './context/context-initialization'
3-
import {
4-
get_git_repository,
5-
prepare_staged_changes
6-
} from './utils/git-repository-utils'
7-
import { generate_commit_message_from_diff } from './views/panel/backend/message-handlers/handle-commit-changes/utils'
83
import { PanelProvider } from './views/panel/backend/panel-provider'
94
import { WebSocketManager } from './services/websocket-manager'
105
import {
@@ -28,7 +23,8 @@ import {
2823
delete_command,
2924
save_context_command,
3025
reference_in_prompt_command,
31-
open_url_command
26+
open_url_command,
27+
generate_commit_message_command
3228
} from './commands'
3329
import { SettingsProvider } from './views/settings/backend/settings-provider'
3430

@@ -71,14 +67,18 @@ export async function activate(context: vscode.ExtensionContext) {
7167
}
7268
}
7369
),
74-
reference_in_prompt_command(panel_provider, workspace_provider),
75-
apply_chat_response_command(context, panel_provider, workspace_provider),
76-
...code_completion_commands(
77-
workspace_provider,
70+
reference_in_prompt_command({ panel_provider, workspace_provider }),
71+
apply_chat_response_command({
72+
context,
73+
panel_provider,
74+
workspace_provider
75+
}),
76+
...code_completion_commands({
77+
file_tree_provider: workspace_provider,
7878
open_editors_provider,
7979
context,
8080
panel_provider
81-
),
81+
}),
8282
...checkpoints_command({
8383
context,
8484
workspace_provider,
@@ -115,29 +115,7 @@ export async function activate(context: vscode.ExtensionContext) {
115115
(section?: string) => {
116116
settings_provider.createOrShow(section)
117117
}
118-
)
119-
)
120-
121-
context.subscriptions.push(
122-
vscode.commands.registerCommand(
123-
'codeWebChat.generateCommitMessage',
124-
async (source_control?: vscode.SourceControl) => {
125-
const repository = get_git_repository(source_control)
126-
if (!repository) return
127-
128-
const diff = await prepare_staged_changes(repository)
129-
130-
if (!diff) return
131-
132-
const commit_message = await generate_commit_message_from_diff({
133-
context,
134-
repository,
135-
diff
136-
})
137-
if (commit_message) {
138-
repository.inputBox.value = commit_message
139-
}
140-
}
141-
)
118+
),
119+
generate_commit_message_command(context)
142120
)
143121
}

0 commit comments

Comments
 (0)