Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(file-browser): reveal 后逐帧等目标行挂载再对齐横轴(Greptile P2 处置)
Zhang Yunjin690de7334b1b
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.tsxindex 3e7ba6040c..01c01edf3e 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@@ -194,27 +194,52 @@ function isHtmlPath(filePath: string): boolean {/*** 把 `data-relpath` 对应的行在横轴上对齐到容器可见区(#4436 契约:行宽由内容- * 决定,深层缩进 + 长名字会撑出横向滚动区)。+ * 决定,深层缩进 + 长名字会撑出横向滚动区)。返回该行当前是否已在 DOM 里。** 非虚拟化版靠 `scrollIntoView` 默认的 `inline: 'nearest'` 一并处理横轴;虚拟化下* 目标行常常是 `scrollToIndex` 之后才被渲染出来,且行绝对定位在宽 100% 的层里、* 自身 `min-w-max` —— 直接量行的矩形更干净。只动横轴、不动纵轴,不跟虚拟器的- * 纵向居中打架;行还没渲染(虚拟器还没把它带进视口)时静默跳过。+ * 纵向居中打架。*/-function alignRowHorizontally(container: HTMLElement | null, relPath: string): void {- if (!container) return;+function alignRowHorizontally(container: HTMLElement | null, relPath: string): boolean {+ if (!container) return false;// 按 dataset.relpath 精确比对,不走属性选择器:relPath 自带 `.` / `/` / `[]`// 等字符,选择器得先转义(且 jsdom 下没有 CSS.escape);渲染中的行数有界(视口 +// overscan),逐行比对代价可忽略。const el = Array.from(container.querySelectorAll<HTMLElement>('[data-relpath]')).find((node) => node.dataset.relpath === relPath,);- if (!el) return;+ if (!el) return false;const row = el.getBoundingClientRect();const box = container.getBoundingClientRect();const delta =row.right > box.right ? row.right - box.right : row.left < box.left ? row.left - box.left : 0;if (delta !== 0) container.scrollLeft += delta;+ return true;+}++/** 逐帧重试的上限(≈2s)。smooth 纵向滚动通常在几百毫秒内结束,这个窗口足够+ * 覆盖长跳跃;超过仍找不到行说明目标已不在树里(或视口从未测量),放弃。 */+const HORIZONTAL_ALIGN_MAX_FRAMES = 120;++/**+ * 逐帧重试,直到目标行进入 DOM 后再对齐横轴。+ *+ * 目标行离当前虚拟窗口较远时,要等 smooth 纵向滚动把它带进 overscan 才会挂载 ——+ * 「调一次 / 等下一帧 / 等固定 320ms」这种固定时刻的尝试可能全部跑在挂载之前,+ * 结果是纵向跳到位、深层长路径仍留在横向视口之外。这里改成挂载驱动:每帧试一次,+ * 行一出现就对齐;上限见上(不常驻,不给热路径留后台任务)。+ */+function alignRowHorizontallyWhenMounted(container: HTMLElement | null, relPath: string): void {+ if (!container) return;+ let frames = 0;+ const attempt = (): void => {+ if (alignRowHorizontally(container, relPath)) return;+ if (frames >= HORIZONTAL_ALIGN_MAX_FRAMES || !container.isConnected) return;+ frames += 1;+ requestAnimationFrame(attempt);+ };+ attempt();}export const FileTreeView = forwardRef<FileTreeViewHandle, FileTreeViewProps>(function FileTreeView(@@ -378,12 +403,9 @@ export const FileTreeView = forwardRef<FileTreeViewHandle, FileTreeViewProps>(fuif (index < 0) return; // 行不在当前树里(父目录未展开 / 文件不存在),静默 no-opvirtualizerRef.current?.scrollToIndex(index, { align: 'center', behavior: 'smooth' });// 横轴:目标行可能仍停在视口右侧之外(深层缩进 + 长名字把行撑宽),- // 纵向到位后把该行横向也拉进可见区。行是由虚拟器按需渲染的,所以- // 现在、下一帧、smooth 纵向滚动结束后各试一次(见 helper 注释)。- const container = containerRef.current;- alignRowHorizontally(container, relPath);- requestAnimationFrame(() => alignRowHorizontally(container, relPath));- window.setTimeout(() => alignRowHorizontally(container, relPath), 320);+ // 纵向到位后把该行横向也拉进可见区。行是虚拟器按需挂载的(远距离跳跃时+ // 要等 smooth 滚动把它带进 overscan),所以逐帧等到它出现再对齐。+ alignRowHorizontallyWhenMounted(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.tsxindex b24946e97d..8a8b2b8894 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@@ -21,7 +21,7 @@import { readFileSync } from 'node:fs';import { resolve } from 'node:path';import { createRef } from 'react';-import { cleanup, render, screen } from '@testing-library/react';+import { cleanup, fireEvent, render, screen } from '@testing-library/react';import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';vi.mock('react-i18next', () => ({@@ -30,7 +30,11 @@ vi.mock('react-i18next', () => ({import { FileTreeView, type FileTreeViewHandle } from '../FileTreeView';import type { DirEntry, UseFileTreeReturn } from '../hooks/useFileTree';-import { installTreeViewportStub, resetTestViewportSize } from './treeViewportStub';+import {+ installTreeViewportStub,+ resetTestViewportSize,+ setTestViewportSize,+} from './treeViewportStub';// jsdom 无布局:不装视口替身的话虚拟器产出 0 行(见 treeViewportStub 注释)。beforeAll(installTreeViewportStub);@@ -76,6 +80,21 @@ afterEach(() => {resetTestViewportSize();});+/** jsdom 无布局:行/容器的矩形手工摆(left/right 足以驱动横轴计算)。 */+function rect(left: number, right: number): DOMRect {+ return {+ left,+ right,+ top: 0,+ bottom: 28,+ width: right - left,+ height: 28,+ x: left,+ y: 0,+ toJSON: () => ({}),+ } as DOMRect;+}+describe('FileTreeView 横向滚动契约', () => {it('滚动容器承接横向溢出,并挂常显横条样式钩子(tree-hscroll)', () => {const { container } = render(@@ -139,18 +158,6 @@ describe('FileTreeView 横向滚动契约', () => {expect(row).toBeTruthy();// jsdom 无布局:手工摆出「容器 200px,目标行 40→320」的几何(行比容器宽 120px)。- const rect = (left: number, right: number): DOMRect =>- ({- left,- right,- top: 0,- bottom: 28,- width: right - left,- height: 28,- x: left,- y: 0,- toJSON: () => ({}),- }) as DOMRect;scroll.getBoundingClientRect = () => rect(0, 200);row!.getBoundingClientRect = () => rect(40, 320);@@ -159,6 +166,64 @@ describe('FileTreeView 横向滚动契约', () => {// 修复前(只滚纵向)这里恒为 0:行名一直停在视口右侧外面。expect(scroll.scrollLeft).toBe(120);});++ it('目标行还没进虚拟窗口时,挂载后仍会把横轴对齐(不依赖固定时刻的尝试)', async () => {+ setTestViewportSize(300);+ // 目标行排在 120 个文件之后(行号 122),远超 300px 视口(约 10 行 ++ // overscan),初始根本不在 DOM 里 —— 「调一次 / 下一帧 / 固定 320ms」这类+ // 固定时刻的尝试会全部跑在挂载之前。+ const many: DirEntry[] = Array.from({ length: 120 }, (_, i) => ({+ name: `f${i}.ts`,+ relPath: `f${i}.ts`,+ type: 'file',+ size: 1,+ mtimeMs: 0,+ }));+ const ref = createRef<FileTreeViewHandle>();+ const { container } = render(+ <FileTreeView+ ref={ref}+ tree={{+ entries: new Map([+ ['', [...many, rootEntries[1]]],+ ['deep', deepEntries],+ ['deep/l2', deeperEntries],+ ]),+ expanded: new Set(['', 'deep', 'deep/l2']),+ loadingPaths: new Set(),+ initialLoading: false,+ loadError: null,+ showIgnoredDirsSupported: true,+ storeKey: 'test-store',+ toggleFolder: vi.fn(),+ collapseAll: vi.fn(),+ refresh: vi.fn(async () => undefined),+ expandToPath: vi.fn(async () => undefined),+ }}+ scrollScope="test-tab"+ selectedPath={null}+ onSelectFile={vi.fn()}+ />,+ );+ const scroll = container.firstElementChild as HTMLElement;+ expect(scroll.querySelector('[data-relpath="deep/l2/l3"]')).toBeNull();++ ref.current?.scrollToPath('deep/l2/l3');++ // 模拟虚拟器把目标行带进视口(真实里由 smooth 纵向滚动触发)。+ scroll.scrollTop = 122 * 29;+ fireEvent.scroll(scroll);+ const row = scroll.querySelector<HTMLElement>('[data-relpath="deep/l2/l3"]');+ expect(row).toBeTruthy();++ // 行挂载后才摆几何:容器 200px、行 40→320。+ scroll.getBoundingClientRect = () => rect(0, 200);+ row!.getBoundingClientRect = () => rect(40, 320);++ await new Promise((resolve) => requestAnimationFrame(() => resolve(null)));++ expect(scroll.scrollLeft).toBe(120);+ });});describe('globals.css 里的 .tree-hscroll 规则', () => {