Skip to content

Commit 4ca632c

Browse files
committed
Refactor symbol replacement spacing and trimming logic for fragments and pasted text
- do we call trim() on text between then replaced symbols? we should ├── apps │ └── editor │ └── src │ ├── utils │ │ └── prompt-builder.ts │ └── views │ └── prompt │ ├── backend │ │ ├── message-handlers │ │ │ ├── handle-copy-prompt.ts │ │ │ ├── handle-preview-prompt.ts │ │ │ ├── handle-save-prompt-pasted-text.ts │ │ │ └── utils │ │ │ ├── build-cli-prompt.ts │ │ │ └── build-prompt-payload.ts │ │ ├── prompt-view-provider.ts │ │ └── utils │ │ ├── persistent-text-storage.ts │ │ └── symbols │ │ ├── fragment │ │ │ └── replace-fragment-symbol.ts │ │ ├── pasted-text │ │ │ └── replace-pasted-text-symbol.ts │ │ └── replace-symbols.ts │ ├── frontend │ │ └── hooks │ │ └── use-prompt │ │ └── use-prompt.ts │ └── types │ └── messages.ts └── packages └── ui └── src └── components └── editor └── common └── prompts └── PromptField └── hooks └── use-handlers └── handle-paste.ts
1 parent e338de8 commit 4ca632c

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const replace_fragment_symbol = async (params: {
1414
const matches = [...instruction.matchAll(regex)]
1515
if (matches.length === 0) return instruction
1616

17-
let result = instruction
17+
let result_string = ''
18+
let last_index = 0
1819

1920
for (const match of matches) {
2021
const full_match = match[0]
@@ -76,13 +77,26 @@ export const replace_fragment_symbol = async (params: {
7677
content = `// Error reading file ${file_path}`
7778
}
7879

80+
const text_between = instruction.slice(last_index, match.index)
81+
const trimmed_text = text_between.trim()
82+
83+
if (trimmed_text) {
84+
result_string += (result_string ? '\n\n' : '') + trimmed_text
85+
}
86+
7987
const range_str = ` (${start_line}-${end_line})`
80-
const replacement = `\n\n\`${file_path}\`${range_str}\n\n\`\`\`\n${content}\n\`\`\`\n\n`
88+
const replacement = `\`${file_path}\`${range_str}\n\n\`\`\`\n${content}\n\`\`\``
89+
90+
result_string += (result_string ? '\n\n' : '') + replacement
91+
last_index = match.index + full_match.length
92+
}
8193

82-
result = result.replace(full_match, replacement)
94+
const tail = instruction.slice(last_index).trim()
95+
if (tail) {
96+
result_string += (result_string ? '\n\n' : '') + tail
8397
}
8498

85-
return result
99+
return result_string
86100
})
87101
)
88102
}

‎apps/editor/src/views/prompt/backend/utils/symbols/pasted-text/replace-pasted-text-symbol.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,28 @@ export const replace_pasted_text_symbol = async (params: {
4141
const match = matches[i]
4242
const replacement = replacements[i]
4343

44-
result_string += instruction.slice(last_index, match.index)
44+
const text_between = instruction.slice(last_index, match.index)
45+
const trimmed_text = text_between.trim()
46+
47+
if (trimmed_text) {
48+
result_string += (result_string ? '\n\n' : '') + trimmed_text
49+
}
4550

4651
if (replacement.success && replacement.content) {
4752
const backticks = replacement.content.includes('```') ? '````' : '```'
48-
result_string += `\n${backticks}\n${replacement.content}\n${backticks}\n`
53+
const block = `${backticks}\n${replacement.content}\n${backticks}`
54+
result_string += (result_string ? '\n\n' : '') + block
4955
} else {
50-
result_string += match[0]
56+
result_string += (result_string ? '\n\n' : '') + match[0]
5157
}
5258

5359
last_index = match.index + match[0].length
5460
}
5561

56-
result_string += instruction.slice(last_index).trim()
62+
const tail = instruction.slice(last_index).trim()
63+
if (tail) {
64+
result_string += (result_string ? '\n\n' : '') + tail
65+
}
5766

5867
return result_string
5968
})

0 commit comments

Comments
 (0)