Skip to content

Named and custom events, getHandlers, signalDOMEvent (Refs #37) - #86

Draft
DJAscendance wants to merge 6 commits into
masterfrom
feat/named-events
Draft

DJAscendance wants to merge 6 commits into
masterfrom
feat/named-events

Conversation

@DJAscendance

@DJAscendance DJAscendance commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Refs #37

Ledger rows: 295 on, 296 getHandlers, 297 off, 298 signal, 299 signalDOMEvent. Also 657 maybeScrollWindow (the scrollCursorIntoView veto).

Base: 7d917785a1b65259b238f35a3fad98343d8a6d02 (master, merged in); parent PR head 2919bd97af2043426f40b1d3558340e9bcf56959
Head: 910c2e085efcd7cd883b5f79dd72b1391fbf8376

Integration: Stacked on #83 (2919bd9); 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

  • cm_core::events::NamedListeners<H>: one copy-on-write Listeners per event name. It provides on_rc, remove/off by id, remove_fn/off_fn by function identity per name, get_handlers (a snapshot, empty for an unknown name) and has_handler. Its semantics are CM5's: a running signal keeps the list it started with.
  • cm_core::Emitter is CodeMirror.on/off/signal on any object, such as an addon's hint data. Values travel as Args = &[&dyn Any], and handlers downcast them.
  • Named tables beside the typed lists:
    • Editor: on_event, on_event_rc, off_event_fn, get_handlers, has_handler, signal_event. Handlers get &mut Cx. Editor::off covers named ids too.
    • Doc: on_event, off_event_fn, get_handlers, has_handler, signal_event. Doc::off covers named ids.
    • LineHandle and TextMarker: on_event, get_handlers, signal_event. off covers named ids.
  • Typed events stay typed (decision D7). A typed event's name in the named table is a separate entry, and only a named signal_event reaches it. This is documented and tested.
  • signalDOMEvent: DomEvent carries kind, key/ch, mouse or text, plus prevent_default() and set_codemirror_ignore(). Editor::signal_dom_event(&e, override) returns defaultPrevented || codemirrorIgnore, and Editor::on_dom_event(kind, f) registers a handler. It is wired where CM5 calls it:
    • key_down: key_events.js:111. It runs before display.shift is set, as in CM5.
    • key_press: key_events.js:154.
    • mouse_down: mouse_events.js:61. This is a 7-line insertion at the top of the function.
    • paste: TextareaInput.js:50.
    • scrollCursorIntoView: scrolling.js:16. When vetoed, the adapter gets no window rect. The editor's own scroll still happens.

Upstream refs (pinned 4ca3d05)

src/util/event.js:11-56; src/edit/key_events.js:111,154; src/edit/mouse_events.js:61; src/input/TextareaInput.js:50; src/display/scrolling.js:16.

Tests and evidence

crates/cm-view/tests/named_events.rs has 8 tests:

  • Emitter values, ordering and identity removal.
  • getHandlers snapshot semantics during a signal (removed still runs, added does not).
  • Typed and named namespaces.
  • Doc, line and marker tables.
  • keydown pre-empted by preventDefault and by codemirrorIgnore, and passed through otherwise.
  • keypress, mousedown and paste pre-emption.
  • The override name.
  • The scrollCursorIntoView veto.

The existing typed-event suites pass unchanged.

Strict suite, before integration: 790 passed, 0 failed, both normally and under xvfb. Without xvfb, the first run had 1 failure, the known app_script flake; the rerun was clean.

Known differences

  • The port has no keyup, dblclick, contextmenu, drag/drop, cut/copy or touch entry points. A host that handles those events calls signal_dom_event itself.
  • Handlers of scrollCursorIntoView run inside the display phase, as in CM5's endOperation. They should only call prevent_default.
  • The unported editor events (scroll, swapDoc, refresh, optionChange, gutterClick) belong to their own rows.

Coverage change

  • 295, 296, 297, 298: PARTIAL → PORTED
  • 299: NOT_PORTED → PORTED
  • 657: PARTIAL → PARTIAL (the veto is now ported; it is still adapter-only)

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.
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).
@github-actions

Copy link
Copy Markdown

MiniMax-M3.1-Flash QA

Review summary

The PR is substantial and generally well structured. The new APIs are covered by focused CM5 oracle recordings, and the changes consistently migrate internal callers from CoordSys::Div to the new internal CoordSys::Doc coordinate system. The named-event implementation also appears to preserve the intended snapshot semantics: handlers added during a signal are not invoked, while handlers removed during the signal still run.

I do not see a clearly reproducible blocking defect in the provided diff. A few areas deserve follow-up before merging.

1. Please verify the CoordSys::Local behavior against the actual CM5 DOM model

from_coord_system currently treats Local as:

left += ox + self.gutter_width - self.display.scroll_left;
top += oy - self.display.scroll_top;

while into_coord_system treats Local as:

y_off += self.metrics.padding_top;

and line_space_origin() includes both padding_top and view_offset().

This produces internally consistent round trips for the current implementation, but the comments describe "local" as relative to the sizer, whereas the implementation appears to model it relative to the text area/line-space origin. The distinction matters if a host uses the returned coordinates directly to position a DOM element or overlay.

Please add a test that verifies the absolute/local origin against the CM5 recording, particularly:

  • a scrolled editor,
  • an editor with padding_top,
  • an editor with a nonzero view_offset,
  • and an editor with a gutter.

If the current model intentionally differs from the browser’s sizer coordinates, that should be documented explicitly and tested separately from the CM5 compatibility surface.

2. Please add coverage for Emitter::off re-entrancy

The Emitter documentation states that removing the last handler may allow the handler’s destructor to use the same emitter again. The implementation appears designed to release the RefCell borrow before dropping the removed entry, which is good, but this behavior is not explicitly covered by the new tests.

Please add a regression test where:

  1. an emitter handler owns a value whose destructor calls back into the emitter;
  2. the handler is removed with off;
  3. the callback successfully registers or signals another handler.

This protects the borrow/drop ordering against future refactors.

3. The DomEvent API should make the event kind’s mutability contract explicit

DomEvent.kind is a public String, while the flags are stored in Cell<bool>. This is consistent with the intended use, but the public type currently permits callers to construct arbitrary event payloads and allows the event kind to be changed after registration.

Consider documenting that:

  • the kind is metadata and should normally be immutable;
  • prevent_default and set_codemirror_ignore are the supported way for a handler to affect editor behavior;
  • handlers registered through on_dom_event should use the same event name passed to signal_dom_event.

This is not a functional blocker, but it will reduce ambiguity for host implementations.

4. The coordinate-system migration should include an API-level compatibility audit

The internal migration from CoordSys::Div to CoordSys::Doc is extensive and appears intentional. However, CoordSys is publicly exported, and existing callers may reasonably have used CoordSys::Div as the public coordinate system.

Please confirm that the intended public contract is:

  • Div: CM5 "div" coordinates;
  • Doc: an additional internal/document coordinate system;
  • Page, Window, and Local: host-facing coordinate systems.

If Div was previously exposed as the primary editor-relative coordinate system, this is a potentially breaking semantic change even though the enum variants themselves remain source-compatible. The release notes or migration documentation should mention the distinction.

5. Verify the full test suite, not only the new oracle tests

The new tests are strong, but they depend on several pre-existing internals:

  • gutter_width,
  • display.view_from,
  • display.view_to,
  • view_offset(),
  • resolve_scroll_to_pos,
  • calculate_scroll_pos.

Please run the complete workspace test suite, including GPU/visual tests and the browser host tests, because the coordinate changes affect rendering, cursor placement, mouse handling, and scroll calculations across crate boundaries. In particular, the visual tests only validate the changed Div → Doc callers locally; they do not validate the new public page/window/local coordinate paths.

Conclusion

Recommendation: approve after addressing or explicitly deferring the coordinate-origin clarification and adding the emitter re-entrancy regression test. I did not identify a definite correctness failure from the supplied diff alone, but the CoordSys::Local interpretation is the highest-risk area because it directly affects host-facing coordinate conversions.

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