Skip to content

Commit 87af086

Browse files
committed
Integrate workspace file filtering into path extraction and update relevant file parsing logic for code blocks
- we don't need to support relative paths, as workspace files are always absolute
1 parent 9da6811 commit 87af086

6 files changed

Lines changed: 58 additions & 29 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ export const parse_relevant_files = (params: {
99
response: string
1010
workspace_files: string[]
1111
}): RelevantFilesItem | null => {
12-
if (params.response.includes('```')) {
12+
if (
13+
params.response.includes('```') &&
14+
!params.response.trimStart().startsWith('**Relevant files:**')
15+
) {
1316
return null
1417
}
1518

16-
const found_paths = extract_paths_from_text(params.response)
17-
18-
const valid_paths = found_paths
19-
.filter((path) => params.workspace_files.includes(path))
20-
.sort((a, b) => params.response.indexOf(a) - params.response.indexOf(b))
19+
const valid_paths = extract_paths_from_text(
20+
params.response,
21+
params.workspace_files
22+
)
2123

2224
if (valid_paths.length == 0) {
2325
return null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
**Relevant files:**
2+
3+
`src/hello.ts`
4+
5+
```typescript
6+
const x = 1;
7+
```

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ describe('relevant files parser', () => {
2020

2121
expect(result).toBeNull()
2222
})
23+
24+
it('accepts when including code blocks if it starts with **Relevant files:**', () => {
25+
const test_case = 'code-blocks-valid'
26+
const text = load_test_case_file(test_case, 'relevant-files.txt')
27+
const result = parse_relevant_files({
28+
response: text,
29+
workspace_files: ['src/hello.ts', 'src/main.ts']
30+
})
31+
32+
expect(result).not.toBeNull()
33+
expect(result?.file_paths).toEqual(['src/hello.ts'])
34+
})
2335
})

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ export const select_clipboard_paths_command = (
2828
workspace_provider
2929
})
3030

31-
const found_paths = extract_paths_from_text(text)
32-
33-
const valid_paths = found_paths
34-
.filter((p) => workspace_files.includes(p))
35-
.sort((a, b) => text.indexOf(a) - text.indexOf(b))
31+
const valid_paths = extract_paths_from_text(text, workspace_files)
3632

3733
if (valid_paths.length === 0) {
3834
vscode.window.showInformationMessage(

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ describe('extract_paths_from_text', () => {
99
1010
Lorem ipsum.`
1111

12-
const result = extract_paths_from_text(text)
12+
const workspace_files = ['src/hello.ts', 'src/welcome.ts']
13+
const result = extract_paths_from_text(text, workspace_files)
1314

1415
expect(result).toContain('src/hello.ts')
1516
expect(result).toContain('src/welcome.ts')
@@ -20,29 +21,38 @@ Lorem ipsum.`
2021
2122
Lorem ipsum.`
2223

23-
const result = extract_paths_from_text(text)
24+
const workspace_files = ['src/hello.ts', 'src/welcome.ts', 'src/main.ts']
25+
const result = extract_paths_from_text(text, workspace_files)
2426

2527
expect(result).toContain('src/hello.ts')
2628
expect(result).toContain('src/welcome.ts')
2729
expect(result).toContain('src/main.ts')
2830
})
2931

30-
it('handles paths with ./ prefix', () => {
31-
const text = 'Please check ./src/main.ts and ./tests/main.spec.ts.'
32-
const result = extract_paths_from_text(text)
33-
34-
expect(result).toContain('./src/main.ts')
35-
expect(result).toContain('src/main.ts')
36-
expect(result).toContain('./tests/main.spec.ts')
37-
expect(result).toContain('tests/main.spec.ts')
38-
})
39-
4032
it('strips trailing punctuation', () => {
4133
const text = 'Look at src/utils.ts, src/types.ts! Also see src/index.ts.'
42-
const result = extract_paths_from_text(text)
34+
const workspace_files = ['src/utils.ts', 'src/types.ts', 'src/index.ts']
35+
const result = extract_paths_from_text(text, workspace_files)
4336

4437
expect(result).toContain('src/utils.ts')
4538
expect(result).toContain('src/types.ts')
4639
expect(result).toContain('src/index.ts')
4740
})
41+
42+
it('extracts files in the root directory', () => {
43+
const text =
44+
'Check out package.json, README.md, and .gitignore for project configurations.'
45+
const workspace_files = [
46+
'package.json',
47+
'README.md',
48+
'.gitignore',
49+
'src/main.ts'
50+
]
51+
const result = extract_paths_from_text(text, workspace_files)
52+
53+
expect(result).toContain('package.json')
54+
expect(result).toContain('README.md')
55+
expect(result).toContain('.gitignore')
56+
expect(result).not.toContain('src/main.ts')
57+
})
4858
})

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
export const extract_paths_from_text = (text: string): string[] => {
1+
export const extract_paths_from_text = (
2+
text: string,
3+
workspace_files: string[]
4+
): string[] => {
25
const found_paths = new Set<string>()
6+
const workspace_files_set = new Set(workspace_files)
37

48
const inline_matches = text.match(/`([^`]+)`/g)
59
if (inline_matches) {
@@ -27,12 +31,10 @@ export const extract_paths_from_text = (text: string): string[] => {
2731
if (word) {
2832
const cleaned = word.trim().replace(/[.?!]+$/, '')
2933
found_paths.add(cleaned)
30-
31-
if (cleaned.startsWith('./')) {
32-
found_paths.add(cleaned.substring(2))
33-
}
3434
}
3535
}
3636

3737
return Array.from(found_paths)
38+
.filter((p) => workspace_files_set.has(p))
39+
.sort((a, b) => text.indexOf(a) - text.indexOf(b))
3840
}

0 commit comments

Comments
 (0)