Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(file-browser): 无效导航也要作废上一次横轴对齐任务(Greptile P2 处置)

Zhang Yunjinf2a5d68d60fd
diff --git a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileTreeView.tsx b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileTreeView.tsx
index 776105e07e..963200c96b 100644
--- a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileTreeView.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileTreeView.tsx
@@ -409,6 +409,11 @@ export const FileTreeView = forwardRef<FileTreeViewHandle, FileTreeViewProps>(fu
ref,
() => ({
scrollToPath: (relPath: string) => {
+ // 每次新的导航意图都先作废上一次还没完成的横轴对齐任务,**包括下面
+ // index < 0 的提前返回**:导航到不存在 / 未展开的路径同样是「用户离开了
+ // 上一个目标」,旧行稍后挂载时不该再把横向视口拉回过时目标。
+ horizontalAlignCancelRef.current?.();
+ horizontalAlignCancelRef.current = null;
// 虚拟化后目标行多半不在 DOM 里(甚至还没进视口),不能再 querySelector +
// scrollIntoView —— 按行索引让虚拟器滚过去,它会把目标行渲染出来。
const index = rowsRef.current.findIndex(
@@ -419,8 +424,6 @@ export const FileTreeView = forwardRef<FileTreeViewHandle, FileTreeViewProps>(fu
// 横轴:目标行可能仍停在视口右侧之外(深层缩进 + 长名字把行撑宽),
// 纵向到位后把该行横向也拉进可见区。行是虚拟器按需挂载的(远距离跳跃时
// 要等 smooth 滚动把它带进 overscan),所以逐帧等到它出现再对齐。
- // 先取消上一次还没完成的导航:否则旧行稍后挂载时会把横向视口拉回去。
- horizontalAlignCancelRef.current?.();
horizontalAlignCancelRef.current = startRowHorizontalAlign(containerRef.current, relPath);
},
}),
diff --git a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/__tests__/FileTreeView.horizontalScroll.test.tsx b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/__tests__/FileTreeView.horizontalScroll.test.tsx
index 1b0f3109c3..9f96b8ddfd 100644
--- a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/__tests__/FileTreeView.horizontalScroll.test.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/__tests__/FileTreeView.horizontalScroll.test.tsx
@@ -270,6 +270,39 @@ describe('FileTreeView 横向滚动契约', () => {
// 旧任务若没被取消:这里会变成 500(旧行右缘 700 - 容器右缘 200)。
expect(scroll.scrollLeft).toBe(0);
});
+
+ it('随后导航到树里不存在的路径,也会作废上一次的对齐任务', async () => {
+ setTestViewportSize(300);
+ const ref = createRef<FileTreeViewHandle>();
+ const { container } = render(
+ <FileTreeView
+ ref={ref}
+ tree={makeFarTargetsTree()}
+ scrollScope="test-tab"
+ selectedPath={null}
+ onSelectFile={vi.fn()}
+ />,
+ );
+ const scroll = container.firstElementChild as HTMLElement;
+
+ // 第一次导航指向一个还没挂载的有效目标;第二次指向树里不存在的路径
+ // (`index < 0` 提前返回)—— 这次「无效导航」也必须让旧任务失效。
+ ref.current?.scrollToPath('deep/l2/l3');
+ ref.current?.scrollToPath('nope/missing.ts');
+
+ // 旧目标随后进了视口:它的对齐任务必须已经被作废。
+ scroll.scrollTop = 122 * 29;
+ fireEvent.scroll(scroll);
+ const staleRow = scroll.querySelector<HTMLElement>('[data-relpath="deep/l2/l3"]');
+ expect(staleRow).toBeTruthy();
+ scroll.getBoundingClientRect = () => rect(0, 200);
+ staleRow!.getBoundingClientRect = () => rect(40, 700);
+
+ await new Promise((resolve) => requestAnimationFrame(() => resolve(null)));
+
+ // 若取消发生在 `index < 0` 检查之后:这里会变成 500。
+ expect(scroll.scrollLeft).toBe(0);
+ });
});
describe('globals.css 里的 .tree-hscroll 规则', () => {