Skip to content

Commit 3bd6076

Browse files
authored
fix(vscode): improve file listing by ensuring unique items and limiting… (#4052)
* fix(chat): improve file listing by ensuring unique items and limiting results * fix(chat): enhance file listing by filtering unique open tabs and adding source context * fix(chat): limit search results by adjusting maxResults based on existing items
1 parent d9b3614 commit 3bd6076

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

clients/vscode/src/chat/utils.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,14 @@ export function chatPanelLocationToVSCodeRange(location: Location | undefined):
169169
return null;
170170
}
171171

172-
export function localUriToListFileItem(uri: Uri, gitProvider: GitProvider): ListFileItem {
172+
export function localUriToListFileItem(
173+
uri: Uri,
174+
gitProvider: GitProvider,
175+
source: "openedInEditor" | "searchResult" = "searchResult",
176+
): ListFileItem {
173177
return {
174178
filepath: localUriToChatPanelFilepath(uri, gitProvider),
179+
source,
175180
};
176181
}
177182

clients/vscode/src/chat/webview.ts

+20-26
Original file line numberDiff line numberDiff line change
@@ -509,40 +509,34 @@ export class ChatWebview extends EventEmitter {
509509
listFileInWorkspace: async (params: ListFilesInWorkspaceParams): Promise<ListFileItem[]> => {
510510
const maxResults = params.limit || 50;
511511
const searchQuery = params.query?.trim();
512+
const res: ListFileItem[] = [];
513+
let openTabs = window.tabGroups.all
514+
.flatMap((group) => group.tabs)
515+
.filter((tab) => tab.input && (tab.input as TabInputText).uri)
516+
.map((tab) => (tab.input as TabInputText).uri);
512517

513-
if (!searchQuery) {
514-
const openTabs = window.tabGroups.all
515-
.flatMap((group) => group.tabs)
516-
.filter((tab) => tab.input && (tab.input as TabInputText).uri);
518+
openTabs = openTabs.filter((uri, idx) => openTabs.indexOf(uri) === idx);
517519

518-
const openTabItems = openTabs.map((tab) =>
519-
localUriToListFileItem((tab.input as TabInputText).uri, this.gitProvider),
520-
);
521-
522-
// If we have less than 5 open tabs, fetch additional files to make total = 5
523-
if (openTabs.length < 5) {
524-
const additionalCount = 5 - openTabs.length;
525-
this.logger.info(`Found ${openTabs.length} open tabs, fetching ${additionalCount} more files`);
526-
const files = await this.findFiles("**/*", { maxResults: additionalCount });
527-
this.logger.info(`Found ${files.length} additional files`);
528-
const fileItems = files.map((uri) => localUriToListFileItem(uri, this.gitProvider));
529-
return [...openTabItems, ...fileItems];
530-
}
531-
532-
this.logger.info(`No query provided, listing ${openTabs.length} opened editors.`);
533-
return openTabItems;
520+
res.push(...openTabs.map((uri) => localUriToListFileItem(uri, this.gitProvider, "openedInEditor")));
521+
if (res.length > maxResults) {
522+
return res.slice(0, maxResults);
534523
}
535524

525+
const globPattern = caseInsensitivePattern(searchQuery);
536526
try {
537-
const globPattern = caseInsensitivePattern(searchQuery);
538-
this.logger.info(`Searching files with pattern: ${globPattern}, limit: ${maxResults}`);
539-
const files = await this.findFiles(globPattern, { maxResults });
540-
this.logger.info(`Found ${files.length} files.`);
541-
return files.map((uri) => localUriToListFileItem(uri, this.gitProvider));
527+
let files = await this.findFiles(globPattern, {
528+
maxResults: maxResults - res.length,
529+
excludes: openTabs.map((uri) => uri.fsPath),
530+
});
531+
files = files.filter(
532+
(uri, idx) => files.indexOf(uri) === idx && !openTabs.some((tabUri) => tabUri.fsPath === uri.fsPath),
533+
);
534+
this.logger.debug(`Found ${files.length} files matching pattern "${globPattern}"`);
535+
res.push(...files.map((uri) => localUriToListFileItem(uri, this.gitProvider, "searchResult")));
542536
} catch (error) {
543537
this.logger.warn("Failed to find files:", error);
544-
return [];
545538
}
539+
return res;
546540
},
547541

548542
readFileContent: async (info: FileRange): Promise<string | null> => {

0 commit comments

Comments
 (0)