Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 项目搜索按可见命中跳转,不再用源码计数定位行
Zhang Yunjin12f251c19b34
diff --git a/apps/desktop/src/renderer/__tests__/markdownTableSearchHighlight.test.ts b/apps/desktop/src/renderer/__tests__/markdownTableSearchHighlight.test.tsindex f268877b15..46c2d30862 100644--- a/apps/desktop/src/renderer/__tests__/markdownTableSearchHighlight.test.ts+++ b/apps/desktop/src/renderer/__tests__/markdownTableSearchHighlight.test.ts@@ -23,6 +23,7 @@ import {searchHighlightField,} from '@/components/markdown/PlaintextEditor';import {+ docSearchIndexOfLine,docSearchRangesField,filterVisibleDocSearchRanges,setDocSearchRangesEffect,@@ -331,3 +332,51 @@ describe('markdown table widget search highlight', () => {view.destroy();});});++describe('docSearchIndexOfLine (project-search jump target)', () => {+ // mermaid 块整块不可见 → findAll 剔除其中的命中。目标行之前若有这类同词+ // 命中,按源码计数会偏大:这里可见命中 4 个(源码计数给 5,越界 → setActive+ // 直接 return,用户点了跳转却什么都没滚)。+ const DOC_WITH_HIDDEN_HIT = [+ '```mermaid',+ 'graph TD',+ ' A[alpha] --> B[x]',+ '```',+ '',+ 'alpha one',+ 'alpha two',+ 'alpha three',+ 'alpha target',+ ].join('\n');++ it('counts only visible hits, so a hidden earlier hit does not offset the target', () => {+ const doc = docOf(DOC_WITH_HIDDEN_HIT);+ const visible = visibleMatches('alpha', doc);+ const lineOf = (from: number) => doc.lineAt(from).number;++ // mermaid 块里(line 3)那处命中被剔除 → 可见命中只剩 line 6/7/8/9 四处+ expect(visible).toHaveLength(4);+ expect(doc.lineAt(visible[0].from).number).toBe(6);+ expect(doc.lineAt(visible[3].from).number).toBe(9);++ // 跳到 line 9 → 下标 3。+ expect(docSearchIndexOfLine(visible, lineOf, 9)).toBe(3);+ // 对照:旧口径(数 line 9 之前的源码命中)会把隐藏那处一起算进去 → 4,+ // 而可见命中总共只有 4 个 → setActive(4) 越界直接 return,跳转根本不发生。+ const linesBeforeTarget = DOC_WITH_HIDDEN_HIT.split(/\r?\n/).slice(0, 8);+ const sourceCount = linesBeforeTarget+ .join('\n')+ .toLowerCase()+ .split('alpha').length - 1;+ expect(sourceCount).toBe(4);+ expect(sourceCount).toBeGreaterThanOrEqual(visible.length);+ });++ it('returns 0 when no visible match is on or after the line', () => {+ const doc = docOf(DOC_WITH_MERMAID);+ const visible = visibleMatches('alpha', doc);+ const lineOf = (from: number) => doc.lineAt(from).number;+ expect(docSearchIndexOfLine(visible, lineOf, 99)).toBe(0);+ expect(docSearchIndexOfLine([], lineOf, 1)).toBe(0);+ });+});diff --git a/apps/desktop/src/renderer/components/markdown/PlaintextEditor.tsx b/apps/desktop/src/renderer/components/markdown/PlaintextEditor.tsxindex a55d788f3e..58e43278c7 100644--- a/apps/desktop/src/renderer/components/markdown/PlaintextEditor.tsx+++ b/apps/desktop/src/renderer/components/markdown/PlaintextEditor.tsx@@ -92,6 +92,7 @@ import {findImageTargets,} from './markdownImageLivePreview';import {+ docSearchIndexOfLine,docSearchRangesField,filterVisibleDocSearchRanges,setDocSearchRangesEffect,@@ -107,6 +108,12 @@ export interface PlaintextEditorSearchHandle {findAll: (query: string) => number;/** Focus / scroll to the nth match (0-indexed). No-op if out of range. */setActive: (index: number) => void;+ /**+ * Index of the first visible match on `line` (1-indexed) or later — the+ * jump target for "open file at line N". Must count the *filtered* matches+ * findAll() returned, not raw source hits.+ */+ indexOfLine: (line: number) => number;/** Drop cached matches + clear highlight. */clear: () => void;}@@ -1208,6 +1215,15 @@ export const PlaintextEditor = forwardRef<PlaintextEditorHandle, PlaintextEditor],});},+ indexOfLine: (line: number): number => {+ const view = cmViewRef.current;+ if (!view) return 0;+ return docSearchIndexOfLine(+ matchesRef.current,+ (from) => view.state.doc.lineAt(from).number,+ line,+ );+ },clear: (): void => {matchesRef.current = [];const view = cmViewRef.current;diff --git a/apps/desktop/src/renderer/components/markdown/docSearchRanges.ts b/apps/desktop/src/renderer/components/markdown/docSearchRanges.tsindex c953d00115..cceec4668f 100644--- a/apps/desktop/src/renderer/components/markdown/docSearchRanges.ts+++ b/apps/desktop/src/renderer/components/markdown/docSearchRanges.ts@@ -95,6 +95,26 @@ function mergeHiddenRanges(ranges: DocSearchHiddenRange[]): DocSearchHiddenRangereturn out;}+/**+ * 第一个落在 `line`(1-indexed)或之后的命中的下标 —— 即"该行第一个命中"在+ * **可见命中列表**里的位置,没有则返回 0(调用方还会 clamp 到 total-1)。+ *+ * 按行号跳转必须用它,不能对源码直接计数:findAll 已把图片 alt、mermaid 块、+ * conceal 标记等看不见的命中剔除,源码计数会把这些一起算进去,序号偏大 →+ * 跳到后面的另一处。`lineOf` 把文档位置映射成行号(通常传+ * `doc.lineAt(from).number`)。+ */+export function docSearchIndexOfLine(+ matches: DocSearchRange[],+ lineOf: (from: number) => number,+ line: number,+): number {+ for (let i = 0; i < matches.length; i++) {+ if (lineOf(matches[i].from) >= line) return i;+ }+ return 0;+}+/** 取出落在 [from, to) 内的命中(表格 widget 用它在自己 DOM 里补画高亮)。 */export function selectDocSearchRangesWithin(ranges: DocSearchRange[],diff --git a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileBodyView.tsx b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileBodyView.tsxindex d756a4daa8..63d793c5ba 100644--- a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileBodyView.tsx+++ b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileBodyView.tsx@@ -90,28 +90,12 @@ import {} from './lib/markdownAutosave';/**- * 数 source text 中 `query`(case-insensitive) 在 `targetLine`(1-indexed) 之前- * 出现了多少次。用作 project-search 跳转: 知道目标行后, 这个计数 = 在 in-file- * 搜索结果列表里目标行第一个匹配的 0-based index(因为 in-file 搜索也是按- * 文档顺序、case-insensitive 排出来的)。+ * project-search 跳转要的是「目标行第一个**可见**命中的下标」。+ * 它由 PlaintextEditor.search.indexOfLine 从 findAll() 的同一份命中集合里算+ * (findAll 已剔除图片 alt / mermaid 块 / conceal 标记等看不见的命中),所以这里+ * 不再对源码计数 —— 源码计数会把那些被剔除的同词命中一起算进去,序号偏大,+ * 跳到后面的另一处。*/-function countMatchesBeforeLine(content: string, query: string, targetLine: number): number {- if (!query) return 0;- const lines = content.split(/\r?\n/);- const target = Math.min(Math.max(targetLine, 1), lines.length);- const needle = query.toLowerCase();- if (!needle) return 0;- let count = 0;- for (let i = 0; i < target - 1; i++) {- const text = lines[i].toLowerCase();- let pos = 0;- while ((pos = text.indexOf(needle, pos)) !== -1) {- count++;- pos += needle.length;- }- }- return count;-}const MARKDOWN_AUTOSAVE_DELAY_MS = 900;const CONTEXT_MENU_VIEWPORT_PADDING = 8;@@ -625,7 +609,7 @@ export const FileBodyView = forwardRef<FileBodyHandle, FileBodyViewProps>(functi) {const jumpKey = `${relPath}|${jumpQuery}|${jumpLine}`;if (lastJumpKeyRef.current !== jumpKey) {- const idx = countMatchesBeforeLine(content.content, jumpQuery, jumpLine);+ const idx = editorRef.current?.search.indexOfLine(jumpLine) ?? 0;initialActive = Math.min(idx, total - 1);// 这里不立刻消费 jumpKey —— 留给下面 jumpEffect 兜底(防止 runner 在// 某些 race 下没跑到 scroll 那段时, jumpEffect 还能补一次)。@@ -728,10 +712,7 @@ export const FileBodyView = forwardRef<FileBodyHandle, FileBodyViewProps>(functi}const targetLine = jumpLine ?? null;- const idx =- targetLine !== null- ? countMatchesBeforeLine(content.content, jumpQuery, targetLine)- : 0;+ const idx = targetLine !== null ? (editorRef.current?.search.indexOfLine(targetLine) ?? 0) : 0;const active = Math.min(idx, lastRun.total - 1);setSearchActive(active);editorRef.current?.search.setActive(active);