Skip to content

Commit de45aec

Browse files
committed
Refactor path searching to correctly handle queries containing dots, enabling fuzzy matching on file extensions and partial names
1 parent a29d30a commit de45aec

3 files changed

Lines changed: 45 additions & 21 deletions

File tree

‎packages/shared/src/utils/search-paths/search-paths.spec.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,37 @@ describe('search_paths', () => {
5555
expect(result[0]).toBe('src/routes/HomePage.tsx')
5656
})
5757

58+
it('should find "src/routes/HomePage.tsx" with query "page.tsx"', () => {
59+
const result = search_paths({ paths, search_value: 'page.tsx' })
60+
expect(result).toHaveLength(1)
61+
expect(result[0]).toBe('src/routes/HomePage.tsx')
62+
})
63+
64+
it('should find "src/routes/HomePage.tsx" with query "rout."', () => {
65+
const result = search_paths({ paths, search_value: 'rout.' })
66+
expect(result).toHaveLength(1)
67+
expect(result[0]).toBe('src/routes/HomePage.tsx')
68+
})
69+
70+
it('should find "src/routes/HomePage.tsx" with query "pag."', () => {
71+
const result = search_paths({ paths, search_value: 'pag.' })
72+
expect(result).toHaveLength(1)
73+
expect(result[0]).toBe('src/routes/HomePage.tsx')
74+
})
75+
76+
it('should find "src/routes/HomePage.tsx" with query "hom."', () => {
77+
const result = search_paths({ paths, search_value: 'hom.' })
78+
expect(result).toHaveLength(1)
79+
expect(result[0]).toBe('src/routes/HomePage.tsx')
80+
})
81+
82+
83+
84+
85+
86+
87+
88+
5889
it('should not find "src/routes/HomePage.tsx" with query "src-page"', () => {
5990
const result = search_paths({ paths, search_value: 'src-page' })
6091
expect(result).toHaveLength(0)
@@ -65,11 +96,6 @@ describe('search_paths', () => {
6596
expect(result).toHaveLength(0)
6697
})
6798

68-
it('should not find "src/routes/HomePage.tsx" with query "page."', () => {
69-
const result = search_paths({ paths, search_value: 'page.' })
70-
expect(result).toHaveLength(0)
71-
})
72-
7399
it('should not find "src/routes/HomePage.tsx" with query "src/"', () => {
74100
const result = search_paths({ paths, search_value: 'src/' })
75101
expect(result).toHaveLength(0)

‎packages/shared/src/utils/search-paths/search-paths.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const search_paths = (params: {
8282
if (!params.search_value) {
8383
return params.paths
8484
}
85-
if (/[/\\\.]$/.test(params.search_value)) {
85+
if (/[/\\]$/.test(params.search_value)) {
8686
return []
8787
}
8888

@@ -95,13 +95,17 @@ export const search_paths = (params: {
9595
return { path: path_item, score: 0 }
9696
}
9797

98-
if (/[^a-z0-9]/.test(search_lower)) {
98+
if (/[^a-z0-9.]/.test(search_lower)) {
9999
return { path: path_item, score: -1 }
100100
}
101101

102+
const fuzzy_query = search_lower.replace(/\./g, '')
103+
if (fuzzy_query.length === 0) {
104+
return { path: path_item, score: -1 }
105+
}
102106
const parts = get_path_parts(path_item)
103107
const score = get_match_score({
104-
query: search_lower,
108+
query: fuzzy_query,
105109
parts,
106110
part_idx: 0,
107111
memo: {}

‎packages/ui/src/components/editor/panel/PromptField/utils/get-file-match-hint-data.ts‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,14 @@ export const get_file_match_hint_data = (
3434
const last_word = value.trim().split(/\s+/).pop()
3535

3636
if (last_word && last_word.length >= 3) {
37-
const search_value = last_word.endsWith('.')
38-
? last_word.slice(0, -1)
39-
: last_word
40-
41-
if (search_value.length >= 3) {
42-
const matching_paths = search_paths({
43-
paths: context_file_paths,
44-
search_value: search_value
45-
})
46-
if (matching_paths.length === 1) {
47-
return { word: last_word, path: matching_paths[0] }
48-
}
37+
const matching_paths = search_paths({
38+
paths: context_file_paths,
39+
search_value: last_word
40+
})
41+
if (matching_paths.length == 1) {
42+
return { word: last_word, path: matching_paths[0] }
4943
}
5044
}
5145

5246
return null
53-
}
47+
}

0 commit comments

Comments
 (0)