plannotator-tui 0.14.0

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
//! A new file has lines on the new side only and a deleted file on the old side only: in a
//! split diff the cursor stays on the side with the lines.

use anyhow::Result;
use diffs_rs::engine::{Command, DiffRowKind, Side};

use super::Diff;

impl Diff {
    /// In a split diff, the cursor on the empty side of a new or a deleted file goes over to
    /// the side with the lines. The viewer keeps the side the cursor was on through every move
    /// and takes a click's pane as it is, so a move from the old side of a changed file, or a
    /// click on a new file's old pane, left the cursor where there is no line: no bar drawn,
    /// nothing to note or select. A changed file's empty cells are left as they are.
    pub(super) fn keep_on_lines(&mut self) -> Result<()> {
        let Some(row) = self.cursor_row() else { return Ok(()) };
        if !self.split() || matches!(row.kind, DiffRowKind::Inset { .. }) {
            return Ok(());
        }
        if self.only_side(row.file).is_some_and(|only| only != row.side) {
            self.engine.execute(Command::SwitchSide)?;
        }
        Ok(())
    }

    /// Whether the file the cursor is in has no lines on `side`: the old side of a new file,
    /// the new side of a deleted one.
    pub(in crate::review) fn lacks_side(&self, side: plannotator_tui_review::Side) -> bool {
        let only = self.cursor_file().and_then(|file| self.only_side(file));
        only.is_some_and(|only| only != super::engine_side(side))
    }

    /// The one side file `file` has lines on, by its status in the patch: the new side of an
    /// added file, the old side of a deleted one. `None` for any other: a changed file has
    /// both sides, even when its hunks show lines on one only (`diff.context = 0`).
    fn only_side(&self, file: usize) -> Option<Side> {
        match &*self.engine.file(file)?.status {
            "Added" => Some(Side::New),
            "Deleted" => Some(Side::Old),
            _ => None,
        }
    }
}