Skip to content

Commit 95fa805

Browse files
authored
promptFileLocator: use fileSystem if the location does not contain a wildcard (microsoft#249994)
* promptFileLocator: use fileSystem if the location does not contain a wildcard * use in listFilesInUserData
1 parent 063d607 commit 95fa805

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

src/vs/workbench/contrib/chat/common/promptSyntax/utils/promptFilesLocator.ts

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,8 @@ export class PromptFilesLocator {
5151
}
5252

5353
private async listFilesInUserData(type: PromptsType, token: CancellationToken): Promise<readonly URI[]> {
54-
try {
55-
const info = await this.fileService.resolve(this.userDataService.currentProfile.promptsHome);
56-
if (info.isDirectory && info.children && !token.isCancellationRequested) {
57-
const result: URI[] = [];
58-
for (const child of info.children) {
59-
if (child.isFile && getPromptFileType(child.resource) === type) {
60-
result.push(child.resource);
61-
}
62-
}
63-
return result;
64-
}
65-
return [];
66-
} catch (error) {
67-
return [];
68-
}
54+
const files = await this.resolveFilesAtLocation(this.userDataService.currentProfile.promptsHome, token);
55+
return files.filter(file => getPromptFileType(file) === type);
6956
}
7057

7158
/**
@@ -142,17 +129,13 @@ export class PromptFilesLocator {
142129
);
143130

144131
const { parent, filePattern } = firstNonGlobParentAndPattern(absoluteLocation);
145-
if (filePattern === undefined && await this.isExistingFile(parent)) {
146-
// if the provided location points to a file, add it
147-
if (getPromptFileType(parent) === type) {
148-
paths.add(parent);
149-
}
150-
} else {
151-
const promptFiles = await this.searchFilesInLocation(parent, filePattern, token);
152-
for (const file of promptFiles) {
153-
if (getPromptFileType(file) === type) {
154-
paths.add(file);
155-
}
132+
133+
const files = (filePattern === undefined)
134+
? await this.resolveFilesAtLocation(parent, token) // if the location does not contain a glob pattern, resolve the location directly
135+
: await this.searchFilesInLocation(parent, filePattern, token);
136+
for (const file of files) {
137+
if (getPromptFileType(file) === type) {
138+
paths.add(file);
156139
}
157140
}
158141
if (token.isCancellationRequested) {
@@ -198,6 +181,31 @@ export class PromptFilesLocator {
198181
return [...result];
199182
}
200183

184+
/**
185+
* Uses the file service to resolve the provided location and return either the file at the location of files in the directory.
186+
*/
187+
private async resolveFilesAtLocation(location: URI, token: CancellationToken): Promise<URI[]> {
188+
try {
189+
const info = await this.fileService.resolve(location);
190+
if (info.isFile) {
191+
return [info.resource];
192+
} else if (info.isDirectory && info.children) {
193+
const result: URI[] = [];
194+
for (const child of info.children) {
195+
if (child.isFile) {
196+
result.push(child.resource);
197+
}
198+
}
199+
return result;
200+
}
201+
} catch (error) {
202+
}
203+
return [];
204+
}
205+
206+
/**
207+
* Uses the search service to find all files at the provided location
208+
*/
201209
private async searchFilesInLocation(
202210
folder: URI,
203211
filePattern: string | undefined,
@@ -230,14 +238,6 @@ export class PromptFilesLocator {
230238
}
231239
return [];
232240
}
233-
234-
private async isExistingFile(uri: URI): Promise<boolean> {
235-
try {
236-
return (await this.fileService.resolve(uri)).isFile;
237-
} catch (e) {
238-
}
239-
return false;
240-
}
241241
}
242242

243243

@@ -341,6 +341,13 @@ const firstNonGlobParentAndPattern = (
341341
// just find all prompt files in the provided location
342342
return { parent: location, filePattern: undefined };
343343
}
344+
if (i === segments.length - 1 && segments[i] === '*' || segments[i] === ``) {
345+
return {
346+
parent: location.with({ path: segments.slice(0, i).join('/') }),
347+
filePattern: undefined
348+
};
349+
}
350+
344351
// the path contains a glob pattern, so we search in last folder that does not contain a glob pattern
345352
return {
346353
parent: location.with({ path: segments.slice(0, i).join('/') }),

0 commit comments

Comments
 (0)