Skip to content

Commit 9cb3041

Browse files
committed
Enhance extract_paths_from_text to use an options object and improve matching for nested paths and filenames
- error message of a running app returns paths relative to their base folder, not the base folder of the repository, e.g. [Pasted text] - [Fragment] should accept params variable, don't destructure inside
1 parent 39a43cf commit 9cb3041

4 files changed

Lines changed: 85 additions & 19 deletions

File tree

‎apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/relevant-files-parser/relevant-files-parser.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export const parse_relevant_files = (params: {
2020
return null
2121
}
2222

23-
const valid_paths = extract_paths_from_text(
24-
params.response,
25-
params.workspace_files
26-
)
23+
const valid_paths = extract_paths_from_text({
24+
text: params.response,
25+
workspace_files: params.workspace_files
26+
})
2727

2828
if (valid_paths.length == 0) {
2929
return null

‎apps/editor/src/commands/select-clipboard-paths-command.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export const select_clipboard_paths_command = (
2828
workspace_provider
2929
})
3030

31-
const valid_paths = extract_paths_from_text(text, workspace_files)
31+
const valid_paths = extract_paths_from_text({
32+
text,
33+
workspace_files
34+
})
3235

3336
if (valid_paths.length === 0) {
3437
vscode.window.showInformationMessage(

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('extract_paths_from_text', () => {
1010
Lorem ipsum.`
1111

1212
const workspace_files = ['src/hello.ts', 'src/welcome.ts']
13-
const result = extract_paths_from_text(text, workspace_files)
13+
const result = extract_paths_from_text({ text, workspace_files })
1414

1515
expect(result).toContain('src/hello.ts')
1616
expect(result).toContain('src/welcome.ts')
@@ -22,7 +22,7 @@ Lorem ipsum.`
2222
Lorem ipsum.`
2323

2424
const workspace_files = ['src/hello.ts', 'src/welcome.ts', 'src/main.ts']
25-
const result = extract_paths_from_text(text, workspace_files)
25+
const result = extract_paths_from_text({ text, workspace_files })
2626

2727
expect(result).toContain('src/hello.ts')
2828
expect(result).toContain('src/welcome.ts')
@@ -32,7 +32,7 @@ Lorem ipsum.`
3232
it('strips trailing punctuation', () => {
3333
const text = 'Look at src/utils.ts, src/types.ts! Also see src/index.ts.'
3434
const workspace_files = ['src/utils.ts', 'src/types.ts', 'src/index.ts']
35-
const result = extract_paths_from_text(text, workspace_files)
35+
const result = extract_paths_from_text({ text, workspace_files })
3636

3737
expect(result).toContain('src/utils.ts')
3838
expect(result).toContain('src/types.ts')
@@ -48,11 +48,40 @@ Lorem ipsum.`
4848
'.gitignore',
4949
'src/main.ts'
5050
]
51-
const result = extract_paths_from_text(text, workspace_files)
51+
const result = extract_paths_from_text({ text, workspace_files })
5252

5353
expect(result).toContain('package.json')
5454
expect(result).toContain('README.md')
5555
expect(result).toContain('.gitignore')
5656
expect(result).not.toContain('src/main.ts')
5757
})
58+
59+
it('extracts nested, relative paths with line and column numbers', () => {
60+
const text =
61+
'`./src/features/checkpoints/actions/restore-checkpoint.ts:56:9`'
62+
const workspace_files = [
63+
'apps/editor/src/features/checkpoints/actions/restore-checkpoint.ts',
64+
'apps/server/src/features/checkpoints/actions/restore-checkpoint.ts'
65+
]
66+
const result = extract_paths_from_text({ text, workspace_files })
67+
68+
expect(result).toContain(
69+
'apps/editor/src/features/checkpoints/actions/restore-checkpoint.ts'
70+
)
71+
expect(result).toContain(
72+
'apps/server/src/features/checkpoints/actions/restore-checkpoint.ts'
73+
)
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+
83+
expect(result).toContain(
84+
'apps/editor/src/features/checkpoints/actions/restore-checkpoint.ts'
85+
)
86+
})
5887
})
Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
export const extract_paths_from_text = (
2-
text: string,
1+
export const extract_paths_from_text = (params: {
2+
text: string
33
workspace_files: string[]
4-
): string[] => {
4+
}): string[] => {
55
const found_paths = new Set<string>()
6-
const workspace_files_set = new Set(workspace_files)
6+
const workspace_files_set = new Set(params.workspace_files)
77

8-
const inline_matches = text.match(/`([^`]+)`/g)
8+
const inline_matches = params.text.match(/`([^`]+)`/g)
99
if (inline_matches) {
1010
inline_matches.forEach((match) => {
1111
found_paths.add(match.replace(/`/g, '').trim())
1212
})
1313
}
1414

15-
const lines = text.split('\n')
15+
const lines = params.text.split('\n')
1616
for (const line of lines) {
1717
const trimmed = line.trim()
1818
if (trimmed.startsWith('-') || trimmed.startsWith('*')) {
@@ -26,15 +26,49 @@ export const extract_paths_from_text = (
2626
}
2727
}
2828

29-
const words = text.replace(/`/g, ' ').split(/[\s,;:'"<>()[\]{}]+/)
29+
const words = params.text.replace(/`/g, ' ').split(/[\s,;:'"<>()[\]{}]+/)
3030
for (const word of words) {
3131
if (word) {
3232
const cleaned = word.trim().replace(/[.?!]+$/, '')
3333
found_paths.add(cleaned)
3434
}
3535
}
3636

37-
return Array.from(found_paths)
38-
.filter((p) => workspace_files_set.has(p))
39-
.sort((a, b) => text.indexOf(a) - text.indexOf(b))
37+
const matched_files_with_index = new Map<string, number>()
38+
39+
for (const original_p of found_paths) {
40+
let p = original_p
41+
if (p.startsWith('./')) {
42+
p = p.substring(2)
43+
}
44+
45+
if (!p) continue
46+
47+
let idx = params.text.indexOf(original_p)
48+
if (idx == -1) {
49+
idx = params.text.indexOf(p)
50+
}
51+
const safe_idx = idx != -1 ? idx : Infinity
52+
53+
if (workspace_files_set.has(p)) {
54+
const current_idx = matched_files_with_index.get(p) ?? Infinity
55+
matched_files_with_index.set(p, Math.min(current_idx, safe_idx))
56+
} else {
57+
for (const wf of params.workspace_files) {
58+
if (wf.endsWith(`/${p}`)) {
59+
const current_idx = matched_files_with_index.get(wf) ?? Infinity
60+
matched_files_with_index.set(wf, Math.min(current_idx, safe_idx))
61+
}
62+
}
63+
}
64+
}
65+
66+
return Array.from(matched_files_with_index.keys()).sort((a, b) => {
67+
const idx_a = matched_files_with_index.get(a) ?? Infinity
68+
const idx_b = matched_files_with_index.get(b) ?? Infinity
69+
if (idx_a == idx_b) {
70+
return a.localeCompare(b)
71+
}
72+
return idx_a - idx_b
73+
})
4074
}

0 commit comments

Comments
 (0)