Skip to content

Commit 153ee51

Browse files
committed
Refactor ASCII tree path extraction into a shared utility and reuse it for commit file selection
- should retrieve whole commit message and try find&parse ascii tree. Example commit message with an ascii tree: [Pasted text] - move files from ascii tree extraction to util
1 parent e42bbdd commit 153ee51

3 files changed

Lines changed: 61 additions & 40 deletions

File tree

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +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'
1314

1415
export const select_commit_files_command = (
1516
workspace_provider: WorkspaceProvider,
@@ -113,7 +114,7 @@ export const select_commit_files_command = (
113114
last_selected_commit_hash = selected_commit.hash
114115

115116
const files_output = execSync(
116-
`git show --name-only --pretty="" ${selected_commit.hash}`,
117+
`git show --name-only --pretty="format:%B" ${selected_commit.hash}`,
117118
{
118119
cwd: repository.rootUri.fsPath,
119120
encoding: 'utf-8'
@@ -129,12 +130,20 @@ export const select_commit_files_command = (
129130
continue
130131
}
131132

132-
const files = files_output
133-
.split('\n')
134-
.map((f) => f.trim())
135-
.filter((f) => f.length > 0)
133+
const files = new Set<string>()
136134

137-
const valid_files = files
135+
const ascii_paths = extract_paths_from_ascii_tree(files_output)
136+
ascii_paths.forEach((p) => files.add(p))
137+
138+
const lines = files_output.split('\n')
139+
for (const line of lines) {
140+
const trimmed = line.trim()
141+
if (trimmed.length > 0) {
142+
files.add(trimmed)
143+
}
144+
}
145+
146+
const valid_files = Array.from(files)
138147
.map((f) => {
139148
const absolute_path = path.join(repository.rootUri.fsPath, f)
140149
return {

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,45 @@ export const generate_ascii_tree = (paths: string[]): string => {
3434
const root = build_tree(paths)
3535
return print_tree(root).join('\n')
3636
}
37+
38+
export const extract_paths_from_ascii_tree = (text: string): string[] => {
39+
const found_paths = new Set<string>()
40+
let tree_stack: { level: number; name: string }[] = []
41+
42+
const lines = text.split('\n')
43+
for (const line of lines) {
44+
const idx1 = line.indexOf('├── ')
45+
const idx2 = line.indexOf('└── ')
46+
const idx =
47+
idx1 != -1 && idx2 != -1 ? Math.min(idx1, idx2) : Math.max(idx1, idx2)
48+
49+
if (idx != -1) {
50+
const prefix = line.substring(0, idx)
51+
if (/^[│\s]*$/.test(prefix)) {
52+
let name = line.substring(idx + 4).trim()
53+
if (name.startsWith('`') && name.endsWith('`')) {
54+
name = name.substring(1, name.length - 1).trim()
55+
}
56+
const level = prefix.length
57+
58+
while (
59+
tree_stack.length > 0 &&
60+
tree_stack[tree_stack.length - 1].level >= level
61+
) {
62+
tree_stack.pop()
63+
}
64+
65+
tree_stack.push({ level, name })
66+
67+
const full_path = tree_stack.map((n) => n.name).join('/')
68+
found_paths.add(full_path.replace(/[.?!]+$/, ''))
69+
} else {
70+
tree_stack = []
71+
}
72+
} else {
73+
tree_stack = []
74+
}
75+
}
76+
77+
return Array.from(found_paths)
78+
}

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

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { extract_paths_from_ascii_tree } from '../ascii-tree'
2+
13
export const extract_paths_from_text = (params: {
24
text: string
35
workspace_files: string[]
@@ -12,7 +14,8 @@ export const extract_paths_from_text = (params: {
1214
})
1315
}
1416

15-
let tree_stack: { level: number; name: string }[] = []
17+
const ascii_paths = extract_paths_from_ascii_tree(params.text)
18+
ascii_paths.forEach((p) => found_paths.add(p))
1619

1720
const lines = params.text.split('\n')
1821
for (const line of lines) {
@@ -26,39 +29,6 @@ export const extract_paths_from_text = (params: {
2629
found_paths.add(path_str.replace(/[.?!]+$/, ''))
2730
}
2831
}
29-
30-
// ASCII tree extraction
31-
const idx1 = line.indexOf('├── ')
32-
const idx2 = line.indexOf('└── ')
33-
const idx =
34-
idx1 !== -1 && idx2 !== -1 ? Math.min(idx1, idx2) : Math.max(idx1, idx2)
35-
36-
if (idx !== -1) {
37-
const prefix = line.substring(0, idx)
38-
if (/^[│\s]*$/.test(prefix)) {
39-
let name = line.substring(idx + 4).trim()
40-
if (name.startsWith('`') && name.endsWith('`')) {
41-
name = name.substring(1, name.length - 1).trim()
42-
}
43-
const level = prefix.length
44-
45-
while (
46-
tree_stack.length > 0 &&
47-
tree_stack[tree_stack.length - 1].level >= level
48-
) {
49-
tree_stack.pop()
50-
}
51-
52-
tree_stack.push({ level, name })
53-
54-
const full_path = tree_stack.map((n) => n.name).join('/')
55-
found_paths.add(full_path.replace(/[.?!]+$/, ''))
56-
} else {
57-
tree_stack = []
58-
}
59-
} else {
60-
tree_stack = []
61-
}
6232
}
6333

6434
const words = params.text.replace(/`/g, ' ').split(/[\s,;:'"<>()[\]{}]+/)

0 commit comments

Comments
 (0)