Skip to content

Commit 8f0325c

Browse files
committed
Update search-files-for-context to validate directory paths and simplify keyword matching with case-insensitive regex
1 parent 5c32b20 commit 8f0325c

1 file changed

Lines changed: 38 additions & 45 deletions

File tree

‎apps/editor/src/commands/search-files-for-context.ts‎

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ export const search_files_for_context_command = (
1616
return vscode.commands.registerCommand(
1717
'codeWebChat.searchFilesForContext',
1818
async (item: any) => {
19-
const folder_path = item?.resourceUri?.fsPath
19+
let folder_path = item?.resourceUri?.fsPath
20+
21+
if (folder_path) {
22+
try {
23+
const stats = await fs.promises.stat(folder_path)
24+
if (!stats.isDirectory()) {
25+
folder_path = path.dirname(folder_path)
26+
}
27+
} catch (error) {
28+
// If we can't validate the path, ignore it and let logic fall back to workspace roots
29+
folder_path = undefined
30+
}
31+
}
2032
let initial_keywords =
2133
extension_context.workspaceState.get<string>(
2234
LAST_SEARCH_FILES_FOR_CONTEXT_QUERY_STATE_KEY
@@ -86,10 +98,8 @@ export const search_files_for_context_command = (
8698

8799
initial_keywords = keywords_input
88100

89-
const term = keywords_input.trim().toLowerCase()
90-
if (term.length === 0) return
91-
92-
const keywords = [[term]]
101+
const search_term = keywords_input.trim()
102+
if (search_term.length == 0) return
93103

94104
let all_files: string[] = []
95105

@@ -103,10 +113,10 @@ export const search_files_for_context_command = (
103113
}
104114
}
105115

106-
const matched_files = await search_files_by_keywords(
107-
all_files,
108-
keywords
109-
)
116+
const matched_files = await search_files_by_keywords({
117+
files: all_files,
118+
search_term: search_term
119+
})
110120

111121
if (matched_files.length == 0) {
112122
vscode.window.showInformationMessage(
@@ -329,73 +339,56 @@ export const search_files_for_context_command = (
329339
)
330340
}
331341

332-
const search_files_by_keywords = async (
333-
files: string[],
334-
keyword_groups: string[][],
335-
progress?: vscode.Progress<{ message?: string; increment?: number }>,
342+
const search_files_by_keywords = async (params: {
343+
files: string[]
344+
search_term: string
345+
progress?: vscode.Progress<{ message?: string; increment?: number }>
336346
token?: vscode.CancellationToken
337-
): Promise<string[]> => {
347+
}): Promise<string[]> => {
338348
const matched_files: string[] = []
339-
const total = files.length
349+
const total = params.files.length
340350
let processed = 0
341351
const increment = total > 0 ? (1 / total) * 100 : 0
342352

343-
const matchers = keyword_groups.map((group) =>
344-
group.map((k) => {
345-
const term = k
346-
347-
if (!term) return () => false
353+
// Escape special characters and treat multiple spaces as flexible whitespace
354+
const escaped_term = params.search_term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
355+
const pattern = escaped_term.replace(/\s+/g, '\\s+')
356+
const regex = new RegExp(pattern, 'mi') // Case-insensitive, multiline
348357

349-
const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
350-
const pattern = escaped.replace(/\s+/g, '\\s+')
351-
352-
const regex = new RegExp(pattern, 'm')
353-
return (text: string) => regex.test(text)
354-
})
355-
)
356-
357-
for (const file_path of files) {
358-
if (token?.isCancellationRequested) break
358+
for (const file_path of params.files) {
359+
if (params.token?.isCancellationRequested) break
359360

360361
try {
361-
const file_name_lower = path.basename(file_path).toLowerCase()
362-
if (
363-
matchers.some((group) => group.every((match) => match(file_name_lower)))
364-
) {
362+
const file_name = path.basename(file_path)
363+
364+
// Check filename first
365+
if (regex.test(file_name)) {
365366
matched_files.push(file_path)
366-
processed++
367-
progress?.report({
368-
increment,
369-
message: `${processed}/${total}`
370-
})
371367
continue
372368
}
373369

374370
// Skip large files (> 1MB) to avoid performance issues
375371
const stats = await fs.promises.stat(file_path)
376372
if (stats.size > 1024 * 1024) {
377373
processed++
378-
progress?.report({
374+
params.progress?.report({
379375
increment,
380376
message: `${processed}/${total}`
381377
})
382378
continue
383379
}
384380

385381
const content = await fs.promises.readFile(file_path, 'utf-8')
386-
const content_lower = content.toLowerCase()
387382

388-
if (
389-
matchers.some((group) => group.every((match) => match(content_lower)))
390-
) {
383+
if (regex.test(content)) {
391384
matched_files.push(file_path)
392385
}
393386
} catch (error) {
394387
// Ignore read errors (binary files, permissions, etc.)
395388
}
396389

397390
processed++
398-
progress?.report({
391+
params.progress?.report({
399392
increment,
400393
message: `${processed}/${total}`
401394
})

0 commit comments

Comments
 (0)