plannotator-tui 0.13.0

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
//! Making and changing notes: what `c`, `s`, `C`, `M` and the rail's `e` and `x` do (and
//! Enter and Delete on a note's box in the diff), and saving what the comment box holds.

use anyhow::Result;
use plannotator_tui_review::{CodeAnnotation, NoteScope, NoteType, Side};

use super::diff::Lines;
use super::{Draft, Focus, Mode, Screen};
use crate::ui::compose::Compose;

impl Screen {
    /// The lines a note made now would cover: the selection, else the cursor's line, else
    /// with the cursor on a note's box the lines it hangs under, so the new note's box
    /// stacks under it.
    fn target_lines(&self) -> Result<Option<Lines>> {
        if let Some(lines) = self.diff.selection() {
            return Ok(Some(lines));
        }
        if let Some(lines) = self.diff.cursor()? {
            return Ok(Some(lines));
        }
        Ok(self.cursor_note().filter(|note| note.scope == NoteScope::Line).map(|note| Lines {
            path: note.file_path.clone(),
            side: note.side,
            first: note.line_start,
            last: note.line_end,
        }))
    }

    /// `c`: comment on the selection or the cursor line; on a file note's box, on the file.
    pub(super) fn start_comment(&mut self) -> Result<()> {
        if self.diff.selection().is_none()
            && self.cursor_note().is_some_and(|note| note.scope == NoteScope::File)
        {
            self.start_file_comment();
            return Ok(());
        }
        let Some(lines) = self.target_lines()? else {
            self.status = Some("move the cursor onto a line of code first".into());
            return Ok(());
        };
        self.compose = Compose::default();
        self.mode = Mode::Compose(Draft::Comment(lines));
        Ok(())
    }

    /// `s`: the comment box filled with the selected new-side lines; what the person makes
    /// of them is the suggested code.
    pub(super) fn start_suggestion(&mut self) -> Result<()> {
        if self.diff.selection().is_none()
            && self.cursor_note().is_some_and(|note| note.scope == NoteScope::File)
        {
            self.status = Some(match self.keys.hint(crate::keys::Action::Comment) {
                key if key.is_empty() => "a suggestion needs lines of code".to_owned(),
                key => format!("a suggestion needs lines of code; {key} adds a file comment"),
            });
            return Ok(());
        }
        let Some(lines) = self.target_lines()? else {
            self.status = Some("move the cursor onto a line of code first".into());
            return Ok(());
        };
        if lines.side == Side::Old {
            self.status = Some("a suggestion replaces new lines: pick lines on the new side".into());
            return Ok(());
        }
        let Some(file) = self.file(&lines.path) else { return Ok(()) };
        let code: Vec<&str> = (lines.first..=lines.last).filter_map(|n| file.line(Side::New, n)).collect();
        self.compose = Compose::with_text(&code.join("\n"));
        self.mode = Mode::Compose(Draft::Suggestion(lines));
        Ok(())
    }

    /// `C`: comment on the whole file under the cursor.
    pub(super) fn start_file_comment(&mut self) {
        let Some(path) = self.file_under_cursor() else {
            self.status = Some("move the cursor onto a line of the file first".into());
            return;
        };
        self.compose = Compose::default();
        self.mode = Mode::Compose(Draft::File(path));
    }

    /// `M`, or the Review menu's first row: comment on the whole change, on no file. It sits
    /// first in the rail and first in the feedback, as the browser's global comment does.
    pub(super) fn start_change_comment(&mut self) {
        self.compose = Compose::default();
        self.mode = Mode::Compose(Draft::Change);
    }

    /// `e` in the rail: the comment box with the note's comment, or its code for a
    /// suggestion.
    pub(super) fn start_edit(&mut self) {
        if let Some(id) = self.selected_note().map(|note| note.id.clone()) {
            self.edit_note(&id);
        }
    }

    /// The comment box with note `id`'s comment, or its code for a suggestion; the rail
    /// selects the note.
    pub(super) fn edit_note(&mut self, id: &str) {
        let Some(note) = self.store.annotations().iter().find(|note| note.id == id) else { return };
        let text = match note.kind {
            NoteType::Suggestion => note.suggested_code.clone(),
            NoteType::Comment | NoteType::Concern => note.text.clone(),
        };
        self.compose = Compose::with_text(&text.unwrap_or_default());
        self.mode = Mode::Compose(Draft::Edit(id.to_owned()));
        self.select_note(id);
    }

    /// Save what the comment box holds. An empty comment saves nothing.
    pub(super) fn save_draft(&mut self, draft: &Draft) -> Result<()> {
        self.mode = Mode::Browse;
        let value = self.compose.value();
        let text = value.trim().to_owned();
        // Code is kept as it is, carriage returns and trailing spaces included, so an unedited
        // suggestion is the original; only a new line typed after the last one goes.
        let code = value.strip_suffix('\n').unwrap_or(&value).to_owned();
        let key = self.capture.patch_key.clone();
        let note = match draft {
            _ if text.is_empty() => {
                self.status = Some("empty note not saved".into());
                return Ok(());
            }
            Draft::Comment(lines) => self.file(&lines.path).map(|file| {
                CodeAnnotation::line_comment(file, lines.side, lines.first, lines.last, text, &key)
            }),
            Draft::Suggestion(lines) => self
                .file(&lines.path)
                .map(|file| CodeAnnotation::suggestion(file, lines.first, lines.last, code, None, &key)),
            Draft::File(path) => self.file(path).map(|file| CodeAnnotation::file_comment(file, text, &key)),
            Draft::Change => Some(CodeAnnotation::general_comment(text, &key)),
            Draft::Outside(lines, code) => Some(CodeAnnotation::outside_diff_comment(
                &lines.path,
                lines.first,
                lines.last,
                code.clone(),
                text,
                &key,
            )),
            Draft::Edit(id) => {
                let changed = self.store.edit(id, |note| {
                    let field = match note.kind {
                        NoteType::Suggestion => &mut note.suggested_code,
                        NoteType::Comment | NoteType::Concern => &mut note.text,
                    };
                    let value = if note.kind == NoteType::Suggestion { code } else { text };
                    let changed = field.as_deref() != Some(value.as_str());
                    *field = Some(value);
                    changed
                })?;
                self.status = Some(if changed { "note updated" } else { "note unchanged" }.into());
                self.after_note_change();
                return Ok(());
            }
        };
        let Some(note) = note else {
            self.status = Some("that file is no longer in the diff".into());
            return Ok(());
        };
        let id = note.id.clone();
        self.add_note(note)?;
        self.status = Some("note saved".into());
        self.inline.saved = Some(id);
        Ok(())
    }

    pub(super) fn add_note(&mut self, note: CodeAnnotation) -> Result<()> {
        let id = note.id.clone();
        self.store.add(note)?;
        // The selection became the note; the next one starts fresh.
        let _ = self.diff.clear_selection();
        self.after_note_change();
        self.select_note(&id);
        Ok(())
    }

    /// `x` in the rail.
    pub(super) fn remove_selected(&mut self) -> Result<()> {
        match self.selected_note().map(|n| n.id.clone()) {
            Some(id) => self.remove_note(&id),
            None => Ok(()),
        }
    }

    /// Remove note `id`, at once, as the rail's `x` does; `undo-finish` brings it back.
    pub(super) fn remove_note(&mut self, id: &str) -> Result<()> {
        if let Some((index, note)) = self.store.take(id)? {
            self.undo.push(super::send::Undo::Removed(index, Box::new(note)));
            self.status = Some(match self.keys.hint(crate::keys::Action::UndoFinish) {
                key if key.is_empty() => "note removed".to_owned(),
                key => format!("note removed \u{b7} {key} undo"),
            });
        }
        self.after_note_change();
        if self.store.annotations().is_empty() {
            self.focus = Focus::Diff;
        }
        Ok(())
    }
}