From 1e9babca458a3906baec8421d5d903c73e02d0ad Mon Sep 17 00:00:00 2001 From: Melody Ruch <396921921@qq.com> Date: Wed, 26 Aug 2026 15:07:07 +0800 Subject: [PATCH] fix(editor): skip statement frame for oversized oracle package bodies --- .../editor/currentStatementFrameLayer.spec.ts | 26 ++++++++++++++++++- .../codemirrorCurrentStatementFrameLayer.ts | 12 +++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/lib/__tests__/editor/currentStatementFrameLayer.spec.ts b/apps/desktop/src/lib/__tests__/editor/currentStatementFrameLayer.spec.ts index be94396f07..f6675200dc 100644 --- a/apps/desktop/src/lib/__tests__/editor/currentStatementFrameLayer.spec.ts +++ b/apps/desktop/src/lib/__tests__/editor/currentStatementFrameLayer.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from "vitest"; import { Text } from "@codemirror/state"; -import { currentStatementFrameLayer, currentStatementFrameRect, FRAME_INSET_PX } from "@/lib/editor/codemirrorCurrentStatementFrameLayer"; +import { currentStatementFrameLayer, currentStatementFrameRect, FRAME_INSET_PX, MAX_FRAME_STATEMENT_LINES } from "@/lib/editor/codemirrorCurrentStatementFrameLayer"; interface CoordRect { left: number; @@ -252,6 +252,30 @@ describe("currentStatementFrameRect", () => { const expectedHeight = (73 - 47) * LINE_HEIGHT + FRAME_INSET_PX * 2; expect(rect!.height).toBe(expectedHeight); }); + + it("skips huge multi-thousand-line statements instead of probing every line (dbx#7226)", () => { + // A large Oracle package body is parsed as one statement spanning the + // whole file. Without a cap, this loop would issue two coordsAtPos + // calls per logical line, freezing the UI on every scroll frame. + const lineCount = MAX_FRAME_STATEMENT_LINES + 2000; + const lines = Array.from({ length: lineCount }, (_, i) => ` proc_body_line_${i};`); + const view = buildView(lines); + const coords = vi.spyOn(view, "coordsAtPos"); + + const rect = currentStatementFrameRect(view, 0, view.state.doc.length); + + expect(rect).toBeNull(); + expect(coords).not.toHaveBeenCalled(); + }); + + it("still frames a statement right at the line-count cap", () => { + const lines = Array.from({ length: MAX_FRAME_STATEMENT_LINES + 1 }, (_, i) => `-- line ${i}`); + const view = buildView(lines); + + const rect = currentStatementFrameRect(view, 0, view.state.doc.length); + + expect(rect).not.toBeNull(); + }); }); describe("currentStatementFrameLayer", () => { diff --git a/apps/desktop/src/lib/editor/codemirrorCurrentStatementFrameLayer.ts b/apps/desktop/src/lib/editor/codemirrorCurrentStatementFrameLayer.ts index 41768baec6..4d45080670 100644 --- a/apps/desktop/src/lib/editor/codemirrorCurrentStatementFrameLayer.ts +++ b/apps/desktop/src/lib/editor/codemirrorCurrentStatementFrameLayer.ts @@ -36,6 +36,17 @@ function spansVisualRows(start: PositionRect, end: PositionRect): boolean { return Math.abs(startMid - endMid) > Math.max(2, Math.min(startHeight, endHeight) / 2); } +/** + * Statements spanning more logical lines than this render no frame. The + * horizontal-bounds loop below does a constant number of geometry probes + * per logical line, so its cost is O(logical lines) — fine for ordinary + * statements, but a multi-thousand-line PL/SQL package body (Oracle, and + * other dialects with BEGIN/END routine bodies) is parsed as a single + * statement spanning the whole file, turning routine scrolling into + * thousands of `coordsAtPos` calls per frame. See dbx#7226. + */ +export const MAX_FRAME_STATEMENT_LINES = 500; + /** * Measure the pixel rectangle of one statement rendered in `view`. * @@ -62,6 +73,7 @@ export function currentStatementFrameRect(view: Viewish, from: number, to: numbe const startLine = doc.lineAt(from); const endLine = doc.lineAt(to); + if (endLine.number - startLine.number > MAX_FRAME_STATEMENT_LINES) return null; const base = view.scrollDOM.getBoundingClientRect(); // Vertical bounds: convert screen coords to content-layer coords.