Skip to content

feat: the cursor's row and the row at a screen cell - #15

Merged
backnotprop merged 3 commits into
mainfrom
feat/cursor-place
Nov 23, 2026
Merged

backnotprop merged 3 commits into
mainfrom
feat/cursor-place

Conversation

@backnotprop

@backnotprop backnotprop commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This adds two read-only methods to Engine so a host can tell which file and row the cursor is on, on every kind of row, and what row is drawn at a screen cell. plannotator-tui's code review needs it: it now guesses the cursor's file from the moves it sends. With wrapping on, after a page move or the wheel near a file heading, that guess names the wrong file, so x and C act on the wrong file.

API

impl Engine {
    pub fn cursor_row(&self) -> Option<DiffRow>;
    pub fn row_at(&self, column: u16, row: u16) -> Option<DiffRow>;
}

#[non_exhaustive]
pub struct DiffRow {
    pub review: ReviewId,
    pub file: usize,          // as Engine::file takes it
    pub path: String,
    pub kind: DiffRowKind,
    pub hunk: Option<usize>,  // on a hunk's heading, its own lines and "No newline" marks; None on revealed context
    pub part: usize,          // which screen row of a wrapped row
    pub old_line: Option<u32>,
    pub new_line: Option<u32>,
    pub side: Side,
}

#[non_exhaustive]
pub enum DiffRowKind { Header, StickyHeader, Hunk(usize), Gap, Metadata, Code, NoNewline }
  • cursor_row reads the cursor itself, so it is right after any command or event, before the next frame. Unlike selected_source, it answers on headings, hunk headings, gaps, binary and mode-only notes, an empty side of a split row, "No newline" marks and every wrapped part.
  • row_at reads the last frame, as a click would. It returns StickyHeader for the heading drawn over the top row, which a click does not move to.
  • Both return Option. They are None for a diff with no files, after replace() until the new files arrive, and on a revealed context line whose source was withdrawn. cursor_row is also None before the first file. row_at is also None before the first draw, with help open, outside the diff area and below the last row. The rustdoc and the embedding guide list these cases.
  • Neither moves the cursor, the view or the selection.
  • Nothing else changes: there are no new fields on Options, no changed signatures, and no change in behaviour. I left the drag code as it is: a drag moves the cursor so the view scrolls at the edges, and row_at would not change that.

Tests

  • src/engine/tests/rows.rs uses a patch with a binary file, a mode-only change, a rename, a deleted file, a new file, a file with two hunks and gaps, a very wide line, a deleted symlink with a new file of the same path, and "No newline" on both sides.
    • Layouts and sizes: unified and split, wrap on and off, at 70x12, 120x24 and 170x60.
    • Moves: every row walked down and up, page moves, hunk and file jumps, first and last, the wheel, a click on every screen row (the sticky heading included), resizes, layout changes, and two events before a frame.
    • After each step, row_at is checked against the drawn text on every screen row: file, kind, hunk and line numbers. cursor_row must equal row_at at the cursor's screen row and agree with selected_source.
    • Another test checks that asking moves nothing.
  • In the same file, a test installs checked sources and clicks every gap open. Revealed context lines and their "No newline" mark must have no hunk, and a hunk's own lines and marks must keep theirs.
  • tests/engine.rs covers a host pane drawn at an offset.

Checks run locally

  • cargo fmt --check, both clippy runs, and the doctests.
  • cargo test --release --locked -- --test-threads=1.
  • The embedded host contract tests.

There is no version bump. The CHANGELOG entry is under "Unreleased".

… row

Engine::cursor_row and Engine::row_at return a DiffRow: the file the row
belongs to (index and path), its DiffRowKind, its wrapped part, its
original line numbers and the side. They answer on headings, sticky
headings, hunk headings, gaps, metadata notes, empty split cells,
No newline marks and wrapped parts, and move nothing.
backnotprop added a commit to plannotator/plannotator-tui that referenced this pull request Nov 23, 2026
…ng it

The code review worked out which file the cursor was in from the moves it sent the
viewer. With wrapping on, after a page move, after the wheel near a heading, after a
click on the sticky heading and when two events came before a frame, that guess named
the wrong file, so x, C, n and N and ctrl+n acted on it and the list and the footer
showed it.

diffs-rs now says which row the cursor is on, on every kind of row, and which row is
drawn at a screen cell (plannotator/diffs-rs#15). The review asks it every time: the
move model and the per-file row projection are gone, and nothing about the position is
kept between events and frames. The list and the footer copy the answer after every
event and before every frame. A selection kept to its hunk now asks the viewer which hunk
the cursor is in, so a jump to the next hunk ends the growing and j moves on from there.

Until diffs-rs publishes the new API, it comes from the branch by [patch.crates-io].

A panic now turns off mouse capture, focus reports, bracketed paste and the keyboard
flags before ratatui's hook gives the screen back.
… docs

- DiffRow::hunk is None on context lines a gap revealed and on their "No newline" mark:
  they are in no hunk. It named the hunk above them.
- DiffRow is #[non_exhaustive], as DiffRowKind is.
- cursor_row and row_at say exactly when they return None, and how side reads on the
  sticky heading and on the column between split panes. Field docs for review and kind,
  a rustdoc example, and an example in the embedding guide by the file marks.
@backnotprop
backnotprop merged commit 1dab5cc into main Nov 23, 2026
3 checks passed
sebastianst pushed a commit to sebastianst/plannotator-tui that referenced this pull request Nov 24, 2026
…lannotator#188)

* feat(review): drag over diff lines with the mouse to select them for a comment

A drag in the diff or the whole-file view selects whole lines on the side it
started on, as the browser's code review anchors them. The lines are shaded
while dragging, dragging past the pane's edge scrolls, and the release makes
them the selection with a comment/suggest toolbar beside it. copy_on_select
copies the lines on release; when it is off, y copies them. The wheel scrolls
the key help.

diffs-rs has clicks and the wheel but no drag, so each drag step clicks the
viewer at the pressed column and reads the line the cursor landed on.

* docs: the mouse in the code review

* fix(review): draw a selection as one block of colour, without the bar or the underline

diffs-rs marks selected lines with a bar in the first cell, which turns into the cursor's
thinner bar on the cursor's row, and underlines their text. The pass over the drawn rows
now blanks that cell, takes the underline off, and fills each selected pane to its end,
the split's middle line or the pane's edge. The keyboard's v selection, a drag and the
whole-file view all look the same: grey on dark, the theme's light blue on light.

* feat(review): drag the lines between the panes to resize the file list and the notes

The line right of the file list and a new one left of the notes panel take presses
before anything else in the review: a drag resizes the pane, a double click fits it
again, and the line lights up under the pointer and while dragged. A press on a line
never moves the cursor or starts a selection. The width uses the same state as < and
>, so it stays through filters and reloads and auto-fit stops overriding it; the diff
keeps its minimum. { and } (widen-notes, narrow-notes) resize the notes by keys.

* fix(review): a lit pane line changes colour only

Bold made terminals draw the line with a shorter glyph, so a lit line broke into dashes
while the unlit one stayed solid.

* feat(review): shade the other side's rows inside a unified selection

In the unified view a removed line sits among the new lines a selection covers, and
was left unshaded, so the selection read as two blocks. Rows strictly between the first
and the last selected row now take the selection colour too. Only the look changes: the
note covers and quotes the selected side's lines alone.

* fix(review): the mouse never ends the review or leaves a drag behind

- A release over lines no longer shown (another view, a reloaded or
  filtered diff) selects nothing and says so. No mouse event can end
  the review: an error is said in the footer.
- One place drops a drag of lines or of a pane's edge, its shading with
  it: a key, a paste, a resize, focus lost, a second press, a move with
  no button held, or any mouse event outside Browse. Focus change
  reporting is on so a release outside the window is noticed.
- A drag and a growing `v` selection stay in the hunk they started in.
  The shading between selected rows skips headings and gaps.
- Which file and row the cursor is on is worked out from the viewer's
  own row layout and the last frame, without pressing keys: `v` no
  longer grows by itself, `n`/`N` put the heading on the top row, and
  a click on a heading scrolls nothing.
- Configs that set `copy` keep it on the document screen: the code
  review copies with its own `copy-lines` action, which takes `copy`'s
  keys when only `copy` is set.
- A double click on a pane's edge needs two clicks on one cell without
  a move. A width chosen for the list or the notes stays the choice
  through a narrower window.
- The toolbar ignores clicks beside its buttons and never covers a
  selected row; after a drag up the view stays put on release, and a
  shift-click grows from the line first pressed. The split's middle is
  found by geometry, so a bar in the code no longer cuts the shading.

* fix(review): the review's toolbar labels use the readable chip colours

Comment takes the document toolbar's comment colour, suggest its own
256-colour pair, and both join the contrast test in ui::chips.

* fix(ui): the review-only chip colours build on Windows, where the review is not compiled

* fix(review): take the cursor's file from the viewer instead of guessing it

The code review worked out which file the cursor was in from the moves it sent the
viewer. With wrapping on, after a page move, after the wheel near a heading, after a
click on the sticky heading and when two events came before a frame, that guess named
the wrong file, so x, C, n and N and ctrl+n acted on it and the list and the footer
showed it.

diffs-rs now says which row the cursor is on, on every kind of row, and which row is
drawn at a screen cell (plannotator/diffs-rs#15). The review asks it every time: the
move model and the per-file row projection are gone, and nothing about the position is
kept between events and frames. The list and the footer copy the answer after every
event and before every frame. A selection kept to its hunk now asks the viewer which hunk
the cursor is in, so a jump to the next hunk ends the growing and j moves on from there.

Until diffs-rs publishes the new API, it comes from the branch by [patch.crates-io].

A panic now turns off mouse capture, focus reports, bracketed paste and the keyboard
flags before ratatui's hook gives the screen back.

* build: take diffs-rs from the branch's latest commit

* fix(review): diffs-rs 0.2.2 from crates.io; ctrl+n past a path's two files

- Depend on diffs-rs 0.2.2, which has Engine::cursor_row and Engine::row_at, instead of the
  branch by [patch.crates-io].
- ctrl+n goes by the viewer's file index and passes over every file of the path the cursor
  is on, so a symlink deleted and a file added in its place no longer bring it back to the
  same file. The count of files not viewed counts each path once.
- The panic hook that turns the input modes off is in place before mouse capture or any
  other mode is on.
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