Skip to content

Commit 62eb04e

Browse files
committed
Add fragment symbol handling across all message handlers and create utility to replace fragment tags with formatted code blocks
1 parent 046db63 commit 62eb04e

8 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎apps/editor/src/views/panel/backend/message-handlers/handle-code-at-cursor.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { replace_skill_symbol } from '../utils/replace-skill-symbol'
2626
import { replace_image_symbol } from '../utils/replace-image-symbol'
2727
import { replace_document_symbol } from '../utils/replace-document-symbol'
2828
import { replace_website_symbol } from '../utils/replace-website-symbol'
29+
import { replace_fragment_symbol } from '../utils/replace-fragment-symbol'
2930

3031
const get_code_at_cursor_config = async (
3132
api_providers_manager: ModelProvidersManager,
@@ -403,6 +404,12 @@ export const handle_code_at_cursor = async (
403404
})
404405
}
405406

407+
if (processed_completion_instructions.includes('<fragment')) {
408+
processed_completion_instructions = replace_fragment_symbol(
409+
processed_completion_instructions
410+
)
411+
}
412+
406413
const files_collector = new FilesCollector({
407414
workspace_provider: panel_provider.workspace_provider,
408415
open_editors_provider: panel_provider.open_editors_provider

‎apps/editor/src/views/panel/backend/message-handlers/handle-copy-prompt.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { replace_saved_context_symbol } from '@/views/panel/backend/utils/replac
1111
import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-symbol'
1212
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
1313
import { replace_document_symbol } from '../utils/replace-document-symbol'
14+
import { replace_fragment_symbol } from '../utils/replace-fragment-symbol'
1415
import {
1516
code_at_cursor_instructions_for_panel,
1617
prune_context_instructions_prefix,
@@ -152,6 +153,12 @@ export const handle_copy_prompt = async (params: {
152153
})
153154
}
154155

156+
if (processed_completion_instructions.includes('<fragment')) {
157+
processed_completion_instructions = replace_fragment_symbol(
158+
processed_completion_instructions
159+
)
160+
}
161+
155162
const missing_text_tag = processed_completion_instructions
156163
? `<missing_text>${processed_completion_instructions}</missing_text>`
157164
: '<missing_text>'
@@ -230,6 +237,10 @@ export const handle_copy_prompt = async (params: {
230237
})
231238
}
232239

240+
if (processed_instructions.includes('<fragment')) {
241+
processed_instructions = replace_fragment_symbol(processed_instructions)
242+
}
243+
233244
let system_instructions_xml = ''
234245

235246
if (params.panel_provider.web_prompt_type == 'edit-context') {

‎apps/editor/src/views/panel/backend/message-handlers/handle-edit-context.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-
2626
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
2727
import { replace_document_symbol } from '../utils/replace-document-symbol'
2828
import { replace_website_symbol } from '../utils/replace-website-symbol'
29+
import { replace_fragment_symbol } from '../utils/replace-fragment-symbol'
2930
import { apply_reasoning_effort } from '@/utils/apply-reasoning-effort'
3031
import { EditContextMessage } from '@/views/panel/types/messages'
3132
import { dictionary } from '@shared/constants/dictionary'
@@ -386,6 +387,10 @@ export const handle_edit_context = async (
386387
})
387388
}
388389

390+
if (processed_instructions.includes('<fragment')) {
391+
processed_instructions = replace_fragment_symbol(processed_instructions)
392+
}
393+
389394
const is_prune_context = panel_provider.api_prompt_type == 'prune-context'
390395
const collected_files = await files_collector.collect_files({
391396
compact: is_prune_context

‎apps/editor/src/views/panel/backend/message-handlers/handle-preview-preset.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Preset } from '@shared/types/preset'
1313
import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-symbol'
1414
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
1515
import { replace_document_symbol } from '../utils/replace-document-symbol'
16+
import { replace_fragment_symbol } from '../utils/replace-fragment-symbol'
1617
import { apply_preset_affixes_to_instruction } from '@/utils/apply-preset-affixes'
1718
import { dictionary } from '@shared/constants/dictionary'
1819
import {
@@ -123,6 +124,12 @@ export const handle_preview_preset = async (
123124
})
124125
}
125126

127+
if (processed_completion_instructions.includes('<fragment')) {
128+
processed_completion_instructions = replace_fragment_symbol(
129+
processed_completion_instructions
130+
)
131+
}
132+
126133
const missing_text_tag = processed_completion_instructions
127134
? `<missing_text>${processed_completion_instructions}</missing_text>`
128135
: '<missing_text>'
@@ -210,6 +217,11 @@ export const handle_preview_preset = async (
210217
})
211218
}
212219

220+
// New fragment handling for non‑cursor prompts
221+
if (processed_instructions.includes('<fragment')) {
222+
processed_instructions = replace_fragment_symbol(processed_instructions)
223+
}
224+
213225
let system_instructions_xml = ''
214226
if (panel_provider.web_prompt_type == 'edit-context') {
215227
const config = vscode.workspace.getConfiguration('codeWebChat')

‎apps/editor/src/views/panel/backend/message-handlers/handle-prune-context.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-
2222
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
2323
import { replace_document_symbol } from '@/views/panel/backend/utils/replace-document-symbol'
2424
import { replace_website_symbol } from '@/views/panel/backend/utils/replace-website-symbol'
25+
import { replace_fragment_symbol } from '@/views/panel/backend/utils/replace-fragment-symbol'
2526
import { apply_reasoning_effort } from '@/utils/apply-reasoning-effort'
2627
import { PruneContextMessage } from '@/views/panel/types/messages'
2728
import { dictionary } from '@shared/constants/dictionary'
@@ -365,6 +366,10 @@ export const handle_prune_context = async (
365366
})
366367
}
367368

369+
if (processed_instructions.includes('<fragment')) {
370+
processed_instructions = replace_fragment_symbol(processed_instructions)
371+
}
372+
368373
const collected_files = await files_collector.collect_files({
369374
compact: true
370375
})

‎apps/editor/src/views/panel/backend/message-handlers/handle-send-to-browser.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-symbol'
1313
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
1414
import { replace_document_symbol } from '../utils/replace-document-symbol'
15+
import { replace_fragment_symbol } from '../utils/replace-fragment-symbol'
1516
import {
1617
code_at_cursor_instructions_for_panel,
1718
prune_context_instructions_prefix,
@@ -163,6 +164,12 @@ export const handle_send_to_browser = async (params: {
163164
})
164165
}
165166

167+
if (processed_completion_instructions.includes('<fragment')) {
168+
processed_completion_instructions = replace_fragment_symbol(
169+
processed_completion_instructions
170+
)
171+
}
172+
166173
const context_text = await files_collector.collect_files()
167174

168175
const relative_path = vscode.workspace.asRelativePath(document.uri)
@@ -277,6 +284,12 @@ export const handle_send_to_browser = async (params: {
277284
})
278285
}
279286

287+
if (processed_instructions.includes('<fragment')) {
288+
processed_instructions = replace_fragment_symbol(
289+
processed_instructions
290+
)
291+
}
292+
280293
let system_instructions_xml = ''
281294
if (params.panel_provider.web_prompt_type == 'edit-context') {
282295
const config = vscode.workspace.getConfiguration('codeWebChat')
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const replace_fragment_symbol = (instruction: string): string => {
2+
const regex =
3+
/<fragment path="([^"]+)"(?: start="([^"]+)")?(?: end="([^"]+)")?>([\s\S]*?)<\/fragment>/g
4+
5+
return instruction.replace(regex, (_match, path, start, end, content) => {
6+
let clean_content = content
7+
8+
if (
9+
clean_content.startsWith('\n<![CDATA[\n') &&
10+
clean_content.endsWith('\n]]>\n')
11+
) {
12+
clean_content = clean_content.slice(11, -5)
13+
} else if (
14+
clean_content.startsWith('<![CDATA[') &&
15+
clean_content.endsWith(']]>')
16+
) {
17+
clean_content = clean_content.slice(9, -3)
18+
} else if (clean_content.startsWith('\n') && clean_content.endsWith('\n')) {
19+
clean_content = clean_content.slice(1, -1)
20+
}
21+
22+
const range_str = start && end ? ` (${start}-${end})` : ''
23+
24+
return `\n\n\`${path}\`${range_str}\n\n\`\`\`\n${clean_content}\n\`\`\`\n\n`
25+
})
26+
}

‎apps/editor/src/views/panel/backend/utils/replace-selection-symbol.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export const replace_selection_symbol = (instruction: string): string => {
1717
const selected_text = active_editor.document.getText(active_editor.selection)
1818
const document = active_editor.document
1919
const current_file_path = vscode.workspace.asRelativePath(document.uri)
20+
const start = active_editor.selection.start
21+
const end = active_editor.selection.end
2022

21-
const replacement_text = `\n<fragment path="${current_file_path}">\n<![CDATA[\n${selected_text}\n]]>\n</fragment>`
23+
const replacement_text = `\n\n\`${current_file_path}\` (${start.line + 1}:${start.character + 1}-${end.line + 1}:${end.character + 1})\n\n\`\`\`\n${selected_text}\n\`\`\`\n\n`
2224

2325
return instruction.replace(/\s*#Selection\s*/g, replacement_text)
2426
}

0 commit comments

Comments
 (0)