feat: add multiple buffers and file switching
This commit is contained in:
@@ -66,10 +66,27 @@ Submitting an empty search repeats the last query. Esc cancels without moving th
|
||||
|
||||
Commands: `:w [path]`, `:q`, `:q!`, `:wq [path]`, `:plugins`, and `:plugin-command [arguments]`.
|
||||
|
||||
`:q` refuses unsaved changes. `:wq` quits only after a successful save. Saves use a same-directory temporary file and rename, preserve existing permissions, and reject target symlinks. UTF-8 text is edited by extended grapheme; tabs render at four-cell stops. LF and trailing-newline structure are preserved; CRLF and CR are normalized to LF. Binary/terminal-control-containing text is rejected. Bracketed paste inserts only in Insert mode and never executes pasted commands in Normal mode.
|
||||
`:q` refuses unsaved changes in any open buffer; `:q!` exits without saving them. `:wq` saves the active buffer and quits only if every buffer is then clean. Saves use a same-directory temporary file and rename, preserve existing permissions, and reject target symlinks. Writing to a path owned by another loaded buffer refuses before modifying the file. UTF-8 text is edited by extended grapheme; tabs render at four-cell stops. LF and trailing-newline structure are preserved; CRLF and CR are normalized to LF. Binary/terminal-control-containing text is rejected. Bracketed paste inserts only in Insert mode and never executes pasted commands in Normal mode.
|
||||
|
||||
Paste is backend input: it is ignored in Normal, Command, Search, and Visual modes, and inserts literal text in Insert mode, with CRLF/CR normalization and a 4 MiB limit. Pasted text is one undo action, separate from typing before and after it. Rejected paste reports an error without changing text, cursor, mode, or pending insert history. Internal clipboard actions are separate from `Input::Paste` and use already-validated buffer text.
|
||||
|
||||
### Buffers
|
||||
|
||||
One buffer is visible at a time. Each retains its text, path, saved state, directed selections and primary, mode, search state, and undo/redo history. The TUI retains separate vertical/horizontal scroll offsets by stable buffer ID, adjusting them only as needed to keep the primary visible after resizing. Clipboard fragments, bindings, plugin hosts, and plugin retirement remain session-wide. Plugin context and effects refer to the active buffer.
|
||||
|
||||
| Command | Behavior |
|
||||
| --- | --- |
|
||||
| `:edit PATH` | Switch to an already-loaded file, or read and validate a new buffer; a nonexistent file opens empty |
|
||||
| `:buffers` | List stable IDs and paths; `%` marks active, `+` marks dirty |
|
||||
| `:buffer N` | Switch to buffer ID `N` |
|
||||
| `:bnext` / `:bprev` | Switch in ascending-ID order, wrapping at either end |
|
||||
| `:bdelete [N]` | Close the active or named buffer; refuse unsaved changes |
|
||||
| `:bdelete! [N]` | Explicitly discard changes and close the active or named buffer |
|
||||
|
||||
IDs start at 1 and are never renumbered or reused. Closing the last buffer creates a fresh unnamed clean buffer rather than quitting. Closing the active buffer selects the next remaining ID, with wraparound. Switching does not save, discard, or reload hidden edits. Existing-file aliases resolve to the same loaded buffer; the supplied path spelling remains the save/display path, so opening through a symlink does not bypass save rejection. Paths are relative to the process working directory; the entire `:edit` argument is the path, including spaces, with no shell expansion.
|
||||
|
||||
Switching from Insert commits its pending undo group and parks that buffer in Normal. Explicit buffer switching from Visual retains that buffer's directed ranges and Visual mode; switching back restores them. Command/search prompts, palette queries, and pending motion counts are transient, not restored per buffer. The new commands appear in the command palette and can be bound by exact command name in Normal mode. When several buffers are open, status shows the active ID and total count.
|
||||
|
||||
### Command palette
|
||||
|
||||
Ctrl+p opens a searchable overlay without leaving the current mode. It lists core actions and registered Ex/plugin commands with their owner, effective bindings in the current mode, and eligible modes. Disabled or overridden bindings are not advertised; actions with no key remain available through the palette. Mode-ineligible entries are dimmed and cannot execute. Retired plugin commands disappear.
|
||||
@@ -84,9 +101,9 @@ Esc or Ctrl+p cancels without changing text, selections, the previous command/se
|
||||
|
||||
One crate, four modules, no workspace or frontend-specific types in the editing backend:
|
||||
|
||||
- `editor`: owns buffer, the primary cursor and selection set, modes and internal clipboard fragments, search state/navigation and grapheme-coordinate range presentation, action metadata and command-palette state/filtering, key notation and binding resolution, modal paste policy, undo/redo, saving, command ownership, capabilities, and validated edit effects. Its low-level interface returns an `Invocation` when an external command is selected.
|
||||
- `session`: `EditorSession` owns the editor and its Ruby hosts together. It loads configuration, registers eligible plugins before installing bindings, executes selected commands, and retires both host and command ownership on invocation or effect-validation failure.
|
||||
- `tui`: translates terminal events to session input, renders read-only editor state, and advertises `text` and `styled_text` capabilities. It has no plugin lifecycle callback, host storage, or paste acceptance policy.
|
||||
- `editor`: owns buffer lifecycle and per-buffer text, saved state, primary/selection set, modes, search, and undo/redo; session-wide clipboard fragments, action metadata and command-palette state/filtering, key notation and binding resolution, modal paste policy, saving, command ownership, capabilities, and validated edit effects. Its low-level interface returns an `Invocation` when an external command is selected.
|
||||
- `session`: `EditorSession` owns the editor and its Ruby hosts together. It loads configuration once, registers eligible plugins before installing shared bindings, executes selected commands against the active buffer, and retires both host and command ownership on invocation or effect-validation failure.
|
||||
- `tui`: translates terminal events to session input, renders read-only active-buffer state, keeps frontend-owned viewports by stable buffer ID, and advertises `text` and `styled_text` capabilities. It has no plugin lifecycle callback, host storage, or paste acceptance policy.
|
||||
- `ruby`: evaluates configuration and runs isolated plugin hosts. Protocol, deadlines, and process cleanup stay inside this module.
|
||||
- The executable selects paths and frontend capabilities, opens an editor session, and launches the TUI.
|
||||
|
||||
@@ -94,6 +111,7 @@ Frontends drive `EditorSession::handle` or `execute` and inspect `editor()`; the
|
||||
Frontends inspect `selections()` for document-ordered, end-exclusive Visual ranges of grapheme coordinates (empty outside Visual), `carets()` for all active cursors in document order, `cursor()` for the primary, and `selection_count()` for the total target count. A range ending at the next row's column 0 includes the previous row's newline; an empty final-row range contains no text. Selection normalization, clipboard contents, edit planning, and history remain inside the editor module.
|
||||
Frontends inspect `palette()` for the optional read-only query, filtered command entries, selected index, effective bindings, and eligibility. Palette input handling and acceptance stay in the editor; registered command execution stays in the session.
|
||||
Frontends inspect `count()` for the optional pending motion prefix; parsing and counted motion dispatch remain editor-owned.
|
||||
Frontends inspect `buffer_id()`, `buffer_count()`, `has_buffer(id)`, and the ascending-ID `buffers()` iterator of borrowed path/dirty/active metadata. Existing text, cursor, selection, search, and mode methods describe only the active buffer.
|
||||
|
||||
The terminal adapter owns raw mode, alternate-screen state, bracketed paste, and its panic hook for one session lifetime. Entry failure, normal return, I/O errors, and unwinding panics restore the caller's hook; active-terminal cleanup runs before caller panic diagnostics. Panics retain their original payload and propagate rather than being swallowed. Terminal state and panic hooks are process-global: run one TUI at a time and do not replace its hook while that session is active.
|
||||
|
||||
@@ -196,7 +214,7 @@ JSON uses Ruby's standard library. Protocol Buffers is not installed or required
|
||||
|
||||
## Current limits
|
||||
|
||||
- One text buffer; Normal, Insert, Command, Search, and characterwise Visual modes. No system clipboard integration, named registers, line/block selection, Vim operators, splits, regex/multiline search, LSP, or graphical frontend.
|
||||
- One visible buffer at a time; Normal, Insert, Command, Search, and characterwise Visual modes. No system clipboard integration, named registers, line/block selection, Vim operators, splits/tabs, regex/multiline search, LSP, or graphical frontend.
|
||||
- Undo uses full-buffer snapshots and movement scans the current line. This favors straightforward correctness over large-file performance.
|
||||
- Plugin invocation is synchronous and may pause input for up to 500 ms before a host timeout. Config/plugin startup has a 2-second deadline per host. Buffer context must fit within a 4 MiB JSON request; a too-large request disables that plugin, not editing.
|
||||
- Disabling a plugin removes its commands and bindings until the next launch. There is no hot reload.
|
||||
|
||||
+1019
-276
File diff suppressed because it is too large
Load Diff
@@ -22,6 +22,8 @@ Visual: motions extend selections, y copy, d/x/Delete cut, c change
|
||||
Insert: Esc returns to Normal retaining carets
|
||||
Search: Enter finds next literal match, Esc cancels (case-sensitive, wraps)
|
||||
Commands: :w [PATH], :q, :q!, :wq [PATH], :plugins, :PLUGIN [ARGS]
|
||||
Buffers: :edit PATH, :buffers, :buffer N, :bnext, :bprev, :bdelete[!] [N]
|
||||
:q refuses any dirty buffer; :wq saves active and quits only if all are clean
|
||||
|
||||
Ruby config: $XDG_CONFIG_HOME/vamp/config.rb or ~/.config/vamp/config.rb
|
||||
Set VAMP_RUBY to choose the Ruby executable.";
|
||||
|
||||
@@ -155,6 +155,67 @@ Vamp.command("healthy") { |editor, args| editor.replace "healthy #{args}" }
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffers_share_config_and_plugin_host_but_keep_history_and_protect_hidden_edits() {
|
||||
let fixture = Fixture::new();
|
||||
let first = fixture.file("buffer.txt", "alpha");
|
||||
let second = fixture.file("second.txt", "beta");
|
||||
fixture.file(
|
||||
"shared.rb",
|
||||
r##"
|
||||
Vamp.plugin "shared", requires: ["text"]
|
||||
calls = 0
|
||||
Vamp.command("stamp") do |editor, _|
|
||||
calls += 1
|
||||
editor.insert "#{calls}:#{File.basename(editor.path)}:"
|
||||
end
|
||||
Vamp.command("fail") { |_, _| raise "shared host failure" }
|
||||
"##,
|
||||
);
|
||||
let mut session = fixture.open(
|
||||
r#"
|
||||
Vamp.configure do |config|
|
||||
config.plugin "shared.rb"
|
||||
config.bind "g", "stamp"
|
||||
end
|
||||
"#,
|
||||
);
|
||||
session.handle(Input::Char('g'));
|
||||
assert_eq!(session.editor().lines(), &["1:buffer.txt:alpha"]);
|
||||
session.execute(&format!("edit {}", second.display()));
|
||||
assert_eq!(session.editor().buffer_id(), 2);
|
||||
session.handle(Input::Char('g'));
|
||||
assert_eq!(session.editor().lines(), &["2:second.txt:beta"]);
|
||||
session.execute("bdelete");
|
||||
assert_eq!(session.editor().buffer_id(), 2);
|
||||
assert_eq!(session.editor().buffer_count(), 2);
|
||||
session.handle(Input::Char('u'));
|
||||
assert_eq!(session.editor().lines(), &["beta"]);
|
||||
session.execute("q");
|
||||
assert!(!session.editor().should_quit());
|
||||
session.execute("wq");
|
||||
assert!(!session.editor().should_quit());
|
||||
assert_eq!(fs::read_to_string(&first).unwrap(), "alpha");
|
||||
assert_eq!(fs::read_to_string(&second).unwrap(), "beta");
|
||||
session.execute("buffer 1");
|
||||
assert_eq!(session.editor().lines(), &["1:buffer.txt:alpha"]);
|
||||
session.handle(Input::Char('u'));
|
||||
assert_eq!(session.editor().lines(), &["alpha"]);
|
||||
session.handle(Input::Char('g'));
|
||||
assert_eq!(session.editor().lines(), &["3:buffer.txt:alpha"]);
|
||||
session.execute("w");
|
||||
assert_eq!(fs::read_to_string(&first).unwrap(), "3:buffer.txt:alpha");
|
||||
session.execute("bnext");
|
||||
assert_eq!(session.editor().lines(), &["beta"]);
|
||||
session.execute("fail");
|
||||
assert!(session.editor().message().contains("disabled"));
|
||||
session.execute("buffer 1");
|
||||
session.handle(Input::Char('g'));
|
||||
assert_eq!(session.editor().lines(), &["3:buffer.txt:alpha"]);
|
||||
session.execute("q");
|
||||
assert!(session.editor().should_quit());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_palette_binding_prepares_arguments_and_retires_failed_plugin_commands() {
|
||||
let fixture = Fixture::new();
|
||||
|
||||
+291
-14
@@ -1,4 +1,6 @@
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt::Write as _,
|
||||
io::{self, Stdout},
|
||||
panic::{self, AssertUnwindSafe, PanicHookInfo},
|
||||
sync::{Arc, Mutex},
|
||||
@@ -109,15 +111,28 @@ impl Drop for TerminalSession {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct BufferViewports {
|
||||
offsets: BTreeMap<usize, (usize, usize)>,
|
||||
}
|
||||
|
||||
impl BufferViewports {
|
||||
fn draw(&mut self, frame: &mut Frame, editor: &Editor) {
|
||||
self.offsets.retain(|id, _| editor.has_buffer(*id));
|
||||
let (top, left) = self.offsets.entry(editor.buffer_id()).or_default();
|
||||
draw(frame, editor, top, left);
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders session state and translates terminal input; the session owns plugin
|
||||
/// execution and retirement, so the adapter needs no lifecycle callback.
|
||||
pub fn run(session: &mut EditorSession) -> io::Result<()> {
|
||||
TerminalSession::run(|| {
|
||||
let mut terminal: Terminal<CrosstermBackend<Stdout>> =
|
||||
Terminal::new(CrosstermBackend::new(io::stdout()))?;
|
||||
let (mut top, mut left) = (0, 0);
|
||||
let mut viewports = BufferViewports::default();
|
||||
while !session.editor().should_quit() {
|
||||
terminal.draw(|frame| draw(frame, session.editor(), &mut top, &mut left))?;
|
||||
terminal.draw(|frame| viewports.draw(frame, session.editor()))?;
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind != KeyEventKind::Release => {
|
||||
if let Some(input) = translate(key) {
|
||||
@@ -325,24 +340,31 @@ fn draw(frame: &mut Frame, editor: &Editor, top: &mut usize, left: &mut usize) {
|
||||
.chars()
|
||||
.map(|c| if c.is_control() { '�' } else { c })
|
||||
.collect();
|
||||
let status = format!(
|
||||
" {} {}{} {}:{}{}{}",
|
||||
editor.mode(),
|
||||
let mut status = format!(" {}", editor.mode());
|
||||
if editor.buffer_count() > 1 {
|
||||
write!(
|
||||
status,
|
||||
" [buffer {}/{}]",
|
||||
editor.buffer_id(),
|
||||
editor.buffer_count()
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
write!(
|
||||
status,
|
||||
" {}{} {}:{}",
|
||||
path,
|
||||
if editor.dirty() { " [+]" } else { "" },
|
||||
cursor.row + 1,
|
||||
cursor.column + 1,
|
||||
)
|
||||
.unwrap();
|
||||
if editor.selection_count() > 1 {
|
||||
format!(" {} selections", editor.selection_count())
|
||||
} else {
|
||||
String::new()
|
||||
},
|
||||
if let Some(count) = editor.count() {
|
||||
format!(" count: {count}")
|
||||
} else {
|
||||
String::new()
|
||||
write!(status, " {} selections", editor.selection_count()).unwrap();
|
||||
}
|
||||
if let Some(count) = editor.count() {
|
||||
write!(status, " count: {count}").unwrap();
|
||||
}
|
||||
);
|
||||
frame.render_widget(
|
||||
Paragraph::new(status).style(Style::default().fg(Color::Black).bg(Color::Cyan)),
|
||||
status_area,
|
||||
@@ -479,6 +501,36 @@ fn draw_palette(frame: &mut Frame, palette: &Palette, mode: Mode) {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::{
|
||||
fs,
|
||||
path::PathBuf,
|
||||
sync::atomic::{AtomicUsize, Ordering},
|
||||
};
|
||||
|
||||
struct Directory(PathBuf);
|
||||
|
||||
impl Directory {
|
||||
fn new() -> Self {
|
||||
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
|
||||
let path =
|
||||
std::env::temp_dir().join(format!("vamp-tui-test-{}-{id}", std::process::id()));
|
||||
fs::create_dir(&path).unwrap();
|
||||
Self(path)
|
||||
}
|
||||
|
||||
fn file(&self, name: &str, text: &str) -> PathBuf {
|
||||
let path = self.0.join(name);
|
||||
fs::write(&path, text).unwrap();
|
||||
path
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Directory {
|
||||
fn drop(&mut self) {
|
||||
let _ = fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn screen_text(terminal: &Terminal<ratatui::backend::TestBackend>) -> String {
|
||||
let buffer = terminal.backend().buffer();
|
||||
@@ -493,6 +545,231 @@ mod tests {
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_switches_restore_independent_vertical_and_horizontal_viewports() {
|
||||
let directory = Directory::new();
|
||||
let a = directory.file("a", &vec!["a".repeat(50); 20].join("\n"));
|
||||
let b = directory.file("b", &vec!["b".repeat(40); 20].join("\n"));
|
||||
let mut editor = Editor::open(Some(a), vec![]).unwrap();
|
||||
let a_id = editor.buffer_id();
|
||||
let mut terminal = Terminal::new(ratatui::backend::TestBackend::new(16, 6)).unwrap();
|
||||
let mut viewports = BufferViewports::default();
|
||||
for _ in 0..14 {
|
||||
editor.handle(Input::Down);
|
||||
}
|
||||
editor.handle(Input::End);
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
assert_eq!(viewports.offsets[&a_id], (11, 37));
|
||||
for _ in 0..2 {
|
||||
editor.handle(Input::Up);
|
||||
}
|
||||
for _ in 0..3 {
|
||||
editor.handle(Input::Left);
|
||||
}
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
let a_viewport = viewports.offsets[&a_id];
|
||||
let a_cursor = terminal.get_cursor_position().unwrap();
|
||||
assert_eq!(a_viewport, (11, 37));
|
||||
|
||||
editor.execute(&format!("edit {}", b.display()));
|
||||
let b_id = editor.buffer_id();
|
||||
assert_ne!(a_id, b_id);
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
assert_eq!(viewports.offsets[&b_id], (0, 0));
|
||||
for _ in 0..8 {
|
||||
editor.handle(Input::Down);
|
||||
}
|
||||
editor.handle(Input::End);
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
editor.handle(Input::Up);
|
||||
editor.handle(Input::Left);
|
||||
editor.handle(Input::Left);
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
let b_viewport = viewports.offsets[&b_id];
|
||||
let b_cursor = terminal.get_cursor_position().unwrap();
|
||||
assert_eq!(b_viewport, (5, 27));
|
||||
|
||||
editor.execute(&format!("buffer {a_id}"));
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
assert_eq!(viewports.offsets[&a_id], a_viewport);
|
||||
assert_eq!(terminal.get_cursor_position().unwrap(), a_cursor);
|
||||
assert_eq!(
|
||||
terminal.backend().buffer()[(a_cursor.x, a_cursor.y)].symbol(),
|
||||
"a"
|
||||
);
|
||||
editor.execute("bnext");
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
assert_eq!(editor.buffer_id(), b_id);
|
||||
assert_eq!(viewports.offsets[&b_id], b_viewport);
|
||||
assert_eq!(terminal.get_cursor_position().unwrap(), b_cursor);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_inactive_buffer_retires_its_viewport_without_moving_active_buffer() {
|
||||
let directory = Directory::new();
|
||||
let a = directory.file("a", &vec!["a".repeat(50); 20].join("\n"));
|
||||
let b = directory.file("b", &vec!["b".repeat(40); 20].join("\n"));
|
||||
let c = directory.file("c", "new buffer");
|
||||
let mut editor = Editor::open(Some(a), vec![]).unwrap();
|
||||
let a_id = editor.buffer_id();
|
||||
let mut terminal = Terminal::new(ratatui::backend::TestBackend::new(16, 6)).unwrap();
|
||||
let mut viewports = BufferViewports::default();
|
||||
for _ in 0..14 {
|
||||
editor.handle(Input::Down);
|
||||
}
|
||||
editor.handle(Input::End);
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
editor.execute(&format!("edit {}", b.display()));
|
||||
let b_id = editor.buffer_id();
|
||||
for _ in 0..8 {
|
||||
editor.handle(Input::Down);
|
||||
}
|
||||
editor.handle(Input::End);
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
let b_viewport = viewports.offsets[&b_id];
|
||||
let b_cursor = terminal.get_cursor_position().unwrap();
|
||||
assert_eq!(viewports.offsets.len(), 2);
|
||||
|
||||
editor.execute(&format!("bdelete {a_id}"));
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
assert_eq!(editor.buffer_id(), b_id);
|
||||
assert!(!editor.has_buffer(a_id));
|
||||
assert!(!viewports.offsets.contains_key(&a_id));
|
||||
assert_eq!(viewports.offsets.len(), 1);
|
||||
assert_eq!(viewports.offsets[&b_id], b_viewport);
|
||||
assert_eq!(terminal.get_cursor_position().unwrap(), b_cursor);
|
||||
editor.execute(&format!("edit {}", c.display()));
|
||||
let c_id = editor.buffer_id();
|
||||
assert!(c_id > b_id);
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
assert_eq!(viewports.offsets[&c_id], (0, 0));
|
||||
assert!(!viewports.offsets.contains_key(&a_id));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restoring_buffer_after_narrow_resize_clamps_caret_inside_text_region() {
|
||||
let directory = Directory::new();
|
||||
let a = directory.file("a", &vec!["a".repeat(50); 20].join("\n"));
|
||||
let b = directory.file("b", "other");
|
||||
let mut editor = Editor::open(Some(a), vec![]).unwrap();
|
||||
let a_id = editor.buffer_id();
|
||||
let mut terminal = Terminal::new(ratatui::backend::TestBackend::new(24, 8)).unwrap();
|
||||
let mut viewports = BufferViewports::default();
|
||||
for _ in 0..14 {
|
||||
editor.handle(Input::Down);
|
||||
}
|
||||
editor.handle(Input::End);
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
assert_eq!(viewports.offsets[&a_id], (9, 29));
|
||||
editor.execute(&format!("edit {}", b.display()));
|
||||
let b_id = editor.buffer_id();
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
terminal.backend_mut().resize(4, 4);
|
||||
terminal
|
||||
.resize(ratatui::layout::Rect::new(0, 0, 4, 4))
|
||||
.unwrap();
|
||||
editor.execute(&format!("buffer {a_id}"));
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
assert_eq!(viewports.offsets[&a_id], (13, 49));
|
||||
assert_eq!(viewports.offsets[&b_id], (0, 0));
|
||||
assert_eq!(
|
||||
terminal.get_cursor_position().unwrap(),
|
||||
ratatui::layout::Position::new(3, 1)
|
||||
);
|
||||
|
||||
for (width, height) in [(1, 1), (24, 8)] {
|
||||
let area = ratatui::layout::Rect::new(0, 0, width, height);
|
||||
terminal.backend_mut().resize(width, height);
|
||||
terminal.resize(area).unwrap();
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
let cursor = terminal.get_cursor_position().unwrap();
|
||||
assert!(area.contains(cursor), "{area:?}: {cursor:?}");
|
||||
assert_eq!(
|
||||
terminal.backend().buffer()[(cursor.x, cursor.y)].symbol(),
|
||||
"a"
|
||||
);
|
||||
assert_eq!(viewports.offsets[&b_id], (0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_identifies_active_buffer_and_restored_visual_selection_count() {
|
||||
let directory = Directory::new();
|
||||
let a = directory.file("a", "word\nword\nword");
|
||||
let b = directory.file("b", "other");
|
||||
let mut editor = Editor::open(Some(a), vec![]).unwrap();
|
||||
let a_id = editor.buffer_id();
|
||||
let mut terminal = Terminal::new(ratatui::backend::TestBackend::new(240, 6)).unwrap();
|
||||
let mut viewports = BufferViewports::default();
|
||||
editor.handle(Input::Char('v'));
|
||||
editor.handle(Input::Ctrl('a'));
|
||||
assert_eq!(editor.selection_count(), 3);
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
assert!(!screen_text(&terminal).contains("[buffer"));
|
||||
editor.execute(&format!("edit {}", b.display()));
|
||||
let b_id = editor.buffer_id();
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
let text = screen_text(&terminal);
|
||||
assert!(
|
||||
text.contains(&format!("NORMAL [buffer {b_id}/2]")),
|
||||
"{text}"
|
||||
);
|
||||
assert!(!text.contains("3 selections"), "{text}");
|
||||
editor.execute(&format!("buffer {a_id}"));
|
||||
terminal
|
||||
.draw(|frame| viewports.draw(frame, &editor))
|
||||
.unwrap();
|
||||
let text = screen_text(&terminal);
|
||||
assert!(
|
||||
text.contains(&format!("VISUAL [buffer {a_id}/2]")),
|
||||
"{text}"
|
||||
);
|
||||
assert!(text.contains("3 selections"), "{text}");
|
||||
let (top, left) = viewports.offsets[&a_id];
|
||||
for selection in editor.selections() {
|
||||
let x = 2 + (selection.start.column - left) as u16;
|
||||
let y = (selection.start.row - top) as u16;
|
||||
assert!(matches!(
|
||||
terminal.backend().buffer()[(x, y)].bg,
|
||||
Color::Blue | Color::Magenta
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_motion_count_is_visible_and_escape_cancels_without_moving() {
|
||||
let mut editor = Editor::open(None, vec![]).unwrap();
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
hello
|
||||
Reference in New Issue
Block a user