Skip to content

Commit 2de2b56

Browse files
committed
Support custom file content retrieval in file search functions
- current behavior: user can search in selected results of 'unstaged files' quick pick, but when using 'intelligent', diff patches are not included. Expected behavior: diff patches are included. └── apps └── editor └── src ├── commands │ ├── generate-commit-message-commands │ │ ├── actions │ │ │ ├── get-prompt-data.ts │ │ │ └── run-generate-action.ts │ │ ├── generate-commit-message-commands.ts │ │ └── utils │ │ └── build-commit-message-prompt.ts │ ├── search-files-commands │ │ ├── search-files-commands.ts │ │ └── utils │ │ └── prompt-for-provided-results.ts │ ├── select-clipboard-paths-command.ts │ ├── select-files-of-commit-command.ts │ ├── select-imported-files-commands │ │ └── utils │ │ └── prompt-for-imported-files.ts │ ├── select-modified-files-command.ts │ └── select-referencing-files-commands │ └── utils │ └── prompt-for-referencing-files.ts ├── constants │ ├── edit-format-instructions.ts │ └── values.ts ├── features │ ├── agentic-search │ │ └── agentic-search.ts │ └── search-files │ ├── index.ts │ ├── search-files.ts │ ├── search-modes │ │ ├── perform-intelligent-search-mode.ts │ │ ├── perform-keywords-search-mode.ts │ │ └── perform-phrase-search-mode.ts │ └── utils │ ├── analyze-files.ts │ ├── check-matches-condition.ts │ ├── create-search-regex.ts │ ├── prompt-for-config.ts │ ├── prompt-for-intelligent-search-results.ts │ ├── prompt-for-keywords-match-mode.ts │ ├── prompt-for-keywords-target.ts │ ├── prompt-for-search-mode.ts │ ├── prompt-for-search-results.ts │ ├── prompt-for-search-term.ts │ ├── prompt-for-shrink-mode.ts │ ├── search-files-by-intelligent.ts │ ├── search-files-by-keywords.ts │ ├── search-files-by-phrase.ts │ └── show-search-results-quick-pick.ts ├── i18n │ └── translations │ ├── commands │ │ └── generate-commit-message-command.ts │ └── common.ts ├── utils │ ├── files-collector.ts │ ├── git-repository-utils.ts │ └── prompt-builder.ts └── views └── prompt ├── backend │ ├── message-handlers │ │ ├── handle-autofill.ts │ │ ├── handle-copy-prompt.ts │ │ ├── handle-make-api-call.ts │ │ ├── handle-preview-prompt.ts │ │ ├── handle-preview-web-configuration.ts │ │ ├── handle-select-edit-format-instructions.ts │ │ └── utils │ │ ├── build-cli-prompt.ts │ │ └── build-prompt-payload.ts │ └── utils │ └── symbols │ └── replace-symbols.ts └── frontend └── Main └── MainView └── MainView.tsx
1 parent c8961ec commit 2de2b56

8 files changed

Lines changed: 85 additions & 30 deletions

File tree

‎apps/editor/src/features/search-files/search-files.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const search_files = async (params: {
2222
is_search_in_selected?: boolean
2323
folder_path?: string
2424
is_workspace_action?: boolean
25+
get_file_content?: (file_path: string) => Promise<string | undefined>
2526
}): Promise<
2627
| { selected_paths: string[]; matched_paths: string[]; title: string }
2728
| undefined
@@ -53,7 +54,8 @@ export const search_files = async (params: {
5354
show_back_button: true,
5455
is_sub_search: true,
5556
is_search_in_selected: params.is_search_in_selected,
56-
folder_path: params.folder_path
57+
folder_path: params.folder_path,
58+
get_file_content: params.get_file_content
5759
})
5860
}
5961

@@ -87,7 +89,8 @@ export const search_files = async (params: {
8789
show_back_button: params.show_back_button,
8890
search_in_results,
8991
is_search_in_selected: params.is_search_in_selected,
90-
is_sub_search: params.is_sub_search
92+
is_sub_search: params.is_sub_search,
93+
get_file_content: params.get_file_content
9194
}
9295

9396
if (search_mode == 'phrase') {
@@ -105,7 +108,8 @@ export const search_files = async (params: {
105108
search_in_results,
106109
is_search_in_selected: params.is_search_in_selected,
107110
is_sub_search: params.is_sub_search,
108-
folder_path: params.folder_path
111+
folder_path: params.folder_path,
112+
get_file_content: params.get_file_content
109113
})
110114
}
111115

‎apps/editor/src/features/search-files/search-modes/perform-intelligent-search-mode.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const perform_intelligent_search_mode = async (params: {
3838
is_search_in_selected?: boolean
3939
is_sub_search?: boolean
4040
folder_path?: string
41+
get_file_content?: (file_path: string) => Promise<string | undefined>
4142
}): Promise<
4243
| { selected_paths: string[]; matched_paths: string[]; title: string }
4344
| undefined
@@ -85,7 +86,8 @@ export const perform_intelligent_search_mode = async (params: {
8586

8687
const analysis = await analyze_files({
8788
workspace_provider: params.workspace_provider,
88-
files: params.files
89+
files: params.files,
90+
get_file_content: params.get_file_content
8991
})
9092

9193
let go_back_to_term = false

‎apps/editor/src/features/search-files/search-modes/perform-keywords-search-mode.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const perform_keywords_search_mode = async (params: {
2727
>
2828
is_search_in_selected?: boolean
2929
is_sub_search?: boolean
30+
get_file_content?: (file_path: string) => Promise<string | undefined>
3031
}): Promise<
3132
| { selected_paths: string[]; matched_paths: string[]; title: string }
3233
| undefined
@@ -146,7 +147,8 @@ export const perform_keywords_search_mode = async (params: {
146147
files,
147148
search_term,
148149
keywords_target,
149-
keywords_match_mode
150+
keywords_match_mode,
151+
get_file_content: params.get_file_content
150152
})
151153
} else {
152154
matched_files = await vscode.window.withProgress(
@@ -165,7 +167,8 @@ export const perform_keywords_search_mode = async (params: {
165167
keywords_target,
166168
keywords_match_mode,
167169
progress,
168-
token
170+
token,
171+
get_file_content: params.get_file_content
169172
})
170173
}
171174
)

‎apps/editor/src/features/search-files/search-modes/perform-phrase-search-mode.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const perform_phrase_search_mode = async (params: {
2020
>
2121
is_search_in_selected?: boolean
2222
is_sub_search?: boolean
23+
get_file_content?: (file_path: string) => Promise<string | undefined>
2324
}): Promise<
2425
| { selected_paths: string[]; matched_paths: string[]; title: string }
2526
| undefined
@@ -78,7 +79,8 @@ export const perform_phrase_search_mode = async (params: {
7879
if (is_fast_search) {
7980
matched_files = await search_files_by_phrase({
8081
files,
81-
search_term
82+
search_term,
83+
get_file_content: params.get_file_content
8284
})
8385
} else {
8486
matched_files = await vscode.window.withProgress(
@@ -95,7 +97,8 @@ export const perform_phrase_search_mode = async (params: {
9597
files,
9698
search_term,
9799
progress,
98-
token
100+
token,
101+
get_file_content: params.get_file_content
99102
})
100103
}
101104
)

‎apps/editor/src/features/search-files/utils/analyze-files.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface FileAnalysisResult {
2121
export const analyze_files = async (params: {
2222
workspace_provider: WorkspaceProvider
2323
files: string[]
24+
get_file_content?: (file_path: string) => Promise<string | undefined>
2425
}): Promise<FileAnalysisResult> => {
2526
let full_tokens = 0
2627
let shrink_tokens = 0
@@ -37,13 +38,21 @@ export const analyze_files = async (params: {
3738
async () => {
3839
for (const file_path of params.files) {
3940
try {
40-
const stats = await fs.promises.stat(file_path)
41-
if (stats.size > 1024 * 1024) continue
41+
let content: string | undefined
42+
if (params.get_file_content) {
43+
content = await params.get_file_content(file_path)
44+
}
45+
46+
if (content === undefined) {
47+
const stats = await fs.promises.stat(file_path)
48+
if (stats.size > 1024 * 1024) continue
4249

43-
const buffer = await fs.promises.readFile(file_path)
44-
if (buffer.includes(0)) continue
50+
const buffer = await fs.promises.readFile(file_path)
51+
if (buffer.includes(0)) continue
52+
53+
content = buffer.toString('utf8')
54+
}
4555

46-
const content = buffer.toString('utf8')
4756
const shrunk_content = shrink_file(content, path.extname(file_path))
4857

4958
const workspace_root =

‎apps/editor/src/features/search-files/utils/search-files-by-keywords.ts‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const search_files_by_keywords = async (params: {
1313
keywords_match_mode?: 'all' | 'some'
1414
progress?: vscode.Progress<{ message?: string; increment?: number }>
1515
token?: vscode.CancellationToken
16+
get_file_content?: (file_path: string) => Promise<string | undefined>
1617
}): Promise<string[]> => {
1718
const matched_files: string[] = []
1819

@@ -62,31 +63,44 @@ export const search_files_by_keywords = async (params: {
6263
continue
6364
}
6465

65-
const stats = await fs.promises.stat(file_path)
6666
let text_to_check = ''
67+
let content: string | undefined
68+
if (params.get_file_content) {
69+
content = await params.get_file_content(file_path)
70+
}
6771

68-
if (stats.size > 1024 * 1024) {
72+
if (content !== undefined) {
6973
if (params.keywords_target == 'both') {
70-
text_to_check = file_name
74+
text_to_check = file_name + '\n' + content
7175
} else {
72-
continue
76+
text_to_check = content
7377
}
7478
} else {
75-
const buffer = await fs.promises.readFile(file_path)
76-
const is_binary = buffer.includes(0)
79+
const stats = await fs.promises.stat(file_path)
7780

78-
if (is_binary) {
81+
if (stats.size > 1024 * 1024) {
7982
if (params.keywords_target == 'both') {
8083
text_to_check = file_name
8184
} else {
8285
continue
8386
}
8487
} else {
85-
const content = buffer.toString('utf-8')
86-
if (params.keywords_target == 'both') {
87-
text_to_check = file_name + '\n' + content
88+
const buffer = await fs.promises.readFile(file_path)
89+
const is_binary = buffer.includes(0)
90+
91+
if (is_binary) {
92+
if (params.keywords_target == 'both') {
93+
text_to_check = file_name
94+
} else {
95+
continue
96+
}
8897
} else {
89-
text_to_check = content
98+
content = buffer.toString('utf-8')
99+
if (params.keywords_target == 'both') {
100+
text_to_check = file_name + '\n' + content
101+
} else {
102+
text_to_check = content
103+
}
90104
}
91105
}
92106
}

‎apps/editor/src/features/search-files/utils/search-files-by-phrase.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const search_files_by_phrase = async (params: {
1010
search_term: string
1111
progress?: vscode.Progress<{ message?: string; increment?: number }>
1212
token?: vscode.CancellationToken
13+
get_file_content?: (file_path: string) => Promise<string | undefined>
1314
}): Promise<string[]> => {
1415
const matched_files: string[] = []
1516
const positive_regex = create_search_regex(params.search_term)
@@ -32,14 +33,21 @@ export const search_files_by_phrase = async (params: {
3233
continue
3334
}
3435

35-
const stats = await fs.promises.stat(file_path)
36-
if (stats.size > 1024 * 1024) continue
36+
let content: string | undefined
37+
if (params.get_file_content) {
38+
content = await params.get_file_content(file_path)
39+
}
40+
41+
if (content === undefined) {
42+
const stats = await fs.promises.stat(file_path)
43+
if (stats.size > 1024 * 1024) continue
3744

38-
const buffer = await fs.promises.readFile(file_path)
39-
const is_binary = buffer.includes(0)
40-
if (is_binary) continue
45+
const buffer = await fs.promises.readFile(file_path)
46+
const is_binary = buffer.includes(0)
47+
if (is_binary) continue
4148

42-
const content = buffer.toString('utf-8')
49+
content = buffer.toString('utf-8')
50+
}
4351

4452
if (positive_regex.test(content)) {
4553
matched_files.push(file_path)

‎apps/editor/src/utils/git-repository-utils.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,13 @@ export const prepare_staged_changes = async (params: {
324324
: undefined
325325
})
326326

327+
const file_content = [
328+
final_diff_content ? final_diff_content : '',
329+
!is_deleted && full_content && !is_too_large ? full_content : ''
330+
]
331+
.filter(Boolean)
332+
.join('\n\n')
333+
327334
const token_count = Math.ceil(file_md.length / 4)
328335
const description_parts = []
329336

@@ -339,6 +346,7 @@ export const prepare_staged_changes = async (params: {
339346
picked: true,
340347
fsPath: change.uri.fsPath,
341348
token_count,
349+
file_content,
342350
buttons: [
343351
{
344352
iconPath: new vscode.ThemeIcon(
@@ -453,6 +461,10 @@ export const prepare_staged_changes = async (params: {
453461

454462
const search_result = await search_files({
455463
get_files: async () => current_selected_fs_paths,
464+
get_file_content: async (file_path: string) => {
465+
const item = items.find((i) => i.fsPath === file_path)
466+
return item?.file_content
467+
},
456468
workspace_provider: params.workspace_provider,
457469
extension_context: params.extension_context,
458470
websocket_manager: params.websocket_manager,

0 commit comments

Comments
 (0)