Skip to content

Commit a4816be

Browse files
committed
Refactor functions to use destructured parameters directly instead of extracting them at the start
- don't destructure params [Fragment] - don't destructure params
1 parent 39c63e7 commit a4816be

6 files changed

Lines changed: 123 additions & 148 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/parsers/multiple-files-parser/helpers/item-management.ts‎

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,51 @@ export const create_or_update_file_item = (params: {
1111
renamed_from?: string
1212
is_deleted?: boolean
1313
}) => {
14-
const {
15-
file_name,
16-
content,
17-
workspace_name,
18-
file_ref_map,
19-
results,
20-
mode = 'overwrite',
21-
renamed_from,
22-
is_deleted
23-
} = params
24-
25-
if (!file_name) {
14+
if (!params.file_name) {
2615
return
2716
}
2817

29-
const processed_content = process_conflict_markers(content)
18+
const processed_content = process_conflict_markers(params.content)
3019

31-
const file_key = `${workspace_name || ''}:${file_name}`
20+
const file_key = `${params.workspace_name || ''}:${params.file_name}`
3221

33-
if (file_ref_map.has(file_key)) {
34-
const existing_file = file_ref_map.get(file_key)!
22+
if (params.file_ref_map.has(file_key)) {
23+
const existing_file = params.file_ref_map.get(file_key)!
3524

36-
if (mode == 'append') {
25+
if (params.mode == 'append') {
3726
existing_file.content = existing_file.content + '\n' + processed_content
3827

39-
const file_index = results.indexOf(existing_file)
28+
const file_index = params.results.indexOf(existing_file)
4029
if (file_index >= 0) {
4130
const text_indices: number[] = []
42-
for (let i = file_index + 1; i < results.length; i++) {
43-
if (results[i].type == 'text') {
31+
for (let i = file_index + 1; i < params.results.length; i++) {
32+
if (params.results[i].type == 'text') {
4433
text_indices.push(i)
4534
}
4635
}
4736

4837
if (text_indices.length > 0) {
4938
const text_contents = text_indices
50-
.map((idx) => (results[idx] as TextItem).content.trim())
39+
.map((idx) => (params.results[idx] as TextItem).content.trim())
5140
.filter((c) => c)
5241

5342
for (let i = text_indices.length - 1; i >= 0; i--) {
54-
results.splice(text_indices[i], 1)
43+
params.results.splice(text_indices[i], 1)
5544
}
5645

5746
if (text_contents.length > 0) {
5847
const combined = text_contents.join('\n')
59-
const updated_file_index = results.indexOf(existing_file)
48+
const updated_file_index = params.results.indexOf(existing_file)
6049
if (
6150
updated_file_index > 0 &&
62-
results[updated_file_index - 1].type == 'text'
51+
params.results[updated_file_index - 1].type == 'text'
6352
) {
64-
const prev_text = results[updated_file_index - 1] as TextItem
53+
const prev_text = params.results[
54+
updated_file_index - 1
55+
] as TextItem
6556
prev_text.content = prev_text.content.trim() + '\n' + combined
6657
} else {
67-
results.splice(updated_file_index, 0, {
58+
params.results.splice(updated_file_index, 0, {
6859
type: 'text' as const,
6960
content: combined
7061
})
@@ -76,38 +67,39 @@ export const create_or_update_file_item = (params: {
7667
existing_file.content = processed_content
7768
}
7869

79-
if (renamed_from) {
80-
existing_file.renamed_from = renamed_from
70+
if (params.renamed_from) {
71+
existing_file.renamed_from = params.renamed_from
8172
}
8273

83-
if (is_deleted !== undefined) {
84-
existing_file.is_deleted = is_deleted
74+
if (params.is_deleted !== undefined) {
75+
existing_file.is_deleted = params.is_deleted
8576
}
8677
} else {
8778
const new_file: FileItem = {
8879
type: 'file' as const,
89-
file_path: file_name,
80+
file_path: params.file_name,
9081
content: processed_content,
91-
workspace_name: workspace_name,
92-
renamed_from,
93-
is_deleted
82+
workspace_name: params.workspace_name,
83+
renamed_from: params.renamed_from,
84+
is_deleted: params.is_deleted
9485
}
95-
file_ref_map.set(file_key, new_file)
96-
results.push(new_file)
86+
params.file_ref_map.set(file_key, new_file)
87+
params.results.push(new_file)
9788
}
9889
}
9990

10091
export const flush_text_block = (params: {
10192
text_block: string
10293
results: (FileItem | TextItem | InlineFileItem)[]
10394
}) => {
104-
const { text_block, results } = params
105-
106-
if (!text_block.trim()) {
95+
if (!params.text_block.trim()) {
10796
return
10897
}
10998

110-
const content = results.length == 0 ? text_block.trim() : text_block.trimEnd()
99+
const content =
100+
params.results.length == 0
101+
? params.text_block.trim()
102+
: params.text_block.trimEnd()
111103

112-
results.push({ type: 'text', content })
104+
params.results.push({ type: 'text', content })
113105
}

‎apps/editor/src/commands/apply-chat-response-command/utils/state-manager.ts‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,30 @@ export const update_undo_button_state = (params: {
1717
position: { line: number; character: number }
1818
} | null
1919
}) => {
20-
const {
21-
context,
22-
panel_provider,
23-
states,
24-
applied_content,
25-
original_editor_state
26-
} = params
27-
if (states && states.length > 0) {
28-
context.workspaceState.update(LAST_APPLIED_CHANGES_STATE_KEY, states)
29-
context.workspaceState.update(
20+
if (params.states && params.states.length > 0) {
21+
params.context.workspaceState.update(
22+
LAST_APPLIED_CHANGES_STATE_KEY,
23+
params.states
24+
)
25+
params.context.workspaceState.update(
3026
LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY,
31-
applied_content
27+
params.applied_content
3228
)
33-
context.workspaceState.update(
29+
params.context.workspaceState.update(
3430
LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY,
35-
original_editor_state
31+
params.original_editor_state
3632
)
37-
panel_provider.set_undo_button_state(true)
33+
params.panel_provider.set_undo_button_state(true)
3834
} else {
39-
context.workspaceState.update(LAST_APPLIED_CHANGES_STATE_KEY, null)
40-
context.workspaceState.update(
35+
params.context.workspaceState.update(LAST_APPLIED_CHANGES_STATE_KEY, null)
36+
params.context.workspaceState.update(
4137
LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY,
4238
null
4339
)
44-
context.workspaceState.update(
40+
params.context.workspaceState.update(
4541
LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY,
4642
null
4743
)
48-
panel_provider.set_undo_button_state(false)
44+
params.panel_provider.set_undo_button_state(false)
4945
}
5046
}

‎apps/editor/src/commands/checkpoints-command/actions/delete-checkpoint.ts‎

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,31 @@ export const delete_checkpoint_with_undo = async (params: {
5656
on_before_show_message?: () => void
5757
on_after_show_message?: () => void
5858
}): Promise<boolean> => {
59-
const {
60-
context,
61-
checkpoint,
62-
panel_provider,
63-
get_active_operation,
64-
set_active_operation,
65-
on_did_update_checkpoints,
66-
on_before_show_message,
67-
on_after_show_message
68-
} = params
69-
70-
if (get_active_operation()) {
71-
await get_active_operation()!.finalize()
72-
set_active_operation(null)
59+
if (params.get_active_operation()) {
60+
await params.get_active_operation()!.finalize()
61+
params.set_active_operation(null)
7362
}
7463

7564
const checkpoints =
76-
context.workspaceState.get<Checkpoint[]>(CHECKPOINTS_STATE_KEY, []) ?? []
65+
params.context.workspaceState.get<Checkpoint[]>(
66+
CHECKPOINTS_STATE_KEY,
67+
[]
68+
) ?? []
7769

7870
// Optimistically remove from state
7971
const updated_checkpoints = checkpoints.filter(
80-
(c) => c.timestamp !== checkpoint.timestamp
72+
(c) => c.timestamp !== params.checkpoint.timestamp
8173
)
82-
await context.workspaceState.update(
74+
await params.context.workspaceState.update(
8375
CHECKPOINTS_STATE_KEY,
8476
updated_checkpoints
8577
)
86-
await panel_provider.send_checkpoints()
87-
on_did_update_checkpoints?.(updated_checkpoints)
78+
await params.panel_provider.send_checkpoints()
79+
params.on_did_update_checkpoints?.(updated_checkpoints)
8880

8981
const finalize = async () => {
9082
try {
91-
const checkpoint_path = get_checkpoint_path(checkpoint.timestamp)
83+
const checkpoint_path = get_checkpoint_path(params.checkpoint.timestamp)
9284
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
9385
recursive: true
9486
})
@@ -101,56 +93,58 @@ export const delete_checkpoint_with_undo = async (params: {
10193
}
10294
}
10395

104-
const operation = { timestamp: checkpoint.timestamp, finalize }
105-
set_active_operation(operation)
96+
const operation = { timestamp: params.checkpoint.timestamp, finalize }
97+
params.set_active_operation(operation)
10698

107-
on_before_show_message?.()
99+
params.on_before_show_message?.()
108100
const choice = await vscode.window.showInformationMessage(
109101
dictionary.information_message.CHECKPOINT_DELETED,
110102
'Undo'
111103
)
112-
on_after_show_message?.()
104+
params.on_after_show_message?.()
113105

114-
const current_active_op = get_active_operation()
106+
const current_active_op = params.get_active_operation()
115107

116108
if (
117109
current_active_op &&
118110
current_active_op.timestamp === operation.timestamp
119111
) {
120112
if (choice == 'Undo') {
121113
const current_checkpoints =
122-
context.workspaceState.get<Checkpoint[]>(CHECKPOINTS_STATE_KEY, []) ??
123-
[]
114+
params.context.workspaceState.get<Checkpoint[]>(
115+
CHECKPOINTS_STATE_KEY,
116+
[]
117+
) ?? []
124118

125-
current_checkpoints.push(checkpoint)
119+
current_checkpoints.push(params.checkpoint)
126120
current_checkpoints.sort((a, b) => b.timestamp - a.timestamp)
127121

128-
await context.workspaceState.update(
122+
await params.context.workspaceState.update(
129123
CHECKPOINTS_STATE_KEY,
130124
current_checkpoints
131125
)
132-
await panel_provider.send_checkpoints()
133-
on_did_update_checkpoints?.(current_checkpoints)
126+
await params.panel_provider.send_checkpoints()
127+
params.on_did_update_checkpoints?.(current_checkpoints)
134128

135-
on_before_show_message?.()
129+
params.on_before_show_message?.()
136130
await vscode.window.showInformationMessage(
137131
dictionary.information_message.CHECKPOINT_RESTORED
138132
)
139-
on_after_show_message?.()
133+
params.on_after_show_message?.()
140134

141-
set_active_operation(null)
135+
params.set_active_operation(null)
142136
return true
143137
} else {
144138
await operation.finalize()
145-
set_active_operation(null)
139+
params.set_active_operation(null)
146140
return false
147141
}
148142
} else if (choice == 'Undo') {
149-
on_before_show_message?.()
143+
params.on_before_show_message?.()
150144
await vscode.window.showInformationMessage(
151145
dictionary.information_message.COULD_NOT_UNDO_ANOTHER_CHECKPOINT_DELETED
152146
)
153-
on_after_show_message?.()
147+
params.on_after_show_message?.()
154148
return false
155149
}
156150

0 commit comments

Comments
 (0)