Skip to content

Cursor drawing: showCursorWhenSelecting, cursorHeight, fat cursor, overwriteToggle (Refs #51) - #92

Draft
DJAscendance wants to merge 14 commits into
masterfrom
feat/cursor-drawing
Draft

DJAscendance wants to merge 14 commits into
masterfrom
feat/cursor-drawing

Conversation

@DJAscendance

@DJAscendance DJAscendance commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Refs #51

Ledger rows: 469 toggleOverwrite, 671 prepareSelection, 672 drawSelectionCursor.

Base: 7d917785a1b65259b238f35a3fad98343d8a6d02 (master, merged in); parent PR head 5ee41bf7dfbe0684c885a175941224129bfe97ad
Head: 0b167ad942cf24b7f95e94d863f17a85bddfe080

Integration: Stacked on #91 (5ee41bf); merge in order #81, #83, #86, #89, #90, #91, #92. Until the PR below it merges, this PR's diff against master includes that PR.

What this adds

  • Editor::cursor_rects() -> Vec<(Pos, Rect)>. These are the cursors prepareSelection draws, with drawSelectionCursor's geometry:
    • Which ranges get a cursor: ranges that reach [viewFrom, viewTo). Empty ranges always get one; with showCursorWhenSelecting, every range does.
    • Position: cursorCoords(head, "div") left/top.
    • Height: max(0, bottom - top) * cursorHeight.
    • Width: with the cm-fat-cursor class, charCoords right - left, or defaultCharWidth() at the end of a line. Otherwise 0.
  • New EditorOptions fields: show_cursor_when_selecting (default false), cursor_height (default 1) and fat_cursor (the wrapper class the vim keymap's normal mode sets).
  • The renderer (render.rs, cursor block only) draws from cursor_rects. A fat cursor takes its width, and the normal and overwrite cursor widths are unchanged.
  • toggle_overwrite fires overwriteToggle (Editor::on_overwrite_toggle, immediate) when the state changes.

Upstream refs (pinned 4ca3d05)

src/display/selection.js:13-60; src/edit/methods.js:352-360; lib/codemirror.css:45-61.

Tests and evidence

  • oracle/cm5-cursors.mjs records 14 cases from CodeMirror 5.65.21 in headless Chromium into crates/cm-view/tests/data/cm5-cursors.json. Page metrics: Metrics::oracle(), setSize(600, 300). The cases cover:
    • empty cursor and tab;
    • a selection with and without showCursorWhenSelecting;
    • multiple ranges;
    • cursorHeight 0.5 and 0;
    • fat cursor on a character, on a tab, at end of line, and with cursorHeight;
    • ranges outside the view and a range that reaches into it.
  • crates/cm-view/tests/cursors.rs::cursors_match_cm5: every cursor's left, top, height and fat width match CM5 within 0.01 px.
  • crates/cm-view/tests/cursors.rs::overwrite_toggle_event
  • The visual_render cursor goldens are unchanged.
  • Strict, on the top of the stack (Cursor drawing: showCursorWhenSelecting, cursorHeight, fat cursor, overwriteToggle (Refs #51) #92 0b167ad, which contains this PR and every PR below it): cargo test --workspace --no-fail-fast gave 834 passed, 0 failed, 0 ignored. The same under xvfb-run -a: 834 passed, 0 failed. tools/web-test.sh: all 5 browser profiles passed 63/63.
  • cm-web is not touched.

Known differences

  • No bidi secondary cursor (pos.other).
  • singleCursorHeightPerLine: false is not modeled.
  • $customCursor and the primary = false path are not ported.
  • The selection-range half of prepareSelection is unchanged and not compared here.

Coverage change

  • 469: PARTIAL → PORTED
  • 671, 672: PARTIAL → PARTIAL (narrower)

After the merge, cursor_rects uses CoordSys::Doc, the internal frame after #81.

CI: pending (Actions cannot start: billing)

)

cursorCoords/charCoords/coordsChar/lineAtHeight/heightAtLine method forms
(cursor_coords_in, char_coords_in, coords_char_in, line_at_height_in,
height_at_line_in) with page and window modes over a host-supplied
window origin and page scroll, start=null/true/false, clipPos, LineHandle
arguments, includeWidgets (no widgets yet) and the end-of-document
adjustment; intoCoordSystem/fromCoordSystem ported with CM5's div
(viewOffset) semantics. The port's internal document-y frame is now
CoordSys::Doc. Checked against a CM5 recording (cm5-coord-systems.json).
… (Refs #54)

get_scroll_info() returns CM5's {left, top, height, width, clientHeight,
clientWidth}; scroll_into_view_target takes null (cursor plus
cursorScrollMargin), a line number, a position, a range, a coordinate box
or a box range (scrollToCoordsRange); scroll_rect_into_view is the
immediate, thresholded scrollIntoView(cm, rect) helper. Checked against a
CM5 recording (cm5-scroll-api.json), including scrollTo and scrollIntoView
inside one operation.
NamedListeners (one copy-on-write list per event name) and cm_core::Emitter
port on/off/getHandlers/signal/hasHandler for string-named and custom
events on any object; the editor, documents, line handles and markers get
a named table beside their typed lists. Editor::signal_dom_event and
on_dom_event port signalDOMEvent; key_down, key_press, mouse_down, paste
and maybeScrollWindow (scrollCursorIntoView) signal before handling, and a
handler that calls prevent_default or sets codemirror_ignore pre-empts
the editor.
… reset (Refs #50)

The editor keeps the blink: restart_blink on focus (also when already
focused), after an operation that changed the selection and after a
handled key; cursor_blink_visible / next_blink_in follow the
cursorBlinkRate option (> 0 blinks, 0 shows, < 0 hides). Blurring stops
the blink and schedules onBlur's 150 ms display.shift reset, run by the
host through run_timers / next_timer_in. The native window draws and
wakes from these instead of its fixed 530 ms timer.
…t hooks, autofocus, phrase (Refs #41)

set_option/get_option map CM5 option names onto EditorOptions, run each
option's handler in an operation (mode, indentUnit, tabSize, lineWrapping,
the gutter options, resetModeState options, undoDepth, viewportMargin) and
fire optionChange. define_option adds custom options with handlers run on
init (CodeMirror.Init) unless notOnInit; the constructor ends with
finishInit, the defineInitHook hooks and autofocus's 20 ms onFocus timer.
line_number_for applies lineNumberFormatter; phrase reads the phrases
option. Stacked on feat/focus-blink (run_timers).
swap_doc detaches the old document, attaches the new one, keeps each
document's scroll position, loads doc.modeOption, resets the highlight
state, line heights and longest line, scrolls to the new document's
position and fires swapDoc delayed. refresh() is the methodOp of
methods.js:411 with the immediate refresh event; set_metrics runs it.
on_resize applies re-measured metrics and setSize. Doc gains
set_mode_option, scroll_pos and set_scroll_pos; the editor writes
doc.modeOption when the mode is set.
…erwriteToggle (Refs #51)

Editor::cursor_rects is prepareSelection/drawSelectionCursor's cursor
geometry: cursors for empty ranges or every range with
showCursorWhenSelecting, among ranges reaching [viewFrom, viewTo), with
cursorHeight scaling and the cm-fat-cursor width (charCoords width or
defaultCharWidth). The renderer draws from it. toggleOverwrite fires
overwriteToggle. Checked against a CM5 recording (oracle/cm5-cursors.mjs).
into_coord_system honors include_widgets as CM5 does: without it the box
moves below the widgets above the line (widgetTopHeight), before the
"line" early return; master's into_coords path is the include_widgets =
false case. Exports keep both sides' items. New test
into_coord_system_include_widgets.
Keep both listener sets (gutter/line-widget events and named events).
… uses lineNumberFor

Keep both sides' options fields, listeners and README sections. The
native renderer, cm-web's frame and the line-number width now format
numbers through line_number_for (lineNumberFor: lineNumberFormatter(i +
firstLineNumber), without subtracting doc.first, as update_line.js:114
and line_numbers.js:34 do). Render number buffers are keyed by text.
New test line_number_formatter_sizes_gutter.
Keep both listener sets. Use master's reg_change_all(first, size). swap_doc
also recomputes has_widgets for the new document and carries addModeClass
into the fresh highlight state. New test swap_doc_line_widget_heights.
…wing

Keep both sides' options and listeners. cursor_rects uses CoordSys::Doc,
the internal document frame after #81 (CM5's "div" plus viewOffset).
@github-actions

Copy link
Copy Markdown

MiniMax-M3.1-Flash QA

Findings

[High] Newly added built-in options are not exposed through set_option / get_option

Files: crates/cm-view/src/options.rs, crates/cm-view/src/editor.rs

EditorOptions now includes:

  • cursor_height → CM5 "cursorHeight"
  • show_cursor_when_selecting → CM5 "showCursorWhenSelecting"
  • finish_init → CM5 "finishInit"

However, none of these names appears in BUILT_IN, and get_field / set_field do not handle them. Consequently:

cm.get_option("cursorHeight");       // None
cm.set_option("cursorHeight", 0.5);  // Err(Unknown(...))

This contradicts the new generic options API and makes the newly introduced options impossible to configure through it. finishInit may be constructor-only in practice, but CM5 still treats it as a built-in option, while cursorHeight and showCursorWhenSelecting are runtime options and must be handled.

Please add the relevant names to BUILT_IN and implement their get/set behavior, including appropriate validation. Add round-trip tests that set these options and verify their effects.


[Medium] Public scroll targets are not clipped before being passed to the scrolling engine

File: crates/cm-view/src/editor.rs

scroll_into_view_target forwards positions and line numbers directly:

ScrollTarget::Line(n) => (Pos::new(n, 0), Pos::new(n, 0)),
ScrollTarget::Pos(p) => (p, p),
ScrollTarget::Range(a, b) => (a, b),

Unlike cursor_coords_in and char_coords_in, which call self.doc.clip_pos, this path does not validate or clip positions against the current document. Public callers can therefore pass an out-of-range line/character or range, potentially producing invalid measurements or panics in the underlying scroll calculation.

Clip all position-based targets before resolving the scroll, and clamp line targets to the document’s valid line range. Add tests for:

  • a position beyond the last line;
  • a character beyond the line length;
  • a reversed or out-of-range range;
  • a line number before the first line or after the last line.

[Medium] Formatted line-number positioning uses a different character count than gutter sizing

Files: crates/cm-view/src/editor.rs, crates/cm-view/src/render.rs

Gutter sizing uses UTF-16 code-unit length:

self.line_number_for(last).encode_utf16().count()

This matches JavaScript string-length semantics and CM5’s recorded behavior. The renderer, however, computes the label width using:

let digits = n.chars().count() as f32;

For a formatter producing non-ASCII text, and especially characters outside the BMP, chars().count() differs from encode_utf16().count(). The label can consequently be positioned using a different width than the gutter reserved for it.

Use the same UTF-16 count for positioning, or preferably measure the actual rendered label width. Add a formatter test containing a non-BMP character, such as 😀, and verify both gutter width and rendered alignment.


Overall assessment

The PR has extensive CM5 oracle coverage and generally strong documentation, but the generic options API currently omits several built-in options introduced by this same change. That is the main correctness issue and should be addressed before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant