Skip to content

Commit 32a276d

Browse files
committed
refactor: improve file match logic
1 parent bfca2ca commit 32a276d

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

packages/ai-native/src/browser/components/ChatEditor.tsx

+23-21
Original file line numberDiff line numberDiff line change
@@ -254,29 +254,31 @@ const CodeBlock = ({
254254
let lastIndex = 0;
255255
const fragments: (string | React.ReactNode)[] = [];
256256

257-
// 处理文件标记
258-
fileMatches.forEach((match, matchIndex) => {
259-
if (match.index !== undefined) {
260-
const spanText = processedText.slice(lastIndex, match.index);
261-
if (spanText) {
262-
fragments.push(<span key={`${index}-${matchIndex}`}>{spanText}</span>);
257+
// 通用处理函数
258+
const processMatches = (matches: RegExpMatchArray[], isFolder: boolean) => {
259+
matches.forEach((match, matchIndex) => {
260+
if (match.index !== undefined) {
261+
const spanText = processedText.slice(lastIndex, match.index);
262+
if (spanText) {
263+
fragments.push(
264+
<span key={`${index}-${matchIndex}-${isFolder ? 'folder' : 'file'}`}>{spanText}</span>,
265+
);
266+
}
267+
fragments.push(
268+
renderAttachment(
269+
match[1],
270+
isFolder,
271+
`${index}-tag-${matchIndex}-${isFolder ? 'folder' : 'file'}`,
272+
),
273+
);
274+
lastIndex = match.index + match[0].length;
263275
}
264-
fragments.push(renderAttachment(match[1], false, `${index}-tag-${matchIndex}`));
265-
lastIndex = match.index + match[0].length;
266-
}
267-
});
276+
});
277+
};
268278

269-
// 处理文件夹标记
270-
folderMatches.forEach((match, matchIndex) => {
271-
if (match.index !== undefined) {
272-
const spanText = processedText.slice(lastIndex, match.index);
273-
if (spanText) {
274-
fragments.push(<span key={`${index}-${matchIndex}`}>{spanText}</span>);
275-
}
276-
fragments.push(renderAttachment(match[1], true, `${index}-tag-${matchIndex}`));
277-
lastIndex = match.index + match[0].length;
278-
}
279-
});
279+
// 处理文件标记
280+
processMatches(fileMatches, false);
281+
processMatches(folderMatches, true);
280282

281283
fragments.push(processedText.slice(lastIndex));
282284
renderedContent.push(...fragments);

packages/ai-native/src/browser/context/llm-context.service.ts

+25-21
Original file line numberDiff line numberDiff line change
@@ -218,31 +218,35 @@ export class LLMContextServiceImpl extends WithEventBus implements LLMContextSer
218218

219219
private async getPartiaFolderStructure(folder: string, level = 2): Promise<string[]> {
220220
const result: string[] = [];
221-
const stat = await this.fileService.getFileStat(folder);
222-
223-
for (const child of stat?.children || []) {
224-
const relativePath = new URI(folder).relative(new URI(child.uri))!.toString();
225-
226-
if (child.isSymbolicLink) {
227-
// 处理软链接
228-
const target = await this.fileService.getFileStat(child.realUri || child.uri);
229-
if (target) {
230-
result.push(`${relativePath} -> ${target} (symbolic link)`);
231-
} else {
232-
result.push(`${relativePath} (broken symbolic link)`);
221+
try {
222+
const stat = await this.fileService.getFileStat(folder);
223+
224+
for (const child of stat?.children || []) {
225+
const relativePath = new URI(folder).relative(new URI(child.uri))!.toString();
226+
227+
if (child.isSymbolicLink) {
228+
// 处理软链接
229+
const target = await this.fileService.getFileStat(child.realUri || child.uri);
230+
if (target) {
231+
result.push(`${relativePath} -> ${target} (symbolic link)`);
232+
} else {
233+
result.push(`${relativePath} (broken symbolic link)`);
234+
}
235+
continue;
233236
}
234-
continue;
235-
}
236237

237-
if (child.type === FileType.Directory) {
238-
result.push(`${relativePath}/`);
239-
if (level > 1) {
240-
const subDirStructure = await this.getPartiaFolderStructure(child.uri, level - 1);
241-
result.push(...subDirStructure.map((subEntry) => `${relativePath}/${subEntry}`));
238+
if (child.type === FileType.Directory) {
239+
result.push(`${relativePath}/`);
240+
if (level > 1) {
241+
const subDirStructure = await this.getPartiaFolderStructure(child.uri, level - 1);
242+
result.push(...subDirStructure.map((subEntry) => `${relativePath}/${subEntry}`));
243+
}
244+
} else if (child.type === FileType.File) {
245+
result.push(relativePath);
242246
}
243-
} else if (child.type === FileType.File) {
244-
result.push(relativePath);
245247
}
248+
} catch {
249+
return result;
246250
}
247251

248252
return result;

0 commit comments

Comments
 (0)