Skip to content

Commit bdffd66

Browse files
committed
Refactor ASCII tree utility into a namespaced module and update references
- use namespace AsciiTree - place `apps/editor/src/utils/ascii-tree.ts` in a folder of the same name (still in utils). move [Fragment] to `apps/editor/src/utils/ascii-tree/utils.ts`
1 parent 153ee51 commit bdffd66

8 files changed

Lines changed: 90 additions & 86 deletions

File tree

‎apps/editor/src/commands/copy-paths-command.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { dictionary } from '@shared/constants/dictionary'
44
import { t } from '../i18n'
55
import { WorkspaceProvider } from '../context/providers/workspace/workspace-provider'
66
import { OpenEditorsProvider } from '../context/providers/open-editors/open-editors-provider'
7-
import { generate_ascii_tree } from '../utils/ascii-tree'
7+
import { AsciiTree } from '../utils/ascii-tree'
88

99
const format_paths = (files: string[]) => {
1010
const config = vscode.workspace.getConfiguration('codeWebChat')
@@ -35,7 +35,7 @@ const format_paths = (files: string[]) => {
3535
})
3636

3737
if (format == 'ascii-tree') {
38-
return generate_ascii_tree(display_paths)
38+
return AsciiTree.generate(display_paths)
3939
} else if (format == 'comma-separated') {
4040
return display_paths.map((display_path) => `\`${display_path}\``).join(', ')
4141
}

‎apps/editor/src/commands/generate-commit-message-command/generate-commit-message-command.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
1818
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
1919
import { create_checkpoint } from '@/features/checkpoints/actions'
2020
import { simplify_prompt_symbols } from '@shared/utils/simplify-prompt-symbols'
21-
import { generate_ascii_tree } from '../../utils/ascii-tree'
21+
import { AsciiTree } from '../../utils/ascii-tree'
2222
import { LAST_ATTACH_ASCII_TREE_STATE_KEY } from '@/constants/state-keys'
2323

2424
const truncate_prompt = (text: string): string => {
@@ -288,7 +288,7 @@ export const generate_commit_message_command = (
288288
const display_paths = selected_files_to_attach.map((p) =>
289289
vscode.workspace.asRelativePath(p).replace(/\\/g, '/')
290290
)
291-
return '\n\n' + generate_ascii_tree(display_paths)
291+
return '\n\n' + AsciiTree.generate(display_paths)
292292
}
293293

294294
return ''

‎apps/editor/src/commands/select-commit-files-command.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { dictionary } from '@shared/constants/dictionary'
1010
import { t } from '@/i18n'
1111
import { display_token_count } from '@/utils/display-token-count'
1212
import { search_files } from '@/features/search-files'
13-
import { extract_paths_from_ascii_tree } from '@/utils/ascii-tree'
13+
import { AsciiTree } from '@/utils/ascii-tree'
1414

1515
export const select_commit_files_command = (
1616
workspace_provider: WorkspaceProvider,
@@ -132,7 +132,7 @@ export const select_commit_files_command = (
132132

133133
const files = new Set<string>()
134134

135-
const ascii_paths = extract_paths_from_ascii_tree(files_output)
135+
const ascii_paths = AsciiTree.extract_paths(files_output)
136136
ascii_paths.forEach((p) => files.add(p))
137137

138138
const lines = files_output.split('\n')

‎apps/editor/src/utils/ascii-tree.ts‎

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { build_tree, print_tree } from './utils'
2+
3+
export namespace AsciiTree {
4+
export const generate = (paths: string[]): string => {
5+
const root = build_tree(paths)
6+
return print_tree(root).join('\n')
7+
}
8+
9+
export const extract_paths = (text: string): string[] => {
10+
const found_paths = new Set<string>()
11+
let tree_stack: { level: number; name: string }[] = []
12+
13+
const lines = text.split('\n')
14+
for (const line of lines) {
15+
const idx1 = line.indexOf('├── ')
16+
const idx2 = line.indexOf('└── ')
17+
const idx =
18+
idx1 != -1 && idx2 != -1 ? Math.min(idx1, idx2) : Math.max(idx1, idx2)
19+
20+
if (idx != -1) {
21+
const prefix = line.substring(0, idx)
22+
if (/^[│\s]*$/.test(prefix)) {
23+
let name = line.substring(idx + 4).trim()
24+
if (name.startsWith('`') && name.endsWith('`')) {
25+
name = name.substring(1, name.length - 1).trim()
26+
}
27+
const level = prefix.length
28+
29+
while (
30+
tree_stack.length > 0 &&
31+
tree_stack[tree_stack.length - 1].level >= level
32+
) {
33+
tree_stack.pop()
34+
}
35+
36+
tree_stack.push({ level, name })
37+
38+
const full_path = tree_stack.map((n) => n.name).join('/')
39+
found_paths.add(full_path.replace(/[.?!]+$/, ''))
40+
} else {
41+
tree_stack = []
42+
}
43+
} else {
44+
tree_stack = []
45+
}
46+
}
47+
48+
return Array.from(found_paths)
49+
}
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './ascii-tree'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export interface TreeNode {
2+
[key: string]: TreeNode
3+
}
4+
5+
export const build_tree = (paths: string[]): TreeNode => {
6+
const root: TreeNode = {}
7+
for (const path of paths) {
8+
const parts = path.split('/')
9+
let current = root
10+
for (const part of parts) {
11+
if (!current[part]) {
12+
current[part] = {}
13+
}
14+
current = current[part]
15+
}
16+
}
17+
return root
18+
}
19+
20+
export const print_tree = (node: TreeNode, prefix = ''): string[] => {
21+
const keys = Object.keys(node).sort()
22+
const lines: string[] = []
23+
for (let i = 0; i < keys.length; i++) {
24+
const key = keys[i]
25+
const is_last = i == keys.length - 1
26+
lines.push(`${prefix}${is_last ? '└── ' : '├── '}${key}`)
27+
const child_prefix = prefix + (is_last ? ' ' : '│ ')
28+
lines.push(...print_tree(node[key], child_prefix))
29+
}
30+
return lines
31+
}

‎apps/editor/src/utils/extract-paths-from-text/extract-paths-from-text.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extract_paths_from_ascii_tree } from '../ascii-tree'
1+
import { AsciiTree } from '../ascii-tree'
22

33
export const extract_paths_from_text = (params: {
44
text: string
@@ -14,7 +14,7 @@ export const extract_paths_from_text = (params: {
1414
})
1515
}
1616

17-
const ascii_paths = extract_paths_from_ascii_tree(params.text)
17+
const ascii_paths = AsciiTree.extract_paths(params.text)
1818
ascii_paths.forEach((p) => found_paths.add(p))
1919

2020
const lines = params.text.split('\n')

0 commit comments

Comments
 (0)