mod effects;
mod focus;
mod split;
mod ui;
use std::io;
use ratatui::{
DefaultTerminal,
crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
};
use ratatui_textarea::{CursorMove, Input, TextArea};
use crate::{
app::focus::Stop,
diff::DiffView,
vim::{self, Dir, Vim},
};
use jj_compose_core::{Draft, Field, Jj, Plan, Split, TaggedCommit, Target, overview};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, clap::ValueEnum)]
pub enum Keymap {
#[default]
Default,
Vim,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum View {
Structured,
Raw,
}
pub struct Session {
pub description: String,
pub change_id: String,
pub keymap: Keymap,
pub raw: bool,
pub diff_tool: Option<String>,
pub at_risk: Vec<TaggedCommit>,
}
pub struct Outcome {
pub exit: Exit,
pub target: Target,
pub recovery: Vec<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Focus {
Diff,
Editor,
}
enum Status {
Error(String),
Info(String),
ConfirmQuit,
}
pub enum Exit {
Abort,
Commit(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Request {
OpenDiff { path: Option<String> },
Split { message: String },
Symbols,
}
pub struct App {
diff: DiffView,
change_id: String,
fields: [TextArea<'static>; Field::ALL.len()],
field: Field,
top_field: Field,
raw: TextArea<'static>,
view: View,
focus: Focus,
original: String,
status: Option<Status>,
keymap: Keymap,
vim: Vim,
search_prompt: Option<String>,
zoomed: bool,
diff_tool: Option<String>,
pending: Option<Request>,
target: Target,
split: Option<Split>,
pub(super) overview: Option<usize>,
pub(super) symbols: Vec<Option<String>>,
pub(super) plan: Plan,
at_risk: Vec<TaggedCommit>,
recovery: Vec<String>,
is_vertical: bool,
}
impl App {
#[must_use]
pub fn new(diff: DiffView, session: Session, target: Target) -> Self {
let original = session.description.trim_end().to_owned();
let (draft, view, status) = match Draft::parse(&original) {
Ok(draft) if !session.raw => (draft, View::Structured, None),
Ok(draft) => (draft, View::Raw, None), Err(e) => (
Draft::default(),
View::Raw,
Some(Status::Info(format!(
"Opened as raw text because {e}. ^t switches to the form."
))),
),
};
let mode = if original.is_empty() {
vim::Mode::Insert
} else {
vim::Mode::Normal
};
Self {
diff,
change_id: session.change_id,
fields: Field::ALL.map(|field| ui::field_editor(field, draft.get(field))),
field: Field::Type,
top_field: Field::Type,
raw: ui::raw_editor(&original),
view,
focus: Focus::Editor,
original,
status,
keymap: session.keymap,
vim: Vim::new(mode),
search_prompt: None,
zoomed: false,
diff_tool: session.diff_tool,
pending: None,
target,
split: None,
overview: None,
symbols: Vec::new(),
plan: Plan::default(),
at_risk: session.at_risk,
recovery: Vec::new(),
is_vertical: false,
}
}
pub fn run(mut self, terminal: &mut DefaultTerminal, jj: &Jj) -> io::Result<Outcome> {
const VERTICAL_BREAKPOINT: u16 = 120;
self.status = self.immutable_warning().or_else(|| self.status.take());
loop {
let size = terminal.size()?;
self.is_vertical = size.width < VERTICAL_BREAKPOINT;
terminal.draw(|frame| self.draw(frame))?;
let exit = match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => self.handle_key(key),
Event::Paste(text) => {
self.paste(&text);
None
}
_ => None,
};
if let Some(exit) = exit {
return Ok(Outcome {
exit,
target: self.target,
recovery: self.recovery,
});
}
if let Some(request) = self.pending.take() {
self.perform(terminal, jj, &request)?;
}
}
}
pub(super) fn overview_rows(&self) -> Vec<overview::Row> {
let plan = self.split.as_ref().map_or(&[][..], Split::plan);
overview::build(self.diff.patch(), plan, &self.symbols)
}
fn choosing(&self) -> bool {
self.focus == Focus::Diff && self.split.is_some()
}
fn enter_split(&mut self, split: Split) {
self.diff.show_hunk(split.at());
self.split = Some(split);
self.focus = Focus::Diff;
self.zoomed = true;
}
fn draft(&self) -> Draft {
let mut draft = Draft::default();
for field in Field::ALL {
draft.set(field, self.fields[field as usize].lines().join("\n"));
}
draft
}
fn message(&self) -> String {
match self.view {
View::Structured => self.draft().render(),
View::Raw => self.raw.lines().join("\n").trim_end().to_owned(),
}
}
fn is_dirty(&self) -> bool {
self.message() != self.original
}
const fn editor(&mut self) -> &mut TextArea<'static> {
match self.view {
View::Structured => &mut self.fields[self.field as usize],
View::Raw => &mut self.raw,
}
}
const fn editor_is_multiline(&self) -> bool {
matches!(self.view, View::Raw) || self.field.is_multiline()
}
fn handle_key(&mut self, key: KeyEvent) -> Option<Exit> {
let confirming = matches!(self.status, Some(Status::ConfirmQuit));
self.status = None;
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let alt = key.modifiers.contains(KeyModifiers::ALT);
if self.search_prompt.is_some() {
return self.search_key(key);
}
match key.code {
KeyCode::Char('s') if ctrl => return self.submit(),
KeyCode::Char('s') if alt => self.start_split(),
KeyCode::Char('c') if ctrl => return self.quit(confirming),
KeyCode::Char('t') if ctrl => self.toggle_view(),
KeyCode::Tab => self.cycle_focus(true),
KeyCode::BackTab => self.cycle_focus(false),
KeyCode::PageDown => self.diff.scroll_down(self.diff.half_page()),
KeyCode::PageUp => self.diff.scroll_up(self.diff.half_page()),
KeyCode::Down if alt => self.diff.scroll_down(1),
KeyCode::Up if alt => self.diff.scroll_up(1),
KeyCode::Left if alt => self.focus_diff(),
KeyCode::Right if alt => self.focus = Focus::Editor,
_ => {
return match self.focus {
Focus::Diff if self.choosing() => self.split_key(key.code, ctrl),
Focus::Diff => self.diff_key(key.code, ctrl, confirming),
Focus::Editor if self.keymap == Keymap::Vim => self.vim_key(key, confirming),
Focus::Editor => self.editor_key(key, confirming),
};
}
}
None
}
fn diff_key(&mut self, code: KeyCode, ctrl: bool, confirming: bool) -> Option<Exit> {
match code {
KeyCode::Char('q') if self.keymap == Keymap::Vim => return self.quit(confirming),
KeyCode::Char('j') | KeyCode::Down => self.diff.scroll_down(1),
KeyCode::Char('k') | KeyCode::Up => self.diff.scroll_up(1),
KeyCode::Char('d') if ctrl => self.diff.scroll_down(self.diff.half_page()),
KeyCode::Char('u') if ctrl => self.diff.scroll_up(self.diff.half_page()),
KeyCode::Char(' ') => self.diff.scroll_down(self.diff.half_page()),
KeyCode::Char('g') | KeyCode::Home => self.diff.scroll_to_top(),
KeyCode::Char('G') | KeyCode::End => self.diff.scroll_to_bottom(),
KeyCode::Char('L') | KeyCode::Right => self.diff.pan_right(8),
KeyCode::Char('H') | KeyCode::Left => self.diff.pan_left(8),
KeyCode::Char('l' | 'i') | KeyCode::Esc | KeyCode::Enter => {
self.focus = Focus::Editor;
}
KeyCode::Char(']') => self.diff.next_file(),
KeyCode::Char('[') => self.diff.prev_file(),
KeyCode::Char('}') => self.diff.next_hunk(),
KeyCode::Char('{') => self.diff.prev_hunk(),
KeyCode::Char('f') => self.diff.toggle_file_list(),
KeyCode::Char('/') => self.search_prompt = Some(String::new()),
KeyCode::Char('n') => self.diff.next_match(),
KeyCode::Char('N') => self.diff.prev_match(),
KeyCode::Char('z') => self.zoomed = !self.zoomed,
KeyCode::Char('o') => {
let path = self.diff.current_file().map(|(_, file)| file.path.clone());
self.pending = Some(Request::OpenDiff { path });
}
KeyCode::Char('O') => self.pending = Some(Request::OpenDiff { path: None }),
KeyCode::Char('s') => self.start_split(),
_ => {}
}
None
}
fn vim_key(&mut self, key: KeyEvent, confirming: bool) -> Option<Exit> {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
if self.vim.mode == vim::Mode::Insert {
if key.code != KeyCode::Esc {
return self.editor_key(key, confirming);
}
self.vim.mode = vim::Mode::Normal;
let editor = self.editor();
if editor.cursor().1 > 0 {
editor.move_cursor(CursorMove::Back);
}
return None;
}
match key.code {
KeyCode::Char('d') if ctrl => self.diff.scroll_down(self.diff.half_page()),
KeyCode::Char('u') if ctrl => self.diff.scroll_up(self.diff.half_page()),
KeyCode::Char('e') if ctrl => self.diff.scroll_down(1),
KeyCode::Char('y') if ctrl => self.diff.scroll_up(1),
_ => {
let form = self.view == View::Structured;
let editor = match self.view {
View::Structured => &mut self.fields[self.field as usize],
View::Raw => &mut self.raw,
};
match self.vim.handle(&Input::from(key), editor) {
vim::Action::Quit => return self.quit(confirming),
vim::Action::NextField if form => self.focus_field(self.field.next()),
vim::Action::Move(dir) => {
self.go(dir);
}
_ => {}
}
}
}
None
}
fn editor_key(&mut self, key: KeyEvent, confirming: bool) -> Option<Exit> {
let field = self.field;
let form = self.view == View::Structured;
let multiline = self.editor_is_multiline();
let plain = key.modifiers.is_empty();
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let alt = key.modifiers.contains(KeyModifiers::ALT);
let editor = self.editor();
let (row, col) = (editor.cursor().0, editor.cursor().1);
let last_row = editor.lines().len() - 1;
let line_len = editor.lines()[row].chars().count();
let field_beyond =
|app: &Self, dir| form && plain && matches!(app.neighbor(dir), Some(Stop::Field(_)));
match key.code {
KeyCode::Esc => return self.quit(confirming),
KeyCode::Enter if !multiline => self.focus_field(field.next()),
KeyCode::Up if row == 0 && field_beyond(self, Dir::Up) => self.go(Dir::Up),
KeyCode::Down if row == last_row && field_beyond(self, Dir::Down) => {
self.go(Dir::Down);
}
KeyCode::Left if col == 0 && field_beyond(self, Dir::Left) => self.go(Dir::Left),
KeyCode::Right
if row == last_row && col == line_len && field_beyond(self, Dir::Right) =>
{
self.go(Dir::Right);
}
KeyCode::Char('z') if ctrl => {
self.editor().undo();
}
KeyCode::Char('w') if alt => self.editor().copy(),
_ => {
self.editor().input(Input::from(key));
}
}
None
}
fn paste(&mut self, text: &str) {
if self.focus != Focus::Editor {
return;
}
let text = text.replace("\r\n", "\n").replace('\r', "\n");
let text = if self.editor_is_multiline() {
text
} else {
text.split_whitespace().collect::<Vec<_>>().join(" ")
};
self.editor().insert_str(text);
}
fn quit(&mut self, confirmed: bool) -> Option<Exit> {
if confirmed || !self.is_dirty() {
return Some(Exit::Abort);
}
self.status = Some(Status::ConfirmQuit);
None
}
fn submit(&mut self) -> Option<Exit> {
let message = if self.view == View::Raw {
self.message()
} else {
match self.draft().build() {
Ok(message) => message,
Err(e) => {
self.status = Some(Status::Error(e.into()));
return None;
}
}
};
if let Some(split) = &self.split {
if split.assigned() == 0 {
self.status = Some(Status::Error(
"nothing assigned: 1-9 puts the hunk on screen in a commit, Esc leaves".into(),
));
return None;
}
if !split.is_everything() {
self.pending = Some(Request::Split { message });
return None;
}
}
Some(Exit::Commit(message))
}
fn search_key(&mut self, key: KeyEvent) -> Option<Exit> {
let prompt = self.search_prompt.as_mut()?;
match key.code {
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
self.search_prompt = None;
return self.quit(false);
}
KeyCode::Char(c) => prompt.push(c),
KeyCode::Backspace => {
prompt.pop();
}
KeyCode::Enter => {
let query = self.search_prompt.take().unwrap_or_default();
if self.diff.search(&query) == 0 && !query.is_empty() {
self.status = Some(Status::Error(format!("no match for `{query}`")));
}
}
KeyCode::Esc => self.search_prompt = None,
_ => {}
}
None
}
}
fn plural(n: usize, word: &str) -> String {
if n == 1 {
word.to_owned()
} else {
format!("{word}s")
}
}
#[cfg(test)]
mod tests {
use super::*;
use ratatui::Terminal;
use crate::app::ui;
const EXAMPLE: &str = "fix(parser): handle empty input\n\n\
This also corrects an unrelated diagnostic.\n\n\
Fixes #42\nReviewed-by: Someone\n";
fn app(description: &str, keymap: Keymap) -> App {
app_with_tool(description, keymap, None)
}
fn app_with_tool(description: &str, keymap: Keymap, diff_tool: Option<&str>) -> App {
App::new(
DiffView::from_output(b"", "diff").unwrap(),
Session {
description: description.into(),
change_id: "abc".into(),
keymap,
raw: false,
diff_tool: diff_tool.map(str::to_owned),
at_risk: Vec::new(),
},
Target {
commit_id: "0123456789abcdef".into(),
change: "zzzzzzzzzzzzzzzz".into(),
change_id: "zzzzzzzz".into(),
immutable: false,
},
)
}
fn press(app: &mut App, code: KeyCode) -> Option<Exit> {
app.handle_key(KeyEvent::new(code, KeyModifiers::NONE))
}
fn alt(app: &mut App, c: char) -> Option<Exit> {
app.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::ALT))
}
fn ctrl(app: &mut App, c: char) -> Option<Exit> {
app.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::CONTROL))
}
fn type_str(app: &mut App, text: &str) {
for c in text.chars() {
press(app, KeyCode::Char(c));
}
}
fn committed(exit: Option<Exit>) -> String {
match exit {
Some(Exit::Commit(message)) => message,
_ => panic!("expected a commit"),
}
}
#[test]
fn types_immediately_and_enter_moves_on() {
let mut app = app("", Keymap::Default);
type_str(&mut app, "feat");
press(&mut app, KeyCode::Enter);
press(&mut app, KeyCode::Enter);
type_str(&mut app, "add a thing");
assert_eq!(committed(ctrl(&mut app, 's')), "feat: add a thing");
}
#[test]
fn opens_existing_description_in_the_form_and_saves_it_unchanged() {
let mut app = app(EXAMPLE, Keymap::Default);
assert_eq!(app.view, View::Structured);
assert!(!app.is_dirty());
assert_eq!(committed(ctrl(&mut app, 's')), EXAMPLE.trim_end());
}
#[test]
fn edits_mid_line() {
let mut app = app(EXAMPLE, Keymap::Default);
app.focus_field(Field::Subject);
press(&mut app, KeyCode::Home);
type_str(&mut app, "gracefully ");
let message = committed(ctrl(&mut app, 's'));
assert!(message.starts_with("fix(parser): gracefully handle empty input\n"));
assert!(message.ends_with("Fixes #42\nReviewed-by: Someone"));
}
#[test]
fn unparseable_description_opens_raw_and_saves_verbatim() {
let mut app = app("WIP\nstuff", Keymap::Default);
assert_eq!(app.view, View::Raw);
assert!(matches!(app.status, Some(Status::Info(_))));
assert_eq!(committed(ctrl(&mut app, 's')), "WIP\nstuff");
}
#[test]
fn raw_view_round_trips_through_the_form() {
let mut app = app(EXAMPLE, Keymap::Default);
ctrl(&mut app, 't');
assert_eq!(app.view, View::Raw);
assert_eq!(app.message(), EXAMPLE.trim_end());
ctrl(&mut app, 't');
assert_eq!(app.view, View::Structured);
assert!(!app.is_dirty());
}
#[test]
fn refuses_to_switch_to_the_form_when_it_would_lose_text() {
let mut app = app("WIP", Keymap::Default);
ctrl(&mut app, 't');
assert_eq!(app.view, View::Raw);
assert!(matches!(app.status, Some(Status::Error(_))));
}
#[test]
fn invalid_form_stays_open_with_an_error() {
let mut app = app("", Keymap::Default);
type_str(&mut app, "two words");
assert!(ctrl(&mut app, 's').is_none());
assert!(matches!(app.status, Some(Status::Error(_))));
}
#[test]
fn quitting_with_edits_asks_first() {
let mut clean = app(EXAMPLE, Keymap::Default);
assert!(matches!(press(&mut clean, KeyCode::Esc), Some(Exit::Abort)));
let mut dirty = app("", Keymap::Default);
type_str(&mut dirty, "x");
assert!(press(&mut dirty, KeyCode::Esc).is_none());
assert!(matches!(press(&mut dirty, KeyCode::Esc), Some(Exit::Abort)));
assert!(press(&mut dirty, KeyCode::Esc).is_none());
type_str(&mut dirty, "y");
assert!(press(&mut dirty, KeyCode::Esc).is_none());
}
#[test]
fn paste_into_a_one_line_field_collapses_newlines() {
let mut app = app("", Keymap::Default);
app.focus_field(Field::Subject);
app.paste("add\r\nthe thing\n");
assert_eq!(app.draft().get(Field::Subject), "add the thing");
app.focus_field(Field::Body);
app.paste("one\r\ntwo");
assert_eq!(app.draft().get(Field::Body), "one\ntwo");
}
#[test]
fn tab_cycles_through_fields_and_diff() {
let mut app = app("", Keymap::Default);
for _ in Field::ALL {
press(&mut app, KeyCode::Tab);
}
assert_eq!(app.focus, Focus::Diff);
press(&mut app, KeyCode::Tab);
assert_eq!((app.focus, app.field), (Focus::Editor, Field::Type));
press(&mut app, KeyCode::BackTab);
assert_eq!(app.focus, Focus::Diff);
}
#[test]
fn h_and_l_move_between_the_form_and_the_diff() {
let mut app = app(EXAMPLE, Keymap::Vim);
app.editor().move_cursor(CursorMove::End);
press(&mut app, KeyCode::Char('h'));
assert_eq!(app.focus, Focus::Editor);
app.editor().move_cursor(CursorMove::Head);
press(&mut app, KeyCode::Char('h'));
assert_eq!(app.focus, Focus::Diff);
press(&mut app, KeyCode::Char('l'));
assert_eq!((app.focus, app.field), (Focus::Editor, Field::Type));
}
#[test]
fn vim_hjkl_follow_the_layout() {
let mut app = app("", Keymap::Vim);
press(&mut app, KeyCode::Esc);
let mut go = |c| {
press(&mut app, KeyCode::Char(c));
(app.focus, app.field)
};
let field = |f| (Focus::Editor, f);
assert_eq!(go('l'), field(Field::Scope));
assert_eq!(go('j'), field(Field::Subject));
assert_eq!(go('k'), field(Field::Scope));
assert_eq!(go('h'), field(Field::Type));
assert_eq!(go('j'), field(Field::Subject));
assert_eq!(go('k'), field(Field::Type));
assert_eq!(go('k'), field(Field::Type));
assert_eq!(go('h').0, Focus::Diff);
assert_eq!(go('l'), field(Field::Type));
let mut app = super::tests::app(EXAMPLE, Keymap::Vim);
press(&mut app, KeyCode::Char('l'));
assert_eq!(app.field, Field::Scope);
press(&mut app, KeyCode::Char('h'));
assert_eq!(app.field, Field::Type);
go('j');
assert_eq!(go('j'), field(Field::Body));
assert_eq!(go('h').0, Focus::Diff);
}
#[test]
fn arrows_at_field_edges_follow_the_layout() {
let mut app = app("", Keymap::Default);
press(&mut app, KeyCode::Right);
assert_eq!(app.field, Field::Scope);
press(&mut app, KeyCode::Down);
assert_eq!(app.field, Field::Subject);
press(&mut app, KeyCode::Up);
assert_eq!(app.field, Field::Scope);
press(&mut app, KeyCode::Left);
assert_eq!(app.field, Field::Type);
press(&mut app, KeyCode::Left);
assert_eq!(app.focus, Focus::Editor);
}
#[test]
fn alt_arrows_jump_to_the_diff_and_back() {
let mut app = app("", Keymap::Default);
let alt = |code| KeyEvent::new(code, KeyModifiers::ALT);
app.handle_key(alt(KeyCode::Left));
assert_eq!(app.focus, Focus::Diff);
app.handle_key(alt(KeyCode::Right));
assert_eq!(app.focus, Focus::Editor);
}
#[test]
fn vim_starts_in_insert_for_new_and_normal_for_existing() {
assert_eq!(app("", Keymap::Vim).vim.mode, vim::Mode::Insert);
assert_eq!(app(EXAMPLE, Keymap::Vim).vim.mode, vim::Mode::Normal);
}
#[test]
fn vim_j_walks_fields_and_q_quits() {
let mut app = app(EXAMPLE, Keymap::Vim);
press(&mut app, KeyCode::Char('j'));
assert_eq!(app.field, Field::Subject);
press(&mut app, KeyCode::Char('j'));
assert_eq!(app.field, Field::Body);
assert!(matches!(
press(&mut app, KeyCode::Char('q')),
Some(Exit::Abort)
));
}
#[test]
fn search_prompt_types_runs_and_cancels() {
let mut app = app(EXAMPLE, Keymap::Default);
app.focus_diff();
press(&mut app, KeyCode::Char('/'));
type_str(&mut app, "zzz");
press(&mut app, KeyCode::Tab);
assert_eq!(app.search_prompt.as_deref(), Some("zzz"));
press(&mut app, KeyCode::Enter);
assert_eq!(app.search_prompt, None);
assert!(matches!(app.status, Some(Status::Error(_))));
press(&mut app, KeyCode::Char('/'));
press(&mut app, KeyCode::Esc);
assert_eq!(app.search_prompt, None);
assert_eq!(app.focus, Focus::Diff);
}
#[test]
fn o_asks_for_the_file_on_top_and_shift_o_for_the_whole_change() {
const DIFF: &[u8] = b"diff --git a/a.rs b/a.rs\n@@ -1 +1 @@\n-old\n+new\n";
let mut app = app("", Keymap::Default);
app.diff = DiffView::from_output(DIFF, "").unwrap();
press(&mut app, KeyCode::Char('o'));
assert_eq!(app.pending, None);
app.focus_diff();
press(&mut app, KeyCode::Char('o'));
let path = Some("a.rs".to_owned());
assert_eq!(app.pending.take(), Some(Request::OpenDiff { path }));
press(&mut app, KeyCode::Char('O'));
assert_eq!(app.pending, Some(Request::OpenDiff { path: None }));
}
const THREE_HUNKS: &[u8] = b"\
diff --git a/a.rs b/a.rs
index 1111111..2222222 100644
--- a/a.rs
+++ b/a.rs
@@ -1,1 +1,1 @@
-one
+ONE
@@ -5,1 +5,1 @@
-five
+FIVE
diff --git a/b.rs b/b.rs
index 3333333..4444444 100644
--- a/b.rs
+++ b/b.rs
@@ -1,1 +1,1 @@
-x
+X
";
fn splitting_app() -> App {
let mut app = app("", Keymap::Default);
app.diff = DiffView::from_output(THREE_HUNKS, "").unwrap();
app
}
#[test]
fn alt_s_starts_choosing_hunks_from_the_first() {
let mut app = splitting_app();
alt(&mut app, 's');
let split = app.split.as_ref().expect("split mode");
assert_eq!(split.total(), 3);
assert_eq!(split.at(), 0);
assert_eq!(app.focus, Focus::Diff);
assert!(app.zoomed, "one hunk fills the pane");
}
#[test]
fn ctrl_x_still_belongs_to_the_editor() {
let mut app = splitting_app();
ctrl(&mut app, 'x');
assert!(app.split.is_none());
}
#[test]
fn a_change_with_one_hunk_cannot_be_split() {
let one = b"diff --git a/a.rs b/a.rs\n@@ -1 +1 @@\n-old\n+new\n";
let mut app = app("", Keymap::Default);
app.diff = DiffView::from_output(one, "").unwrap();
alt(&mut app, 's');
assert!(app.split.is_none());
assert!(matches!(app.status, Some(Status::Error(_))));
}
#[test]
fn a_split_in_progress_always_has_the_keys() {
let mut app = splitting_app();
app.enter_split(
Split::new(app.diff.hunks().iter().map(|h| (h.id, h.path.clone()))).unwrap(),
);
assert_eq!(app.focus, Focus::Diff);
assert!(app.choosing());
press(&mut app, KeyCode::Char('y'));
assert_eq!(
app.split.as_ref().unwrap().at(),
1,
"y decided a hunk rather than being typed"
);
assert_eq!(app.fields[Field::Type as usize].lines()[0], "");
}
#[test]
fn leaving_the_pane_mid_split_gives_the_form_the_keys() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Tab);
assert_eq!(app.focus, Focus::Editor);
assert!(!app.choosing(), "the hunk view must not be on screen");
app.focus_diff();
assert!(app.choosing());
press(&mut app, KeyCode::Char('n'));
assert_eq!(app.split.as_ref().unwrap().at(), 1);
}
#[test]
fn enter_goes_to_the_message_without_seeing_every_hunk() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Char(' '));
press(&mut app, KeyCode::Enter);
assert_eq!(app.focus, Focus::Editor);
assert!(!app.zoomed);
assert!(!app.choosing());
assert_eq!(
app.split.as_ref().unwrap().assigned(),
1,
"the plan is still held"
);
}
#[test]
fn esc_leaves_split_mode_without_choosing() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Esc);
assert!(app.split.is_none());
assert_eq!(app.focus, Focus::Editor);
}
#[test]
fn saving_with_no_hunks_marked_explains_itself_instead() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Enter);
type_str(&mut app, "feat");
press(&mut app, KeyCode::Tab);
press(&mut app, KeyCode::Tab);
type_str(&mut app, "something");
assert!(ctrl(&mut app, 's').is_none());
assert!(app.pending.is_none(), "no split was requested");
assert!(matches!(app.status, Some(Status::Error(_))));
}
#[test]
fn saving_some_marked_hunks_asks_for_a_split() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Char(' '));
press(&mut app, KeyCode::Enter);
type_str(&mut app, "feat");
press(&mut app, KeyCode::Tab);
press(&mut app, KeyCode::Tab);
type_str(&mut app, "only the first hunk");
assert!(ctrl(&mut app, 's').is_none(), "the app stays open");
let Some(Request::Split { message }) = app.pending.take() else {
panic!("expected a split request");
};
assert!(
message.starts_with("feat: only the first hunk"),
"{message}"
);
}
#[test]
fn marking_every_hunk_just_describes_the_change() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Char('A'));
press(&mut app, KeyCode::Enter);
type_str(&mut app, "feat");
press(&mut app, KeyCode::Tab);
press(&mut app, KeyCode::Tab);
type_str(&mut app, "all of it");
let exit = ctrl(&mut app, 's');
assert!(matches!(exit, Some(Exit::Commit(_))));
assert!(
app.pending.is_none(),
"splitting off everything is a describe"
);
}
#[test]
fn one_commits_hunks_make_a_patch_of_only_themselves() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Char('n'));
press(&mut app, KeyCode::Char('1'));
let picked = app.split.as_ref().unwrap().picked(1);
let text = app.diff.patch().subset(&picked);
assert!(text.contains("+FIVE"), "{text}");
assert!(!text.contains("+ONE"), "{text}");
assert!(!text.contains("b.rs"), "{text}");
}
#[test]
fn digits_put_hunks_in_different_commits() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Char('2'));
press(&mut app, KeyCode::Char('n'));
press(&mut app, KeyCode::Char('1'));
press(&mut app, KeyCode::Char('n'));
press(&mut app, KeyCode::Char('2'));
let split = app.split.as_ref().unwrap();
assert_eq!(split.plan(), [Some(2), Some(1), Some(2)]);
assert_eq!(split.next_box(), Some(1), "lowest box commits first");
assert_eq!(split.count(2), 2);
assert_eq!(split.planned(), 2);
}
#[test]
fn a_hunk_can_be_moved_between_commits_or_taken_out() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Char('1'));
assert_eq!(app.split.as_ref().unwrap().box_of(), Some(1));
press(&mut app, KeyCode::Char('3'));
assert_eq!(app.split.as_ref().unwrap().box_of(), Some(3));
press(&mut app, KeyCode::Char('3'));
assert_eq!(app.split.as_ref().unwrap().box_of(), None);
press(&mut app, KeyCode::Char('2'));
press(&mut app, KeyCode::Char('0'));
assert_eq!(app.split.as_ref().unwrap().assigned(), 0);
}
#[test]
fn a_follows_the_commit_the_hunk_is_already_in() {
let mut app = splitting_app();
alt(&mut app, 's');
press(&mut app, KeyCode::Char('3'));
press(&mut app, KeyCode::Char('a'));
assert_eq!(app.split.as_ref().unwrap().plan(), [Some(3), Some(3), None]);
}
#[test]
fn an_immutable_revision_warns_and_names_the_tags_at_risk() {
let mut app = splitting_app();
assert!(app.immutable_warning().is_none(), "mutable: nothing to say");
app.target.immutable = true;
assert!(matches!(app.immutable_warning(), Some(Status::Error(_))));
app.at_risk = vec![TaggedCommit {
tag: "v0.1.0".to_owned(),
commit: "aca8d2e2".to_owned(),
summary: "feat: three things at once".to_owned(),
}];
let Some(Status::Error(text)) = app.immutable_warning() else {
panic!("expected an error status");
};
assert!(text.contains("v0.1.0"), "{text}");
assert!(text.contains("1 tag"), "{text}");
}
#[test]
fn the_immutable_marker_shows_only_on_protected_history() {
assert_eq!(ui::immutable_marker(false).content, "");
assert!(ui::immutable_marker(true).content.contains("immutable"));
}
#[test]
fn the_diff_hint_names_the_configured_tool() {
let hint = |tool| {
let mut app = app_with_tool("", Keymap::Default, tool);
app.focus_diff();
app.status_line()
.spans
.iter()
.map(|s| s.content.to_string())
.collect::<String>()
};
assert!(
hint(Some("meld")).contains("o/O meld"),
"{}",
hint(Some("meld"))
);
assert!(hint(None).contains("o/O open"));
}
#[test]
fn zoom_ends_when_focus_leaves_the_diff() {
let mut app = app("", Keymap::Default);
let mut terminal = Terminal::new(ratatui::backend::TestBackend::new(120, 30)).unwrap();
app.focus_diff();
press(&mut app, KeyCode::Char('z'));
terminal.draw(|frame| app.draw(frame)).unwrap();
assert!(app.zoomed);
press(&mut app, KeyCode::Char('l'));
terminal.draw(|frame| app.draw(frame)).unwrap();
assert!(!app.zoomed);
}
#[test]
fn integration_multi_split_kanban_workflow() {
let mut app = splitting_app();
app.focus_diff();
press(&mut app, KeyCode::Char('s'));
assert!(app.choosing());
press(&mut app, KeyCode::Char('1'));
press(&mut app, KeyCode::Char('n'));
press(&mut app, KeyCode::Char('2'));
let split = app.split.as_ref().unwrap();
assert_eq!(split.plan(), [Some(1), Some(2), None]);
assert_eq!(split.next_box(), Some(1), "Box 1 should be queued first");
press(&mut app, KeyCode::Enter);
assert_eq!(app.focus, Focus::Editor);
assert!(!app.choosing(), "Should leave hunk selection mode");
type_str(&mut app, "feat");
press(&mut app, KeyCode::Tab); press(&mut app, KeyCode::Tab); type_str(&mut app, "first split");
assert!(
ctrl(&mut app, 's').is_none(),
"App must stay open because more boxes remain"
);
let Some(Request::Split { message }) = app.pending.take() else {
panic!("Expected a Split request to be emitted!");
};
assert!(
message.starts_with("feat: first split"),
"Emitted message was: {message}"
);
}
}