Skip to content

Commit e42bbdd

Browse files
committed
Add support for extracting file paths from ASCII tree structures
- `apps/editor/src/utils/extract-paths-from-text/extract-paths-from-text.ts`should be able to parse ascii trees generated with `apps/editor/src/utils/ascii-tree.ts` - remove support for selecting relative paths
1 parent ad1ee00 commit e42bbdd

2 files changed

Lines changed: 77 additions & 30 deletions

File tree

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

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,40 +56,59 @@ Lorem ipsum.`
5656
expect(result).not.toContain('src/main.ts')
5757
})
5858

59-
it('extracts nested, relative paths with line and column numbers', () => {
60-
const text =
61-
'`./src/features/checkpoints/actions/restore-checkpoint.ts:56:9`'
59+
it('extracts paths starting with a slash', () => {
60+
const text = 'Look at /src/hello.ts'
61+
const workspace_files = ['src/hello.ts']
62+
const result = extract_paths_from_text({ text, workspace_files })
63+
64+
expect(result).toContain('src/hello.ts')
65+
})
66+
67+
it('extracts paths from an ASCII tree', () => {
68+
const text = `Here is the project structure:
69+
└── apps
70+
├── editor
71+
│ └── src
72+
│ ├── features
73+
│ │ └── checkpoints
74+
│ │ └── actions
75+
│ │ └── restore-checkpoint.ts
76+
│ └── utils
77+
│ ├── ascii-tree.ts
78+
│ └── extract-paths-from-text
79+
│ ├── extract-paths-from-text.spec.ts
80+
│ └── extract-paths-from-text.ts
81+
└── server
82+
└── src
83+
└── features
84+
└── checkpoints
85+
└── actions
86+
└── restore-checkpoint.ts`
87+
6288
const workspace_files = [
6389
'apps/editor/src/features/checkpoints/actions/restore-checkpoint.ts',
64-
'apps/server/src/features/checkpoints/actions/restore-checkpoint.ts'
90+
'apps/editor/src/utils/ascii-tree.ts',
91+
'apps/editor/src/utils/extract-paths-from-text/extract-paths-from-text.spec.ts',
92+
'apps/editor/src/utils/extract-paths-from-text/extract-paths-from-text.ts',
93+
'apps/server/src/features/checkpoints/actions/restore-checkpoint.ts',
94+
'other-file.ts'
6595
]
96+
6697
const result = extract_paths_from_text({ text, workspace_files })
6798

6899
expect(result).toContain(
69100
'apps/editor/src/features/checkpoints/actions/restore-checkpoint.ts'
70101
)
102+
expect(result).toContain('apps/editor/src/utils/ascii-tree.ts')
71103
expect(result).toContain(
72-
'apps/server/src/features/checkpoints/actions/restore-checkpoint.ts'
104+
'apps/editor/src/utils/extract-paths-from-text/extract-paths-from-text.spec.ts'
73105
)
74-
})
75-
76-
it('extracts path by filename', () => {
77-
const text = 'Edit the restore-checkpoint.ts file.'
78-
const workspace_files = [
79-
'apps/editor/src/features/checkpoints/actions/restore-checkpoint.ts'
80-
]
81-
const result = extract_paths_from_text({ text, workspace_files })
82-
83106
expect(result).toContain(
84-
'apps/editor/src/features/checkpoints/actions/restore-checkpoint.ts'
107+
'apps/editor/src/utils/extract-paths-from-text/extract-paths-from-text.ts'
85108
)
86-
})
87-
88-
it('extracts paths starting with a slash', () => {
89-
const text = 'Look at /src/hello.ts'
90-
const workspace_files = ['src/hello.ts']
91-
const result = extract_paths_from_text({ text, workspace_files })
92-
93-
expect(result).toContain('src/hello.ts')
109+
expect(result).toContain(
110+
'apps/server/src/features/checkpoints/actions/restore-checkpoint.ts'
111+
)
112+
expect(result).not.toContain('other-file.ts')
94113
})
95114
})

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export const extract_paths_from_text = (params: {
1212
})
1313
}
1414

15+
let tree_stack: { level: number; name: string }[] = []
16+
1517
const lines = params.text.split('\n')
1618
for (const line of lines) {
1719
const trimmed = line.trim()
@@ -24,6 +26,39 @@ export const extract_paths_from_text = (params: {
2426
found_paths.add(path_str.replace(/[.?!]+$/, ''))
2527
}
2628
}
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+
}
2762
}
2863

2964
const words = params.text.replace(/`/g, ' ').split(/[\s,;:'"<>()[\]{}]+/)
@@ -55,13 +90,6 @@ export const extract_paths_from_text = (params: {
5590
if (workspace_files_set.has(p)) {
5691
const current_idx = matched_files_with_index.get(p) ?? Infinity
5792
matched_files_with_index.set(p, Math.min(current_idx, safe_idx))
58-
} else {
59-
for (const wf of params.workspace_files) {
60-
if (wf.endsWith(`/${p}`)) {
61-
const current_idx = matched_files_with_index.get(wf) ?? Infinity
62-
matched_files_with_index.set(wf, Math.min(current_idx, safe_idx))
63-
}
64-
}
6593
}
6694
}
6795

0 commit comments

Comments
 (0)