Skip to content

Coordinate systems: page/window modes and CM5 method forms (Refs #53) - #81

Draft
DJAscendance wants to merge 2 commits into
masterfrom
feat/coord-systems
Draft

DJAscendance wants to merge 2 commits into
masterfrom
feat/coord-systems

Conversation

@DJAscendance

@DJAscendance DJAscendance commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Refs #53

Ledger rows: 452 cursorCoords, 453 charCoords, 454 coordsChar, 455 lineAtHeight, 456 heightAtLine, 737 intoCoordSystem, 738 fromCoordSystem.

Base: 7d917785a1b65259b238f35a3fad98343d8a6d02 (master, merged in)
Head: a4a6293496d23fce990c73a899a0b823e574e6cb

Integration: Bottom of the stack #81, #83, #86, #89, #90, #91, #92; merge in that order. Until the PR below it merges, this PR's diff against master includes that PR.

What this adds

  • CoordSys::Page and CoordSys::Window. The native "window" is the host's drawing surface. Display::window_origin is where the editor's top-left corner sits in it (wrapper.getBoundingClientRect()), and Display::page_scroll is the page scroll (pageXOffset/pageYOffset). Both are supplied by the host and default to (0, 0). The text area (lineSpace) box is computed from them plus gutter_width, the scroll offsets, paddingTop and viewOffset. This is documented on CoordSys and in the README.
  • CoordSys::Div now has CM5's meaning: relative to lineSpace, i.e. document y minus viewOffset (Editor::view_offset()). The port's internal document-y frame (CM5 div with viewOffset 0), which the renderer, moveV/findPosV and goLineUp/Down use, is renamed to CoordSys::Doc. Behavior is unchanged at those call sites (render.rs, commands.rs, movement.rs, cm-web frame.rs, and the visual/browser_host tests).
  • into_coord_system(line, rect, sys, include_widgets) and from_coord_system(left, top, sys), ported from position_measurement.js. As in CM5, "line" goes through the window path in fromCoordSystem.
  • Method forms, with None meaning CM5's default "page":
    • cursor_coords_in(start, mode). start is a CoordsStart: Head, From, To (null/true/false), or a clipped Pos.
    • char_coords_in(pos, mode), with clipPos.
    • coords_char_in(left, top, mode).
    • line_at_height_in(h, mode). As CM5 does (and as recorded), a height above the document gives first, and a height past the end gives first + size.
    • height_at_line_in(LineArg, mode, include_widgets). Takes a number or a LineHandle. Clamps to the document, and a line past the last gives the bottom of the document. include_widgets is accepted and documented, but line widgets are not ported.

Upstream refs (pinned 4ca3d05)

src/edit/methods.js:184-216; src/measurement/position_measurement.js:285-345; src/line/utils_line.js:61; src/display/update_display.js:128-130.

Tests and evidence

  • crates/cm-view/tests/coord_systems.rs::coord_systems_match_cm5: 1,040 exact comparisons against tests/data/cm5-coord-systems.json. That file was recorded by the new oracle/cm5-coord-systems.mjs from CM5 5.65.21 in headless Chromium. It covers 4 editors (scrolled with viewOffset 2584, at top, line numbers, firstLineNumber 5), with the editor offset in the page and the page scrolled, every mode, and all argument forms.
  • coord_system_round_trip_and_deleted_handle.
  • 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.
  • tools/web-test.sh (cm-web was touched by the rename): chromium, firefox, webkit, chromium-mobile and webkit-mobile all PASS 63/63.

Known differences

  • heightAtLine(visualLine) and the collapsed-span loop of coordsChar are not modeled (collapsed ranges are a layout-wide gap).
  • A handle of a deleted line gives NaN.
  • cm-web does not fill window_origin/page_scroll yet.
  • Found while recording: with line numbers the port's gutter is 1 px narrower than CM5's. maybeUpdateLineNumberWidth adds + 1 to lineNumInnerWidth (line_numbers.js:40). The test takes CM5's gutter width so that only the coordinate code is compared. The gutter fix belongs to the gutter work.

Coverage change

  • 452, 453, 454, 455, 738: PARTIAL → PORTED
  • 456, 737: PARTIAL → PARTIAL (folded lines and deleted handles remain; widgets are done after the integration merge)

into_coord_system honors include_widgets. Without it, the box moves below the widgets above the line (widgetTopHeight), as master's into_coords does; see the new test into_coord_system_include_widgets.

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

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

MiniMax-M3.1-Flash QA

Findings

P1 — CoordSys::Local horizontal conversion appears to use the wrong origin

File: crates/cm-view/src/editor.rs
Locations: into_coord_system, from_coord_system

The new documentation describes "local" as being relative to the sizer, while "div" is relative to display.lineSpace. However, the implementation currently treats the horizontal coordinate as identical between those systems:

CoordSys::Local => {
    // `display.sizer.getBoundingClientRect()`.
    let (ox, oy) = self.display.window_origin;
    left += ox + self.gutter_width - self.display.scroll_left;
    top += oy - self.display.scroll_top;
}
...
let (lx, ly) = self.line_space_origin();
(left - lx, top - ly)

Because lx is exactly ox + gutter_width - scroll_left, the horizontal operations cancel:

left + lx - lx == left

Thus a local X coordinate is returned as a div X coordinate without applying the sizer-to-lineSpace horizontal offset. This only works if the local and div X origins are identical, which is generally not true when the gutter is present or the editor is horizontally scrolled.

The conversion should explicitly account for the difference between the sizer origin and the line-space origin. At minimum, the implementation should use a separately computed sizer origin rather than adding and then subtracting line_space_origin().

This can produce incorrect results for:

  • coords_char_in(..., Some(CoordSys::Local))
  • line_at_height_in(..., Some(CoordSys::Local))
  • Any host code mapping local mouse coordinates back to document positions

The new oracle test should catch this if the CM5 recording exercises a nonzero gutter or horizontal scroll. Please add an explicit assertion for a local-coordinate round trip with a nonzero gutter and horizontal scroll, rather than relying only on the generated fixtures.


P1 — CoordSys::Local is not actually relative to the sizer in into_coord_system

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

into_coord_system changes only the vertical coordinate for CoordSys::Local:

let mut y_off = self.height_at_line(line);
match sys {
    CoordSys::Local => y_off += self.metrics.padding_top,
    CoordSys::Doc => {}
    _ => y_off -= self.view_offset(),
}

There is no horizontal adjustment for Local, even though the documented local origin is the sizer and the div origin is the line space. The page/window paths explicitly add line_space_origin() to both left and right, whereas the local path does not.

Consequently, into_coord_system(..., CoordSys::Local) and from_coord_system(..., CoordSys::Local) are not symmetric when the sizer and line-space horizontal origins differ. This is particularly problematic for lines with widgets, where the local conversion is expected to preserve the line-space content coordinate while accounting for the sizer’s layout origin.

Please verify the implementation against the CM5 intoCoordSystem/fromCoordSystem behavior for a line-numbered editor with a nonzero gutter, and add a direct inverse test for the local system with gutter_width != 0 and scroll_left != 0.


P2 — The coordinate-system tests may not exercise the newly added implementation strongly enough

File: crates/cm-view/tests/coord_systems.rs

The oracle test compares the public methods against recorded CM5 results, which is valuable, but the round-trip test only checks:

let (x, y) = cm.from_coord_system(s.left, s.top, sys);
assert_eq!((x, y + cm.view_offset()), (r.left, r.top), "{sys:?}");

This compares only the top-left point of the character rectangle. It does not validate:

  • right and bottom
  • Horizontal local-origin behavior
  • The exact PosInfo fields after a local-to-document conversion
  • Round trips for positions other than one particular line/column

Since the implementation is introducing a new coordinate system and changing several internal coordinate conventions, the test should assert complete rectangle round trips and should include cases where:

  • the gutter is nonzero,
  • scroll_left is nonzero,
  • the editor has a nonzero window_origin,
  • the page scroll differs from the editor scroll.

P2 — height_at_line_in uses a special end path that deserves an explicit oracle assertion

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

For a line number past the document, the implementation does:

if end {
    top + self.doc_height() - self.height_at_line(n)
}

This is intended to return the bottom of the document, but it depends on height_at_line(n) and doc_height() remaining consistent with the coordinate-system conversion, especially when widgets are present and when include_widgets is false.

The generated oracle data does include out-of-range line numbers, but the current test does not separately identify which failures concern the end path. A smaller, explicit unit test would make regressions easier to diagnose and would protect this special behavior independently of the large fixture:

  • height_at_line_in(last_line + 1, ...)
  • height_at_line_in(i64::MAX, ...)
  • both include_widgets values
  • all coordinate systems

P2 — Public API compatibility should be considered before adding required fields to Display

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

Adding public fields to Display is source-compatible for field access but can break downstream code that constructs Display using a struct literal without ..Default::default():

pub struct Display {
    ...
    pub window_origin: (f64, f64),
    pub page_scroll: (f64, f64),
}

If Display is intended to be public host-facing API, consider either:

  • providing a constructor/defaulting helper,
  • documenting the breaking change in the release notes, or
  • introducing a separate host/geometry structure rather than extending the public struct.

This is especially relevant because the README explicitly presents these fields as host-supplied values.


Overall assessment

The PR has a good structure: it adds explicit coordinate-system types, separates document/internal coordinates from host-facing coordinates, and introduces an oracle-based compatibility test. The main concern is the implementation of the new "local" coordinate system. Its horizontal conversion currently appears to cancel out the sizer/line-space offset, which can make Local behave like "div" and produce incorrect hit-testing results in editors with gutters or horizontal scrolling.

I would request changes before merging until the "local" horizontal origin is corrected and covered by a targeted test with nonzero gutter and horizontal scroll.

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.
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