mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
fix(vcs): keep a diff pane's horizontal scrollbar at its bottom edge (#3933)
In the aligned side-by-side diff the panes scroll together in the outer MergeView, so each pane's scroller is as tall as its content and its horizontal scrollbar renders at the bottom of the content: with wrapping off and lines wider than the pane, the bar floats mid-screen for short diffs and sits below the viewport for long ones, reachable only after scrolling the diff to its end. Every split diff but a fitted block now scrolls its panes themselves (as the unaligned mode already does), so both scrollbars stay at the pane's edges. The padded panes lay their lines out identically, so a small sync copies one pane's scrollTop to the other; the overview lane is per pane as in the unaligned mode, and the outer-scroller lane support goes away. A fitted block keeps one stripe, on the right pane (IDEA's shared scrollbar), not one per pane. How it was verified: - new e2e/diffScrollbar.spec.ts asserts in all three modes (unaligned, aligned, unified) that the right pane's scroller spans the host viewport while its content is ~24k px wider than it; - e2e diff suite (27 cases: small diffs, a 44,000-character line, 10,000- line files padded / sync / unified / collapsed, soft-wrap toggles) and the overview-lane and diff-navigation specs pass; - unit tests: new PaddedPanes sync cases (follow, sync off, animated scroll source) and the updated per-pane lane assertions pass; the full suite shows only the two pre-existing Windows-checkout failures (scripts/check-webkit path separators, generated theme docs); - pnpm typecheck, lint, knip and build pass; touched files are oxfmt-clean. Co-authored-by: moyining <724405566@qq.com>
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* The horizontal scrollbar of a diff pane: with wrapping off and lines wider
|
||||
* than the pane, it must sit at the pane's bottom edge — reachable without
|
||||
* scrolling the diff content to its end — in every viewer mode.
|
||||
*/
|
||||
import { rmSync } from "node:fs";
|
||||
import { dirname } from "node:path";
|
||||
import { readSides, tempDiffRepo } from "./diffRepo";
|
||||
import { expect, test } from "./fixtures";
|
||||
|
||||
let repo = "";
|
||||
test.beforeAll(() => {
|
||||
repo = tempDiffRepo();
|
||||
});
|
||||
test.afterAll(() => rmSync(dirname(repo), { recursive: true, force: true }));
|
||||
|
||||
for (const [name, options] of [
|
||||
["side-by-side without alignment", { mode: "split", alignChanges: false }],
|
||||
["side-by-side aligned", { mode: "split", alignChanges: true }],
|
||||
["unified", { mode: "unified", alignChanges: false }],
|
||||
] as const) {
|
||||
test(`the horizontal scrollbar spans the pane viewport (${name})`, async ({ workbench: page }) => {
|
||||
const path = "src/soft-wrap.ts";
|
||||
const { left, right } = readSides(repo, path);
|
||||
await page.evaluate(
|
||||
async ({ path, left, right, options }) => {
|
||||
localStorage.setItem("openidea.vcs.diffOptions", JSON.stringify(options));
|
||||
const actions = "/src/platform/actions.ts";
|
||||
const { executeAction } = await import(/* @vite-ignore */ actions);
|
||||
await executeAction("diff.compareTexts", {
|
||||
path,
|
||||
title: path,
|
||||
left: { text: left, label: "HEAD" },
|
||||
right: { text: right, label: "Working tree" },
|
||||
});
|
||||
},
|
||||
{
|
||||
path,
|
||||
left,
|
||||
right,
|
||||
options: {
|
||||
...options,
|
||||
ignore: "none",
|
||||
highlight: "words",
|
||||
collapse: true,
|
||||
syncScroll: true,
|
||||
softWrap: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
const geometry = await page
|
||||
.locator(".cm-editor.cm-merge-b")
|
||||
.first()
|
||||
.evaluate((ed) => {
|
||||
const scroller = ed.querySelector<HTMLElement>(".cm-scroller")!;
|
||||
const host = (ed.closest(".cm-mergeView") ?? ed).parentElement!;
|
||||
return {
|
||||
wide: scroller.scrollWidth,
|
||||
clientW: scroller.clientWidth,
|
||||
scrollerBottom: scroller.getBoundingClientRect().bottom,
|
||||
viewportBottom: host.getBoundingClientRect().bottom,
|
||||
};
|
||||
});
|
||||
// The lines are far wider than the pane: there is something to scroll.
|
||||
expect(geometry.wide).toBeGreaterThan(geometry.clientW + 1_000);
|
||||
// The scroller spans the pane's viewport, so its horizontal scrollbar is at
|
||||
// the bottom edge instead of the content's end.
|
||||
expect(geometry.scrollerBottom).toBeCloseTo(geometry.viewportBottom, 0);
|
||||
});
|
||||
}
|
||||
@@ -58,14 +58,9 @@ export function unfoldAround(state: EditorState, pos: number): StateEffect<unkno
|
||||
return out;
|
||||
}
|
||||
|
||||
/** The class an outer scroller gets while a lane draws its scrollbar (its native one is hidden). */
|
||||
const OUTER_SCROLLER = "cm-openidea-overviewScroller";
|
||||
|
||||
class OverviewLane {
|
||||
dom: HTMLElement;
|
||||
/** What scrolls the document: the editor's own scroller, or an outer one the editor is laid out in. */
|
||||
private scroller: HTMLElement;
|
||||
private resize: ResizeObserver | null = null;
|
||||
private scheduled = false;
|
||||
/** Set while a resize burst is being coalesced; see [`scheduleMarksLater`]. */
|
||||
private markTimer: number | undefined;
|
||||
@@ -78,13 +73,10 @@ class OverviewLane {
|
||||
*/
|
||||
private thumb: HTMLElement;
|
||||
|
||||
constructor(
|
||||
private view: EditorView,
|
||||
outer?: HTMLElement,
|
||||
) {
|
||||
this.scroller = outer ?? view.scrollDOM;
|
||||
constructor(private view: EditorView) {
|
||||
this.scroller = view.scrollDOM;
|
||||
this.dom = document.createElement("div");
|
||||
this.dom.className = outer ? "cm-openidea-overview cm-openidea-overview-sticky" : "cm-openidea-overview";
|
||||
this.dom.className = "cm-openidea-overview";
|
||||
this.dom.setAttribute("aria-hidden", "true");
|
||||
this.thumb = document.createElement("div");
|
||||
this.thumb.className = "cm-openidea-overviewThumb";
|
||||
@@ -93,13 +85,7 @@ class OverviewLane {
|
||||
view.dom.addEventListener("mousemove", this.onPointerMove);
|
||||
view.dom.addEventListener("mouseleave", this.onPointerLeave);
|
||||
this.scroller.addEventListener("scroll", this.onScroll, { passive: true });
|
||||
if (outer) {
|
||||
// First in the editor, sticking to the top of the outer scroller's viewport.
|
||||
view.dom.prepend(this.dom);
|
||||
outer.classList.add(OUTER_SCROLLER);
|
||||
this.resize = new ResizeObserver(() => this.schedule());
|
||||
this.resize.observe(outer);
|
||||
} else view.dom.appendChild(this.dom);
|
||||
view.dom.appendChild(this.dom);
|
||||
this.schedule();
|
||||
}
|
||||
|
||||
@@ -175,10 +161,6 @@ class OverviewLane {
|
||||
};
|
||||
},
|
||||
write: ({ rows, caret, unit, laneHeight }) => {
|
||||
if (this.scroller !== this.view.scrollDOM) {
|
||||
this.dom.style.height = `${laneHeight}px`;
|
||||
this.dom.style.marginBottom = `-${laneHeight}px`;
|
||||
}
|
||||
const key =
|
||||
rows.map((r) => `${r.top},${r.height},${r.mark.color},${r.mark.kind}`).join("|") +
|
||||
`#${caret}${unit}${laneHeight}`;
|
||||
@@ -323,21 +305,10 @@ class OverviewLane {
|
||||
this.view.dom.removeEventListener("mousemove", this.onPointerMove);
|
||||
this.view.dom.removeEventListener("mouseleave", this.onPointerLeave);
|
||||
this.scroller.removeEventListener("scroll", this.onScroll);
|
||||
this.resize?.disconnect();
|
||||
if (this.scroller !== this.view.scrollDOM) this.scroller.classList.remove(OUTER_SCROLLER);
|
||||
this.dom.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The overview lane over an outer scroller that holds the editor at its full height (the panes of
|
||||
* an aligned side-by-side diff scroll together in their container): `scroller` finds it once the
|
||||
* editor is in the DOM. The lane sticks to the right edge of this editor.
|
||||
*/
|
||||
export function overviewLaneIn(scroller: (view: EditorView) => HTMLElement | null): Extension {
|
||||
return ViewPlugin.define((view) => new OverviewLane(view, scroller(view) ?? undefined));
|
||||
}
|
||||
|
||||
export const overviewLane: Extension = [
|
||||
ViewPlugin.define((view) => new OverviewLane(view)),
|
||||
EditorView.editorAttributes.of({ class: "cm-openidea-withOverview" }),
|
||||
|
||||
@@ -79,13 +79,6 @@ export const overviewStyles = {
|
||||
cursor: "default",
|
||||
zIndex: "3",
|
||||
},
|
||||
// Over an outer scroller (an aligned diff): stays at the top of its viewport, taking no room.
|
||||
".cm-openidea-overview-sticky": {
|
||||
position: "sticky",
|
||||
bottom: "auto",
|
||||
flex: "none",
|
||||
alignSelf: "flex-end",
|
||||
},
|
||||
".cm-openidea-overviewMark": {
|
||||
position: "absolute",
|
||||
left: "3px",
|
||||
|
||||
@@ -146,15 +146,15 @@
|
||||
.host :global(.cm-mergeView) {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow-y: auto;
|
||||
/* The panes scroll themselves (`.paneScroll`); a fitted block is content-tall and its host scrolls. */
|
||||
overflow: hidden;
|
||||
}
|
||||
.host :global(.cm-mergeViewEditors) {
|
||||
min-height: 100%;
|
||||
}
|
||||
.host :global(.cm-mergeViewEditor) {
|
||||
min-width: 0;
|
||||
/* Clips like the library's `hidden` without becoming a scroll container, so the aligned view's
|
||||
overview lane sticks to the MergeView's viewport (cm/lanes.ts `overviewLaneIn`). */
|
||||
/* Clips like the library's `hidden` without becoming a scroll container. */
|
||||
overflow: clip;
|
||||
}
|
||||
.host :global(.cm-merge-revert) {
|
||||
@@ -225,25 +225,23 @@
|
||||
.noHighlight :global(.cm-diffChangedGutter) {
|
||||
background: none !important;
|
||||
}
|
||||
/* Side-by-side without alignment (IDEA's default): each pane scrolls itself. */
|
||||
.unaligned :global(.cm-mergeView) {
|
||||
overflow: hidden;
|
||||
}
|
||||
.unaligned :global(.cm-mergeViewEditors) {
|
||||
/* Every split diff but a fitted block: each pane scrolls itself (both axes), so its
|
||||
horizontal scrollbar stays at the pane's bottom edge — a content-tall scroller would
|
||||
render it at the content's end, out of reach until the diff is scrolled there. */
|
||||
.paneScroll :global(.cm-mergeViewEditors) {
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
}
|
||||
.unaligned :global(.cm-mergeViewEditor) {
|
||||
.paneScroll :global(.cm-mergeViewEditor) {
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
/* Overrides MergeView's base theme, which leaves scrolling to the outer view. */
|
||||
.unaligned :global(.cm-mergeViewEditor > .cm-editor),
|
||||
.unaligned :global(.cm-mergeViewEditor > .cm-editor > .cm-scroller) {
|
||||
.paneScroll :global(.cm-mergeViewEditor > .cm-editor),
|
||||
.paneScroll :global(.cm-mergeViewEditor > .cm-editor > .cm-scroller) {
|
||||
height: 100% !important;
|
||||
}
|
||||
.unaligned :global(.cm-mergeViewEditor > .cm-editor > .cm-scroller) {
|
||||
.paneScroll :global(.cm-mergeViewEditor > .cm-editor > .cm-scroller) {
|
||||
overflow: auto !important;
|
||||
}
|
||||
.host :global(.cm-diffDivider) {
|
||||
|
||||
@@ -98,7 +98,7 @@ import {
|
||||
type DiffViewerHost,
|
||||
type DiffViewerPane,
|
||||
} from "./diffViewerProviders";
|
||||
import { UnalignedPanes } from "./diffScroll";
|
||||
import { PaddedPanes, UnalignedPanes, type DiffPanes } from "./diffScroll";
|
||||
import { groupOf } from "./changes";
|
||||
import { inclusionGutter } from "./hunkInclusion";
|
||||
import { hunksOverlapping } from "./hunks";
|
||||
@@ -115,8 +115,8 @@ interface Views {
|
||||
b: EditorView;
|
||||
a: EditorView | null;
|
||||
merge: MergeView | null;
|
||||
/** Unpadded side-by-side panes with synchronized scrolling (IDEA). */
|
||||
panes: UnalignedPanes | null;
|
||||
/** The panes' scroll machinery: unpadded panes sync through the changes, padded ones copy their scroll. */
|
||||
panes: DiffPanes | null;
|
||||
destroy(): void;
|
||||
}
|
||||
|
||||
@@ -579,15 +579,20 @@ export function DiffEditor({
|
||||
let views: Views;
|
||||
if (viewMode === "split") {
|
||||
let merge: MergeView | null = null;
|
||||
const panes = aligned
|
||||
// Every split diff but a fitted block scrolls its panes themselves: a content-tall
|
||||
// pane would render its horizontal scrollbar at the content's end, out of reach
|
||||
// until the diff is scrolled there. The padded panes copy their scroll instead.
|
||||
const panes: DiffPanes | null = fit
|
||||
? null
|
||||
: new UnalignedPanes({
|
||||
sync: syncScroll.current,
|
||||
arrows:
|
||||
editableA || editableB
|
||||
? { toA: editableA, toB: editableB, apply: (i, source) => merge && applyChunk(merge, i, source) }
|
||||
: undefined,
|
||||
});
|
||||
: aligned
|
||||
? new PaddedPanes(syncScroll.current)
|
||||
: new UnalignedPanes({
|
||||
sync: syncScroll.current,
|
||||
arrows:
|
||||
editableA || editableB
|
||||
? { toA: editableA, toB: editableB, apply: (i, source) => merge && applyChunk(merge, i, source) }
|
||||
: undefined,
|
||||
});
|
||||
// IDEA's colouring: lines by chunk type, carried into the gutters, and typed word highlights.
|
||||
const colours = [
|
||||
changedLineGutters,
|
||||
@@ -600,8 +605,8 @@ export function DiffEditor({
|
||||
extensions: [
|
||||
side(attached.a, editableA, "left"),
|
||||
colours,
|
||||
// Aligned panes scroll together: one stripe, on the right (as IDEA's shared scrollbar).
|
||||
panes ? diffOverview("split") : [],
|
||||
// A fitted block keeps one stripe, on the right (as IDEA's shared scrollbar).
|
||||
fit ? [] : diffOverview("split"),
|
||||
panes?.extension ?? [],
|
||||
focusTracker("a"),
|
||||
findWatch("a"),
|
||||
@@ -614,7 +619,7 @@ export function DiffEditor({
|
||||
extensions: [
|
||||
side(attached.b, editableB, "right"),
|
||||
colours,
|
||||
diffOverview(panes ? "split" : "aligned"),
|
||||
diffOverview("split"),
|
||||
panes?.extension ?? [],
|
||||
listener,
|
||||
focusTracker("b"),
|
||||
@@ -651,7 +656,7 @@ export function DiffEditor({
|
||||
};
|
||||
if (scrollTop != null)
|
||||
requestAnimationFrame(() => {
|
||||
// The left pane of unpadded panes follows by itself.
|
||||
// A fitted block scrolls on the merge view; the panes scroll themselves.
|
||||
if (!panes) (mv.dom as HTMLElement).scrollTop = scrollTop;
|
||||
else mv.b.scrollDOM.scrollTop = scrollTop;
|
||||
});
|
||||
@@ -737,6 +742,7 @@ export function DiffEditor({
|
||||
lang,
|
||||
viewMode,
|
||||
single,
|
||||
fit,
|
||||
aligned,
|
||||
opts.collapse,
|
||||
opts.ignore,
|
||||
@@ -756,29 +762,34 @@ export function DiffEditor({
|
||||
useEffect(() => {
|
||||
const views = viewsRef.current;
|
||||
if (!views) return;
|
||||
// Aligned panes scroll in MergeView's outer element, which scrollSnapshot does not cover.
|
||||
// A fitted block has no scroller of its own: anchoring would scroll the all-in-one diff.
|
||||
const outer = !fit && views.merge && !views.panes ? views.merge.dom : null;
|
||||
const top = outer?.getBoundingClientRect().top;
|
||||
const block = top == null ? null : views.b.lineBlockAtHeight(Math.max(0, top - views.b.documentTop));
|
||||
const anchor =
|
||||
block && top != null
|
||||
? EditorView.scrollIntoView(block.from, { y: "start", yMargin: block.top + views.b.documentTop - top })
|
||||
: null;
|
||||
// Padded panes (an aligned split) re-insert MergeView's alignment spacers on their next measure,
|
||||
// after the wrap reflow, so a scroll snapshot drifts: anchor each pane to its visible line and
|
||||
// re-apply once the spacers land. Unaligned panes have no spacers; a fitted block has no scroller.
|
||||
const panes = views.panes instanceof PaddedPanes ? views.panes : null;
|
||||
const anchorFor = (view: EditorView) => {
|
||||
const top = view.scrollDOM.getBoundingClientRect().top;
|
||||
const block = view.lineBlockAtHeight(Math.max(0, top - view.documentTop));
|
||||
return EditorView.scrollIntoView(block.from, { y: "start", yMargin: block.top + view.documentTop - top });
|
||||
};
|
||||
const anchors = panes ? { a: views.a ? anchorFor(views.a) : null, b: anchorFor(views.b) } : null;
|
||||
// The re-anchored right pane leads the sync: the left pane's delayed reflow follows it rather than
|
||||
// being read as a user scroll that pulls the right pane back to where it was.
|
||||
if (anchors) panes?.leadScroll("b");
|
||||
for (const view of [views.a, views.b]) {
|
||||
if (view)
|
||||
view.dispatch({
|
||||
effects: [
|
||||
wrapSlot.reconfigure(softWrap ? EditorView.lineWrapping : []),
|
||||
...(outer ? (view === views.b && anchor ? [anchor] : []) : [view.scrollSnapshot()]),
|
||||
],
|
||||
});
|
||||
if (!view) continue;
|
||||
const anchor = anchors ? (view === views.a ? anchors.a : anchors.b) : view.scrollSnapshot();
|
||||
view.dispatch({
|
||||
effects: [wrapSlot.reconfigure(softWrap ? EditorView.lineWrapping : []), ...(anchor ? [anchor] : [])],
|
||||
});
|
||||
}
|
||||
// MergeView inserts alignment spacers on its next measure, after the panes have reflowed.
|
||||
if (anchor) {
|
||||
if (anchors) {
|
||||
let frame = requestAnimationFrame(() => {
|
||||
frame = requestAnimationFrame(() => {
|
||||
if (viewsRef.current === views) views.b.dispatch({ effects: anchor });
|
||||
if (viewsRef.current !== views) return;
|
||||
panes?.leadScroll("b");
|
||||
if (views.a && anchors.a) views.a.dispatch({ effects: anchors.a });
|
||||
views.b.dispatch({ effects: anchors.b });
|
||||
});
|
||||
});
|
||||
return () => cancelAnimationFrame(frame);
|
||||
@@ -1630,7 +1641,7 @@ export function DiffEditor({
|
||||
className={clsx(
|
||||
styles.host,
|
||||
opts.highlight === "none" && styles.noHighlight,
|
||||
viewMode === "split" && !aligned && styles.unaligned,
|
||||
viewMode === "split" && !fit && styles.paneScroll,
|
||||
)}
|
||||
hidden={!!binary || !!error || !ready}
|
||||
aria-label={diffTitle(data)}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from "vitest";
|
||||
import { Chunk, MergeView } from "@codemirror/merge";
|
||||
import { Text } from "@codemirror/state";
|
||||
import type { EditorView } from "@codemirror/view";
|
||||
import { bandAt, bandPath, transferY, UnalignedPanes, type Band } from "../diffScroll";
|
||||
import { bandAt, bandPath, transferY, PaddedPanes, UnalignedPanes, type Band } from "../diffScroll";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
@@ -179,6 +179,70 @@ describe("synchronized navigation scrolling", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("padded pane scrolling (aligned diff)", () => {
|
||||
function panes(sync = true) {
|
||||
const root = document.createElement("div");
|
||||
const view = () => {
|
||||
const wrapper = root.appendChild(document.createElement("div"));
|
||||
const dom = wrapper.appendChild(document.createElement("div"));
|
||||
const scrollDOM = dom.appendChild(document.createElement("div"));
|
||||
Object.defineProperty(scrollDOM, "scrollHeight", { value: 4000 });
|
||||
scrollDOM.scrollTo = vi.fn();
|
||||
return { dom, scrollDOM } as unknown as EditorView;
|
||||
};
|
||||
const a = view();
|
||||
const b = view();
|
||||
const controller = new PaddedPanes(sync);
|
||||
controller.attach({ a, b } as unknown as MergeView);
|
||||
const scroll = (view: EditorView, top: number) => {
|
||||
view.scrollDOM.scrollTop = top;
|
||||
view.scrollDOM.dispatchEvent(new Event("scroll"));
|
||||
};
|
||||
return { a, b, controller, scroll };
|
||||
}
|
||||
|
||||
it("copies the scrolled pane's top to the other one", () => {
|
||||
const p = panes();
|
||||
try {
|
||||
p.scroll(p.b, 500);
|
||||
expect(p.a.scrollDOM.scrollTop).toBe(500);
|
||||
p.scroll(p.a, 200);
|
||||
expect(p.b.scrollDOM.scrollTop).toBe(200);
|
||||
} finally {
|
||||
p.controller.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
it("leaves the panes alone when synchronized scrolling is off", () => {
|
||||
const p = panes(false);
|
||||
try {
|
||||
p.scroll(p.b, 500);
|
||||
expect(p.a.scrollDOM.scrollTop).toBe(0);
|
||||
} finally {
|
||||
p.controller.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the navigation source in charge while its scroll animates", () => {
|
||||
vi.useFakeTimers();
|
||||
const p = panes();
|
||||
try {
|
||||
p.controller.scrollTo("b", { top: 2000, behavior: "smooth" });
|
||||
p.scroll(p.b, 300);
|
||||
expect(p.a.scrollDOM.scrollTop).toBe(300);
|
||||
// The follower's echo must not pull the source back.
|
||||
p.scroll(p.a, 300);
|
||||
expect(p.b.scrollDOM.scrollTop).toBe(300);
|
||||
vi.advanceTimersByTime(150);
|
||||
p.scroll(p.a, 100);
|
||||
expect(p.b.scrollDOM.scrollTop).toBe(100);
|
||||
} finally {
|
||||
p.controller.destroy();
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("divider arrows", () => {
|
||||
const dividerOf = (arrows: { toA: boolean; toB: boolean } | undefined) => {
|
||||
const root = document.createElement("div");
|
||||
|
||||
@@ -64,15 +64,10 @@ describe("diff overview stripe (#1505)", () => {
|
||||
expect(mv.b.state.facet(scrollbarMarks)).toEqual([]);
|
||||
});
|
||||
|
||||
it("draws the aligned view's lane in the right pane over the shared scroller", () => {
|
||||
const mv = splitView([], diffOverview("aligned"));
|
||||
const lane = mv.b.dom.firstElementChild as HTMLElement;
|
||||
expect(lane.classList.contains("cm-openidea-overview-sticky")).toBe(true);
|
||||
expect(mv.dom.classList.contains("cm-openidea-overviewScroller")).toBe(true);
|
||||
expect(mv.a.dom.querySelector(".cm-openidea-overview")).toBeNull();
|
||||
mv.destroy();
|
||||
expect(mv.dom.classList.contains("cm-openidea-overviewScroller")).toBe(false);
|
||||
merge = null;
|
||||
it("gives every split pane its own overview lane", () => {
|
||||
const mv = splitView();
|
||||
expect(mv.a.dom.querySelector(".cm-openidea-overview")).not.toBeNull();
|
||||
expect(mv.b.dom.querySelector(".cm-openidea-overview")).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ import { scrollbarMarks, type ScrollbarMark } from "@/editor/api";
|
||||
import { openideaHighlightStyle } from "@/editor/core/cm/highlight";
|
||||
import { languageDescriptionFor, loadSupport } from "@/editor/core/cm/languages";
|
||||
import { findExtension } from "@/editor/core/cm/find";
|
||||
import { overviewLane, overviewLaneIn } from "@/editor/core/cm/lanes";
|
||||
import { overviewLane } from "@/editor/core/cm/lanes";
|
||||
import { LINE_INSET } from "@/editor/core/cm/lineInset";
|
||||
import { caretExtension } from "@/editor/core/cm/prefs";
|
||||
import { fontExtension, tooltipBounds } from "@/editor/core/cm/setup";
|
||||
@@ -236,7 +236,7 @@ const changedLines = chunkDecorations(lineDeco);
|
||||
const stripeColor = byKind((kind) => `var(--diff-${kind}-stripe)`);
|
||||
|
||||
/** The diff viewer's layouts, as the overview lane sees them. */
|
||||
export type DiffLayout = "split" | "aligned" | "unified";
|
||||
export type DiffLayout = "split" | "unified";
|
||||
|
||||
/**
|
||||
* The changes on the overview lane as marks across its width, after IDEA's diff error stripe
|
||||
@@ -278,14 +278,11 @@ const chunkMarks = (unified: boolean) =>
|
||||
});
|
||||
|
||||
/**
|
||||
* The overview lane with the diff's changes, over the pane's scrollbar. The panes of an aligned
|
||||
* side-by-side diff scroll together in the MergeView, whose scrollbar the right pane's lane takes.
|
||||
* The overview lane with the diff's changes, over the pane's scrollbar. Every
|
||||
* diff pane scrolls itself, so each carries its own lane.
|
||||
*/
|
||||
export function diffOverview(layout: DiffLayout): Extension {
|
||||
return [
|
||||
chunkMarks(layout === "unified"),
|
||||
layout === "aligned" ? overviewLaneIn((view) => view.dom.closest<HTMLElement>(".cm-mergeView")) : overviewLane,
|
||||
];
|
||||
return [chunkMarks(layout === "unified"), overviewLane];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -169,11 +169,136 @@ function loadRatio(fallback = 50): number {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* The scroll machinery of a side-by-side diff's panes: added to both editors,
|
||||
* attached to their MergeView, and torn down with the diff.
|
||||
*/
|
||||
export interface DiffPanes {
|
||||
/** Extensions for both editors. */
|
||||
extension: Extension;
|
||||
attach(mv: MergeView): void;
|
||||
/** Turns synchronized scrolling on (bringing the other pane along) or off. */
|
||||
setSync(on: boolean): void;
|
||||
/** Scrolls one pane, the other following. */
|
||||
scrollTo(side: Side, options: ScrollToOptions): void;
|
||||
destroy(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scroll synchronization for the padded panes of an aligned side-by-side diff:
|
||||
* MergeView pads the panes so their lines lay out identically, so one `scrollTop`
|
||||
* fits both. Each pane scrolls itself, which keeps its horizontal scrollbar at
|
||||
* the pane's bottom edge — a content-tall scroller would render it at the bottom
|
||||
* of the content, out of reach until the diff is scrolled to its end.
|
||||
*/
|
||||
export class PaddedPanes implements DiffPanes {
|
||||
readonly extension: Extension = [];
|
||||
private mv: MergeView | null = null;
|
||||
private sync: boolean;
|
||||
private scrolling: Side | null = null;
|
||||
private scrollTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
/** The scroll position a pane was last moved to by the other one, to tell its echo from the user. */
|
||||
private readonly moved: Record<Side, number | null> = { a: null, b: null };
|
||||
private readonly cleanup: (() => void)[] = [];
|
||||
|
||||
constructor(sync: boolean) {
|
||||
this.sync = sync;
|
||||
}
|
||||
|
||||
attach(mv: MergeView) {
|
||||
this.mv = mv;
|
||||
for (const side of ["a", "b"] as const) {
|
||||
const el = this.view(side).scrollDOM;
|
||||
const onScroll = () => this.scrolled(side);
|
||||
el.addEventListener("scroll", onScroll, { passive: true });
|
||||
this.cleanup.push(() => el.removeEventListener("scroll", onScroll));
|
||||
}
|
||||
this.resync();
|
||||
}
|
||||
|
||||
/** Brings the left pane in line with the right one. */
|
||||
resync() {
|
||||
if (this.mv && this.sync) this.follow("b");
|
||||
}
|
||||
|
||||
setSync(on: boolean) {
|
||||
if (on === this.sync) return;
|
||||
this.interruptScroll();
|
||||
this.sync = on;
|
||||
this.resync();
|
||||
}
|
||||
|
||||
scrollTo(side: Side, options: ScrollToOptions) {
|
||||
this.interruptScroll();
|
||||
this.scrolling = this.sync ? side : null;
|
||||
this.releaseScroll();
|
||||
this.view(side).scrollDOM.scrollTo(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks `side` as the source of a layout-driven scroll (a soft-wrap toggle re-anchors it), so the
|
||||
* other pane's delayed reflow follows it instead of being mistaken for a user scroll pulling back.
|
||||
*/
|
||||
leadScroll(side: Side) {
|
||||
this.scrolling = this.sync ? side : null;
|
||||
this.releaseScroll();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
clearTimeout(this.scrollTimer);
|
||||
for (const f of this.cleanup) f();
|
||||
this.mv = null;
|
||||
}
|
||||
|
||||
private view(side: Side): EditorView {
|
||||
return side === "a" ? this.mv!.a : this.mv!.b;
|
||||
}
|
||||
|
||||
private scrolled(from: Side) {
|
||||
if (this.scrolling) {
|
||||
// A follower's delayed layout scroll must not pull back the navigation source.
|
||||
this.follow(this.scrolling);
|
||||
this.releaseScroll();
|
||||
return;
|
||||
}
|
||||
const el = this.view(from).scrollDOM;
|
||||
const moved = this.moved[from];
|
||||
const echo = moved !== null && Math.abs(el.scrollTop - moved) <= 1;
|
||||
this.moved[from] = null;
|
||||
if (!echo && this.sync) this.follow(from);
|
||||
}
|
||||
|
||||
private follow(from: Side) {
|
||||
const to: Side = from === "a" ? "b" : "a";
|
||||
const el = this.view(to).scrollDOM;
|
||||
const top = this.view(from).scrollDOM.scrollTop;
|
||||
if (Math.abs(el.scrollTop - top) < 1) return;
|
||||
const before = el.scrollTop;
|
||||
el.scrollTop = top;
|
||||
if (el.scrollTop !== before) this.moved[to] = el.scrollTop;
|
||||
}
|
||||
|
||||
private releaseScroll() {
|
||||
clearTimeout(this.scrollTimer);
|
||||
if (this.scrolling) this.scrollTimer = setTimeout(() => (this.scrolling = null), 150);
|
||||
}
|
||||
|
||||
private interruptScroll() {
|
||||
const side = this.scrolling;
|
||||
this.scrolling = null;
|
||||
clearTimeout(this.scrollTimer);
|
||||
if (side && this.mv) {
|
||||
const el = this.view(side).scrollDOM;
|
||||
el.scrollTo({ top: el.scrollTop, behavior: "instant" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scroll synchronization and the divider for a MergeView whose panes are not
|
||||
* padded into alignment. Add `extension` to both editors, then `attach` the view.
|
||||
*/
|
||||
export class UnalignedPanes {
|
||||
export class UnalignedPanes implements DiffPanes {
|
||||
readonly extension: Extension;
|
||||
private mv: MergeView | null = null;
|
||||
private readonly divider = document.createElement("div");
|
||||
|
||||
@@ -263,10 +263,6 @@ a:hover {
|
||||
::-webkit-scrollbar-corner {
|
||||
background: transparent;
|
||||
}
|
||||
/* An outer scroller whose scrollbar the editor's overview lane draws (cm/lanes.ts `overviewLaneIn`). */
|
||||
.cm-openidea-overviewScroller::-webkit-scrollbar:vertical {
|
||||
width: 0;
|
||||
}
|
||||
|
||||
/* Settings › Appearance › Antialiasing (IDE / Editor). */
|
||||
:root[data-aa-ide="subpixel"] body {
|
||||
|
||||
Reference in New Issue
Block a user