Skip to content

Commit d993d8d

Browse files
committed
Replace manual file search with VS Code's findFiles API for improved performance and filename-only matching
1 parent d35a54a commit d993d8d

4 files changed

Lines changed: 980 additions & 12 deletions

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
Refactoring `search_files_for_context_command` to utilize `vscode.workspace.findFiles` significantly improves performance by leveraging VS Code's internal file indexing, replacing the previous manual `fs` iteration.
2+
3+
> **Note:** This change shifts the behavior to **filename-only search** (glob-based). It will no longer grep the content of files, as `vscode.workspace.findFiles` searches paths.
4+
5+
### Updated file: `apps/editor/src/commands/search-files-for-context.ts`
6+
7+
```diff
8+
--- a/apps/editor/src/commands/search-files-for-context.ts
9+
+++ b/apps/editor/src/commands/search-files-for-context.ts
10+
@@ -2,5 +2,4 @@
11+
import * as path from 'path'
12+
-import * as fs from 'fs'
13+
import { WorkspaceProvider } from '../context/providers/workspace/workspace-provider'
14+
import { Logger } from '@shared/utils/logger'
15+
@@ -112,65 +111,26 @@
16+
initial_keywords = keywords_input
17+
18+
- const term = keywords_input.trim().toLowerCase()
19+
+ const term = keywords_input.trim()
20+
if (term.length === 0) return
21+
22+
- const keywords = [[term]]
23+
-
24+
- let all_files: string[] = []
25+
-
26+
- if (folder_path) {
27+
- all_files = await workspace_provider.find_all_files(folder_path)
28+
- } else {
29+
- const roots = workspace_provider.get_workspace_roots()
30+
- for (const root of roots) {
31+
- const files = await workspace_provider.find_all_files(root)
32+
- all_files.push(...files)
33+
- }
34+
- }
35+
-
36+
- const matched_files = await search_files_by_keywords(
37+
- all_files,
38+
- keywords
39+
- )
40+
+ // If the user input contains a wildcard, treat it as a glob.
41+
+ // Otherwise, wrap it to search for any file containing the term.
42+
+ const glob_pattern = term.includes('*') ? term : `**/*${term}*`
43+
+ let matched_uris: vscode.Uri[] = []
44+
+
45+
+ if (folder_path) {
46+
+ const relative_pattern = new vscode.RelativePattern(
47+
+ folder_path,
48+
+ glob_pattern
49+
+ )
50+
+ matched_uris = await vscode.workspace.findFiles(relative_pattern)
51+
+ } else {
52+
+ matched_uris = await vscode.workspace.findFiles(glob_pattern)
53+
+ }
54+
+
55+
+ const matched_files = matched_uris.map((uri) => uri.fsPath)
56+
57+
if (matched_files.length == 0) {
58+
vscode.window.showInformationMessage(
59+
- 'No files found containing these keywords.'
60+
+ 'No files found matching this pattern.'
61+
)
62+
continue
63+
@@ -321,57 +281,2 @@
64+
}
65+
)
66+
}
67+
-
68+
-
69+
-const search_files_by_keywords = async (
70+
- files: string[],
71+
- keyword_groups: string[][],
72+
- progress?: vscode.Progress<{ message?: string; increment?: number }>,
73+
- token?: vscode.CancellationToken
74+
-): Promise<string[]> => {
75+
- const matched_files: string[] = []
76+
- const total = files.length
77+
- let processed = 0
78+
- const increment = total > 0 ? (1 / total) * 100 : 0
79+
-
80+
-
81+
- const matchers = keyword_groups.map((group) =>
82+
- group.map((k) => {
83+
- const term = k
84+
-
85+
-
86+
- if (!term) return () => false
87+
-
88+
-
89+
- const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
90+
- const pattern = escaped.replace(/\s+/g, '\\s+')
91+
-
92+
-
93+
- const regex = new RegExp(pattern, 'm')
94+
- return (text: string) => regex.test(text)
95+
- })
96+
- )
97+
-
98+
-
99+
- for (const file_path of files) {
100+
- if (token?.isCancellationRequested) break
101+
-
102+
-
103+
- try {
104+
- const file_name_lower = path.basename(file_path).toLowerCase()
105+
- if (
106+
- matchers.some((group) => group.every((match) => match(file_name_lower)))
107+
- ) {
108+
- matched_files.push(file_path)
109+
- processed++
110+
- progress?.report({
111+
- increment,
112+
- message: `${processed}/${total}`
113+
- })
114+
- continue
115+
- }
116+
-
117+
-
118+
- // Skip large files (> 1MB) to avoid performance issues
119+
- const stats = await fs.promises.stat(file_path)
120+
- if (stats.size > 1024 * 1024) {
121+
- processed++
122+
- progress?.report({
123+
- increment,
124+
- message: `${processed}/${total}`
125+
- })
126+
- continue
127+
- }
128+
-
129+
-
130+
- const content = await fs.promises.readFile(file_path, 'utf-8')
131+
- const content_lower = content.toLowerCase()
132+
-
133+
-
134+
- if (
135+
- matchers.some((group) => group.every((match) => match(content_lower)))
136+
- ) {
137+
- matched_files.push(file_path)
138+
- }
139+
- } catch (error) {
140+
- // Ignore read errors (binary files, permissions, etc.)
141+
- }
142+
-
143+
-
144+
- processed++
145+
- progress?.report({
146+
- increment,
147+
- message: `${processed}/${total}`
148+
- })
149+
- }
150+
-
151+
-
152+
- return matched_files
153+
-}
154+
155+
156+
```

0 commit comments

Comments
 (0)