Skip to content

Commit 406e210

Browse files
committed
Introduce get markdown backticks utility and refactor code block generation across modules
- Replace all instances of hardcoded backtick wrapping (like `content.includes('```') ? '````' : '```'`) with dynamic logic that finds the longest continuous sequence of backticks in the string using `.match(/`{3,}/g)`, and wraps the text using that max length plus one (minimum 3 backticks). - a shared util should be created for this [Changes] - Update `apps/editor/src/utils/get-markdown-backticks.ts` to calculate the maximum backtick length using a `RegExp.exec` loop or `.reduce()` instead of `Math.max(...match)` to prevent `RangeError: Maximum call stack size exceeded` on files with thousands of matches. └── apps └── editor └── src ├── commands │ └── apply-response-command │ └── utils │ └── response-parser │ └── response-parser.ts ├── features │ └── search-files │ ├── search-modes │ │ └── perform-intelligent-search-mode.ts │ └── utils │ ├── analyze-files.ts │ └── search-files-by-intelligent.ts ├── utils │ ├── files-collector.ts │ ├── get-markdown-backticks.ts │ └── prompt-builder.ts └── views └── prompt └── backend ├── message-handlers │ ├── handle-patch-repair.ts │ └── handle-voice-input.ts └── utils └── symbols ├── pasted-text │ └── replace-pasted-text-symbol.ts ├── saved-context │ └── replace-saved-context-symbol.ts ├── skill │ └── replace-skill-symbol.ts └── website └── replace-website-symbol.ts
1 parent 4ca632c commit 406e210

12 files changed

Lines changed: 33 additions & 20 deletions

File tree

‎apps/editor/src/features/search-files/search-modes/perform-intelligent-search-mode.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { ConfigWebConfigurationFormat } from '@/utils/web-configuration-format-c
3030
import { invoke_agentic_cli } from '@/utils/agentic-cli-invocation'
3131
import { extract_paths_from_bullet_list } from '@/utils/extract-paths-from-bullet-list'
3232
import { shrink_file } from '../utils/shrink-file'
33+
import { get_markdown_backticks } from '@/utils/get-markdown-backticks'
3334

3435
export const perform_intelligent_search_mode = async (params: {
3536
files: string[]
@@ -247,7 +248,8 @@ export const perform_intelligent_search_mode = async (params: {
247248
const content_to_use = shrink_result
248249
? shrink_file(file.content, path.extname(file.file_path))
249250
: file.content
250-
md_files += `### File: \`${file.display_path}\`\n\n\`\`\`\n${content_to_use}\n\`\`\`\n\n`
251+
const backticks = get_markdown_backticks(content_to_use)
252+
md_files += `### File: \`${file.display_path}\`\n\n${backticks}\n${content_to_use}\n${backticks}\n\n`
251253
}
252254

253255
const config = vscode.workspace.getConfiguration('codeWebChat')
@@ -400,7 +402,7 @@ export const perform_intelligent_search_mode = async (params: {
400402
const content_to_use = shrink_result
401403
? shrink_file(file.content, path.extname(file.file_path))
402404
: file.content
403-
const backticks = content_to_use.includes('```') ? '````' : '```'
405+
const backticks = get_markdown_backticks(content_to_use)
404406
md_files += `### File: \`${file.display_path}\`\n\n${backticks}\n${content_to_use}\n${backticks}\n\n`
405407
}
406408

‎apps/editor/src/features/search-files/utils/analyze-files.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provi
55
import { shrink_file } from './shrink-file'
66
import { t } from '@/i18n'
77
import { normalize_path } from '@/utils/normalize-path'
8+
import { get_markdown_backticks } from '@/utils/get-markdown-backticks'
89

910
export interface FileData {
1011
file_path: string
@@ -86,9 +87,8 @@ export const analyze_files = async (params: {
8687

8788
files_data.push({ file_path, display_path, content })
8889

89-
const backticks_shrunk = shrunk_content.includes('```')
90-
? '````'
91-
: '```'
90+
const backticks_shrunk = get_markdown_backticks(shrunk_content)
91+
9292
const wrapped_shrunk = `### File: \`${display_path}\`\n\n${backticks_shrunk}\n${shrunk_content}\n${backticks_shrunk}\n\n`
9393
shrink_tokens += Math.floor(wrapped_shrunk.length / 4)
9494
} catch (e) {}

‎apps/editor/src/features/search-files/utils/search-files-by-intelligent.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Provider } from '@/services/providers-manager'
1212
import { t } from '@/i18n'
1313
import { PROVIDERS } from '@/constants/providers'
1414
import { shrink_file } from './shrink-file'
15+
import { get_markdown_backticks } from '@/utils/get-markdown-backticks'
1516

1617
export const search_files_by_intelligent = async (
1718
files_data: FileData[],
@@ -25,7 +26,7 @@ export const search_files_by_intelligent = async (
2526
const content_to_use = shrink_result
2627
? shrink_file(file.content, path.extname(file.file_path))
2728
: file.content
28-
const backticks = content_to_use.includes('```') ? '````' : '```'
29+
const backticks = get_markdown_backticks(content_to_use)
2930
md_files += `### File: \`${file.display_path}\`\n\n${backticks}\n${content_to_use}\n${backticks}\n\n`
3031
}
3132

‎apps/editor/src/utils/files-collector.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { natural_sort } from '@/utils/natural-sort'
55
import { OpenEditorsProvider } from '@/context/providers/open-editors/open-editors-provider'
66
import { is_binary_file } from './is-binary'
77
import { normalize_path } from './normalize-path'
8+
import { get_markdown_backticks } from './get-markdown-backticks'
89

910
export namespace FilesCollector {
1011
export const collect_files = async (params: {
@@ -87,7 +88,7 @@ export namespace FilesCollector {
8788
if (is_binary) {
8889
collected_text += `### File: \`${display_path}\`\n\nBinary file\n\n`
8990
} else {
90-
const backticks = content.includes('```') ? '````' : '```'
91+
const backticks = get_markdown_backticks(content)
9192
collected_text += `### File: \`${display_path}\`\n\n${backticks}\n${content}\n${backticks}\n\n`
9293
}
9394
} catch (error) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const get_markdown_backticks = (content: string): string => {
2+
const match = content.match(/`{3,}/g)
3+
return '`'.repeat(Math.max(3, (match ? match.reduce((max, m) => Math.max(max, m.length), 0) : 0) + 1))
4+
}

‎apps/editor/src/utils/prompt-builder.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { normalize_path } from './normalize-path'
22
import { DIFF_PLACEHOLDERS } from '@/constants/values'
3+
import { get_markdown_backticks } from './get-markdown-backticks'
34

45
export namespace PromptBuilder {
56
export const build_file_context = (params: {
@@ -11,7 +12,7 @@ export namespace PromptBuilder {
1112
if (params.is_binary || params.content === undefined) {
1213
return `### File: \`${display_path}\`\n\nBinary file\n\n`
1314
}
14-
const backticks = params.content.includes('```') ? '````' : '```'
15+
const backticks = get_markdown_backticks(params.content)
1516
return `### File: \`${display_path}\`\n\n${backticks}\n${params.content}\n${backticks}\n\n`
1617
}
1718

@@ -45,14 +46,14 @@ export namespace PromptBuilder {
4546
if (is_placeholder) {
4647
result += `${diff_content}\n\n`
4748
} else {
48-
const backticks = diff_content.includes('```') ? '````' : '```'
49+
const backticks = get_markdown_backticks(diff_content)
4950
result += `${backticks}diff\n${diff_content}\n${backticks}\n\n`
5051
}
5152
}
5253

5354
if (params.full_content?.trimEnd()) {
5455
const full_content = params.full_content.trimEnd()
55-
const backticks = full_content.includes('```') ? '````' : '```'
56+
const backticks = get_markdown_backticks(full_content)
5657
result += `${backticks}\n${full_content}\n${backticks}\n\n`
5758
}
5859

‎apps/editor/src/views/prompt/backend/message-handlers/handle-patch-repair.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from '@/constants/instructions'
3131
import { ConfigWebConfigurationFormat } from '@/utils/web-configuration-format-converters'
3232
import { invoke_agentic_cli } from '@/utils/agentic-cli-invocation'
33+
import { get_markdown_backticks } from '@/utils/get-markdown-backticks'
3334

3435
export const handle_patch_repair = async (params: {
3536
prompt_view_provider: PromptViewProvider
@@ -249,7 +250,7 @@ export const handle_patch_repair = async (params: {
249250
if (action === 'invoke-agent') {
250251
let cli_prompt = ''
251252
for (const item of files_to_process) {
252-
const backticks = item.file_state.content.includes('```') ? '````' : '```'
253+
const backticks = get_markdown_backticks(item.file_state.content)
253254
const display_path =
254255
!is_single_root_folder_workspace && item.file_state.workspace_name
255256
? `${item.file_state.workspace_name}/${item.file_state.file_path}`
@@ -291,7 +292,7 @@ export const handle_patch_repair = async (params: {
291292
if (action === 'copy') {
292293
let chatbot_prompt = ''
293294
for (const item of files_to_process) {
294-
const backticks = item.file_state.content.includes('```') ? '````' : '```'
295+
const backticks = get_markdown_backticks(item.file_state.content)
295296
const display_path =
296297
!is_single_root_folder_workspace && item.file_state.workspace_name
297298
? `${item.file_state.workspace_name}/${item.file_state.file_path}`
@@ -371,9 +372,7 @@ export const handle_patch_repair = async (params: {
371372
if (selected_web_configuration_name) {
372373
let chatbot_prompt = ''
373374
for (const item of files_to_process) {
374-
const backticks = item.file_state.content.includes('```')
375-
? '````'
376-
: '```'
375+
const backticks = get_markdown_backticks(item.file_state.content)
377376
const display_path =
378377
!is_single_root_folder_workspace && item.file_state.workspace_name
379378
? `${item.file_state.workspace_name}/${item.file_state.file_path}`

‎apps/editor/src/views/prompt/backend/message-handlers/handle-voice-input.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { get_api_configuration } from '@/utils/get-api-configuration'
2525
import { show_incomplete_setup_warning } from '@/utils/show-missing-configuration-notification'
2626
import { get_error_message } from '@/utils/get-error-message'
2727
import { invoke_agentic_cli } from '@/utils/agentic-cli-invocation'
28+
import { get_markdown_backticks } from '@/utils/get-markdown-backticks'
2829

2930
const MIN_RECORDING_DURATION = 1000
3031

@@ -270,7 +271,7 @@ const stop_recording = async (prompt_view_provider: PromptViewProvider) => {
270271
vscode.Uri.file(file_path)
271272
)
272273
const content = document.getText()
273-
const backticks = content.includes('```') ? '````' : '```'
274+
const backticks = get_markdown_backticks(content)
274275
files_context += `### File: \`${display_path}\`\n\n${backticks}\n${content}\n${backticks}\n\n`
275276
} catch (error) {
276277
Logger.warn({

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode'
22
import { PersistentTextStorage } from '../../persistent-text-storage'
3+
import { get_markdown_backticks } from '@/utils/get-markdown-backticks'
34

45
export const replace_pasted_text_symbol = async (params: {
56
instructions: string[]
@@ -49,7 +50,7 @@ export const replace_pasted_text_symbol = async (params: {
4950
}
5051

5152
if (replacement.success && replacement.content) {
52-
const backticks = replacement.content.includes('```') ? '````' : '```'
53+
const backticks = get_markdown_backticks(replacement.content)
5354
const block = `${backticks}\n${replacement.content}\n${backticks}`
5455
result_string += (result_string ? '\n\n' : '') + block
5556
} else {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '@/features/context-restoration'
1010
import { Logger } from '@shared/utils/logger'
1111
import { SymbolCacheManager } from '../symbol-cache'
12+
import { get_markdown_backticks } from '@/utils/get-markdown-backticks'
1213

1314
export const replace_saved_context_symbol = async (params: {
1415
instructions: string[]
@@ -98,7 +99,7 @@ export const replace_saved_context_symbol = async (params: {
9899
params.workspace_provider.get_workspace_root_for_file(p)
99100
const relative_path = root ? path.relative(root, p) : p
100101

101-
const backticks = content.includes('```') ? '````' : '```'
102+
const backticks = get_markdown_backticks(content)
102103
files_markdown += `### File: \`${relative_path.replace(
103104
/\\/g,
104105
'/'

0 commit comments

Comments
 (0)