Skip to content

Commit 8e108f3

Browse files
committed
Standardize selected text formatting in chat instructions using file path and triple backticks
1 parent 14deb6f commit 8e108f3

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

packages/vscode/src/commands/chat-commands.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode'
22
import { FilesCollector } from '../helpers/files-collector'
33
import { WebSocketManager } from '../services/websocket-manager'
4+
import { replace_selection_placeholder } from '../utils/replace-selection-placeholder'
45
import { apply_preset_affixes_to_instruction } from '../helpers/apply-preset-affixes'
56
import { EditFormat } from '@shared/types/edit-format'
67

@@ -42,8 +43,12 @@ async function handle_chat_command(
4243
: ''
4344

4445
if (editor && !editor.selection.isEmpty) {
45-
const selected_text = editor.document.getText(editor.selection)
46-
instructions = `\`${current_file_path}\`\n\`\`\`\n${selected_text}\n\`\`\`\n${instructions}`
46+
if (instructions.includes('@selection')) {
47+
instructions = replace_selection_placeholder(instructions)
48+
} else {
49+
const selected_text = editor.document.getText(editor.selection)
50+
instructions = `\`${current_file_path}\`\n\`\`\`\n${selected_text}\n\`\`\`\n${instructions}`
51+
}
4752
}
4853

4954
const files_collector = new FilesCollector(
@@ -143,8 +148,12 @@ export function chat_using_command(
143148

144149
let processed_instructions = instructions
145150
if (editor && !editor.selection.isEmpty) {
146-
const selected_text = editor.document.getText(editor.selection)
147-
processed_instructions = `\`${current_file_path}\`\n\`\`\`\n${selected_text}\n\`\`\`\n${instructions}`
151+
if (instructions.includes('@selection')) {
152+
processed_instructions = replace_selection_placeholder(instructions)
153+
} else {
154+
const selected_text = editor.document.getText(editor.selection)
155+
processed_instructions = `\`${current_file_path}\`\n\`\`\`\n${selected_text}\n\`\`\`\n${instructions}`
156+
}
148157
}
149158

150159
const config = vscode.workspace.getConfiguration('codeWebChat')

packages/vscode/src/utils/replace-selection-placeholder/replace-selection-placeholder.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,10 @@ export const replace_selection_placeholder = (instruction: string): string => {
1515
}
1616

1717
const selected_text = active_editor.document.getText(active_editor.selection)
18+
const document = active_editor.document
19+
const current_file_path = vscode.workspace.asRelativePath(document.uri)
1820

19-
// Check if the selected text is a single line
20-
const is_single_line = !selected_text.includes('\n')
21+
const replacement_text = `\n\`${current_file_path}\`\n\`\`\`\n${selected_text}\n\`\`\`\n`
2122

22-
if (is_single_line) {
23-
// For single-line text, wrap with single backticks
24-
return instruction.replace(/@selection/g, `\`${selected_text}\``)
25-
} else {
26-
// For multi-line text, wrap with triple backticks as before
27-
return instruction.replace(
28-
/@selection/g,
29-
`\n\`\`\`\n${selected_text}\n\`\`\`\n`
30-
)
31-
}
23+
return instruction.replace(/@selection/g, replacement_text)
3224
}

0 commit comments

Comments
 (0)