diff --git a/src/app.rs b/src/app.rs index 5024fbe..3e14c04 100644 --- a/src/app.rs +++ b/src/app.rs @@ -44,6 +44,7 @@ pub(crate) struct App { pub(crate) quick_fix: crate::buffers::fixes::QuickFix, pub(crate) search: crate::buffers::search::Search, pub(crate) goto_line: crate::buffers::goto_line::GotoLine, + pub(crate) paste_scroll: Option, // Draw icons from a Nerd Font (WINDOW menu). pub(crate) nerd_font: bool, } @@ -73,18 +74,22 @@ impl App { terminal.draw(|frame| self.draw(frame))?; // Wait for event; ignore key release to avoid handling a keystroke twice. - // Wake up sooner while a completion answer is expected. + // Wake up sooner while a completion answer is expected or the view scrolls. let mut timeout = interval.saturating_sub(last_disk_check.elapsed()); - if self.terminal.visible || self.completion.is_waiting() || self.quick_fix.is_waiting() + if self.terminal.visible + || self.completion.is_waiting() + || self.quick_fix.is_waiting() + || self.paste_scroll.is_some() { timeout = timeout.min(Duration::from_millis(20)); } - if event::poll(timeout)? - && let Event::Key(key) = event::read()? - && key.kind != KeyEventKind::Release - { - // Update status_bar_text - self.handle_key(key); + if event::poll(timeout)? { + match event::read()? { + Event::Key(key) if key.kind != KeyEventKind::Release => self.handle_key(key), + // Pasted text comes in one piece and becomes one edit. + Event::Paste(text) => self.handle_paste(text), + _ => {} + } } } diff --git a/src/buffers/mod.rs b/src/buffers/mod.rs index 8818f13..e8610d4 100644 --- a/src/buffers/mod.rs +++ b/src/buffers/mod.rs @@ -21,6 +21,7 @@ mod editor_status; pub(crate) mod fixes; pub(crate) mod goto_line; pub(crate) mod menu; +pub(crate) mod paste; mod render; pub(crate) mod search; pub(crate) mod terminal; diff --git a/src/buffers/paste.rs b/src/buffers/paste.rs new file mode 100644 index 0000000..c48091f --- /dev/null +++ b/src/buffers/paste.rs @@ -0,0 +1,177 @@ +// Copyright (C) 2026 CharOfString +// +// +// This software is free software: you can redistribute it and/or modify it under the terms of the +// GNU General Public License as published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with this software. If +// not, see . + +//! Text pasted by the terminal emulator (bracketed paste). + +use crate::app::App; +use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; +use ratatui_code_editor::actions::InsertText; +use std::time::{Duration, Instant}; + +// The scroll to the cursor ends well before 300ms, even with a slow frame. +const SCROLL_TIME: Duration = Duration::from_millis(250); + +// After a paste, the view moves from where it was to the cursor. +pub(crate) struct Scroll { + from: usize, + start: Instant, +} + +// The first visible line at a point of the scroll; progress goes from 0 to 1. +fn scroll_offset(from: usize, to: usize, progress: f64) -> usize { + // Ease out: move fast first, then slow down near the cursor. + let eased = 1.0 - (1.0 - progress.min(1.0)).powi(3); + (from as f64 + (to as f64 - from as f64) * eased).round() as usize +} + +impl App { + // The terminal emulator sends the whole pasted text in one event. + pub(crate) fn handle_paste(&mut self, text: String) { + // The shell in the terminal pane gets the text unchanged. + if self.terminal.focused && !self.prompt_open() { + if let Err(err) = self.terminal.paste(&text) { + self.status_bar_text = format!("Terminal input failed: {err}"); + } + return; + } + + // Prompts, the menu and key chords read single keys, so type the text into them. + // Line breaks and other control characters are left out. + if self.keys_go_elsewhere() { + for c in text.chars().filter(|c| !c.is_control()) { + self.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE)); + } + return; + } + + // Insert the text as it is, in one edit. Typing it key by key would + // indent each new line again and redraw the screen after each key. + // Terminals send line breaks as "\r", but the buffer uses "\n". + let text = text.replace("\r\n", "\n").replace('\r', "\n"); + let from = self.buffer.editor.get_offset_y(); + self.completion.close(); + self.buffer.editor.apply(InsertText { text }); + self.buffer.editor.focus(&self.editor_area); + + // The view is already at the cursor; only the drawing scrolls there. + if self.buffer.editor.get_offset_y() != from { + self.paste_scroll = Some(Scroll { + from, + start: Instant::now(), + }); + } + } + + // The first line to draw while the paste scroll runs, or None when it is over. + pub(crate) fn paste_scroll_offset(&mut self) -> Option { + let scroll = self.paste_scroll.as_ref()?; + let progress = scroll.start.elapsed().as_secs_f64() / SCROLL_TIME.as_secs_f64(); + if progress >= 1.0 { + self.paste_scroll = None; + return None; + } + // Aim at the real view, which keys may move while the scroll runs. + let to = self.buffer.editor.get_offset_y(); + Some(scroll_offset(scroll.from, to, progress)) + } + + // True when typed keys do not go straight into the text. + fn keys_go_elsewhere(&self) -> bool { + self.prompt_open() + || self.search.is_active() + || self.goto_line.is_busy() + || self.quick_fix.is_open() + || self.ctrl_x_wait_flag + || self.ctrl_c_wait_flag + || self.ctrl_c_t_wait_flag + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn paste_keeps_indentation_and_undoes_in_one_step() { + let mut app = App::default(); + app.buffer.editor.set_content("fn main() {\n}\n"); + app.buffer.editor.set_cursor(12); + app.handle_paste(" if ok {\r run();\r }\r\n".into()); + assert_eq!( + app.buffer.editor.get_content(), + "fn main() {\n if ok {\n run();\n }\n}\n" + ); + assert_eq!(app.buffer.editor.get_cursor(), 45); + + app.handle_key(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL)); + app.handle_key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::NONE)); + assert_eq!(app.buffer.editor.get_content(), "fn main() {\n}\n"); + } + + #[test] + fn scroll_starts_at_the_old_view_and_ends_at_the_cursor() { + assert_eq!(scroll_offset(0, 85, 0.0), 0); + // Ease out: past half way at half time. + let middle = scroll_offset(0, 85, 0.5); + assert!(42 < middle && middle < 85, "{middle}"); + assert_eq!(scroll_offset(0, 85, 1.0), 85); + assert_eq!(scroll_offset(85, 10, 2.0), 10); + } + + #[test] + fn paste_scroll_is_only_drawn_and_ends_within_300ms() { + use ratatui::{Terminal, backend::TestBackend}; + let screen = |terminal: &Terminal| -> String { + let buffer = terminal.backend().buffer(); + buffer.content.iter().map(|cell| cell.symbol()).collect() + }; + let mut app = App::default(); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + // Draw once so the paste knows the size of the editor. + terminal.draw(|frame| app.draw(frame)).unwrap(); + app.handle_paste((0..100).map(|n| format!("line {n}\r")).collect()); + let end = app.buffer.editor.get_offset_y(); + assert!(end > 0 && app.paste_scroll.is_some()); + + // Drawing does not move the real view away from the cursor. + terminal.draw(|frame| app.draw(frame)).unwrap(); + assert_eq!(app.buffer.editor.get_offset_y(), end); + + // When the time is over, the view rests at the cursor. + assert!(SCROLL_TIME < Duration::from_millis(300)); + app.paste_scroll.as_mut().unwrap().start -= SCROLL_TIME; + terminal.draw(|frame| app.draw(frame)).unwrap(); + assert!(app.paste_scroll.is_none()); + assert!(screen(&terminal).contains("line 99")); + assert!(!screen(&terminal).contains("line 0 ")); + } + + #[test] + fn prompts_get_the_text_without_line_breaks() { + let mut app = App::default(); + app.save_path_input = Some(String::new()); + app.handle_paste("notes.txt\r".into()); + assert_eq!(app.save_path_input.as_deref(), Some("notes.txt")); + assert_eq!(app.buffer.editor.get_content(), ""); + } + + #[test] + fn focused_terminal_gets_the_text() { + let mut app = App::default(); + app.terminal.visible = true; + app.terminal.focused = true; + app.handle_paste("ls\r".into()); + assert_eq!(app.buffer.editor.get_content(), ""); + } +} diff --git a/src/buffers/render.rs b/src/buffers/render.rs index 9625b07..cfe0d63 100644 --- a/src/buffers/render.rs +++ b/src/buffers/render.rs @@ -65,6 +65,11 @@ impl App { Layout::vertical([Constraint::Min(0), Constraint::Length(2)]) .areas(border.inner(editor_area)); self.editor_area = content_area; + // While the paste scroll runs, draw the editor parts from its current line. + let offset_y = self.buffer.editor.get_offset_y(); + if let Some(line) = self.paste_scroll_offset() { + self.buffer.editor.set_offset_y(line); + } frame.render_widget(border, editor_area); frame.render_widget(&self.buffer.editor, self.editor_area); self.draw_diagnostics(frame); @@ -91,6 +96,7 @@ impl App { { frame.set_cursor_position((x, y)); } + self.buffer.editor.set_offset_y(offset_y); // status_bar_text bar. let status_bar_text_text = if let Some(input) = &self.cpp_checks.path_input { diff --git a/src/buffers/search/mod.rs b/src/buffers/search/mod.rs index 3526d10..a78a5d1 100644 --- a/src/buffers/search/mod.rs +++ b/src/buffers/search/mod.rs @@ -57,6 +57,11 @@ impl Search { pub(crate) fn reads_input(&self) -> bool { matches!(self.mode, Some(Mode::ReplaceFrom(_) | Mode::ReplaceTo(..))) } + + // True while search or replace is running. + pub(crate) fn is_active(&self) -> bool { + self.mode.is_some() + } } impl App { diff --git a/src/buffers/terminal.rs b/src/buffers/terminal.rs index 131ce69..b9aa59a 100644 --- a/src/buffers/terminal.rs +++ b/src/buffers/terminal.rs @@ -149,13 +149,32 @@ impl Terminal { } pub(crate) fn input(&mut self, key: KeyEvent) -> io::Result<()> { + let Some(session) = &self.session else { + return Ok(()); + }; + let bytes = encode_key(key, session.parser.screen().application_cursor()); + self.write(&bytes) + } + + // Send pasted text. Mark its start and end when the program asked for that. + pub(crate) fn paste(&mut self, text: &str) -> io::Result<()> { + let Some(session) = &self.session else { + return Ok(()); + }; + if session.parser.screen().bracketed_paste() { + self.write(format!("\x1b[200~{text}\x1b[201~").as_bytes()) + } else { + self.write(text.as_bytes()) + } + } + + fn write(&mut self, bytes: &[u8]) -> io::Result<()> { if let Some(session) = &mut self.session { session.parser.screen_mut().set_scrollback(0); if session.exited { return Ok(()); } - let bytes = encode_key(key, session.parser.screen().application_cursor()); - session.writer.write_all(&bytes)?; + session.writer.write_all(bytes)?; session.writer.flush()?; } Ok(()) @@ -325,15 +344,19 @@ fn encode_key(key: KeyEvent, application_cursor: bool) -> Vec { } impl crate::app::App { - pub(crate) fn handle_terminal_key(&mut self, key: KeyEvent) -> bool { - // Modal editor prompts retain their input and focus. - if self.cpp_check_modal() + // True while a dialog, a prompt or the menu reads the keys. + pub(crate) fn prompt_open(&self) -> bool { + self.cpp_check_modal() || self.overwrite_confirm || self.save_path_input.is_some() || self.search.reads_input() || self.goto_line.is_open() || self.menu.active.is_some() - { + } + + pub(crate) fn handle_terminal_key(&mut self, key: KeyEvent) -> bool { + // Modal editor prompts retain their input and focus. + if self.prompt_open() { return false; } let plain = |c| key.code == KeyCode::Char(c) && key.modifiers.is_empty(); diff --git a/src/main.rs b/src/main.rs index 93b388d..48b59e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,10 @@ mod lsp; mod utils; use app::App; +use crossterm::{ + event::{DisableBracketedPaste, EnableBracketedPaste}, + execute, +}; use std::{ffi::OsString, io, path::PathBuf}; // Main app of Pointer. @@ -37,7 +41,13 @@ fn main() -> io::Result<()> { app.buffer.open(path)?; } - ratatui::run(|terminal| app.run(terminal)) + ratatui::run(|terminal| { + // Ask the terminal emulator to send pasted text in one piece, not as single keys. + execute!(io::stdout(), EnableBracketedPaste)?; + let result = app.run(terminal); + execute!(io::stdout(), DisableBracketedPaste)?; + result + }) } // Validate the file arguments