Merge pull request #2 from km-clay/readline_refactor

Readline refactor
This commit is contained in:
2025-06-09 02:34:17 -04:00
committed by GitHub
13 changed files with 3897 additions and 2820 deletions

View File

@@ -13,13 +13,15 @@ debug = true
bitflags = "2.8.0" bitflags = "2.8.0"
clap = { version = "4.5.38", features = ["derive"] } clap = { version = "4.5.38", features = ["derive"] }
glob = "0.3.2" glob = "0.3.2"
insta = "1.42.2" nix = { version = "0.29.0", features = ["uio", "term", "user", "hostname", "fs", "default", "signal", "process", "event", "ioctl", "poll"] }
nix = { version = "0.29.0", features = ["uio", "term", "user", "hostname", "fs", "default", "signal", "process", "event", "ioctl"] }
pretty_assertions = "1.4.1"
regex = "1.11.1" regex = "1.11.1"
unicode-segmentation = "1.12.0" unicode-segmentation = "1.12.0"
unicode-width = "0.2.0" unicode-width = "0.2.0"
[dev-dependencies]
insta = "1.42.2"
pretty_assertions = "1.4.1"
[[bin]] [[bin]]
name = "fern" name = "fern"
path = "src/fern.rs" path = "src/fern.rs"

View File

@@ -103,7 +103,7 @@ fn fern_interactive() {
.unwrap() .unwrap()
.map(|mode| mode.parse::<FernEditMode>().unwrap_or_default()) .map(|mode| mode.parse::<FernEditMode>().unwrap_or_default())
.unwrap(); .unwrap();
let input = match prompt::read_line(edit_mode) { let input = match prompt::readline(edit_mode) {
Ok(line) => { Ok(line) => {
readline_err_count = 0; readline_err_count = 0;
line line

View File

@@ -22,7 +22,7 @@ fn get_prompt() -> ShResult<String> {
expand_prompt(&prompt) expand_prompt(&prompt)
} }
pub fn read_line(edit_mode: FernEditMode) -> ShResult<String> { pub fn readline(edit_mode: FernEditMode) -> ShResult<String> {
let prompt = get_prompt()?; let prompt = get_prompt()?;
let mut reader: Box<dyn Readline> = match edit_mode { let mut reader: Box<dyn Readline> = match edit_mode {
FernEditMode::Vi => Box::new(FernVi::new(Some(prompt))?), FernEditMode::Vi => Box::new(FernVi::new(Some(prompt))?),

View File

@@ -1,6 +1,6 @@
use std::{env, fmt::{Write,Display}, fs::{self, OpenOptions}, io::Write as IoWrite, path::{Path, PathBuf}, str::FromStr, time::{Duration, SystemTime, UNIX_EPOCH}}; use std::{env, fmt::{Write,Display}, fs::{self, OpenOptions}, io::Write as IoWrite, path::{Path, PathBuf}, str::FromStr, time::{Duration, SystemTime, UNIX_EPOCH}};
use crate::libsh::{error::{ShErr, ShErrKind, ShResult}, term::{Style, Styled}}; use crate::libsh::error::{ShErr, ShErrKind, ShResult};
use crate::prelude::*; use crate::prelude::*;
use super::vicmd::Direction; // surprisingly useful use super::vicmd::Direction; // surprisingly useful
@@ -206,6 +206,10 @@ impl History {
&self.entries &self.entries
} }
pub fn masked_entries(&self) -> &[HistEntry] {
&self.search_mask
}
pub fn push_empty_entry(&mut self) { pub fn push_empty_entry(&mut self) {
} }
@@ -245,6 +249,7 @@ impl History {
} }
pub fn constrain_entries(&mut self, constraint: SearchConstraint) { pub fn constrain_entries(&mut self, constraint: SearchConstraint) {
flog!(DEBUG,constraint);
let SearchConstraint { kind, term } = constraint; let SearchConstraint { kind, term } = constraint;
match kind { match kind {
SearchKind::Prefix => { SearchKind::Prefix => {
@@ -273,7 +278,7 @@ impl History {
if self.cursor_entry().is_some_and(|ent| ent.is_new() && !ent.command().is_empty()) { if self.cursor_entry().is_some_and(|ent| ent.is_new() && !ent.command().is_empty()) {
let entry = self.hint_entry()?; let entry = self.hint_entry()?;
let prefix = self.cursor_entry()?.command(); let prefix = self.cursor_entry()?.command();
Some(entry.command().strip_prefix(prefix)?.to_string()) Some(entry.command().to_string())
} else { } else {
None None
} }

View File

@@ -3,7 +3,7 @@ use unicode_segmentation::UnicodeSegmentation;
// Credit to Rustyline for the design ideas in this module // Credit to Rustyline for the design ideas in this module
// https://github.com/kkawakam/rustyline // https://github.com/kkawakam/rustyline
#[derive(Clone,Debug)] #[derive(Clone,PartialEq,Eq,Debug)]
pub struct KeyEvent(pub KeyCode, pub ModKeys); pub struct KeyEvent(pub KeyCode, pub ModKeys);
@@ -92,7 +92,7 @@ impl KeyEvent {
} }
} }
#[derive(Clone,Debug)] #[derive(Clone,PartialEq,Eq,Debug)]
pub enum KeyCode { pub enum KeyCode {
UnknownEscSeq, UnknownEscSeq,
Backspace, Backspace,

View File

File diff suppressed because it is too large Load Diff

View File

@@ -1,141 +1,186 @@
use std::time::Duration;
use history::{History, SearchConstraint, SearchKind}; use history::{History, SearchConstraint, SearchKind};
use keys::{KeyCode, KeyEvent, ModKeys}; use keys::{KeyCode, KeyEvent, ModKeys};
use linebuf::{strip_ansi_codes_and_escapes, LineBuf, SelectionAnchor, SelectionMode}; use linebuf::{LineBuf, SelectAnchor, SelectMode};
use mode::{CmdReplay, ModeReport, ViInsert, ViMode, ViNormal, ViReplace, ViVisual}; use nix::libc::STDOUT_FILENO;
use term::Terminal; use term::{get_win_size, raw_mode, KeyReader, Layout, LineWriter, TermReader, TermWriter};
use unicode_width::UnicodeWidthStr; use vicmd::{CmdFlags, Motion, MotionCmd, RegisterName, To, Verb, VerbCmd, ViCmd};
use vicmd::{Motion, MotionCmd, RegisterName, To, Verb, VerbCmd, ViCmd}; use vimode::{CmdReplay, ModeReport, ViInsert, ViMode, ViNormal, ViReplace, ViVisual};
use crate::libsh::{error::{ShErr, ShErrKind, ShResult}, term::{Style, Styled}}; use crate::libsh::{error::{ShErr, ShErrKind, ShResult}, sys::sh_quit, term::{Style, Styled}};
use crate::prelude::*; use crate::prelude::*;
pub mod keys;
pub mod term; pub mod term;
pub mod linebuf; pub mod linebuf;
pub mod layout;
pub mod keys;
pub mod vicmd; pub mod vicmd;
pub mod mode;
pub mod register; pub mod register;
pub mod vimode;
pub mod history; pub mod history;
const LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore\nmagna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\nconsequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
/*
* Known issues:
* If the line buffer scrolls past the terminal height, shit gets fucked
* the cursor sometimes spazzes out during redraw, but ends up in the right place
*/
/// Unified interface for different line editing methods
pub trait Readline { pub trait Readline {
fn readline(&mut self) -> ShResult<String>; fn readline(&mut self) -> ShResult<String>;
} }
pub struct FernVi { pub struct FernVi {
term: Terminal, pub reader: Box<dyn KeyReader>,
line: LineBuf, pub writer: Box<dyn LineWriter>,
history: History, pub prompt: String,
prompt: String, pub mode: Box<dyn ViMode>,
mode: Box<dyn ViMode>, pub old_layout: Option<Layout>,
last_action: Option<CmdReplay>, pub repeat_action: Option<CmdReplay>,
last_movement: Option<MotionCmd>, pub repeat_motion: Option<MotionCmd>,
pub editor: LineBuf,
pub history: History
} }
impl Readline for FernVi { impl Readline for FernVi {
fn readline(&mut self) -> ShResult<String> { fn readline(&mut self) -> ShResult<String> {
/* a monument to the insanity of debugging this shit let raw_mode_guard = raw_mode(); // Restores termios state on drop
self.term.writeln("This is a line!");
self.term.writeln("This is a line!"); loop {
self.term.writeln("This is a line!"); raw_mode_guard.disable_for(|| self.print_line())?;
let prompt_thing = "prompt thing -> ";
self.term.write(prompt_thing); let Some(key) = self.reader.read_key() else {
let line = "And another!"; raw_mode_guard.disable_for(|| self.writer.flush_write("\n"))?;
let mut iters: usize = 0; std::mem::drop(raw_mode_guard);
let mut newlines_written = 0; return Err(ShErr::simple(ShErrKind::ReadlineErr, "EOF"))
loop { };
iters += 1; flog!(DEBUG, key);
for i in 0..iters {
self.term.writeln(line);
}
std::thread::sleep(Duration::from_secs(1));
self.clear_lines(iters,prompt_thing.len() + 1);
}
panic!()
*/
self.print_buf(false)?;
loop {
let key = self.term.read_key();
if let KeyEvent(KeyCode::Char('V'), ModKeys::CTRL) = key {
self.handle_verbatim()?;
continue
}
if self.should_accept_hint(&key) { if self.should_accept_hint(&key) {
self.line.accept_hint(); self.editor.accept_hint();
self.history.update_pending_cmd(self.line.as_str()); self.history.update_pending_cmd(self.editor.as_str());
self.print_buf(true)?; self.print_line()?;
continue continue
} }
let Some(cmd) = self.mode.handle_key(key) else { let Some(mut cmd) = self.mode.handle_key(key) else {
continue continue
}; };
cmd.alter_line_motion_if_no_verb();
if self.should_grab_history(&cmd) { if self.should_grab_history(&cmd) {
flog!(DEBUG, "scrolling");
self.scroll_history(cmd); self.scroll_history(cmd);
self.print_buf(true)?; self.print_line()?;
continue continue
} }
if cmd.should_submit() { if cmd.should_submit() {
self.term.unposition_cursor()?; raw_mode_guard.disable_for(|| self.writer.flush_write("\n"))?;
self.term.write("\n"); std::mem::drop(raw_mode_guard);
let command = std::mem::take(&mut self.line).pack_line(); return Ok(self.editor.take_buf())
if !command.is_empty() { }
// We're just going to trim the command
// reduces clutter in the case of two history commands whose only difference is insignificant whitespace if cmd.verb().is_some_and(|v| v.1 == Verb::EndOfFile) {
self.history.update_pending_cmd(&command); if self.editor.buffer.is_empty() {
self.history.save()?; std::mem::drop(raw_mode_guard);
sh_quit(0);
} else {
self.editor.buffer.clear();
continue
} }
return Ok(command);
} }
let line = self.line.to_string(); flog!(DEBUG,cmd);
self.exec_cmd(cmd.clone())?;
let new_line = self.line.as_str();
let has_changes = line != new_line;
flog!(DEBUG, has_changes);
if has_changes { let before = self.editor.buffer.clone();
self.history.update_pending_cmd(self.line.as_str()); self.exec_cmd(cmd)?;
let after = self.editor.as_str();
if before != after {
self.history.update_pending_cmd(self.editor.as_str());
} }
self.print_buf(true)?; let hint = self.history.get_hint();
self.editor.set_hint(hint);
} }
} }
} }
impl FernVi { impl FernVi {
pub fn new(prompt: Option<String>) -> ShResult<Self> { pub fn new(prompt: Option<String>) -> ShResult<Self> {
let prompt = prompt.unwrap_or("$ ".styled(Style::Green | Style::Bold));
let line = LineBuf::new();//.with_initial(LOREM_IPSUM);
let term = Terminal::new();
let history = History::new()?;
Ok(Self { Ok(Self {
term, reader: Box::new(TermReader::new()),
line, writer: Box::new(TermWriter::new(STDOUT_FILENO)),
history, prompt: prompt.unwrap_or("$ ".styled(Style::Green)),
prompt,
mode: Box::new(ViInsert::new()), mode: Box::new(ViInsert::new()),
last_action: None, old_layout: None,
last_movement: None, repeat_action: None,
repeat_motion: None,
editor: LineBuf::new().with_initial("this buffer has (some delimited) text", 0),
history: History::new()?
}) })
} }
pub fn get_layout(&mut self) -> Layout {
let line = self.editor.to_string();
flog!(DEBUG,line);
let to_cursor = self.editor.slice_to_cursor().unwrap_or_default();
let (cols,_) = get_win_size(STDIN_FILENO);
Layout::from_parts(
/*tab_stop:*/ 8,
cols,
&self.prompt,
to_cursor,
&line
)
}
pub fn scroll_history(&mut self, cmd: ViCmd) {
flog!(DEBUG,"scrolling");
/*
if self.history.cursor_entry().is_some_and(|ent| ent.is_new()) {
let constraint = SearchConstraint::new(SearchKind::Prefix, self.editor.to_string());
self.history.constrain_entries(constraint);
}
*/
let count = &cmd.motion().unwrap().0;
let motion = &cmd.motion().unwrap().1;
flog!(DEBUG,count,motion);
flog!(DEBUG,self.history.masked_entries());
let entry = match motion {
Motion::LineUpCharwise => {
let Some(hist_entry) = self.history.scroll(-(*count as isize)) else {
return
};
flog!(DEBUG,"found entry");
flog!(DEBUG,hist_entry.command());
hist_entry
}
Motion::LineDownCharwise => {
let Some(hist_entry) = self.history.scroll(*count as isize) else {
return
};
flog!(DEBUG,"found entry");
flog!(DEBUG,hist_entry.command());
hist_entry
}
_ => unreachable!()
};
let col = self.editor.saved_col.unwrap_or(self.editor.cursor_col());
let mut buf = LineBuf::new().with_initial(entry.command(),0);
let line_end = buf.end_of_line();
if let Some(dest) = self.mode.hist_scroll_start_pos() {
match dest {
To::Start => {
/* Already at 0 */
}
To::End => {
// History entries cannot be empty
// So this subtraction is safe (maybe)
buf.cursor.add(line_end);
}
}
} else {
let target = (col).min(line_end);
buf.cursor.add(target);
}
self.editor = buf
}
pub fn should_accept_hint(&self, event: &KeyEvent) -> bool { pub fn should_accept_hint(&self, event: &KeyEvent) -> bool {
if self.line.at_end_of_buffer() && self.line.has_hint() { flog!(DEBUG,self.editor.cursor_at_max());
flog!(DEBUG,self.editor.cursor);
if self.editor.cursor_at_max() && self.editor.has_hint() {
match self.mode.report_mode() { match self.mode.report_mode() {
ModeReport::Replace | ModeReport::Replace |
ModeReport::Insert => { ModeReport::Insert => {
@@ -164,211 +209,97 @@ impl FernVi {
false false
} }
} }
/// Ctrl+V handler
pub fn handle_verbatim(&mut self) -> ShResult<()> {
let mut buf = [0u8; 8];
let mut collected = Vec::new();
loop { pub fn should_grab_history(&mut self, cmd: &ViCmd) -> bool {
let n = self.term.read_byte(&mut buf[..1]);
if n == 0 {
continue;
}
collected.push(buf[0]);
// If it starts with ESC, treat as escape sequence
if collected[0] == 0x1b {
loop {
let n = self.term.peek_byte(&mut buf[..1]);
if n == 0 {
break
}
collected.push(buf[0]);
// Ends a CSI sequence
if (0x40..=0x7e).contains(&buf[0]) {
break;
}
}
let Ok(seq) = std::str::from_utf8(&collected) else {
return Ok(())
};
let cmd = ViCmd {
register: Default::default(),
verb: Some(VerbCmd(1, Verb::Insert(seq.to_string()))),
motion: None,
raw_seq: seq.to_string(),
};
self.line.exec_cmd(cmd)?;
}
// Optional: handle other edge cases, e.g., raw control codes
if collected[0] < 0x20 || collected[0] == 0x7F {
let ctrl_seq = std::str::from_utf8(&collected).unwrap();
let cmd = ViCmd {
register: Default::default(),
verb: Some(VerbCmd(1, Verb::Insert(ctrl_seq.to_string()))),
motion: None,
raw_seq: ctrl_seq.to_string(),
};
self.line.exec_cmd(cmd)?;
break;
}
// Try to parse as UTF-8 if it's a valid Unicode sequence
if let Ok(s) = std::str::from_utf8(&collected) {
if s.chars().count() == 1 {
let ch = s.chars().next().unwrap();
// You got a literal Unicode char
eprintln!("Got char: {:?}", ch);
break;
}
}
}
Ok(())
}
pub fn scroll_history(&mut self, cmd: ViCmd) {
if self.history.cursor_entry().is_some_and(|ent| ent.is_new()) {
let constraint = SearchConstraint::new(SearchKind::Prefix, self.line.to_string());
self.history.constrain_entries(constraint);
}
let count = &cmd.motion().unwrap().0;
let motion = &cmd.motion().unwrap().1;
flog!(DEBUG,count,motion);
let entry = match motion {
Motion::LineUp => {
let Some(hist_entry) = self.history.scroll(-(*count as isize)) else {
return
};
flog!(DEBUG,"found entry");
flog!(DEBUG,hist_entry.command());
hist_entry
}
Motion::LineDown => {
let Some(hist_entry) = self.history.scroll(*count as isize) else {
return
};
flog!(DEBUG,"found entry");
flog!(DEBUG,hist_entry.command());
hist_entry
}
_ => unreachable!()
};
let col = self.line.saved_col().unwrap_or(self.line.cursor_column());
let mut buf = LineBuf::new().with_initial(entry.command());
let line_end = buf.end_of_line();
if let Some(dest) = self.mode.hist_scroll_start_pos() {
match dest {
To::Start => {
/* Already at 0 */
}
To::End => {
// History entries cannot be empty
// So this subtraction is safe (maybe)
buf.cursor_fwd_to(line_end + 1);
}
}
} else {
let target = (col + 1).min(line_end + 1);
buf.cursor_fwd_to(target);
}
self.line = buf
}
pub fn should_grab_history(&self, cmd: &ViCmd) -> bool {
cmd.verb().is_none() && cmd.verb().is_none() &&
( (
cmd.motion().is_some_and(|m| matches!(m, MotionCmd(_, Motion::LineUp))) && cmd.motion().is_some_and(|m| matches!(m, MotionCmd(_, Motion::LineUpCharwise))) &&
self.line.start_of_line() == 0 self.editor.start_of_line() == 0
) || ) ||
( (
cmd.motion().is_some_and(|m| matches!(m, MotionCmd(_, Motion::LineDown))) && cmd.motion().is_some_and(|m| matches!(m, MotionCmd(_, Motion::LineDownCharwise))) &&
self.line.end_of_line() == self.line.byte_len() self.editor.end_of_line() == self.editor.cursor_max() &&
!self.history.cursor_entry().is_some_and(|ent| ent.is_new())
) )
} }
pub fn print_buf(&mut self, refresh: bool) -> ShResult<()> {
let (height,width) = self.term.get_dimensions()?; pub fn print_line(&mut self) -> ShResult<()> {
if refresh { let new_layout = self.get_layout();
self.term.unwrite()?; if let Some(layout) = self.old_layout.as_ref() {
self.writer.clear_rows(layout)?;
} }
let hint = self.history.get_hint();
self.line.set_hint(hint);
let offset = self.calculate_prompt_offset(); self.writer.redraw(
self.line.set_first_line_offset(offset); &self.prompt,
self.line.update_term_dims((height,width)); &self.editor,
let mut line_buf = self.prompt.clone(); &new_layout
line_buf.push_str(&self.line.to_string()); )?;
self.term.recorded_write(&line_buf, offset)?; self.writer.flush_write(&self.mode.cursor_style())?;
self.term.position_cursor(self.line.cursor_display_coords(width))?;
self.term.write(&self.mode.cursor_style()); self.old_layout = Some(new_layout);
Ok(()) Ok(())
} }
pub fn calculate_prompt_offset(&self) -> usize {
if self.prompt.ends_with('\n') {
return 0
}
strip_ansi_codes_and_escapes(self.prompt.lines().last().unwrap_or_default()).width() + 1 // 1 indexed
}
pub fn exec_cmd(&mut self, mut cmd: ViCmd) -> ShResult<()> { pub fn exec_cmd(&mut self, mut cmd: ViCmd) -> ShResult<()> {
let mut selecting = false; let mut selecting = false;
let mut is_insert_mode = false;
if cmd.is_mode_transition() { if cmd.is_mode_transition() {
let count = cmd.verb_count(); let count = cmd.verb_count();
let mut mode: Box<dyn ViMode> = match cmd.verb().unwrap().1 { let mut mode: Box<dyn ViMode> = match cmd.verb().unwrap().1 {
Verb::Change | Verb::Change |
Verb::InsertModeLineBreak(_) | Verb::InsertModeLineBreak(_) |
Verb::InsertMode => { Verb::InsertMode => {
is_insert_mode = true;
Box::new(ViInsert::new().with_count(count as u16)) Box::new(ViInsert::new().with_count(count as u16))
} }
Verb::NormalMode => { Verb::NormalMode => {
Box::new(ViNormal::new()) Box::new(ViNormal::new())
} }
Verb::ReplaceMode => {
Box::new(ViReplace::new().with_count(count as u16)) Verb::ReplaceMode => Box::new(ViReplace::new()),
}
Verb::VisualModeSelectLast => { Verb::VisualModeSelectLast => {
if self.mode.report_mode() != ModeReport::Visual { if self.mode.report_mode() != ModeReport::Visual {
self.line.start_selecting(SelectionMode::Char(SelectionAnchor::End)); self.editor.start_selecting(SelectMode::Char(SelectAnchor::End));
} }
let mut mode: Box<dyn ViMode> = Box::new(ViVisual::new()); let mut mode: Box<dyn ViMode> = Box::new(ViVisual::new());
std::mem::swap(&mut mode, &mut self.mode); std::mem::swap(&mut mode, &mut self.mode);
self.line.set_cursor_clamp(self.mode.clamp_cursor()); self.editor.set_cursor_clamp(self.mode.clamp_cursor());
self.line.set_move_cursor_on_undo(self.mode.move_cursor_on_undo());
self.term.write(&mode.cursor_style()); return self.editor.exec_cmd(cmd)
return self.line.exec_cmd(cmd)
} }
Verb::VisualMode => { Verb::VisualMode => {
selecting = true; selecting = true;
self.line.start_selecting(SelectionMode::Char(SelectionAnchor::End));
Box::new(ViVisual::new()) Box::new(ViVisual::new())
} }
_ => unreachable!() _ => unreachable!()
}; };
flog!(DEBUG, self.mode.report_mode());
flog!(DEBUG, mode.report_mode());
std::mem::swap(&mut mode, &mut self.mode); std::mem::swap(&mut mode, &mut self.mode);
flog!(DEBUG, self.mode.report_mode());
self.line.set_cursor_clamp(self.mode.clamp_cursor());
self.line.set_move_cursor_on_undo(self.mode.move_cursor_on_undo());
self.term.write(&mode.cursor_style());
if mode.is_repeatable() { if mode.is_repeatable() {
self.last_action = mode.as_replay(); self.repeat_action = mode.as_replay();
} }
self.line.exec_cmd(cmd)?;
self.editor.exec_cmd(cmd)?;
self.editor.set_cursor_clamp(self.mode.clamp_cursor());
if selecting { if selecting {
self.line.start_selecting(SelectionMode::Char(SelectionAnchor::End)); self.editor.start_selecting(SelectMode::Char(SelectAnchor::End));
} else { } else {
self.line.stop_selecting(); self.editor.stop_selecting();
}
if is_insert_mode {
self.editor.mark_insert_mode_start_pos();
} else {
self.editor.clear_insert_mode_start_pos();
} }
return Ok(()) return Ok(())
} else if cmd.is_cmd_repeat() { } else if cmd.is_cmd_repeat() {
let Some(replay) = self.last_action.clone() else { let Some(replay) = self.repeat_action.clone() else {
return Ok(()) return Ok(())
}; };
let ViCmd { verb, .. } = cmd; let ViCmd { verb, .. } = cmd;
@@ -381,7 +312,7 @@ impl FernVi {
for _ in 0..repeat { for _ in 0..repeat {
let cmds = cmds.clone(); let cmds = cmds.clone();
for cmd in cmds { for cmd in cmds {
self.line.exec_cmd(cmd)? self.editor.exec_cmd(cmd)?
} }
} }
} }
@@ -399,7 +330,7 @@ impl FernVi {
return Ok(()) // it has to have a verb to be repeatable, something weird happened return Ok(()) // it has to have a verb to be repeatable, something weird happened
} }
} }
self.line.exec_cmd(cmd)?; self.editor.exec_cmd(cmd)?;
} }
_ => unreachable!("motions should be handled in the other branch") _ => unreachable!("motions should be handled in the other branch")
} }
@@ -407,19 +338,20 @@ impl FernVi {
} else if cmd.is_motion_repeat() { } else if cmd.is_motion_repeat() {
match cmd.motion.as_ref().unwrap() { match cmd.motion.as_ref().unwrap() {
MotionCmd(count,Motion::RepeatMotion) => { MotionCmd(count,Motion::RepeatMotion) => {
let Some(motion) = self.last_movement.clone() else { let Some(motion) = self.repeat_motion.clone() else {
return Ok(()) return Ok(())
}; };
let repeat_cmd = ViCmd { let repeat_cmd = ViCmd {
register: RegisterName::default(), register: RegisterName::default(),
verb: None, verb: None,
motion: Some(motion), motion: Some(motion),
raw_seq: format!("{count};") raw_seq: format!("{count};"),
flags: CmdFlags::empty()
}; };
return self.line.exec_cmd(repeat_cmd); return self.editor.exec_cmd(repeat_cmd);
} }
MotionCmd(count,Motion::RepeatMotionRev) => { MotionCmd(count,Motion::RepeatMotionRev) => {
let Some(motion) = self.last_movement.clone() else { let Some(motion) = self.repeat_motion.clone() else {
return Ok(()) return Ok(())
}; };
let mut new_motion = motion.invert_char_motion(); let mut new_motion = motion.invert_char_motion();
@@ -428,9 +360,10 @@ impl FernVi {
register: RegisterName::default(), register: RegisterName::default(),
verb: None, verb: None,
motion: Some(new_motion), motion: Some(new_motion),
raw_seq: format!("{count},") raw_seq: format!("{count},"),
flags: CmdFlags::empty()
}; };
return self.line.exec_cmd(repeat_cmd); return self.editor.exec_cmd(repeat_cmd);
} }
_ => unreachable!() _ => unreachable!()
} }
@@ -440,23 +373,24 @@ impl FernVi {
if self.mode.report_mode() == ModeReport::Visual { if self.mode.report_mode() == ModeReport::Visual {
// The motion is assigned in the line buffer execution, so we also have to assign it here // The motion is assigned in the line buffer execution, so we also have to assign it here
// in order to be able to repeat it // in order to be able to repeat it
let range = self.line.selected_range().unwrap(); let range = self.editor.select_range().unwrap();
cmd.motion = Some(MotionCmd(1,Motion::Range(range.start, range.end))) cmd.motion = Some(MotionCmd(1,Motion::Range(range.0, range.1)))
} }
self.last_action = Some(CmdReplay::Single(cmd.clone())); self.repeat_action = Some(CmdReplay::Single(cmd.clone()));
} }
if cmd.is_char_search() { if cmd.is_char_search() {
self.last_movement = cmd.motion.clone() self.repeat_motion = cmd.motion.clone()
} }
self.line.exec_cmd(cmd.clone())?; self.editor.exec_cmd(cmd.clone())?;
if self.mode.report_mode() == ModeReport::Visual && cmd.verb().is_some_and(|v| v.1.is_edit()) { if self.mode.report_mode() == ModeReport::Visual && cmd.verb().is_some_and(|v| v.1.is_edit()) {
self.line.stop_selecting(); self.editor.stop_selecting();
let mut mode: Box<dyn ViMode> = Box::new(ViNormal::new()); let mut mode: Box<dyn ViMode> = Box::new(ViNormal::new());
std::mem::swap(&mut mode, &mut self.mode); std::mem::swap(&mut mode, &mut self.mode);
} }
Ok(()) Ok(())
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,9 @@
use bitflags::bitflags;
use super::register::{append_register, read_register, write_register}; use super::register::{append_register, read_register, write_register};
//TODO: write tests that take edit results and cursor positions from actual neovim edits and test them against the behavior of this editor
#[derive(Clone,Copy,Debug)] #[derive(Clone,Copy,Debug)]
pub struct RegisterName { pub struct RegisterName {
name: Option<char>, name: Option<char>,
@@ -52,12 +56,22 @@ impl Default for RegisterName {
} }
} }
bitflags! {
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CmdFlags: u32 {
const VISUAL = 1<<0;
const VISUAL_LINE = 1<<1;
const VISUAL_BLOCK = 1<<2;
}
}
#[derive(Clone,Default,Debug)] #[derive(Clone,Default,Debug)]
pub struct ViCmd { pub struct ViCmd {
pub register: RegisterName, pub register: RegisterName,
pub verb: Option<VerbCmd>, pub verb: Option<VerbCmd>,
pub motion: Option<MotionCmd>, pub motion: Option<MotionCmd>,
pub raw_seq: String, pub raw_seq: String,
pub flags: CmdFlags,
} }
impl ViCmd { impl ViCmd {
@@ -82,6 +96,15 @@ impl ViCmd {
pub fn motion_count(&self) -> usize { pub fn motion_count(&self) -> usize {
self.motion.as_ref().map(|m| m.0).unwrap_or(1) self.motion.as_ref().map(|m| m.0).unwrap_or(1)
} }
pub fn normalize_counts(&mut self) {
let Some(verb) = self.verb.as_mut() else { return };
let Some(motion) = self.motion.as_mut() else { return };
let VerbCmd(v_count, _) = verb;
let MotionCmd(m_count, _) = motion;
let product = *v_count * *m_count;
verb.0 = 1;
motion.0 = product;
}
pub fn is_repeatable(&self) -> bool { pub fn is_repeatable(&self) -> bool {
self.verb.as_ref().is_some_and(|v| v.1.is_repeatable()) self.verb.as_ref().is_some_and(|v| v.1.is_repeatable())
} }
@@ -95,13 +118,36 @@ impl ViCmd {
self.motion.as_ref().is_some_and(|m| matches!(m.1, Motion::CharSearch(..))) self.motion.as_ref().is_some_and(|m| matches!(m.1, Motion::CharSearch(..)))
} }
pub fn should_submit(&self) -> bool { pub fn should_submit(&self) -> bool {
self.verb.as_ref().is_some_and(|v| matches!(v.1, Verb::AcceptLine)) self.verb.as_ref().is_some_and(|v| matches!(v.1, Verb::AcceptLineOrNewline))
} }
pub fn is_undo_op(&self) -> bool { pub fn is_undo_op(&self) -> bool {
self.verb.as_ref().is_some_and(|v| matches!(v.1, Verb::Undo | Verb::Redo)) self.verb.as_ref().is_some_and(|v| matches!(v.1, Verb::Undo | Verb::Redo))
} }
pub fn is_inplace_edit(&self) -> bool {
self.verb.as_ref().is_some_and(|v| matches!(v.1, Verb::ReplaceCharInplace(_,_) | Verb::ToggleCaseInplace(_))) &&
self.motion.is_none()
}
pub fn is_line_motion(&self) -> bool { pub fn is_line_motion(&self) -> bool {
self.motion.as_ref().is_some_and(|m| matches!(m.1, Motion::LineUp | Motion::LineDown)) self.motion.as_ref().is_some_and(|m| {
matches!(m.1,
Motion::LineUp |
Motion::LineDown |
Motion::LineUpCharwise |
Motion::LineDownCharwise
)
})
}
/// If a ViCmd has a linewise motion, but no verb, we change it to charwise
pub fn alter_line_motion_if_no_verb(&mut self) {
if self.is_line_motion() && self.verb.is_none() {
if let Some(motion) = self.motion.as_mut() {
match motion.1 {
Motion::LineUp => motion.1 = Motion::LineUpCharwise,
Motion::LineDown => motion.1 = Motion::LineDownCharwise,
_ => unreachable!()
}
}
}
} }
pub fn is_mode_transition(&self) -> bool { pub fn is_mode_transition(&self) -> bool {
self.verb.as_ref().is_some_and(|v| { self.verb.as_ref().is_some_and(|v| {
@@ -140,12 +186,13 @@ impl MotionCmd {
#[non_exhaustive] #[non_exhaustive]
pub enum Verb { pub enum Verb {
Delete, Delete,
DeleteChar(Anchor),
Change, Change,
Yank, Yank,
ReplaceChar(char), Rot13, // lol
Substitute, ReplaceChar(char), // char to replace with, number of chars to replace
ToggleCase, ReplaceCharInplace(char,u16), // char to replace with, number of chars to replace
ToggleCaseInplace(u16), // Number of chars to toggle
ToggleCaseRange,
ToLower, ToLower,
ToUpper, ToUpper,
Complete, Complete,
@@ -166,47 +213,31 @@ pub enum Verb {
JoinLines, JoinLines,
InsertChar(char), InsertChar(char),
Insert(String), Insert(String),
Breakline(Anchor),
Indent, Indent,
Dedent, Dedent,
Equalize, Equalize,
AcceptLine, AcceptLineOrNewline,
Rot13, // lol
Builder(VerbBuilder),
EndOfFile EndOfFile
} }
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum VerbBuilder {
}
impl Verb { impl Verb {
pub fn needs_motion(&self) -> bool {
matches!(self,
Self::Indent |
Self::Dedent |
Self::Delete |
Self::Change |
Self::Yank
)
}
pub fn is_repeatable(&self) -> bool { pub fn is_repeatable(&self) -> bool {
matches!(self, matches!(self,
Self::Delete | Self::Delete |
Self::DeleteChar(_) |
Self::Change | Self::Change |
Self::ReplaceChar(_) | Self::ReplaceChar(_) |
Self::Substitute | Self::ReplaceCharInplace(_,_) |
Self::ToLower | Self::ToLower |
Self::ToUpper | Self::ToUpper |
Self::ToggleCase | Self::ToggleCaseRange |
Self::ToggleCaseInplace(_) |
Self::Put(_) | Self::Put(_) |
Self::ReplaceMode | Self::ReplaceMode |
Self::InsertModeLineBreak(_) | Self::InsertModeLineBreak(_) |
Self::JoinLines | Self::JoinLines |
Self::InsertChar(_) | Self::InsertChar(_) |
Self::Insert(_) | Self::Insert(_) |
Self::Breakline(_) |
Self::Indent | Self::Indent |
Self::Dedent | Self::Dedent |
Self::Equalize Self::Equalize
@@ -215,11 +246,11 @@ impl Verb {
pub fn is_edit(&self) -> bool { pub fn is_edit(&self) -> bool {
matches!(self, matches!(self,
Self::Delete | Self::Delete |
Self::DeleteChar(_) |
Self::Change | Self::Change |
Self::ReplaceChar(_) | Self::ReplaceChar(_) |
Self::Substitute | Self::ReplaceCharInplace(_,_) |
Self::ToggleCase | Self::ToggleCaseRange |
Self::ToggleCaseInplace(_) |
Self::ToLower | Self::ToLower |
Self::ToUpper | Self::ToUpper |
Self::RepeatLast | Self::RepeatLast |
@@ -229,7 +260,6 @@ impl Verb {
Self::JoinLines | Self::JoinLines |
Self::InsertChar(_) | Self::InsertChar(_) |
Self::Insert(_) | Self::Insert(_) |
Self::Breakline(_) |
Self::Rot13 | Self::Rot13 |
Self::EndOfFile Self::EndOfFile
) )
@@ -238,7 +268,8 @@ impl Verb {
matches!(self, matches!(self,
Self::Change | Self::Change |
Self::InsertChar(_) | Self::InsertChar(_) |
Self::ReplaceChar(_) Self::ReplaceChar(_) |
Self::ReplaceCharInplace(_,_)
) )
} }
} }
@@ -251,15 +282,20 @@ pub enum Motion {
BeginningOfFirstWord, BeginningOfFirstWord,
BeginningOfLine, BeginningOfLine,
EndOfLine, EndOfLine,
BackwardWord(To, Word), WordMotion(To,Word,Direction),
ForwardWord(To, Word),
CharSearch(Direction,Dest,char), CharSearch(Direction,Dest,char),
BackwardChar, BackwardChar,
ForwardChar, ForwardChar,
BackwardCharForced, // These two variants can cross line boundaries
ForwardCharForced,
LineUp, LineUp,
LineUpCharwise,
ScreenLineUp, ScreenLineUp,
ScreenLineUpCharwise,
LineDown, LineDown,
LineDownCharwise,
ScreenLineDown, ScreenLineDown,
ScreenLineDownCharwise,
BeginningOfScreenLine, BeginningOfScreenLine,
FirstGraphicalOnScreenLine, FirstGraphicalOnScreenLine,
HalfOfScreen, HalfOfScreen,
@@ -267,23 +303,65 @@ pub enum Motion {
WholeBuffer, WholeBuffer,
BeginningOfBuffer, BeginningOfBuffer,
EndOfBuffer, EndOfBuffer,
ToColumn(usize), ToColumn,
ToDelimMatch,
ToBrace(Direction),
ToBracket(Direction),
ToParen(Direction),
Range(usize,usize), Range(usize,usize),
Builder(MotionBuilder),
RepeatMotion, RepeatMotion,
RepeatMotionRev, RepeatMotionRev,
Null Null
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Clone,Copy,PartialEq,Eq,Debug)]
pub enum MotionBuilder { pub enum MotionBehavior {
CharSearch(Option<Direction>,Option<Dest>,Option<char>), Exclusive,
TextObj(Option<TextObj>,Option<Bound>) Inclusive,
Linewise
} }
impl Motion { impl Motion {
pub fn needs_verb(&self) -> bool { pub fn behavior(&self) -> MotionBehavior {
matches!(self, Self::TextObj(_, _)) if self.is_linewise() {
MotionBehavior::Linewise
} else if self.is_exclusive() {
MotionBehavior::Exclusive
} else {
MotionBehavior::Inclusive
}
}
pub fn is_exclusive(&self) -> bool {
matches!(&self,
Self::BeginningOfLine |
Self::BeginningOfFirstWord |
Self::BeginningOfScreenLine |
Self::FirstGraphicalOnScreenLine |
Self::LineDownCharwise |
Self::LineUpCharwise |
Self::ScreenLineUpCharwise |
Self::ScreenLineDownCharwise |
Self::ToColumn |
Self::TextObj(TextObj::Sentence(_),_) |
Self::TextObj(TextObj::Paragraph(_),_) |
Self::CharSearch(Direction::Backward, _, _) |
Self::WordMotion(To::Start,_,_) |
Self::ToBrace(_) |
Self::ToBracket(_) |
Self::ToParen(_) |
Self::ScreenLineDown |
Self::ScreenLineUp |
Self::Range(_, _)
)
}
pub fn is_linewise(&self) -> bool {
matches!(self,
Self::WholeLine |
Self::LineUp |
Self::LineDown |
Self::ScreenLineDown |
Self::ScreenLineUp
)
} }
} }
@@ -297,14 +375,11 @@ pub enum TextObj {
/// `iw`, `aw` — inner word, around word /// `iw`, `aw` — inner word, around word
Word(Word), Word(Word),
/// for stuff like 'dd'
Line,
/// `is`, `as` — inner sentence, around sentence /// `is`, `as` — inner sentence, around sentence
Sentence, Sentence(Direction),
/// `ip`, `ap` — inner paragraph, around paragraph /// `ip`, `ap` — inner paragraph, around paragraph
Paragraph, Paragraph(Direction),
/// `i"`, `a"` — inner/around double quotes /// `i"`, `a"` — inner/around double quotes
DoubleQuote, DoubleQuote,

File diff suppressed because it is too large Load Diff

View File

@@ -26,6 +26,7 @@ pub mod error;
pub mod getopt; pub mod getopt;
pub mod script; pub mod script;
pub mod highlight; pub mod highlight;
pub mod readline;
/// Unsafe to use outside of tests /// Unsafe to use outside of tests
pub fn get_nodes<F1>(input: &str, filter: F1) -> Vec<Node> pub fn get_nodes<F1>(input: &str, filter: F1) -> Vec<Node>

654
src/tests/readline.rs Normal file
View File

@@ -0,0 +1,654 @@
use std::collections::VecDeque;
use crate::{libsh::term::{Style, Styled}, prompt::readline::{history::History, keys::{KeyCode, KeyEvent, ModKeys}, linebuf::LineBuf, term::{raw_mode, KeyReader, LineWriter}, vimode::{ViInsert, ViMode, ViNormal}, FernVi, Readline}};
use pretty_assertions::assert_eq;
use super::super::*;
#[derive(Default,Debug)]
struct TestReader {
pub bytes: VecDeque<u8>
}
impl TestReader {
pub fn new() -> Self {
Self::default()
}
pub fn with_initial(mut self, bytes: &[u8]) -> Self {
let bytes = bytes.iter();
self.bytes.extend(bytes);
self
}
pub fn parse_esc_seq_from_bytes(&mut self) -> Option<KeyEvent> {
let mut seq = vec![0x1b];
let b1 = self.bytes.pop_front()?;
seq.push(b1);
match b1 {
b'[' => {
let b2 = self.bytes.pop_front()?;
seq.push(b2);
match b2 {
b'A' => Some(KeyEvent(KeyCode::Up, ModKeys::empty())),
b'B' => Some(KeyEvent(KeyCode::Down, ModKeys::empty())),
b'C' => Some(KeyEvent(KeyCode::Right, ModKeys::empty())),
b'D' => Some(KeyEvent(KeyCode::Left, ModKeys::empty())),
b'1'..=b'9' => {
let mut digits = vec![b2];
while let Some(&b) = self.bytes.front() {
seq.push(b);
self.bytes.pop_front();
if b == b'~' || b == b';' {
break;
} else if b.is_ascii_digit() {
digits.push(b);
} else {
break;
}
}
let key = match digits.as_slice() {
[b'1'] => KeyCode::Home,
[b'3'] => KeyCode::Delete,
[b'4'] => KeyCode::End,
[b'5'] => KeyCode::PageUp,
[b'6'] => KeyCode::PageDown,
[b'7'] => KeyCode::Home, // xterm alternate
[b'8'] => KeyCode::End, // xterm alternate
[b'1', b'5'] => KeyCode::F(5),
[b'1', b'7'] => KeyCode::F(6),
[b'1', b'8'] => KeyCode::F(7),
[b'1', b'9'] => KeyCode::F(8),
[b'2', b'0'] => KeyCode::F(9),
[b'2', b'1'] => KeyCode::F(10),
[b'2', b'3'] => KeyCode::F(11),
[b'2', b'4'] => KeyCode::F(12),
_ => KeyCode::Esc,
};
Some(KeyEvent(key, ModKeys::empty()))
}
_ => Some(KeyEvent(KeyCode::Esc, ModKeys::empty())),
}
}
b'O' => {
let b2 = self.bytes.pop_front()?;
seq.push(b2);
let key = match b2 {
b'P' => KeyCode::F(1),
b'Q' => KeyCode::F(2),
b'R' => KeyCode::F(3),
b'S' => KeyCode::F(4),
_ => KeyCode::Esc,
};
Some(KeyEvent(key, ModKeys::empty()))
}
_ => Some(KeyEvent(KeyCode::Esc, ModKeys::empty())),
}
}
}
impl KeyReader for TestReader {
fn read_key(&mut self) -> Option<KeyEvent> {
use core::str;
let mut collected = Vec::with_capacity(4);
loop {
let byte = self.bytes.pop_front()?;
collected.push(byte);
// If it's an escape sequence, delegate
if collected[0] == 0x1b && collected.len() == 1 {
if let Some(&_next @ (b'[' | b'0')) = self.bytes.front() {
println!("found escape seq");
let seq = self.parse_esc_seq_from_bytes();
println!("{seq:?}");
return seq
}
}
// Try parse as valid UTF-8
if let Ok(s) = str::from_utf8(&collected) {
return Some(KeyEvent::new(s, ModKeys::empty()));
}
if collected.len() >= 4 {
break;
}
}
None
}
}
pub struct TestWriter {
}
impl TestWriter {
pub fn new() -> Self {
Self {}
}
}
impl LineWriter for TestWriter {
fn clear_rows(&mut self, _layout: &prompt::readline::term::Layout) -> libsh::error::ShResult<()> {
Ok(())
}
fn redraw(
&mut self,
_prompt: &str,
_line: &LineBuf,
_new_layout: &prompt::readline::term::Layout,
) -> libsh::error::ShResult<()> {
Ok(())
}
fn flush_write(&mut self, _buf: &str) -> libsh::error::ShResult<()> {
Ok(())
}
}
impl FernVi {
pub fn new_test(prompt: Option<String>,input: &str, initial: &str) -> Self {
Self {
reader: Box::new(TestReader::new().with_initial(input.as_bytes())),
writer: Box::new(TestWriter::new()),
prompt: prompt.unwrap_or("$ ".styled(Style::Green)),
mode: Box::new(ViInsert::new()),
old_layout: None,
repeat_action: None,
repeat_motion: None,
history: History::new().unwrap(),
editor: LineBuf::new().with_initial(initial, 0)
}
}
}
fn fernvi_test(input: &str, initial: &str) -> String {
let mut fernvi = FernVi::new_test(None,input,initial);
let raw_mode = raw_mode();
let line = fernvi.readline().unwrap();
std::mem::drop(raw_mode);
line
}
fn normal_cmd(cmd: &str, buf: &str, cursor: usize) -> (String,usize) {
let cmd = ViNormal::new()
.cmds_from_raw(cmd)
.pop()
.unwrap();
let mut buf = LineBuf::new().with_initial(buf, cursor);
buf.exec_cmd(cmd).unwrap();
(buf.as_str().to_string(),buf.cursor.get())
}
#[test]
fn vimode_insert_cmds() {
let raw = "abcdefghijklmnopqrstuvwxyz1234567890-=[];'<>/\\x1b";
let mut mode = ViInsert::new();
let cmds = mode.cmds_from_raw(raw);
insta::assert_debug_snapshot!(cmds)
}
#[test]
fn vimode_normal_cmds() {
let raw = "d2wg?5b2P5x";
let mut mode = ViNormal::new();
let cmds = mode.cmds_from_raw(raw);
insta::assert_debug_snapshot!(cmds)
}
#[test]
fn linebuf_empty_linebuf() {
let mut buf = LineBuf::new();
assert_eq!(buf.as_str(), "");
buf.update_graphemes_lazy();
assert_eq!(buf.grapheme_indices(), &[]);
assert!(buf.slice(0..0).is_none());
}
#[test]
fn linebuf_ascii_content() {
let mut buf = LineBuf::new().with_initial("hello", 0);
buf.update_graphemes_lazy();
assert_eq!(buf.grapheme_indices(), &[0, 1, 2, 3, 4]);
assert_eq!(buf.grapheme_at(0), Some("h"));
assert_eq!(buf.grapheme_at(4), Some("o"));
assert_eq!(buf.slice(1..4), Some("ell"));
assert_eq!(buf.slice_to(2), Some("he"));
assert_eq!(buf.slice_from(2), Some("llo"));
}
#[test]
fn linebuf_unicode_graphemes() {
let mut buf = LineBuf::new().with_initial("a🇺🇸b́c", 0);
buf.update_graphemes_lazy();
let indices = buf.grapheme_indices();
assert_eq!(indices.len(), 4); // 4 graphemes + 1 end marker
assert_eq!(buf.grapheme_at(0), Some("a"));
assert_eq!(buf.grapheme_at(1), Some("🇺🇸"));
assert_eq!(buf.grapheme_at(2), Some("")); // b + combining accent
assert_eq!(buf.grapheme_at(3), Some("c"));
assert_eq!(buf.grapheme_at(4), None); // out of bounds
assert_eq!(buf.slice(0..2), Some("a🇺🇸"));
assert_eq!(buf.slice(1..3), Some("🇺🇸b́"));
assert_eq!(buf.slice(2..4), Some("b́c"));
}
#[test]
fn linebuf_slice_to_from_cursor() {
let mut buf = LineBuf::new().with_initial("abçd", 2);
buf.update_graphemes_lazy();
assert_eq!(buf.slice_to_cursor(), Some("ab"));
assert_eq!(buf.slice_from_cursor(), Some("çd"));
}
#[test]
fn linebuf_out_of_bounds_slices() {
let mut buf = LineBuf::new().with_initial("test", 0);
buf.update_graphemes_lazy();
assert_eq!(buf.grapheme_at(5), None); // out of bounds
assert_eq!(buf.slice(2..5), None); // end out of bounds
assert_eq!(buf.slice(4..4), None); // valid but empty
}
#[test]
fn linebuf_this_line() {
let initial = "This is the first line\nThis is the second line\nThis is the third line\nThis is the fourth line";
let mut buf = LineBuf::new().with_initial(initial, 57);
let (start,end) = buf.this_line();
assert_eq!(buf.slice(start..end), Some("This is the third line\n"))
}
#[test]
fn linebuf_prev_line() {
let initial = "This is the first line\nThis is the second line\nThis is the third line\nThis is the fourth line";
let mut buf = LineBuf::new().with_initial(initial, 57);
let (start,end) = buf.nth_prev_line(1).unwrap();
assert_eq!(buf.slice(start..end), Some("This is the second line\n"))
}
#[test]
fn linebuf_prev_line_first_line_is_empty() {
let initial = "\nThis is the first line\nThis is the second line\nThis is the third line\nThis is the fourth line";
let mut buf = LineBuf::new().with_initial(initial, 36);
let (start,end) = buf.nth_prev_line(1).unwrap();
assert_eq!(buf.slice(start..end), Some("This is the first line\n"))
}
#[test]
fn linebuf_next_line() {
let initial = "This is the first line\nThis is the second line\nThis is the third line\nThis is the fourth line";
let mut buf = LineBuf::new().with_initial(initial, 57);
let (start,end) = buf.nth_next_line(1).unwrap();
assert_eq!(buf.slice(start..end), Some("This is the fourth line"))
}
#[test]
fn linebuf_next_line_last_line_is_empty() {
let initial = "This is the first line\nThis is the second line\nThis is the third line\nThis is the fourth line\n";
let mut buf = LineBuf::new().with_initial(initial, 57);
let (start,end) = buf.nth_next_line(1).unwrap();
assert_eq!(buf.slice(start..end), Some("This is the fourth line\n"))
}
#[test]
fn linebuf_next_line_several_trailing_newlines() {
let initial = "This is the first line\nThis is the second line\nThis is the third line\nThis is the fourth line\n\n\n\n";
let mut buf = LineBuf::new().with_initial(initial, 81);
let (start,end) = buf.nth_next_line(1).unwrap();
assert_eq!(buf.slice(start..end), Some("\n"))
}
#[test]
fn linebuf_next_line_only_newlines() {
let initial = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
let mut buf = LineBuf::new().with_initial(initial, 7);
let (start,end) = buf.nth_next_line(1).unwrap();
assert_eq!(start, 8);
assert_eq!(buf.slice(start..end), Some("\n"))
}
#[test]
fn linebuf_prev_line_only_newlines() {
let initial = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
let mut buf = LineBuf::new().with_initial(initial, 7);
let (start,end) = buf.nth_prev_line(1).unwrap();
assert_eq!(buf.slice(start..end), Some("\n"));
assert_eq!(start, 6);
}
#[test]
fn linebuf_cursor_motion() {
let mut buf = LineBuf::new().with_initial("Thé quíck 🦊 bröwn fóx jumpś óver the 💤 lázy dóg 🐶", 0);
buf.update_graphemes_lazy();
let total = buf.grapheme_indices.as_ref().unwrap().len();
for i in 0..total {
buf.cursor.set(i);
let expected_to = buf.buffer.get(..buf.grapheme_indices_owned()[i]).unwrap_or("").to_string();
let expected_from = if i + 1 < total {
buf.buffer.get(buf.grapheme_indices_owned()[i]..).unwrap_or("").to_string()
} else {
// last grapheme, ends at buffer end
buf.buffer.get(buf.grapheme_indices_owned()[i]..).unwrap_or("").to_string()
};
let expected_at = {
let start = buf.grapheme_indices_owned()[i];
let end = buf.grapheme_indices_owned().get(i + 1).copied().unwrap_or(buf.buffer.len());
buf.buffer.get(start..end).map(|slice| slice.to_string())
};
assert_eq!(
buf.slice_to_cursor(),
Some(expected_to.as_str()),
"Failed at cursor position {i}: slice_to_cursor"
);
assert_eq!(
buf.slice_from_cursor(),
Some(expected_from.as_str()),
"Failed at cursor position {i}: slice_from_cursor"
);
assert_eq!(
buf.grapheme_at(i).map(|slice| slice.to_string()),
expected_at,
"Failed at cursor position {i}: grapheme_at"
);
}
}
#[test]
fn editor_delete_word() {
assert_eq!(normal_cmd(
"dw",
"The quick brown fox jumps over the lazy dog",
16),
("The quick brown jumps over the lazy dog".into(), 16)
);
}
#[test]
fn editor_delete_backwards() {
assert_eq!(normal_cmd(
"2db",
"The quick brown fox jumps over the lazy dog",
16),
("The fox jumps over the lazy dog".into(), 4)
);
}
#[test]
fn editor_rot13_five_words_backwards() {
assert_eq!(normal_cmd(
"g?5b",
"The quick brown fox jumps over the lazy dog",
31),
("The dhvpx oebja sbk whzcf bire the lazy dog".into(), 4)
);
}
#[test]
fn editor_delete_word_on_whitespace() {
assert_eq!(normal_cmd(
"dw",
"The quick brown fox",
10), //on the whitespace between "quick" and "brown"
("The quick brown fox".into(), 10)
);
}
#[test]
fn editor_delete_5_words() {
assert_eq!(normal_cmd(
"5dw",
"The quick brown fox jumps over the lazy dog",
16,),
("The quick brown dog".into(), 16)
);
}
#[test]
fn editor_delete_end_includes_last() {
assert_eq!(normal_cmd(
"de",
"The quick brown fox::::jumps over the lazy dog",
16),
("The quick brown ::::jumps over the lazy dog".into(), 16)
);
}
#[test]
fn editor_delete_end_unicode_word() {
assert_eq!(normal_cmd(
"de",
"naïve café world",
0),
(" café world".into(), 0)
);
}
#[test]
fn editor_inplace_edit_cursor_position() {
assert_eq!(normal_cmd(
"5~",
"foobar",
0),
("FOOBAr".into(), 4)
);
assert_eq!(normal_cmd(
"5rg",
"foobar",
0),
("gggggr".into(), 4)
);
}
#[test]
fn editor_insert_mode_not_clamped() {
assert_eq!(normal_cmd(
"a",
"foobar",
5),
("foobar".into(), 6)
)
}
#[test]
fn editor_overshooting_motions() {
assert_eq!(normal_cmd(
"5dw",
"foo bar",
0),
("".into(), 0)
);
assert_eq!(normal_cmd(
"3db",
"foo bar",
0),
("foo bar".into(), 0)
);
assert_eq!(normal_cmd(
"3dj",
"foo bar",
0),
("foo bar".into(), 0)
);
assert_eq!(normal_cmd(
"3dk",
"foo bar",
0),
("foo bar".into(), 0)
);
}
#[test]
fn editor_textobj_quoted() {
assert_eq!(normal_cmd(
"di\"",
"this buffer has \"some \\\"quoted\" text",
0),
("this buffer has \"\" text".into(), 17)
);
assert_eq!(normal_cmd(
"da\"",
"this buffer has \"some \\\"quoted\" text",
0),
("this buffer has text".into(), 16)
);
assert_eq!(normal_cmd(
"di'",
"this buffer has 'some \\'quoted' text",
0),
("this buffer has '' text".into(), 17)
);
assert_eq!(normal_cmd(
"da'",
"this buffer has 'some \\'quoted' text",
0),
("this buffer has text".into(), 16)
);
assert_eq!(normal_cmd(
"di`",
"this buffer has `some \\`quoted` text",
0),
("this buffer has `` text".into(), 17)
);
assert_eq!(normal_cmd(
"da`",
"this buffer has `some \\`quoted` text",
0),
("this buffer has text".into(), 16)
);
}
#[test]
fn editor_textobj_delimited() {
assert_eq!(normal_cmd(
"di)",
"this buffer has (some \\(\\)(inner) \\(\\)delimited) text",
0),
("this buffer has () text".into(), 17)
);
assert_eq!(normal_cmd(
"da)",
"this buffer has (some \\(\\)(inner) \\(\\)delimited) text",
0),
("this buffer has text".into(), 16)
);
assert_eq!(normal_cmd(
"di]",
"this buffer has [some \\[\\][inner] \\[\\]delimited] text",
0),
("this buffer has [] text".into(), 17)
);
assert_eq!(normal_cmd(
"da]",
"this buffer has [some \\[\\][inner] \\[\\]delimited] text",
0),
("this buffer has text".into(), 16)
);
assert_eq!(normal_cmd(
"di}",
"this buffer has {some \\{\\}{inner} \\{\\}delimited} text",
0),
("this buffer has {} text".into(), 17)
);
assert_eq!(normal_cmd(
"da}",
"this buffer has {some \\{\\}{inner} \\{\\}delimited} text",
0),
("this buffer has text".into(), 16)
);
assert_eq!(normal_cmd(
"di>",
"this buffer has <some \\<\\><inner> \\<\\>delimited> text",
0),
("this buffer has <> text".into(), 17)
);
assert_eq!(normal_cmd(
"da>",
"this buffer has <some \\<\\><inner> \\<\\>delimited> text",
0),
("this buffer has text".into(), 16)
);
}
const LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\nCurabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra.";
#[test]
fn editor_delete_line_up() {
assert_eq!(normal_cmd(
"dk",
LOREM_IPSUM,
237),
("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\nCurabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra.".into(), 240,)
)
}
#[test]
fn fernvi_test_simple() {
assert_eq!(fernvi_test(
"foo bar\x1bbdw\r",
""),
"foo "
)
}
#[test]
fn fernvi_test_mode_change() {
assert_eq!(fernvi_test(
"foo bar biz buzz\x1bbbb2cwbiz buzz bar\r",
""),
"foo biz buzz bar buzz"
)
}
#[test]
fn fernvi_test_lorem_ipsum_1() {
assert_eq!(fernvi_test(
"\x1bwwwwwwww5dWdBdBjjdwjdwbbbcwasdasdasdasd\x1b\r",
LOREM_IPSUM),
"Lorem ipsum dolor sit amet, incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in repin voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur asdasdasdasd occaecat cupinon proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\nCurabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra."
)
}
#[test]
fn fernvi_test_lorem_ipsum_undo() {
assert_eq!(fernvi_test(
"\x1bwwwwwwwwainserting some characters now...\x1bu\r",
LOREM_IPSUM),
LOREM_IPSUM
)
}
#[test]
fn fernvi_test_lorem_ipsum_ctrl_w() {
assert_eq!(fernvi_test(
"\x1bj5wiasdasdkjhaksjdhkajshd\x17wordswordswords\x17somemorewords\x17\x1b[D\x1b[D\x17\x1b\r",
LOREM_IPSUM),
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim am, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\nCurabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra."
)
}