Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions src/webview/cm/decorations/block-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@
// quote (calloutTypeForOutermost) so a nested `> >` inner marker never double-emits
// a second type class — the container type wins. When the caret is INSIDE the block
// the `[!TYPE]` marker row is REVEALED and gets `quoll-callout-marker` (header
// weight); when OUTSIDE, the marker StateField conceals the whole row and this
// module migrates the rounded `-open` corner onto the first
// VISIBLE body line via the same-source `calloutMarkerConceal` predicate. The
// weight); when OUTSIDE, the marker StateField conceals the marker — a titled row
// stays visible as the title row (still `quoll-callout-marker`), while an untitled
// row is hidden whole and this module migrates the rounded `-open` corner onto the
// first VISIBLE body line via the same-source `calloutMarkerConceal` span. The
// `[!TYPE]` marker stays literal editable text (decoration-only, byte-identical
// round-trip); an unrecognised token (`[!FOO]`) matches nothing → generic panel.
//
Expand Down Expand Up @@ -108,9 +109,10 @@
// contribute to quollSyntaxExclusionZones: frontmatter-field (its span is
// `---`-fenced, so it never parses as FencedCode / Blockquote — this gate is
// indeed cheap insurance for it) and callout-marker-conceal (concealed `[!TYPE]`
// rows ARE Blockquote syntax — the point-exclusion here is load-bearing: it skips
// the concealed row and migrates the rounded `-open` corner to the first visible
// body line; see callout-marker-conceal.ts:7-13). A shared union helper
// rows ARE Blockquote syntax — the exclusion here is load-bearing: it skips an
// untitled concealed row, whose zone covers the whole line, and the corner migrates
// to the first visible body line; a TITLED row's zone ends at the title, so
// `lineInExclusionZone` keeps styling it; see callout-marker-conceal.ts header). A shared union helper
// (Codex Conf 86) is deferred — it would also touch list-hang-indent, out of
// this PR's 1-purpose scope.
//
Expand Down Expand Up @@ -163,7 +165,7 @@ import {
hasBlockquoteAncestor,
} from "./callout.js";
import { quollSyntaxExclusionZones } from "./orchestrator.js";
import { pointInExclusionZone } from "./shared.js";
import { lineInExclusionZone } from "./shared.js";
import type { BuildContext } from "./types.js";

/** Persistent Lezer node type — the `.node` of an iterate cursor. Derived from
Expand Down Expand Up @@ -386,7 +388,7 @@ function buildBlockLineDecorations(
const toLine = doc.lineAt(clampTo - 1).number;
for (let n = fromLine; n <= toLine; n++) {
const line = doc.line(n);
if (pointInExclusionZone(line.from, zones)) {
if (lineInExclusionZone(line.from, line.to, zones)) {
continue;
}
const classes = classesForLine(n);
Expand Down Expand Up @@ -559,26 +561,28 @@ function buildBlockquoteRuleWithBoundaryInfo(
// (unknown / plain / nested-inner) → no callout class → the generic Phase-1 panel.
const calloutType = calloutTypeForOutermost(doc, node.node);
// When the caret is OUTSIDE the callout, the marker StateField conceals the
// `[!TYPE]` row (collapsed to zero height, whole line published to the exclusion
// facet). `markerConcealed` is derived from the SAME single-source predicate the
// StateField uses, on the SAME state, so the two can never disagree.
const markerConcealed = calloutMarkerConceal(doc, ctx.selection, node.node) !== null;
// Migrate the rounded edge off a concealed boundary fence OR a concealed callout
// marker onto the adjacent visible line. Start one line inward when the marker
// is concealed (it collapses to zero height), then walk past any remaining
// `[!TYPE]` marker. An UNTITLED row is hidden whole (collapsed to zero height,
// whole line published to the exclusion facet); a TITLED row hides only the
// marker and stays visible as the title row. `markerRowHidden` is derived from
// the SAME single-source span the StateField uses, on the SAME state, so the two
// can never disagree.
const markerRowHidden = calloutMarkerConceal(doc, ctx.selection, node.node)?.hidesLine === true;
// Migrate the rounded edge off a concealed boundary fence OR a hidden callout
// marker row onto the adjacent visible line. Start one line inward when the
// marker row is hidden (it collapses to zero height), then walk past any remaining
// concealed rows — a concealed LEADING fence collapses too (the callout body
// starts with a fenced block; caret outside the callout ⇒ outside the fence), so
// the visible top edge is the first non-concealed body line. This subsumes the
// fence-only single step: for a non-callout with a concealed open fence
// `markerConcealed` is false → start at nodeFirstLine, then the loop steps once
// `markerRowHidden` is false → start at nodeFirstLine, then the loop steps once
// (identical to the previous single check). closeLine is UNCHANGED.
//
// No `hasConcealableMarker` gate is added on blockquoteRule for this: the callout
// conceal is FACET-PUBLISHED, so blockquoteRule's `structural` trigger already
// rebuilds on the exclusion-facet identity change that a conceal↔reveal flip
// produces (R2). Contrast the fence case, whose conceal is NOT facet-published →
// it genuinely needs the explicit `hasBoundaryFence` selectionSet gate.
let openLine = markerConcealed ? nodeFirstLine + 1 : nodeFirstLine;
let openLine = markerRowHidden ? nodeFirstLine + 1 : nodeFirstLine;
while (openLine < nodeLastLine && isConcealed(openLine)) {
openLine += 1;
}
Expand Down Expand Up @@ -606,10 +610,11 @@ function buildBlockquoteRuleWithBoundaryInfo(
}
if (calloutType !== null) {
out.push(CALLOUT_CLASS, calloutClassForType(calloutType));
// The concealed marker line is skipped by buildBlockLineDecorations
// (facet-excluded), so this only fires on a REVEALED marker row — the
// `!markerConcealed` guard keeps the header class off the migrated row.
if (n === nodeFirstLine && !markerConcealed) {
// A hidden marker row is skipped by buildBlockLineDecorations
// (facet-excluded), so this only fires on a VISIBLE marker row (revealed,
// or concealed down to its title) — the `!markerRowHidden` guard keeps the
// header class off the migrated row.
if (n === nodeFirstLine && !markerRowHidden) {
out.push(CALLOUT_MARKER_CLASS);
}
}
Expand Down
134 changes: 78 additions & 56 deletions src/webview/cm/decorations/callout-marker-conceal.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
// StateField that CONCEALS the `[!TYPE]` marker row of a callout when the caret is
// StateField that CONCEALS the `[!TYPE]` marker of a callout when the caret is
// OUTSIDE the whole callout block (mirrors fenced-code-collapse.ts's rebuild-on-
// selection template). Per concealed marker line it emits, over the WHOLE line:
// - Decoration.line({ class: CALLOUT_MARKER_HIDDEN_CLASS }) — the zero-height
// collapse (the theme copies `.quoll-fenced-code-fence-hidden`), and
// - Decoration.replace({}) over [line.from, line.to) — hides the row content,
// and publishes [line.from, line.to] to quollSyntaxExclusionZones so:
// - the orchestrator's arbitrate() drops EVERY inline reveal decoration on that
// line (the leading `>`, inline marks, links) → exactly ONE replace covers the
// row, with no overlapping-replace crash surface (v2 Finding 2); and
// - block-style.ts skips the concealed marker line (buildBlockLineDecorations
// point-excludes it) and migrates the rounded `-open` corner
// onto the first visible body line.
// selection template). The concealed span is `calloutConcealSpan` (callout.ts): the
// whole first line when untitled, else from the first `>` to where the title starts. Per concealed marker it
// emits ONE Decoration.replace({}) over that span and publishes the SAME span to
// quollSyntaxExclusionZones, so:
// - the orchestrator's arbitrate() drops EVERY inline reveal decoration overlapping
// it (the leading `>`, anything inside the marker) → exactly ONE replace covers
// the marker, with no overlapping-replace crash surface (v2 Finding 2). Inline
// marks in the title (`**title**`) start at or after the span end, so they stay;
// - an UNTITLED row (the span is the whole line) also gets
// Decoration.line({ class: CALLOUT_MARKER_HIDDEN_CLASS }) — the zero-height
// collapse (the theme copies `.quoll-fenced-code-fence-hidden`) — and block-style
// skips that line (the zone covers it whole) and migrates the rounded `-open`
// corner onto the first visible body line. A TITLED row stays visible as the
// callout's title row and keeps its corner (REV-20261203-15).
//
// A StateField (NOT an inline reveal provider) because a marker line can carry
// inline markup (`> [!TIP] **title**`): an inline provider hiding only the
// `[!TYPE]` run while blockquote-reveal independently hides the `>` would leave TWO
// overlapping Decoration.replace on the line → CodeMirror throws and blanks the
// whole set. Owning the whole line as ONE replace + publishing the zone is the
// single-replace design (CLAUDE.md callout invariant + the plan's crash regression).
// A StateField (NOT an inline reveal provider) because the conceal is BLOCK-scoped
// (a caret anywhere in the callout reveals it) and its span must be published as an
// exclusion zone: that is what arbitrates blockquote-reveal's own `>` hide away, so
// ONE replace owns `>` + marker and no second replace can overlap it (the plan's
// crash regression, `> [!TIP] **title**`).
//
// Display-only: never mutates bytes → the `[!TYPE]` source round-trips identically.
// The concealed row is a NON-atomic inline replace, so its byte positions stay
Expand All @@ -32,31 +34,36 @@ import { Decoration, type DecorationSet, EditorView } from "@codemirror/view";
import { type Interval, intersects, mergeIntervals } from "../bounded-recompute.js";
import { hostDocumentReseed } from "../host-reseed.js";
import { expandToEnclosingBlock, requiresFullBoundedRebuild } from "../structural-guard.js";
import { CALLOUT_MARKER_HIDDEN_CLASS, calloutTypeForOutermost } from "./callout.js";
import { CALLOUT_MARKER_HIDDEN_CLASS, calloutConcealSpan } from "./callout.js";
import { quollSyntaxExclusionZones } from "./orchestrator.js";
import { intersectsAnySelection } from "./shared.js";

type Zone = { from: number; to: number };
type Marker = Zone & { hidesLine: boolean };

/** Selection-INDEPENDENT facts about one outermost callout that HAS a body (a
* marker-only callout is never a record — it never conceals). `markerFrom` is
* both the block's first-line start AND the concealed marker span's start;
* `blockTo` is the block's last CONTENT line end (`doc.lineAt(node.to - 1).to`,
* marker-only callout is never a record — it never conceals). `blockFrom` is the
* block's first-line start (the reveal extent's start and the unique de-dup key);
* [markerFrom, markerTo) + `hidesLine` are `calloutConcealSpan`'s result (a titled
* indented row's `markerFrom` sits past the indent); `blockTo` is the block's last
* CONTENT line end (`doc.lineAt(node.to - 1).to`,
* robust to a Lezer `to` overshoot per [[quoll-lezer-table-to-overshoots-trailing-line]]).
* The selection-dependent conceal decision is applied later in `deriveMarkers`. */
interface CalloutRecord {
blockFrom: number;
markerFrom: number;
markerTo: number;
hidesLine: boolean;
blockTo: number;
}

interface ConcealState {
/** Selection-independent, doc-ordered by `markerFrom`. The changed-range bounded
/** Selection-independent, doc-ordered by `blockFrom`. The changed-range bounded
* recompute (Task 2) reuses/rebuilds THIS; markers/decorations/zones derive from it. */
records: readonly CalloutRecord[];
/** Concealed marker-line spans for the CURRENT selection, doc-ordered. Compared on
* a selection-only move to keep `prev` verbatim (F3). */
markers: readonly Zone[];
markers: readonly Marker[];
decorations: DecorationSet;
/** Published exclusion zones — same content as `markers`, a distinct field so the
* facet provider returns a stable reference whenever `update` keeps `prev` (F3). */
Expand All @@ -69,7 +76,7 @@ interface ConcealState {
* iterate materialises only the touched subtree — the whole point of the bounding
* (PERF.md: the cost is whole-tree materialisation, NOT node descent, so this is
* the changed-range shape, NOT a prune-descent). Doc order → already sorted by
* `markerFrom`. Selection-INDEPENDENT: the caret does not affect record membership.
* `blockFrom`. Selection-INDEPENDENT: the caret does not affect record membership.
* `calloutTypeForOutermost` already excludes nested (`> >` inner) AND list-nested
* (`- > [!NOTE]`) blockquotes (the marker regex anchors at `^ {0,3}>`), so no
* container-descent special-casing is needed — a top-level callout is the only shape
Expand All @@ -84,15 +91,20 @@ function buildRange(state: EditorState, rangeFrom: number, rangeTo: number): Cal
from: rangeFrom,
to: rangeTo,
enter: (node) => {
if (node.name !== "Blockquote" || calloutTypeForOutermost(doc, node.node) === null) {
if (node.name !== "Blockquote") {
return;
}
const firstLine = doc.lineAt(node.from);
const lastLine = doc.lineAt(node.to - 1);
if (lastLine.number === firstLine.number) {
return; // marker-only callout: never a record (never conceals)
const span = calloutConcealSpan(doc, node.node); // null: not a callout / marker-only
if (span === null) {
return;
}
out.push({ markerFrom: firstLine.from, markerTo: firstLine.to, blockTo: lastLine.to });
out.push({
blockFrom: doc.lineAt(node.from).from,
markerFrom: span.from,
markerTo: span.to,
hidesLine: span.hidesLine,
blockTo: doc.lineAt(node.to - 1).to,
});
},
});
return out;
Expand All @@ -116,73 +128,83 @@ function computeExtendedSpan(tr: Transaction): Interval[] {
}

/** Changed-range bounded record recompute: reuse every prior record whose FULL block
* extent [markerFrom, blockTo] is neither TOUCHED by the change nor intersecting the
* extent [blockFrom, blockTo] is neither TOUCHED by the change nor intersecting the
* extendedSpan (position-mapped through `tr.changes`), and re-walk the tree only
* INSIDE each span interval. Soundness: a callout's record can change only from
* within its block (interior change → touched → re-walked, and the overlapping
* Blockquote node — even one starting far above the span — is re-entered by the
* bounded `iterate` and re-emitted with its true marker line) or from an adjacent
* line (G1 ±1 → intersects span → re-walked). De-dup by `markerFrom` (unique per
* line (G1 ±1 → intersects span → re-walked). De-dup by `blockFrom` (unique per
* callout): a fresh walk over a span always wins over a reused mapping. */
function computeBoundedRecords(
prev: readonly CalloutRecord[],
tr: Transaction,
intervals: Interval[]
): CalloutRecord[] {
const byMarker = new Map<number, CalloutRecord>();
const byBlock = new Map<number, CalloutRecord>();
for (const rec of prev) {
const touched = tr.changes.touchesRange(rec.markerFrom, rec.blockTo) !== false;
const newFrom = tr.changes.mapPos(rec.markerFrom, 1);
const touched = tr.changes.touchesRange(rec.blockFrom, rec.blockTo) !== false;
const newFrom = tr.changes.mapPos(rec.blockFrom, 1);
const newTo = tr.changes.mapPos(rec.blockTo, -1);
if (!touched && !intersects(intervals, newFrom, newTo)) {
const markerTo = tr.changes.mapPos(rec.markerTo, -1);
if (newFrom === rec.markerFrom && markerTo === rec.markerTo && newTo === rec.blockTo) {
// Untouched: the marker span moves rigidly with the block start.
const shift = newFrom - rec.blockFrom;
if (shift === 0 && newTo === rec.blockTo) {
// Zero-shift reuse: return the SAME object (block structure unchanged AND
// positions unmoved — an edit strictly BELOW the record). Object identity is
// the non-vacuity anchor the reuse test asserts (mirrors imageBlockField
// reusing the exact deco on a no-shift).
byMarker.set(rec.markerFrom, rec);
byBlock.set(rec.blockFrom, rec);
} else {
// Position shift only: block structure unchanged (untouched + outside span).
byMarker.set(newFrom, { markerFrom: newFrom, markerTo, blockTo: newTo });
byBlock.set(newFrom, {
blockFrom: newFrom,
markerFrom: rec.markerFrom + shift,
markerTo: rec.markerTo + shift,
hidesLine: rec.hidesLine,
blockTo: newTo,
});
}
}
}
for (const iv of intervals) {
for (const rec of buildRange(tr.state, iv.from, iv.to)) {
byMarker.set(rec.markerFrom, rec); // fresh wins (a callout spanning two intervals de-dupes)
byBlock.set(rec.blockFrom, rec); // fresh wins (a callout spanning two intervals de-dupes)
}
}
return [...byMarker.values()].sort((a, b) => a.markerFrom - b.markerFrom);
return [...byBlock.values()].sort((a, b) => a.blockFrom - b.blockFrom);
}

/** Apply the SELECTION-dependent conceal decision to a record list. A callout's
* marker row conceals iff no selection range intersects its full block span
* [markerFrom, blockTo] — identical to `calloutBlockRevealed` / `calloutMarkerConceal`
* [blockFrom, blockTo] — identical to `calloutBlockRevealed` / `calloutMarkerConceal`
* in callout.ts (the marker-only + not-a-callout cases are already excluded at
* record-build time). Records are doc-ordered, so markers come out doc-ordered. */
function deriveMarkers(records: readonly CalloutRecord[], state: EditorState): Zone[] {
const markers: Zone[] = [];
function deriveMarkers(records: readonly CalloutRecord[], state: EditorState): Marker[] {
const markers: Marker[] = [];
for (const rec of records) {
if (!intersectsAnySelection(state.selection, rec.markerFrom, rec.blockTo)) {
markers.push({ from: rec.markerFrom, to: rec.markerTo });
if (!intersectsAnySelection(state.selection, rec.blockFrom, rec.blockTo)) {
markers.push({ from: rec.markerFrom, to: rec.markerTo, hidesLine: rec.hidesLine });
}
}
return markers;
}

/** The per-marker line-deco + replace share the SAME line.from; a RangeSetBuilder
* REJECTS a replace added before a line decoration at the same pos (line decos have
* a lower startSide). `Decoration.set(_, true)` sorts them so CM accepts the pair. */
function buildDecorations(markers: readonly Zone[]): DecorationSet {
const ranges = markers.flatMap((m) => [
Decoration.line({ class: CALLOUT_MARKER_HIDDEN_CLASS }).range(m.from),
Decoration.replace({}).range(m.from, m.to),
]);
/** An untitled marker's line-deco + replace share the SAME line.from; a
* RangeSetBuilder REJECTS a replace added before a line decoration at the same pos
* (line decos have a lower startSide). `Decoration.set(_, true)` sorts them so CM
* accepts the pair. */
function buildDecorations(markers: readonly Marker[]): DecorationSet {
const ranges = markers.flatMap((m) => {
const replace = Decoration.replace({}).range(m.from, m.to);
return m.hidesLine
? [Decoration.line({ class: CALLOUT_MARKER_HIDDEN_CLASS }).range(m.from), replace]
: [replace];
});
return Decoration.set(ranges, true);
}

function assemble(records: readonly CalloutRecord[], markers: readonly Zone[]): ConcealState {
function assemble(records: readonly CalloutRecord[], markers: readonly Marker[]): ConcealState {
return {
records,
markers,
Expand Down
Loading
Loading