Skip to content

Commit afc5716

Browse files
committed
Refactor git markdown builder functions to use options objects instead of positional arguments
- branch name is never replaced in [Fragment] causing notification: `No changes found between current branch and {branch name}.` - [Fragment] is never used - use params variable [Fragment] don't destructure - [Fragment] are never used └── apps └── editor └── src ├── i18n │ └── translations │ └── views │ └── prompt │ └── utils │ └── symbols │ └── git │ └── replace-git-symbols.ts └── views └── prompt └── backend ├── message-handlers │ ├── handle-hash-sign-quick-pick │ │ └── symbols │ │ ├── changes-symbol.ts │ │ └── commit-symbol.ts │ ├── handle-preview-changes-symbol.ts │ └── handle-preview-commit-symbol.ts └── utils └── symbols ├── fragment │ └── replace-fragment-symbol.ts ├── git │ └── replace-git-symbols.ts ├── image │ └── replace-image-symbol.ts ├── pasted-text │ └── replace-pasted-text-symbol.ts ├── replace-symbols.ts ├── saved-context │ └── replace-saved-context-symbol.ts ├── skill │ └── replace-skill-symbol.ts ├── symbol-cache.ts └── website └── replace-website-symbol.ts
1 parent 6fa776c commit afc5716

3 files changed

Lines changed: 55 additions & 61 deletions

File tree

‎apps/editor/src/views/prompt/backend/message-handlers/handle-hash-sign-quick-pick/symbols/changes-symbol.ts‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,17 @@ export const handle_changes_item = async (
172172
for (let i = 0; i < branch_items.length; i++) {
173173
if (!is_active) break
174174

175-
const branch = branch_items[i].label
175+
const branch_name = branch_items[i].label
176176
try {
177177
let diff_base: string
178-
if (current_branch === branch) {
178+
if (current_branch === branch_name) {
179179
const { stdout } = await execAsync(
180-
`git merge-base HEAD ${branch}`,
180+
`git merge-base HEAD ${branch_name}`,
181181
{ cwd }
182182
)
183183
diff_base = stdout.trim()
184184
} else {
185-
diff_base = branch
185+
diff_base = branch_name
186186
}
187187

188188
const { stdout: diff } = await execAsync(
@@ -196,13 +196,11 @@ export const handle_changes_item = async (
196196
if (!diff || diff.trim().length === 0) {
197197
branch_items[i].description = 'No changes'
198198
} else {
199-
const replacement_text = build_changes_markdown(
199+
const replacement_text = build_changes_markdown({
200200
diff,
201-
cwd,
202-
diff_base,
203-
branch,
204-
is_multi_root ? folder_name : undefined
205-
)
201+
branch_name,
202+
path_prefix: is_multi_root ? folder_name : undefined
203+
})
206204
const token_count = Math.ceil(replacement_text.length / 4)
207205
branch_items[i].description = display_token_count(token_count)
208206
}

‎apps/editor/src/views/prompt/backend/message-handlers/handle-hash-sign-quick-pick/symbols/commit-symbol.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,16 @@ export const handle_commit_item = async (
207207
let msg_token_count = 0
208208

209209
try {
210-
let commit_message_body = ''
210+
let commit_message = ''
211211
const raw_msg = execSync(`git show -s --format=%B ${commit_hash}`, {
212212
cwd: selected_folder.uri.fsPath,
213213
encoding: 'utf-8'
214214
}).toString()
215-
commit_message_body = AsciiTree.strip_from_text(raw_msg)
215+
commit_message = AsciiTree.strip_from_text(raw_msg)
216216

217-
if (commit_message_body) {
217+
if (commit_message) {
218218
msg_token_count = Math.ceil(
219-
`---\n\n${commit_message_body}\n\n---\n\n`.length / 4
219+
`---\n\n${commit_message}\n\n---\n\n`.length / 4
220220
)
221221
}
222222

@@ -225,13 +225,13 @@ export const handle_commit_item = async (
225225
encoding: 'utf-8'
226226
}).toString()
227227

228-
const replacement_text = build_commit_changes_markdown(
228+
const replacement_text = build_commit_changes_markdown({
229229
diff,
230-
selected_folder.uri.fsPath,
231230
commit_hash,
232-
workspace_folders.length > 1 ? selected_folder.name : undefined,
233-
commit_message_body
234-
)
231+
path_prefix:
232+
workspace_folders.length > 1 ? selected_folder.name : undefined,
233+
commit_message
234+
})
235235
diff_token_count = Math.ceil(replacement_text.length / 4)
236236
} catch (error) {
237237
// ignore

‎apps/editor/src/views/prompt/backend/utils/symbols/git/replace-git-symbols.ts‎

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ const clean_git_diff = (diff_text: string): string => {
5656
.join('\n')
5757
}
5858

59-
export const build_changes_markdown = (
60-
diff: string,
61-
cwd: string,
62-
diff_base: string,
63-
branch_name: string,
59+
export const build_changes_markdown = (params: {
60+
diff: string
61+
branch_name: string
6462
path_prefix?: string
65-
): string => {
63+
}): string => {
6664
// Split diff into per-file sections. Each section starts with 'diff --git '.
67-
const file_diffs = diff.split(/^diff --git /m).filter((d) => d.trim() != '')
65+
const file_diffs = params.diff
66+
.split(/^diff --git /m)
67+
.filter((d) => d.trim() != '')
6868
if (file_diffs.length == 0) {
6969
return ''
7070
}
@@ -93,16 +93,16 @@ export const build_changes_markdown = (
9393
}
9494

9595
if (file_path) {
96-
const display_path = path_prefix
97-
? `${path_prefix}/${file_path}`
96+
const display_path = params.path_prefix
97+
? `${params.path_prefix}/${file_path}`
9898
: file_path
9999

100100
changes_content += `### File: \`${display_path}\`\n\n`
101101

102-
if (path_prefix) {
102+
if (params.path_prefix) {
103103
full_file_diff = patch_diff_paths(
104104
full_file_diff,
105-
path_prefix,
105+
params.path_prefix,
106106
file_path,
107107
old_path,
108108
new_path
@@ -116,7 +116,7 @@ export const build_changes_markdown = (
116116
}
117117

118118
return changes_content
119-
? `# Diff with ${branch_name}\n\n${changes_content}`
119+
? `# Diff with ${params.branch_name}\n\n${changes_content}`
120120
: ''
121121
}
122122

@@ -229,7 +229,7 @@ export const replace_changes_symbol = async (params: {
229229
vscode.window.showInformationMessage(
230230
t(
231231
'views.prompt.utils.symbols.git.replace-git-symbols.no-changes-found-between-branches-in-folder',
232-
{ branch_name, folder_name }
232+
{ 'branch name': branch_name, 'folder name': folder_name }
233233
)
234234
)
235235
if (params.symbols_cache) {
@@ -239,13 +239,11 @@ export const replace_changes_symbol = async (params: {
239239
continue
240240
}
241241

242-
const replacement_text = build_changes_markdown(
242+
const replacement_text = build_changes_markdown({
243243
diff,
244-
target_folder.uri.fsPath,
245-
diff_base,
246244
branch_name,
247-
workspace_folders.length > 1 ? folder_name : undefined
248-
)
245+
path_prefix: workspace_folders.length > 1 ? folder_name : undefined
246+
})
249247
const link_hash = `diff-with-${branch_name
250248
.toLowerCase()
251249
.replace(/[^a-z0-9]+/g, '-')
@@ -340,7 +338,7 @@ export const replace_changes_symbol = async (params: {
340338
vscode.window.showInformationMessage(
341339
t(
342340
'views.prompt.utils.symbols.git.replace-git-symbols.no-changes-found-between-branches',
343-
{ branch_name }
341+
{ 'branch name': branch_name }
344342
)
345343
)
346344
if (params.symbols_cache) {
@@ -350,12 +348,10 @@ export const replace_changes_symbol = async (params: {
350348
continue
351349
}
352350

353-
const replacement_text = build_changes_markdown(
351+
const replacement_text = build_changes_markdown({
354352
diff,
355-
repository.rootUri.fsPath,
356-
diff_base,
357353
branch_name
358-
)
354+
})
359355
const link_hash = `diff-with-${branch_name
360356
.toLowerCase()
361357
.replace(/[^a-z0-9]+/g, '-')
@@ -391,14 +387,15 @@ export const replace_changes_symbol = async (params: {
391387
return { instruction: result_instruction, changes_definitions }
392388
}
393389

394-
export const build_commit_changes_markdown = (
395-
diff: string,
396-
cwd: string,
397-
commit_hash: string,
398-
path_prefix?: string,
390+
export const build_commit_changes_markdown = (params: {
391+
diff: string
392+
commit_hash: string
393+
path_prefix?: string
399394
commit_message?: string
400-
): string => {
401-
const file_diffs = diff.split(/^diff --git /m).filter((d) => d.trim() != '')
395+
}): string => {
396+
const file_diffs = params.diff
397+
.split(/^diff --git /m)
398+
.filter((d) => d.trim() != '')
402399

403400
if (file_diffs.length == 0) {
404401
return ''
@@ -428,16 +425,16 @@ export const build_commit_changes_markdown = (
428425
}
429426

430427
if (file_path) {
431-
const display_path = path_prefix
432-
? `${path_prefix}/${file_path}`
428+
const display_path = params.path_prefix
429+
? `${params.path_prefix}/${file_path}`
433430
: file_path
434431

435432
changes_content += `### File: \`${display_path}\`\n\n`
436433

437-
if (path_prefix) {
434+
if (params.path_prefix) {
438435
full_file_diff = patch_diff_paths(
439436
full_file_diff,
440-
path_prefix,
437+
params.path_prefix,
441438
file_path,
442439
old_path,
443440
new_path
@@ -451,11 +448,11 @@ export const build_commit_changes_markdown = (
451448
}
452449

453450
if (changes_content) {
454-
const short_hash = commit_hash.substring(0, 7)
451+
const short_hash = params.commit_hash.substring(0, 7)
455452
const title_text = `Commit ${short_hash}`
456453
let msg_text = ''
457-
if (commit_message) {
458-
const trimmed = commit_message.trim()
454+
if (params.commit_message) {
455+
const trimmed = params.commit_message.trim()
459456
if (trimmed) {
460457
const lines = trimmed.split('\n')
461458
const first_line = `**${lines[0].trim()}**`
@@ -570,13 +567,12 @@ export const replace_commit_symbol = async (params: {
570567
continue
571568
}
572569

573-
replacement_text = build_commit_changes_markdown(
570+
replacement_text = build_commit_changes_markdown({
574571
diff,
575-
target_folder.uri.fsPath,
576572
commit_hash,
577-
workspace_folders.length > 1 ? folder_name : undefined,
578-
commit_message_body
579-
)
573+
path_prefix: workspace_folders.length > 1 ? folder_name : undefined,
574+
commit_message: commit_message_body
575+
})
580576
} else {
581577
if (commit_message_body) {
582578
replacement_text = `---\n\n${commit_message_body}\n\n---\n\n`

0 commit comments

Comments
 (0)