Skip to content

Commit 34a4264

Browse files
committed
Implement support for document symbols to handle long text pastes as referenced files and rename image handling commands for consistency
1 parent faf063e commit 34a4264

24 files changed

Lines changed: 356 additions & 29 deletions

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
import { replace_saved_context_symbol } from '@/views/panel/backend/utils/replace-saved-context-symbol'
2828
import { replace_skill_symbol } from '../utils/replace-skill-symbol'
2929
import { replace_image_symbol } from '../utils/replace-image-symbol'
30+
import { replace_document_symbol } from '../utils/replace-document-symbol'
3031

3132
const get_code_completion_config = async (
3233
api_providers_manager: ModelProvidersManager,
@@ -411,6 +412,12 @@ export const handle_code_completion = async (
411412
})
412413
}
413414

415+
if (processed_completion_instructions.includes('#Document(')) {
416+
processed_completion_instructions = await replace_document_symbol({
417+
instruction: processed_completion_instructions
418+
})
419+
}
420+
414421
const files_collector = new FilesCollector(
415422
panel_provider.workspace_provider,
416423
panel_provider.open_editors_provider

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { replace_saved_context_symbol } from '@/views/panel/backend/utils/replace-saved-context-symbol'
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'
13+
import { replace_document_symbol } from '../utils/replace-document-symbol'
1314
import {
1415
code_at_cursor_instructions_for_panel,
1516
prune_context_instructions_prefix,
@@ -148,6 +149,12 @@ export const handle_copy_prompt = async (params: {
148149
})
149150
}
150151

152+
if (processed_completion_instructions.includes('#Document(')) {
153+
processed_completion_instructions = await replace_document_symbol({
154+
instruction: processed_completion_instructions
155+
})
156+
}
157+
151158
const missing_text_tag = processed_completion_instructions
152159
? `<missing_text>${processed_completion_instructions}</missing_text>`
153160
: '<missing_text>'
@@ -220,6 +227,12 @@ export const handle_copy_prompt = async (params: {
220227
})
221228
}
222229

230+
if (processed_instructions.includes('#Document(')) {
231+
processed_instructions = await replace_document_symbol({
232+
instruction: processed_instructions
233+
})
234+
}
235+
223236
let system_instructions_xml = ''
224237

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { replace_selection_symbol } from '@/views/panel/backend/utils/replace-se
2525
import { PanelProvider } from '@/views/panel/backend/panel-provider'
2626
import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-symbol'
2727
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
28+
import { replace_document_symbol } from '../utils/replace-document-symbol'
2829
import { apply_reasoning_effort } from '@/utils/apply-reasoning-effort'
2930
import { EditContextMessage } from '@/views/panel/types/messages'
3031
import { dictionary } from '@shared/constants/dictionary'
@@ -371,6 +372,12 @@ export const handle_edit_context = async (
371372
})
372373
}
373374

375+
if (processed_instructions.includes('#Document(')) {
376+
processed_instructions = await replace_document_symbol({
377+
instruction: processed_instructions
378+
})
379+
}
380+
374381
const is_prune_context = panel_provider.api_prompt_type == 'prune-context'
375382
const collected_files = await files_collector.collect_files({
376383
compact: is_prune_context
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as vscode from 'vscode'
2+
import * as os from 'os'
3+
import * as path from 'path'
4+
5+
export const handle_open_prompt_document = async (message: {
6+
hash: string
7+
}) => {
8+
try {
9+
const hash = message.hash
10+
const filename = `cwc-document-${hash}.txt`
11+
const file_path = path.join(os.tmpdir(), filename)
12+
13+
const doc = await vscode.workspace.openTextDocument(file_path)
14+
await vscode.window.showTextDocument(doc)
15+
} catch (error) {
16+
vscode.window.showErrorMessage(`Failed to open document: ${error}`)
17+
}
18+
}

apps/editor/src/views/panel/backend/message-handlers/handle-open-temp-image.ts renamed to apps/editor/src/views/panel/backend/message-handlers/handle-open-prompt-image.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import * as vscode from 'vscode'
22
import * as os from 'os'
33
import * as path from 'path'
44
import * as fs from 'fs'
5-
import { OpenTempImageMessage } from '../../types/messages'
5+
import { OpenPromptImageMessage } from '../../types/messages'
66

7-
export const handle_open_temp_image = async (message: OpenTempImageMessage) => {
7+
export const handle_open_prompt_image = async (
8+
message: OpenPromptImageMessage
9+
) => {
810
try {
911
const hash = message.hash
1012
const txt_filename = `cwc-image-${hash}.txt`

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
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'
15+
import { replace_document_symbol } from '../utils/replace-document-symbol'
1516
import { apply_preset_affixes_to_instruction } from '@/utils/apply-preset-affixes'
1617
import { dictionary } from '@shared/constants/dictionary'
1718
import {
@@ -128,6 +129,12 @@ export const handle_preview_preset = async (
128129
})
129130
}
130131

132+
if (processed_completion_instructions.includes('#Document(')) {
133+
processed_completion_instructions = await replace_document_symbol({
134+
instruction: processed_completion_instructions
135+
})
136+
}
137+
131138
const missing_text_tag = processed_completion_instructions
132139
? `<missing_text>${processed_completion_instructions}</missing_text>`
133140
: '<missing_text>'
@@ -209,6 +216,12 @@ export const handle_preview_preset = async (
209216
})
210217
}
211218

219+
if (processed_instructions.includes('#Document(')) {
220+
processed_instructions = await replace_document_symbol({
221+
instruction: processed_instructions
222+
})
223+
}
224+
212225
let system_instructions_xml = ''
213226
if (panel_provider.web_prompt_type == 'edit-context') {
214227
const config = vscode.workspace.getConfiguration('codeWebChat')

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { replace_selection_symbol } from '@/views/panel/backend/utils/replace-se
2323
import { PanelProvider } from '@/views/panel/backend/panel-provider'
2424
import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-symbol'
2525
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
26+
import { replace_document_symbol } from '@/views/panel/backend/utils/replace-document-symbol'
2627
import { apply_reasoning_effort } from '@/utils/apply-reasoning-effort'
2728
import { PruneContextMessage } from '@/views/panel/types/messages'
2829
import { dictionary } from '@shared/constants/dictionary'
@@ -352,6 +353,12 @@ export const handle_prune_context = async (
352353
})
353354
}
354355

356+
if (processed_instructions.includes('#Document(')) {
357+
processed_instructions = await replace_document_symbol({
358+
instruction: processed_instructions
359+
})
360+
}
361+
355362
const collected_files = await files_collector.collect_files({
356363
compact: true
357364
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as vscode from 'vscode'
2+
import * as os from 'os'
3+
import * as path from 'path'
4+
import * as fs from 'fs'
5+
import * as crypto from 'crypto'
6+
import { SavePromptDocumentMessage } from '../../types/messages'
7+
import { PanelProvider } from '../panel-provider'
8+
9+
export const handle_save_prompt_document = async (
10+
panel_provider: PanelProvider,
11+
message: SavePromptDocumentMessage
12+
) => {
13+
try {
14+
const hash = crypto.createHash('md5').update(message.text).digest('hex')
15+
const filename = `cwc-document-${hash}.txt`
16+
const file_path = path.join(os.tmpdir(), filename)
17+
await fs.promises.writeFile(file_path, message.text, 'utf-8')
18+
panel_provider.add_text_at_cursor_position(`#Document(${hash})`)
19+
} catch (error) {
20+
vscode.window.showErrorMessage(`Failed to save document: ${error}`)
21+
}
22+
}

apps/editor/src/views/panel/backend/message-handlers/handle-save-temp-image.ts renamed to apps/editor/src/views/panel/backend/message-handlers/handle-save-prompt-image.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import * as os from 'os'
33
import * as path from 'path'
44
import * as fs from 'fs'
55
import * as crypto from 'crypto'
6-
import { SaveTempImageMessage } from '../../types/messages'
6+
import { SavePromptImageMessage } from '../../types/messages'
77
import { PanelProvider } from '../panel-provider'
88

9-
export const handle_save_temp_image = async (
9+
export const handle_save_prompt_image = async (
1010
panel_provider: PanelProvider,
11-
message: SaveTempImageMessage
11+
message: SavePromptImageMessage
1212
) => {
1313
try {
1414
const buffer = Buffer.from(message.content_base64, 'base64')

‎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
@@ -11,6 +11,7 @@ import {
1111
} from '@/views/panel/backend/utils/replace-git-symbols'
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'
14+
import { replace_document_symbol } from '../utils/replace-document-symbol'
1415
import {
1516
code_at_cursor_instructions_for_panel,
1617
prune_context_instructions_prefix,
@@ -175,6 +176,12 @@ export const handle_send_to_browser = async (params: {
175176
})
176177
}
177178

179+
if (processed_completion_instructions.includes('#Document(')) {
180+
processed_completion_instructions = await replace_document_symbol({
181+
instruction: processed_completion_instructions
182+
})
183+
}
184+
178185
const context_text = await files_collector.collect_files({
179186
exclude_path: active_path
180187
})
@@ -284,6 +291,12 @@ export const handle_send_to_browser = async (params: {
284291
})
285292
}
286293

294+
if (processed_instructions.includes('#Document(')) {
295+
processed_instructions = await replace_document_symbol({
296+
instruction: processed_instructions
297+
})
298+
}
299+
287300
let system_instructions_xml = ''
288301
if (params.panel_provider.web_prompt_type == 'edit-context') {
289302
const config = vscode.workspace.getConfiguration('codeWebChat')

0 commit comments

Comments
 (0)