Skip to main content

fresh/app/
clipboard.rs

1//! Clipboard and multi-cursor operations for the Editor.
2//!
3//! This module contains clipboard operations and multi-cursor actions:
4//! - Copy/cut/paste operations
5//! - Copy with formatting (HTML with syntax highlighting)
6//! - Multi-cursor add above/below/at next match
7
8use ratatui::style::{Modifier, Style};
9use rust_i18n::t;
10use std::sync::atomic::{AtomicU64, Ordering};
11use std::time::{Duration, Instant};
12
13use crate::input::multi_cursor::{
14    add_cursor_above, add_cursor_at_next_match, add_cursor_below, line_end_positions_in_selection,
15    AddCursorResult,
16};
17use crate::model::buffer_position::byte_to_2d;
18use crate::model::cursor::Cursor;
19use crate::model::event::{BufferId, CursorId, Event};
20use crate::primitives::word_navigation::{
21    find_vi_word_end, find_word_start_left, find_word_start_right,
22};
23use crate::services::async_bridge::AsyncMessage;
24use crate::view::virtual_text::{VirtualTextId, VirtualTextPosition};
25
26use super::Editor;
27
28/// Per-paste timeout. The async-paste path renders a placeholder
29/// marker and lets the user keep editing; if the background arboard
30/// read doesn't return within this window, the marker is removed and
31/// the paste is silently cancelled. 500 ms is comfortably longer than
32/// any reasonable clipboard round trip and short enough that users
33/// recognise a stalled paste before they've moved on.
34pub(crate) const PASTE_ASYNC_DEADLINE: Duration = Duration::from_millis(500);
35
36/// Inline-wait budget at the top of `paste()`. Before going async, we
37/// race the arboard read against this duration; if the clipboard
38/// responds within the window (the common case on a responsive
39/// system, ~3ms), we paste inline and skip the placeholder entirely
40/// — the user sees zero perceptible latency, indistinguishable from
41/// the old synchronous path. Only when arboard takes longer than
42/// this do we fall through to the placeholder/event-bridge path.
43///
44/// 50ms catches typical X11/Wayland clipboard round trips even on
45/// slower systems (the prior 20ms budget was missing them — anything
46/// in the 20-50ms band fell into the slow placeholder+bridge path,
47/// which a slow renderer compounds into hundreds of ms of perceived
48/// latency since each render frame is gated on the render itself).
49/// It's at the edge of the ~50ms human latency-perception threshold,
50/// so a worst-case inline wait still feels nearly instant; on a hung
51/// clipboard it's a short, bounded stall before the async path takes
52/// over.
53pub(crate) const PASTE_INLINE_WAIT: Duration = Duration::from_millis(50);
54
55/// Hard cap on concurrent pending pastes. Each entry costs one virtual
56/// text + one marker + one OS thread; in practice the deadline keeps
57/// the count near zero. The cap exists only to bound damage from a
58/// runaway macro / wedged process holding the clipboard forever.
59const MAX_PENDING_PASTES: usize = 64;
60
61/// Single anchor a paste will land at when its read returns. Stored
62/// per-cursor at dispatch time (selections having been deleted first
63/// so the anchor sits at the eventual insertion point).
64#[derive(Debug, Clone, Copy)]
65pub struct PasteAnchor {
66    /// Virtual text rendering the visual "▍" placeholder; also owns
67    /// the underlying marker that tracks the position through edits.
68    pub virtual_text_id: VirtualTextId,
69}
70
71/// In-flight async paste. Lives in `Editor::paste_pending` keyed by
72/// `request_id` between dispatching the background read and receiving
73/// the matching `AsyncMessage::ClipboardPasteResult`. Multiple may be
74/// pending at once (each Ctrl+V allocates a new id) and each captures
75/// the OS clipboard contents at the moment its own thread starts.
76#[derive(Debug, Clone)]
77pub struct PendingPaste {
78    /// Wall-clock cutoff. The tick walks `paste_pending` and removes
79    /// any entry past this point; arboard threads that come back
80    /// afterwards find no matching entry and are dropped. (The
81    /// request id is the map key, not stored here.)
82    pub deadline: Instant,
83    /// Buffer the anchors live in. Used at resolve time so a paste
84    /// initiated in buffer A still lands in A even if the user
85    /// switched to buffer B during the wait. If the buffer was closed
86    /// in the meantime the entire entry is discarded.
87    pub buffer_id: BufferId,
88    /// One anchor per cursor at dispatch time (after any selection
89    /// deletes were applied). Insertions happen in descending position
90    /// order at resolve time so earlier offsets stay valid.
91    pub anchors: Vec<PasteAnchor>,
92    /// Cursor count captured at dispatch — column-mode paste (one line
93    /// per cursor) is decided against this snapshot, not against the
94    /// live cursor list which may have changed during the wait.
95    pub cursor_count_at_dispatch: usize,
96    /// Buffer line-ending captured at dispatch, used to convert the
97    /// clipboard's LF-normalised text back to the buffer's format
98    /// before insertion.
99    pub line_ending: crate::model::buffer::LineEnding,
100    /// Wall-clock when paste() was called, used by the `paste_timing`
101    /// trace target to measure end-to-end latency from Ctrl+V to the
102    /// pasted text appearing on screen.
103    pub dispatched_at: Instant,
104}
105
106static NEXT_PASTE_REQUEST_ID: AtomicU64 = AtomicU64::new(1);
107
108pub(crate) fn allocate_paste_request_id() -> u64 {
109    NEXT_PASTE_REQUEST_ID.fetch_add(1, Ordering::Relaxed)
110}
111
112// These are the clipboard and multi-cursor operations on Editor.
113//
114// MOTIVATION FOR SEPARATION:
115// - Buffer operations need: multi-cursor, selections, event sourcing, undo/redo
116// - Prompt operations need: simple string manipulation, no selection tracking
117// - Sharing code would force prompts to use Buffer (expensive) or buffers to
118//   lose features (selections, multi-cursor, undo)
119//
120// Both use the same clipboard storage (self.clipboard) ensuring copy/paste
121// works across buffer editing and prompt input.
122
123impl Editor {
124    /// Copy the current selection to clipboard
125    ///
126    /// If no selection exists, copies the entire current line (like VSCode/Rider/Zed).
127    /// For block selections, copies only the rectangular region.
128    pub fn copy_selection(&mut self) {
129        // Check if any cursor has a block selection (takes priority)
130        let has_block_selection = self
131            .active_cursors()
132            .iter()
133            .any(|(_, cursor)| cursor.has_block_selection());
134
135        if has_block_selection {
136            // Block selection: copy rectangular region
137            let text = self.copy_block_selection_text();
138            if !text.is_empty() {
139                self.clipboard.copy(text);
140                self.active_window_mut().status_message = Some(t!("clipboard.copied").to_string());
141            }
142            return;
143        }
144
145        // Check if any cursor has a normal selection
146        let has_selection = self
147            .active_cursors()
148            .iter()
149            .any(|(_, cursor)| cursor.selection_range().is_some());
150
151        if has_selection {
152            // Original behavior: copy selected text
153            let ranges: Vec<_> = self
154                .active_cursors()
155                .iter()
156                .filter_map(|(_, cursor)| cursor.selection_range())
157                .collect();
158
159            let mut text = String::new();
160            let state = self.active_state_mut();
161            for range in ranges {
162                if !text.is_empty() {
163                    text.push('\n');
164                }
165                let range_text = state.get_text_range(range.start, range.end);
166                text.push_str(&range_text);
167            }
168
169            if !text.is_empty() {
170                self.clipboard.copy(text);
171                self.active_window_mut().status_message = Some(t!("clipboard.copied").to_string());
172            }
173        } else {
174            // No selection: copy entire line(s) for each cursor
175            let estimated_line_length = 80;
176            let mut text = String::new();
177
178            // Collect cursor positions first
179            let positions: Vec<_> = self
180                .active_cursors()
181                .iter()
182                .map(|(_, c)| c.position)
183                .collect();
184            let state = self.active_state_mut();
185
186            for pos in positions {
187                let mut iter = state.buffer.line_iterator(pos, estimated_line_length);
188                if let Some((_start, content)) = iter.next_line() {
189                    if !text.is_empty() {
190                        text.push('\n');
191                    }
192                    text.push_str(&content);
193                }
194            }
195
196            if !text.is_empty() {
197                self.clipboard.copy(text);
198                self.active_window_mut().status_message =
199                    Some(t!("clipboard.copied_line").to_string());
200            }
201        }
202    }
203
204    /// Extract text from block (rectangular) selection
205    ///
206    /// For block selection, we need to extract a rectangular region defined by:
207    /// - The block anchor (stored as Position2D with line and column)
208    /// - The current cursor position (byte offset, converted to 2D)
209    ///
210    /// This works for both small and large files by using line_iterator
211    /// for iteration and only using 2D positions for column extraction.
212    pub(crate) fn copy_block_selection_text(&mut self) -> String {
213        let estimated_line_length = 120;
214
215        // Collect block selection info from all cursors
216        let block_infos: Vec<_> = self
217            .active_cursors()
218            .iter()
219            .filter_map(|(_, cursor)| {
220                if !cursor.has_block_selection() {
221                    return None;
222                }
223                let block_anchor = cursor.block_anchor?;
224                let anchor_byte = cursor.anchor?; // byte offset of anchor
225                let cursor_byte = cursor.position;
226                Some((block_anchor, anchor_byte, cursor_byte))
227            })
228            .collect();
229
230        let mut result = String::new();
231
232        for (block_anchor, anchor_byte, cursor_byte) in block_infos {
233            // Get current cursor position as 2D
234            let cursor_2d = {
235                let state = self.active_state();
236                byte_to_2d(&state.buffer, cursor_byte)
237            };
238
239            // Calculate column bounds (min and max columns for the rectangle)
240            let min_col = block_anchor.column.min(cursor_2d.column);
241            let max_col = block_anchor.column.max(cursor_2d.column);
242
243            // Calculate line bounds using byte positions
244            let start_byte = anchor_byte.min(cursor_byte);
245            let end_byte = anchor_byte.max(cursor_byte);
246
247            // Use line_iterator to iterate through lines
248            let state = self.active_state_mut();
249            let mut iter = state
250                .buffer
251                .line_iterator(start_byte, estimated_line_length);
252
253            // Collect lines within the block selection range
254            let mut lines_text = Vec::new();
255            loop {
256                let line_start = iter.current_position();
257
258                // Stop if we've passed the end of the selection
259                if line_start > end_byte {
260                    break;
261                }
262
263                if let Some((_offset, line_content)) = iter.next_line() {
264                    // Extract the column range from this line
265                    // Remove trailing newline for column calculation
266                    let content_without_newline = line_content.trim_end_matches(&['\n', '\r'][..]);
267                    let chars: Vec<char> = content_without_newline.chars().collect();
268
269                    // Extract characters from min_col to max_col (exclusive)
270                    let extracted: String = chars
271                        .iter()
272                        .skip(min_col)
273                        .take(max_col.saturating_sub(min_col))
274                        .collect();
275
276                    lines_text.push(extracted);
277
278                    // If this line extends past end_byte, we're done
279                    if line_start + line_content.len() > end_byte {
280                        break;
281                    }
282                } else {
283                    break;
284                }
285            }
286
287            // Join the extracted text from each line
288            if !result.is_empty() && !lines_text.is_empty() {
289                result.push('\n');
290            }
291            result.push_str(&lines_text.join("\n"));
292        }
293
294        result
295    }
296
297    /// Copy selection with a specific theme's formatting
298    ///
299    /// If theme_name is empty, opens a prompt to select a theme.
300    /// Otherwise, copies the selected text as HTML with inline CSS styles.
301    pub fn copy_selection_with_theme(&mut self, theme_name: &str) {
302        // Check if there's a selection first
303        let has_selection = self
304            .active_cursors()
305            .iter()
306            .any(|(_, cursor)| cursor.selection_range().is_some());
307
308        if !has_selection {
309            self.active_window_mut().status_message =
310                Some(t!("clipboard.no_selection").to_string());
311            return;
312        }
313
314        // Empty theme = open theme picker prompt
315        if theme_name.is_empty() {
316            self.start_copy_with_formatting_prompt();
317            return;
318        }
319        use crate::services::styled_html::render_styled_html;
320
321        // Get the requested theme from registry
322        let theme = match self.theme_registry.get_cloned(theme_name) {
323            Some(t) => t,
324            None => {
325                self.active_window_mut().status_message =
326                    Some(format!("Theme '{}' not found", theme_name));
327                return;
328            }
329        };
330
331        // Collect ranges and their byte offsets
332        let ranges: Vec<_> = self
333            .active_cursors()
334            .iter()
335            .filter_map(|(_, cursor)| cursor.selection_range())
336            .collect();
337
338        if ranges.is_empty() {
339            self.active_window_mut().status_message =
340                Some(t!("clipboard.no_selection").to_string());
341            return;
342        }
343
344        // Get the overall range for highlighting
345        let min_offset = ranges.iter().map(|r| r.start).min().unwrap_or(0);
346        let max_offset = ranges.iter().map(|r| r.end).max().unwrap_or(0);
347
348        // Collect text and highlight spans from state
349        let (text, highlight_spans) = {
350            let state = self.active_state_mut();
351
352            // Collect text from all ranges
353            let mut text = String::new();
354            for range in &ranges {
355                if !text.is_empty() {
356                    text.push('\n');
357                }
358                let range_text = state.get_text_range(range.start, range.end);
359                text.push_str(&range_text);
360            }
361
362            if text.is_empty() {
363                (text, Vec::new())
364            } else {
365                // Get highlight spans for the selected region
366                let highlight_spans = state.highlighter.highlight_viewport(
367                    &state.buffer,
368                    min_offset,
369                    max_offset,
370                    &theme,
371                    0, // No context needed since we're copying exact selection
372                );
373                (text, highlight_spans)
374            }
375        };
376
377        if text.is_empty() {
378            self.active_window_mut().status_message = Some(t!("clipboard.no_text").to_string());
379            return;
380        }
381
382        // Adjust highlight spans to be relative to the copied text
383        let adjusted_spans: Vec<_> = if ranges.len() == 1 {
384            let base_offset = ranges[0].start;
385            highlight_spans
386                .into_iter()
387                .filter_map(|span| {
388                    if span.range.end <= base_offset || span.range.start >= ranges[0].end {
389                        return None;
390                    }
391                    let start = span.range.start.saturating_sub(base_offset);
392                    let end = (span.range.end - base_offset).min(text.len());
393                    if start < end {
394                        Some(crate::primitives::highlighter::HighlightSpan {
395                            range: start..end,
396                            color: span.color,
397                            bg: None,
398                            category: span.category,
399                        })
400                    } else {
401                        None
402                    }
403                })
404                .collect()
405        } else {
406            Vec::new()
407        };
408
409        // Render the styled text to HTML
410        let html = render_styled_html(&text, &adjusted_spans, &theme);
411
412        // Copy the HTML to clipboard (with plain text fallback)
413        if self.clipboard.copy_html(&html, &text) {
414            self.active_window_mut().status_message =
415                Some(t!("clipboard.copied_with_theme", theme = theme_name).to_string());
416        } else {
417            self.clipboard.copy(text);
418            self.active_window_mut().status_message =
419                Some(t!("clipboard.copied_plain").to_string());
420        }
421    }
422
423    /// Start the theme selection prompt for copy with formatting
424    fn start_copy_with_formatting_prompt(&mut self) {
425        use crate::view::prompt::PromptType;
426
427        let available_themes = self.theme_registry.list();
428        // Resolve the config value (portable form) to a canonical registry
429        // key so the picker can pre-highlight the current theme.
430        let resolved_current = self
431            .theme_registry
432            .resolve_key(&self.config.theme.0)
433            .unwrap_or_else(|| self.config.theme.0.clone());
434        let current_theme_key = resolved_current.as_str();
435
436        // Find the index of the current theme (match by key first, then name)
437        let current_index = available_themes
438            .iter()
439            .position(|info| info.key == *current_theme_key)
440            .or_else(|| {
441                let normalized = crate::view::theme::normalize_theme_name(current_theme_key);
442                available_themes.iter().position(|info| {
443                    crate::view::theme::normalize_theme_name(&info.name) == normalized
444                })
445            })
446            .unwrap_or(0);
447
448        let suggestions: Vec<crate::input::commands::Suggestion> = available_themes
449            .iter()
450            .map(|info| {
451                let is_current = Some(info) == available_themes.get(current_index);
452                let description = if is_current {
453                    Some(format!("{} (current)", info.key))
454                } else {
455                    Some(info.key.clone())
456                };
457                crate::input::commands::Suggestion {
458                    description_spans: None,
459                    text: info.name.clone(),
460                    description,
461                    value: Some(info.key.clone()),
462                    disabled: false,
463                    keybinding: None,
464                    source: None,
465                }
466            })
467            .collect();
468
469        self.active_window_mut().prompt = Some(crate::view::prompt::Prompt::with_suggestions(
470            "Copy with theme: ".to_string(),
471            PromptType::CopyWithFormattingTheme,
472            suggestions,
473        ));
474
475        if let Some(prompt) = self.active_window_mut().prompt.as_mut() {
476            if !prompt.suggestions.is_empty() {
477                prompt.selected_suggestion = Some(current_index);
478                prompt.input = current_theme_key.to_string();
479                prompt.cursor_pos = prompt.input.len();
480            }
481        }
482    }
483
484    /// Cut the current selection to clipboard
485    ///
486    /// If no selection exists, cuts the entire current line (like VSCode/Rider/Zed).
487    pub fn cut_selection(&mut self) {
488        // Check if any cursor has a selection
489        let has_selection = self
490            .active_cursors()
491            .iter()
492            .any(|(_, cursor)| cursor.selection_range().is_some());
493
494        // Copy first (this handles both selection and whole-line cases)
495        self.copy_selection();
496
497        if has_selection {
498            // Delete selected text from all cursors
499            // IMPORTANT: Sort deletions by position to ensure we process from end to start
500            let mut deletions: Vec<_> = self
501                .active_cursors()
502                .iter()
503                .filter_map(|(_, c)| c.selection_range())
504                .collect();
505            // Sort by start position so reverse iteration processes from end to start
506            deletions.sort_by_key(|r| r.start);
507
508            let primary_id = self.active_cursors().primary_id();
509            let state = self.active_state_mut();
510            let events: Vec<_> = deletions
511                .iter()
512                .rev()
513                .map(|range| {
514                    let deleted_text = state.get_text_range(range.start, range.end);
515                    Event::Delete {
516                        range: range.clone(),
517                        deleted_text,
518                        cursor_id: primary_id,
519                    }
520                })
521                .collect();
522
523            // Apply events with atomic undo using bulk edit for O(n) performance
524            if events.len() > 1 {
525                // Use optimized bulk edit for multi-cursor cut
526                if let Some(bulk_edit) = self.apply_events_as_bulk_edit(events, "Cut".to_string()) {
527                    self.active_event_log_mut().append(bulk_edit);
528                }
529            } else if let Some(event) = events.into_iter().next() {
530                self.log_and_apply_event(&event);
531            }
532
533            if !deletions.is_empty() {
534                self.active_window_mut().status_message = Some(t!("clipboard.cut").to_string());
535            }
536        } else {
537            // No selection: delete entire line(s) for each cursor
538            let estimated_line_length = 80;
539
540            // Collect line ranges for each cursor
541            // IMPORTANT: Sort deletions by position to ensure we process from end to start
542            let positions: Vec<_> = self
543                .active_cursors()
544                .iter()
545                .map(|(_, c)| c.position)
546                .collect();
547            let mut deletions: Vec<_> = {
548                let state = self.active_state_mut();
549                positions
550                    .into_iter()
551                    .filter_map(|pos| {
552                        let mut iter = state.buffer.line_iterator(pos, estimated_line_length);
553                        let line_start = iter.current_position();
554                        iter.next_line().map(|(_start, content)| {
555                            let line_end = line_start + content.len();
556                            line_start..line_end
557                        })
558                    })
559                    .collect()
560            };
561            // Sort by start position so reverse iteration processes from end to start
562            deletions.sort_by_key(|r| r.start);
563
564            let primary_id = self.active_cursors().primary_id();
565            let state = self.active_state_mut();
566            let events: Vec<_> = deletions
567                .iter()
568                .rev()
569                .map(|range| {
570                    let deleted_text = state.get_text_range(range.start, range.end);
571                    Event::Delete {
572                        range: range.clone(),
573                        deleted_text,
574                        cursor_id: primary_id,
575                    }
576                })
577                .collect();
578
579            // Apply events with atomic undo using bulk edit for O(n) performance
580            if events.len() > 1 {
581                // Use optimized bulk edit for multi-cursor cut
582                if let Some(bulk_edit) =
583                    self.apply_events_as_bulk_edit(events, "Cut line".to_string())
584                {
585                    self.active_event_log_mut().append(bulk_edit);
586                }
587            } else if let Some(event) = events.into_iter().next() {
588                self.log_and_apply_event(&event);
589            }
590
591            if !deletions.is_empty() {
592                self.active_window_mut().status_message =
593                    Some(t!("clipboard.cut_line").to_string());
594            }
595        }
596    }
597
598    /// Paste the clipboard content at all cursor positions
599    ///
600    /// Handles:
601    /// - Single cursor paste
602    /// - Multi-cursor paste (pastes at each cursor)
603    /// - Selection replacement (deletes selection before inserting)
604    /// - Atomic undo (single undo step for entire operation)
605    pub fn paste(&mut self) {
606        // Defensive fast-paths. Prompt/terminal/file-explorer paste
607        // routes go through their own actions (PromptPaste,
608        // TerminalPaste, FileExplorerPaste); the buffer paste path
609        // below assumes there's a real buffer view in front of us. If
610        // we somehow landed here under one of those modes anyway,
611        // hand off to the synchronous service-level paste.
612        if self.active_window().prompt.is_some() || self.active_window().terminal_mode {
613            if let Some(text) = self.clipboard.paste() {
614                self.paste_text(text);
615            }
616            return;
617        }
618
619        // No bridge (early bootstrap / test harness): there is no
620        // event loop to deliver the async result through, so a
621        // background read would never come back. The no-bridge
622        // configuration also implies no display, so the synchronous
623        // arboard call won't actually block.
624        let sender = match self.async_bridge.as_ref() {
625            Some(bridge) => bridge.sender(),
626            None => {
627                if let Some(text) = self.clipboard.paste() {
628                    self.paste_text(text);
629                }
630                return;
631            }
632        };
633
634        // System clipboard disabled (internal-only test mode, or user
635        // opted out via config). Spinning up a thread for arboard is
636        // pointless when we already know we won't touch the OS.
637        if !self.clipboard.uses_system_clipboard() || self.clipboard.is_internal_only() {
638            if let Some(text) = self.clipboard.paste_internal() {
639                self.paste_text(text);
640            }
641            return;
642        }
643
644        // Bound concurrent pendings. A clipboard owner stuck for an
645        // unusual length of time, combined with Ctrl+V autorepeat,
646        // could otherwise grow the map without limit. The deadline
647        // keeps the count near zero in normal use.
648        if self.paste_pending.len() >= MAX_PENDING_PASTES {
649            tracing::warn!(
650                "MAX_PENDING_PASTES ({}) reached, ignoring Ctrl+V",
651                MAX_PENDING_PASTES
652            );
653            return;
654        }
655
656        let buffer_id = self.active_buffer();
657        let line_ending = self.active_state().buffer.line_ending();
658
659        // Kick the arboard read off on its own thread RIGHT AWAY,
660        // before touching the buffer. Two channels: a private
661        // `inline_tx` (bounded to 1) we race against a short timer
662        // for the fast path, and the editor's `AsyncBridge` for the
663        // slow path. The background thread tries `inline_tx` first
664        // and falls back to the bridge only if the inline receiver
665        // is gone (we dropped it after timing out).
666        //
667        // Each thread does its own `arboard::Clipboard::new().get_text()`,
668        // so back-to-back Ctrl+V with different OS-clipboard contents
669        // in between still picks each one up — the contents captured
670        // are whatever the OS clipboard held when this thread reached
671        // `get_text`.
672        let request_id = allocate_paste_request_id();
673        let dispatch_at = Instant::now();
674        let (inline_tx, inline_rx) = std::sync::mpsc::sync_channel::<Option<String>>(1);
675        let bridge_sender = sender.clone();
676        let thread_request_id = request_id;
677        std::thread::Builder::new()
678            .name("clipboard-paste".into())
679            .spawn(move || {
680                let arboard_start = Instant::now();
681                let text = arboard::Clipboard::new()
682                    .and_then(|mut cb| cb.get_text())
683                    .ok()
684                    .filter(|s| !s.is_empty());
685                let arboard_ms = arboard_start.elapsed().as_millis();
686                let len = text.as_ref().map(|s| s.len()).unwrap_or(0);
687                // Try the inline channel first. If the main thread
688                // is still inside its `recv_timeout`, the send
689                // succeeds and the fast path applies the paste. If
690                // the main thread already gave up and dropped
691                // `inline_rx`, fall through to the bridge for the
692                // async (placeholder) path.
693                match inline_tx.send(text.clone()) {
694                    Ok(()) => {
695                        tracing::info!(
696                            target: "paste_timing",
697                            "[req {}] arboard returned in {}ms ({} bytes), delivered via INLINE",
698                            thread_request_id, arboard_ms, len
699                        );
700                    }
701                    Err(_) => {
702                        tracing::info!(
703                            target: "paste_timing",
704                            "[req {}] arboard returned in {}ms ({} bytes), inline gone — sending via bridge",
705                            thread_request_id, arboard_ms, len
706                        );
707                        if let Err(e) = bridge_sender.send(AsyncMessage::ClipboardPasteResult {
708                            request_id: thread_request_id,
709                            text,
710                        }) {
711                            tracing::trace!("clipboard paste result delivery failed: {}", e);
712                        }
713                    }
714                }
715            })
716            .ok();
717
718        // Now race a short inline wait against the spawned read.
719        // Doing the selection-delete *after* this wait would be
720        // wrong: a fast inline paste needs the selection cleared
721        // first so it can replace it via `paste_text`'s normal
722        // logic. So delete the selection now (it's a synchronous
723        // local operation, ~µs) and only THEN race the wait.
724        let cursor_selections: Vec<(CursorId, std::ops::Range<usize>)> = self
725            .active_cursors()
726            .iter()
727            .filter_map(|(id, c)| c.selection_range().map(|r| (id, r)))
728            .collect();
729
730        if !cursor_selections.is_empty() {
731            let mut delete_events = Vec::with_capacity(cursor_selections.len());
732            for (cursor_id, range) in &cursor_selections {
733                let deleted_text = self
734                    .active_state_mut()
735                    .get_text_range(range.start, range.end);
736                delete_events.push(Event::Delete {
737                    range: range.clone(),
738                    deleted_text,
739                    cursor_id: *cursor_id,
740                });
741            }
742            delete_events.sort_by(|a, b| {
743                let pa = if let Event::Delete { range, .. } = a {
744                    range.start
745                } else {
746                    0
747                };
748                let pb = if let Event::Delete { range, .. } = b {
749                    range.start
750                } else {
751                    0
752                };
753                pb.cmp(&pa)
754            });
755            if let Err(e) = self.apply_events_to_buffer_as_bulk_edit(
756                buffer_id,
757                delete_events,
758                "Paste (clear selection)".to_string(),
759            ) {
760                tracing::warn!("paste selection delete failed: {}", e);
761                return;
762            }
763        }
764
765        // Inline wait: if arboard came back within budget, paste
766        // synchronously and skip the placeholder entirely — the
767        // user sees the paste appear in the same frame as the
768        // keystroke, indistinguishable from the old synchronous
769        // path. If the read is still in flight after the budget,
770        // drop `inline_rx` (which signals the thread to deliver via
771        // the bridge instead) and continue to the placeholder path.
772        match inline_rx.recv_timeout(PASTE_INLINE_WAIT) {
773            Ok(text) => {
774                tracing::info!(
775                    target: "paste_timing",
776                    "[req {}] fast path: inline result in {}ms, no placeholder needed",
777                    request_id,
778                    dispatch_at.elapsed().as_millis()
779                );
780                if let Some(t) = text {
781                    self.paste_text(t);
782                }
783                return;
784            }
785            Err(_) => {
786                tracing::info!(
787                    target: "paste_timing",
788                    "[req {}] inline wait timed out after {}ms — falling back to placeholder",
789                    request_id,
790                    dispatch_at.elapsed().as_millis()
791                );
792                // Dropping `inline_rx` here would race the thread
793                // (it might be mid-send). Keep it alive until after
794                // we've drained any last-second arrival.
795                if let Ok(text) = inline_rx.try_recv() {
796                    tracing::info!(
797                        target: "paste_timing",
798                        "[req {}] caught race — fast path after timeout",
799                        request_id
800                    );
801                    if let Some(t) = text {
802                        self.paste_text(t);
803                    }
804                    return;
805                }
806                drop(inline_rx);
807            }
808        }
809
810        // Slow path: plant placeholders and register the pending
811        // paste so the eventual bridge delivery lands at the anchor.
812        let mut positions: Vec<usize> = self
813            .active_cursors()
814            .iter()
815            .map(|(_, c)| c.position)
816            .collect();
817        positions.sort_unstable();
818        positions.dedup();
819        let cursor_count = positions.len();
820
821        if positions.is_empty() {
822            return;
823        }
824
825        let placeholder_style = Style::default().add_modifier(Modifier::DIM);
826        let anchors: Vec<PasteAnchor> = {
827            let Some(state) = self.buffers_mut().get_mut(&buffer_id) else {
828                return;
829            };
830            positions
831                .iter()
832                .map(|&pos| {
833                    let id = state.virtual_texts.add(
834                        &mut state.marker_list,
835                        pos,
836                        "▍".to_string(),
837                        placeholder_style,
838                        VirtualTextPosition::BeforeChar,
839                        -100,
840                    );
841                    PasteAnchor {
842                        virtual_text_id: id,
843                    }
844                })
845                .collect()
846        };
847
848        let deadline = Instant::now() + PASTE_ASYNC_DEADLINE;
849        tracing::info!(
850            target: "paste_timing",
851            "[req {}] slow path: placeholder planted, registering for async delivery",
852            request_id
853        );
854
855        self.paste_pending.insert(
856            request_id,
857            PendingPaste {
858                deadline,
859                buffer_id,
860                anchors,
861                cursor_count_at_dispatch: cursor_count,
862                line_ending,
863                dispatched_at: dispatch_at,
864            },
865        );
866
867        // Signal the input dispatcher to skip the immediate render
868        // for this keystroke, AND set a hard render-suppression
869        // deadline that the main loop checks. The placeholder is in
870        // the buffer; the next render that fires after the deadline
871        // (or after the paste resolves, whichever is first) will
872        // pick it up. For a common fast-ish clipboard the resolve
873        // beats the deadline by a wide margin and that single
874        // post-resolve render is the only frame the user sees —
875        // instead of paying for two full `terminal.draw` cycles.
876        // The suppression window is bounded by the paste deadline
877        // so a wedged clipboard can't permanently veto rendering.
878        self.paste_slow_path_just_armed = true;
879        self.paste_render_suppress_until = Some(deadline);
880    }
881
882    /// Consume the "paste just went async" flag set by the slow
883    /// placeholder path of `paste()`. Returns whether it was set
884    /// (so the caller can suppress the otherwise-automatic render).
885    pub(crate) fn take_paste_slow_path_armed(&mut self) -> bool {
886        std::mem::take(&mut self.paste_slow_path_just_armed)
887    }
888
889    /// True when the main loop should hold off on rendering a frame
890    /// because an async paste is in flight and its placeholder
891    /// shouldn't get its own (expensive) render before the paste
892    /// itself resolves. The suppression auto-expires at the paste
893    /// deadline so a hung clipboard can't permanently veto renders.
894    pub fn should_suppress_render(&self) -> bool {
895        match self.paste_render_suppress_until {
896            Some(until) => Instant::now() < until,
897            None => false,
898        }
899    }
900
901    /// Resolve an in-flight async paste keyed by `request_id`.
902    ///
903    /// - Drops the result if no entry matches: a deadline-fired
904    ///   timeout already cleaned up the anchors, or a different
905    ///   paste cycle is in flight.
906    /// - If `text` is `Some` and the target buffer still exists,
907    ///   inserts at every anchor's current position (column-mode
908    ///   distributed using the dispatch-time cursor count).
909    /// - Cleans up the placeholder virtual texts in all cases so the
910    ///   visible "▍" markers go away.
911    pub(crate) fn resolve_pending_paste(&mut self, request_id: u64, text: Option<String>) {
912        let Some(pending) = self.paste_pending.remove(&request_id) else {
913            tracing::info!(
914                target: "paste_timing",
915                "[req {}] resolve called but no matching entry (already cancelled/stale)",
916                request_id
917            );
918            return;
919        };
920        let total_ms = pending.dispatched_at.elapsed().as_millis();
921        let text_len = text.as_ref().map(|s| s.len()).unwrap_or(0);
922        tracing::info!(
923            target: "paste_timing",
924            "[req {}] resolving after {}ms ({} bytes from clipboard)",
925            request_id, total_ms, text_len
926        );
927
928        // Clear the render-suppression window if this was the last
929        // pending paste (so the about-to-be-applied insertion can
930        // render in this frame). If other pastes are still in flight
931        // the suppression stays so we keep batching their renders.
932        if self.paste_pending.is_empty() {
933            self.paste_render_suppress_until = None;
934        }
935
936        // Bail out if the buffer is gone (closed during the wait).
937        // The buffer's drop took its `virtual_texts` and `marker_list`
938        // with it, so the anchors are already cleaned up.
939        if self.buffers().get(&pending.buffer_id).is_none() {
940            tracing::debug!(
941                "paste request {} resolved against closed buffer {:?}, discarding",
942                request_id,
943                pending.buffer_id
944            );
945            return;
946        }
947
948        // Resolve each anchor's current position via the marker tree.
949        // Skip any anchor whose marker was deleted by an intervening
950        // edit (e.g. the user deleted through the placeholder).
951        let mut anchor_positions: Vec<(usize, usize)> = {
952            let state = self
953                .buffers()
954                .get(&pending.buffer_id)
955                .expect("checked above");
956            pending
957                .anchors
958                .iter()
959                .enumerate()
960                .filter_map(|(i, a)| {
961                    let mid = state.virtual_texts.marker_id_of(a.virtual_text_id)?;
962                    let pos = state.marker_list.get_position(mid)?;
963                    Some((i, pos))
964                })
965                .collect()
966        };
967
968        if let Some(raw_text) = text.filter(|s| !s.is_empty()) {
969            // Normalise to LF (mirrors `paste_text`) so column-mode
970            // line splitting is unambiguous, then convert back to the
971            // buffer's line ending captured at dispatch.
972            let normalized = raw_text.replace("\r\n", "\n").replace('\r', "\n");
973            let mut lines_for_distribution: Vec<&str> = normalized.split('\n').collect();
974            if lines_for_distribution.len() > 1 && lines_for_distribution.last() == Some(&"") {
975                lines_for_distribution.pop();
976            }
977            let use_column_paste = pending.cursor_count_at_dispatch > 1
978                && lines_for_distribution.len() > 1
979                && lines_for_distribution.len() == pending.cursor_count_at_dispatch
980                && anchor_positions.len() == pending.cursor_count_at_dispatch;
981
982            let paste_text_full = match pending.line_ending {
983                crate::model::buffer::LineEnding::LF => normalized.clone(),
984                crate::model::buffer::LineEnding::CRLF => normalized.replace('\n', "\r\n"),
985                crate::model::buffer::LineEnding::CR => normalized.replace('\n', "\r"),
986            };
987
988            // Sort anchors by position descending so each insertion
989            // doesn't shift subsequent ones forward. The original
990            // index is retained for column-mode line lookup.
991            anchor_positions.sort_by(|a, b| b.1.cmp(&a.1));
992
993            let total = pending.cursor_count_at_dispatch;
994            let mut events = Vec::with_capacity(anchor_positions.len());
995            for (original_index, pos) in &anchor_positions {
996                let text_for_anchor = if use_column_paste {
997                    // Topmost cursor (smallest position) gets the
998                    // first line — matches `paste_text`'s mapping so
999                    // a block-selected round-trip preserves shape.
1000                    lines_for_distribution[total - 1 - (total - 1 - *original_index)].to_string()
1001                } else {
1002                    paste_text_full.clone()
1003                };
1004                events.push(Event::Insert {
1005                    position: *pos,
1006                    text: text_for_anchor,
1007                    // No cursor moves on this insert: the user has
1008                    // been editing freely, and yanking their cursor
1009                    // to the paste site (which might be far away)
1010                    // would be the freeze bug in a different form.
1011                    cursor_id: CursorId::UNDO_SENTINEL,
1012                });
1013            }
1014
1015            if let Err(e) = self.apply_events_to_buffer_as_bulk_edit(
1016                pending.buffer_id,
1017                events,
1018                "Paste".to_string(),
1019            ) {
1020                tracing::warn!("paste insertion failed: {}", e);
1021            } else {
1022                self.set_status_message(t!("clipboard.pasted").to_string());
1023            }
1024        } else {
1025            // Deadline fired or read returned empty. Leave the buffer
1026            // untouched; cleanup of the placeholder markers below.
1027            tracing::debug!(
1028                "paste request {} resolved with no text — removing anchors",
1029                request_id
1030            );
1031        }
1032
1033        // Remove the placeholder virtual texts (and their markers).
1034        let Some(state) = self.buffers_mut().get_mut(&pending.buffer_id) else {
1035            return;
1036        };
1037        for anchor in pending.anchors {
1038            state
1039                .virtual_texts
1040                .remove(&mut state.marker_list, anchor.virtual_text_id);
1041        }
1042    }
1043
1044    /// Walk pending pastes, cancelling any whose deadline has passed.
1045    /// Returns true when at least one entry was cancelled (the caller
1046    /// should redraw to refresh the now-empty placeholder cells).
1047    pub(crate) fn check_paste_deadline(&mut self) -> bool {
1048        let now = Instant::now();
1049        let expired_ids: Vec<u64> = self
1050            .paste_pending
1051            .iter()
1052            .filter_map(|(id, pending)| (now >= pending.deadline).then_some(*id))
1053            .collect();
1054        if expired_ids.is_empty() {
1055            return false;
1056        }
1057        for id in expired_ids {
1058            tracing::debug!(
1059                "paste request {} hit {}ms deadline, cancelling",
1060                id,
1061                PASTE_ASYNC_DEADLINE.as_millis()
1062            );
1063            self.resolve_pending_paste(id, None);
1064        }
1065        true
1066    }
1067
1068    /// Earliest deadline across all in-flight pastes, used by the
1069    /// tick loop to know when to wake.
1070    ///
1071    /// Returns the SOONER of:
1072    ///  - the actual cancel deadline of the earliest pending paste
1073    ///    (`PASTE_ASYNC_DEADLINE` from dispatch), and
1074    ///  - a 1 ms drain hint, so the loop wakes ~1ms after the
1075    ///    background `clipboard-paste` thread sends its result on
1076    ///    the `AsyncBridge`. The bridge is an mpsc channel with no
1077    ///    wake mechanism, so the editor only sees the result when
1078    ///    `editor_tick` next runs — without the 1 ms hint the loop
1079    ///    could sleep for up to 50ms (idle poll) or 16ms (frame
1080    ///    budget) per iteration, and a slow render env (which gates
1081    ///    the next render on `FRAME_DURATION`) compounds that into
1082    ///    a several-hundred-millisecond perceived paste latency.
1083    ///
1084    /// CPU cost is bounded: the deadline cap of
1085    /// `PASTE_ASYNC_DEADLINE` (500 ms) means at most ~500 extra tick
1086    /// iterations per paste cycle. Each iteration is a `try_recv_all`
1087    /// on the bridge plus a few cheap checks; no rendering work
1088    /// happens unless something actually changed.
1089    pub(crate) fn next_paste_deadline(&self) -> Option<Instant> {
1090        let cancel_deadline = self.paste_pending.values().map(|p| p.deadline).min()?;
1091        let drain_hint = Instant::now() + Duration::from_millis(1);
1092        Some(cancel_deadline.min(drain_hint))
1093    }
1094
1095    /// Whether at least one async paste is in flight. Exposed mainly
1096    /// for tests and instrumentation; the input loop no longer keys
1097    /// off this — input is dispatched immediately and the anchor
1098    /// catches the eventual paste.
1099    pub fn is_paste_pending(&self) -> bool {
1100        !self.paste_pending.is_empty()
1101    }
1102
1103    /// Cancel any pending pastes whose anchors live in the given
1104    /// buffer. Called by the buffer-close path so we don't try to
1105    /// insert into a freed buffer when the result arrives. The
1106    /// buffer's `virtual_texts` and `marker_list` are about to be
1107    /// dropped along with the buffer, so we just forget the entries
1108    /// — no virtual-text removal needed.
1109    pub fn cancel_pending_pastes_for_buffer(&mut self, buffer_id: BufferId) {
1110        self.paste_pending
1111            .retain(|_, pending| pending.buffer_id != buffer_id);
1112        if self.paste_pending.is_empty() {
1113            self.paste_render_suppress_until = None;
1114        }
1115    }
1116
1117    /// Route a terminal-initiated bracketed paste to a focused
1118    /// floating panel (Orchestrator picker / New-Session form / plugin
1119    /// overlay) or focused dock when one owns the keyboard.
1120    ///
1121    /// Bracketed paste arrives as a single `Event::Paste` rather than
1122    /// per-key events, so — unlike typed characters and `Ctrl+V` — it
1123    /// never passes through `dispatch_floating_widget_key`. Without this
1124    /// routing it falls straight through to `paste_text`, which targets
1125    /// the buffer underneath the modal (the user-reported bug: pasting
1126    /// into the New-Session dialog dumped the text into the obscured
1127    /// file instead of the focused field).
1128    ///
1129    /// Returns `true` when a panel owns the keyboard (the paste was
1130    /// either inserted into its focused `Text` widget, or deliberately
1131    /// swallowed because focus isn't on a text field — a modal with no
1132    /// text input focused must ignore the paste, not leak it into the
1133    /// hidden buffer). Returns `false` when no panel owns the keyboard,
1134    /// so the caller falls back to the normal `paste_text` path.
1135    pub(crate) fn paste_bracketed_into_focused_panel(&mut self, text: &str) -> bool {
1136        // The Settings dialog is a capture-all modal overlay that owns the
1137        // keyboard above any panel. A bracketed paste must reach its focused
1138        // text input (or be swallowed when no field is focused) rather than
1139        // leaking into the buffer obscured behind it — the same class of bug
1140        // the floating-panel routing below fixes (issue #2268). Gate on
1141        // `visible`, not mere presence: `close_settings` only hides the
1142        // state (it isn't dropped), and a lingering hidden dialog must not
1143        // swallow pastes meant for the buffer.
1144        if self.settings_state.as_ref().is_some_and(|s| s.visible) {
1145            if let Some(settings) = self.settings_state.as_mut() {
1146                if settings.paste_into_focused_text(text) {
1147                    self.set_status_message(t!("clipboard.pasted").to_string());
1148                }
1149            }
1150            return true;
1151        }
1152
1153        // Mirror the keyboard-dispatch precedence in `handle_key`: a
1154        // focused centered modal wins over a focused dock.
1155        let slot = if self
1156            .floating_widget_panel
1157            .as_ref()
1158            .is_some_and(|f| f.focused)
1159        {
1160            super::PanelSlot::Floating
1161        } else if self.dock.as_ref().is_some_and(|d| d.focused) {
1162            super::PanelSlot::Dock
1163        } else {
1164            return false;
1165        };
1166        let Some(panel_id) = self.panel(slot).map(|f| f.panel_id) else {
1167            return false;
1168        };
1169        if self.panel_focused_widget_is_text(panel_id) {
1170            // Single-line `TextEdit` strips embedded newlines; multi-line
1171            // stores plain `\n`. Normalise CRLF / CR → LF first, matching
1172            // the `Action::Paste` widget-routing path.
1173            let normalized = text.replace("\r\n", "\n").replace('\r', "\n");
1174            self.handle_widget_insert_str(panel_id, &normalized);
1175            self.set_status_message(t!("clipboard.pasted").to_string());
1176        }
1177        true
1178    }
1179
1180    /// Paste text directly into the editor
1181    ///
1182    /// Handles:
1183    /// - Line ending normalization (CRLF/CR → buffer's format)
1184    /// - Single cursor paste
1185    /// - Multi-cursor paste (pastes at each cursor)
1186    /// - Column-mode paste: when the cursor count equals the number of
1187    ///   clipboard lines, each cursor receives a distinct line (matches
1188    ///   VSCode/Notepad++ behavior, see issue #1057). This makes a
1189    ///   block-selected copy/paste round-trip preserve its rectangular shape.
1190    /// - Selection replacement (deletes selection before inserting)
1191    /// - Atomic undo (single undo step for entire operation)
1192    /// - Routing to prompt if one is open
1193    pub fn paste_text(&mut self, paste_text: String) {
1194        if paste_text.is_empty() {
1195            return;
1196        }
1197
1198        // Normalize line endings: first convert all to LF, then to buffer's format
1199        // This handles Windows clipboard (CRLF), old Mac (CR), and Unix (LF)
1200        let normalized = paste_text.replace("\r\n", "\n").replace('\r', "\n");
1201
1202        // If a prompt is open, paste into the prompt (prompts use LF internally)
1203        if let Some(prompt) = self.active_window_mut().prompt.as_mut() {
1204            prompt.insert_str(&normalized);
1205            self.update_prompt_suggestions();
1206            self.active_window_mut().status_message = Some(t!("clipboard.pasted").to_string());
1207            return;
1208        }
1209
1210        // If in terminal mode, send paste to the terminal PTY
1211        if self.active_window().terminal_mode {
1212            self.active_window_mut()
1213                .send_terminal_input(normalized.as_bytes());
1214            return;
1215        }
1216
1217        // Collect cursor info sorted in reverse order by position
1218        let mut cursor_data: Vec<_> = self
1219            .active_cursors()
1220            .iter()
1221            .map(|(cursor_id, cursor)| {
1222                let selection = cursor.selection_range();
1223                let insert_position = selection
1224                    .as_ref()
1225                    .map(|r| r.start)
1226                    .unwrap_or(cursor.position);
1227                (cursor_id, selection, insert_position)
1228            })
1229            .collect();
1230        cursor_data.sort_by_key(|(_, _, pos)| std::cmp::Reverse(*pos));
1231
1232        // Decide whether to distribute one clipboard line per cursor
1233        // (column-mode paste). We split on LF (after normalization above) and
1234        // ignore a single trailing empty entry from a trailing newline so that
1235        // "a\nb\nc" and "a\nb\nc\n" both yield 3 lines.
1236        let mut lines_for_distribution: Vec<&str> = normalized.split('\n').collect();
1237        if lines_for_distribution.len() > 1 && lines_for_distribution.last() == Some(&"") {
1238            lines_for_distribution.pop();
1239        }
1240        let use_column_paste = cursor_data.len() > 1
1241            && lines_for_distribution.len() > 1
1242            && lines_for_distribution.len() == cursor_data.len();
1243
1244        // Convert to buffer's line ending format (only used in non-column mode;
1245        // a single column-paste line never contains an embedded newline).
1246        let paste_text_full = match self.active_state().buffer.line_ending() {
1247            crate::model::buffer::LineEnding::LF => normalized.clone(),
1248            crate::model::buffer::LineEnding::CRLF => normalized.replace('\n', "\r\n"),
1249            crate::model::buffer::LineEnding::CR => normalized.replace('\n', "\r"),
1250        };
1251
1252        // Get deleted text for each selection
1253        let cursor_data_with_text: Vec<_> = {
1254            let state = self.active_state_mut();
1255            cursor_data
1256                .into_iter()
1257                .map(|(cursor_id, selection, insert_position)| {
1258                    let deleted_text = selection
1259                        .as_ref()
1260                        .map(|r| state.get_text_range(r.start, r.end));
1261                    (cursor_id, selection, insert_position, deleted_text)
1262                })
1263                .collect()
1264        };
1265
1266        // Build events for each cursor.
1267        //
1268        // cursor_data_with_text is sorted by position DESCENDING (so events
1269        // applied in vector order don't invalidate earlier offsets). For column
1270        // paste we want the topmost cursor (smallest position) to receive the
1271        // first clipboard line, so we index into `lines_for_distribution` from
1272        // the back when iterating.
1273        let total = cursor_data_with_text.len();
1274        let mut events = Vec::new();
1275        for (i, (cursor_id, selection, insert_position, deleted_text)) in
1276            cursor_data_with_text.into_iter().enumerate()
1277        {
1278            if let (Some(range), Some(text)) = (selection, deleted_text) {
1279                events.push(Event::Delete {
1280                    range,
1281                    deleted_text: text,
1282                    cursor_id,
1283                });
1284            }
1285            let text = if use_column_paste {
1286                lines_for_distribution[total - 1 - i].to_string()
1287            } else {
1288                paste_text_full.clone()
1289            };
1290            events.push(Event::Insert {
1291                position: insert_position,
1292                text,
1293                cursor_id,
1294            });
1295        }
1296
1297        // Apply events with atomic undo using bulk edit for O(n) performance
1298        if events.len() > 1 {
1299            // Use optimized bulk edit for multi-cursor paste
1300            if let Some(bulk_edit) = self.apply_events_as_bulk_edit(events, "Paste".to_string()) {
1301                self.active_event_log_mut().append(bulk_edit);
1302            }
1303        } else if let Some(event) = events.into_iter().next() {
1304            self.log_and_apply_event(&event);
1305        }
1306
1307        self.active_window_mut().status_message = Some(t!("clipboard.pasted").to_string());
1308    }
1309
1310    /// Set clipboard content for testing purposes
1311    /// This sets the internal clipboard and enables internal-only mode to avoid
1312    /// system clipboard interference between parallel tests
1313    #[doc(hidden)]
1314    pub fn set_clipboard_for_test(&mut self, text: String) {
1315        self.clipboard.set_internal(text);
1316        self.clipboard.set_internal_only(true);
1317    }
1318
1319    /// Paste from internal clipboard only (for testing)
1320    /// This bypasses the system clipboard to avoid interference from CI environments
1321    #[doc(hidden)]
1322    pub fn paste_for_test(&mut self) {
1323        // Get content from internal clipboard only (ignores system clipboard)
1324        let paste_text = match self.clipboard.paste_internal() {
1325            Some(text) => text,
1326            None => return,
1327        };
1328
1329        // Use the same paste logic as the regular paste method
1330        self.paste_text(paste_text);
1331    }
1332
1333    /// Get clipboard content for testing purposes
1334    /// Returns the internal clipboard content
1335    #[doc(hidden)]
1336    pub fn clipboard_content_for_test(&self) -> String {
1337        self.clipboard.get_internal().to_string()
1338    }
1339
1340    /// Copy a buffer's file path to the clipboard.
1341    ///
1342    /// When `relative` is true the path is made relative to the workspace root;
1343    /// if the file lives outside the workspace the absolute path is used as a
1344    /// safe fallback (the user still gets a usable path rather than nothing).
1345    /// When `relative` is false the absolute path is always copied.
1346    ///
1347    /// If the buffer has no associated file (unsaved scratch buffer) or the
1348    /// buffer id is unknown, a status message is shown and the clipboard is
1349    /// left untouched.
1350    pub fn copy_buffer_path(&mut self, buffer_id: crate::model::event::BufferId, relative: bool) {
1351        let path = self
1352            .buffers()
1353            .get(&buffer_id)
1354            .and_then(|state| state.buffer.file_path().map(|p| p.to_path_buf()));
1355        let Some(path) = path else {
1356            self.active_window_mut().status_message =
1357                Some(t!("clipboard.no_file_path").to_string());
1358            return;
1359        };
1360
1361        let path_str = if relative {
1362            path.strip_prefix(self.working_dir())
1363                .unwrap_or(&path)
1364                .to_string_lossy()
1365                .into_owned()
1366        } else {
1367            path.to_string_lossy().into_owned()
1368        };
1369
1370        self.clipboard.copy(path_str.clone());
1371        self.active_window_mut().status_message =
1372            Some(t!("clipboard.copied_path", path = &path_str).to_string());
1373    }
1374
1375    /// Copy the active buffer's file path. See [`Self::copy_buffer_path`].
1376    pub fn copy_active_buffer_path(&mut self, relative: bool) {
1377        let buffer_id = self.active_buffer();
1378        self.copy_buffer_path(buffer_id, relative);
1379    }
1380
1381    /// Add a cursor at the next occurrence of the selected text
1382    /// If no selection, first selects the entire word at cursor position.
1383    ///
1384    /// When an active substring search has placed the cursor at a match
1385    /// (cursor inside `search_state.matches[i]..matches[i] + match_lengths[i]`),
1386    /// the search match is selected instead of the surrounding word.  This
1387    /// way subsequent presses look for the search substring rather than the
1388    /// whole word, which would skip other substring occurrences (issue #1697).
1389    pub fn add_cursor_at_next_match(&mut self) {
1390        if let Some(range) = self.active_window().search_match_at_primary_cursor() {
1391            let primary_id = self.active_cursors().primary_id();
1392            let primary = self.active_cursors().primary();
1393            let event = Event::MoveCursor {
1394                cursor_id: primary_id,
1395                old_position: primary.position,
1396                new_position: range.end,
1397                old_anchor: primary.anchor,
1398                new_anchor: Some(range.start),
1399                old_sticky_column: primary.sticky_column,
1400                new_sticky_column: 0,
1401            };
1402            self.active_event_log_mut().append(event.clone());
1403            self.apply_event_to_active_buffer(&event);
1404            return;
1405        }
1406
1407        let cursors = self.active_cursors().clone();
1408        let state = self.active_state_mut();
1409        match add_cursor_at_next_match(state, &cursors) {
1410            AddCursorResult::Success {
1411                cursor,
1412                total_cursors,
1413            } => {
1414                // Create AddCursor event with the next cursor ID
1415                let next_id = CursorId(self.active_cursors().count());
1416                let event = Event::AddCursor {
1417                    cursor_id: next_id,
1418                    position: cursor.position,
1419                    anchor: cursor.anchor,
1420                };
1421
1422                // Log and apply the event
1423                self.active_event_log_mut().append(event.clone());
1424                self.apply_event_to_active_buffer(&event);
1425
1426                self.active_window_mut().status_message =
1427                    Some(t!("clipboard.added_cursor_match", count = total_cursors).to_string());
1428            }
1429            AddCursorResult::WordSelected {
1430                word_start,
1431                word_end,
1432            } => {
1433                // Select the word by updating the primary cursor
1434                let primary_id = self.active_cursors().primary_id();
1435                let primary = self.active_cursors().primary();
1436                let event = Event::MoveCursor {
1437                    cursor_id: primary_id,
1438                    old_position: primary.position,
1439                    new_position: word_end,
1440                    old_anchor: primary.anchor,
1441                    new_anchor: Some(word_start),
1442                    old_sticky_column: primary.sticky_column,
1443                    new_sticky_column: 0,
1444                };
1445
1446                // Log and apply the event
1447                self.active_event_log_mut().append(event.clone());
1448                self.apply_event_to_active_buffer(&event);
1449            }
1450            AddCursorResult::Failed { message } => {
1451                self.active_window_mut().status_message = Some(message);
1452            }
1453        }
1454    }
1455
1456    /// Add a cursor above the primary cursor at the same column
1457    pub fn add_cursor_above(&mut self) {
1458        let cursors = self.active_cursors().clone();
1459        let state = self.active_state_mut();
1460        match add_cursor_above(state, &cursors) {
1461            AddCursorResult::Success {
1462                cursor,
1463                total_cursors,
1464            } => {
1465                // Create AddCursor event with the next cursor ID
1466                let next_id = CursorId(self.active_cursors().count());
1467                let event = Event::AddCursor {
1468                    cursor_id: next_id,
1469                    position: cursor.position,
1470                    anchor: cursor.anchor,
1471                };
1472
1473                // Log and apply the event
1474                self.active_event_log_mut().append(event.clone());
1475                self.apply_event_to_active_buffer(&event);
1476
1477                self.active_window_mut().status_message =
1478                    Some(t!("clipboard.added_cursor_above", count = total_cursors).to_string());
1479            }
1480            AddCursorResult::Failed { message } => {
1481                self.active_window_mut().status_message = Some(message);
1482            }
1483            AddCursorResult::WordSelected { .. } => unreachable!(),
1484        }
1485    }
1486
1487    /// Add a cursor below the primary cursor at the same column
1488    pub fn add_cursor_below(&mut self) {
1489        let cursors = self.active_cursors().clone();
1490        let state = self.active_state_mut();
1491        match add_cursor_below(state, &cursors) {
1492            AddCursorResult::Success {
1493                cursor,
1494                total_cursors,
1495            } => {
1496                // Create AddCursor event with the next cursor ID
1497                let next_id = CursorId(self.active_cursors().count());
1498                let event = Event::AddCursor {
1499                    cursor_id: next_id,
1500                    position: cursor.position,
1501                    anchor: cursor.anchor,
1502                };
1503
1504                // Log and apply the event
1505                self.active_event_log_mut().append(event.clone());
1506                self.apply_event_to_active_buffer(&event);
1507
1508                self.active_window_mut().status_message =
1509                    Some(t!("clipboard.added_cursor_below", count = total_cursors).to_string());
1510            }
1511            AddCursorResult::Failed { message } => {
1512                self.active_window_mut().status_message = Some(message);
1513            }
1514            AddCursorResult::WordSelected { .. } => unreachable!(),
1515        }
1516    }
1517
1518    /// Place a cursor at the end of every line covered by ANY existing
1519    /// cursor's selection (or each cursor's own line if it has no selection).
1520    /// Matches VSCode's "Add Cursor to Line Ends" / Sublime's "Split Selection
1521    /// into Lines": every existing cursor contributes, no cursor is silently
1522    /// dropped. Two cursors on the same line collapse to a single cursor.
1523    /// All selections are cleared.
1524    pub fn add_cursors_to_line_ends(&mut self) {
1525        let cursors = self.active_cursors().clone();
1526        let state = self.active_state_mut();
1527        let positions = line_end_positions_in_selection(state, &cursors);
1528
1529        if positions.is_empty() {
1530            self.active_window_mut().status_message =
1531                Some(t!("clipboard.added_cursors_to_line_ends_failed").to_string());
1532            return;
1533        }
1534
1535        // Sort the existing cursors in document order and map them index-wise
1536        // onto the new positions. This preserves cursor IDs where possible —
1537        // important for undo/redo — and minimises the move distance for each
1538        // surviving cursor.
1539        let mut existing: Vec<(CursorId, Cursor)> =
1540            cursors.iter().map(|(id, c)| (id, *c)).collect();
1541        existing.sort_by_key(|(_, c)| c.position);
1542
1543        let mut events: Vec<Event> = Vec::new();
1544        let reuse = existing.len().min(positions.len());
1545
1546        for i in 0..reuse {
1547            let (cursor_id, cur) = existing[i];
1548            let target = positions[i];
1549            events.push(Event::MoveCursor {
1550                cursor_id,
1551                old_position: cur.position,
1552                new_position: target,
1553                old_anchor: cur.anchor,
1554                new_anchor: None,
1555                old_sticky_column: cur.sticky_column,
1556                new_sticky_column: 0,
1557            });
1558        }
1559
1560        // If two cursors collapsed onto the same line, dedup left us with
1561        // fewer positions than cursors — drop the extras.
1562        for &(cursor_id, cur) in existing.iter().skip(reuse) {
1563            events.push(Event::RemoveCursor {
1564                cursor_id,
1565                position: cur.position,
1566                anchor: cur.anchor,
1567            });
1568        }
1569
1570        // Add fresh cursors for any extra line ends, with IDs strictly above
1571        // the highest existing one so we never collide with a cursor an undo
1572        // could re-insert later.
1573        let next_free_id = cursors
1574            .iter()
1575            .map(|(id, _)| id.0)
1576            .max()
1577            .map(|m| m + 1)
1578            .unwrap_or(0);
1579        for (i, &pos) in positions.iter().enumerate().skip(reuse) {
1580            let new_id = CursorId(next_free_id + i - reuse);
1581            events.push(Event::AddCursor {
1582                cursor_id: new_id,
1583                position: pos,
1584                anchor: None,
1585            });
1586        }
1587
1588        let total = positions.len();
1589        let batch = Event::Batch {
1590            events,
1591            description: "Add cursors to line ends".to_string(),
1592        };
1593        self.active_event_log_mut().append(batch.clone());
1594        self.apply_event_to_active_buffer(&batch);
1595
1596        self.active_window_mut().status_message =
1597            Some(t!("clipboard.added_cursors_to_line_ends", count = total).to_string());
1598    }
1599
1600    // =========================================================================
1601    // Vi-style yank operations (copy range without requiring selection)
1602    // =========================================================================
1603
1604    /// Yank (copy) from cursor to next word start
1605    pub fn yank_word_forward(&mut self) {
1606        let cursor_positions: Vec<_> = self
1607            .active_cursors()
1608            .iter()
1609            .map(|(_, c)| c.position)
1610            .collect();
1611        let ranges: Vec<_> = {
1612            let state = self.active_state();
1613            cursor_positions
1614                .into_iter()
1615                .filter_map(|start| {
1616                    let end = find_word_start_right(&state.buffer, start);
1617                    if end > start {
1618                        Some(start..end)
1619                    } else {
1620                        None
1621                    }
1622                })
1623                .collect()
1624        };
1625
1626        if ranges.is_empty() {
1627            return;
1628        }
1629
1630        // Copy text from all ranges
1631        let mut text = String::new();
1632        let state = self.active_state_mut();
1633        for range in ranges {
1634            if !text.is_empty() {
1635                text.push('\n');
1636            }
1637            let range_text = state.get_text_range(range.start, range.end);
1638            text.push_str(&range_text);
1639        }
1640
1641        if !text.is_empty() {
1642            let len = text.len();
1643            self.clipboard.copy(text);
1644            self.active_window_mut().status_message =
1645                Some(t!("clipboard.yanked", count = len).to_string());
1646        }
1647    }
1648
1649    /// Yank (copy) from cursor to vim word end (inclusive)
1650    pub fn yank_vi_word_end(&mut self) {
1651        let cursor_positions: Vec<_> = self
1652            .active_cursors()
1653            .iter()
1654            .map(|(_, c)| c.position)
1655            .collect();
1656        let ranges: Vec<_> = {
1657            let state = self.active_state();
1658            cursor_positions
1659                .into_iter()
1660                .filter_map(|start| {
1661                    let word_end = find_vi_word_end(&state.buffer, start);
1662                    let end = (word_end + 1).min(state.buffer.len());
1663                    if end > start {
1664                        Some(start..end)
1665                    } else {
1666                        None
1667                    }
1668                })
1669                .collect()
1670        };
1671
1672        if ranges.is_empty() {
1673            return;
1674        }
1675
1676        let mut text = String::new();
1677        let state = self.active_state_mut();
1678        for range in ranges {
1679            if !text.is_empty() {
1680                text.push('\n');
1681            }
1682            let range_text = state.get_text_range(range.start, range.end);
1683            text.push_str(&range_text);
1684        }
1685
1686        if !text.is_empty() {
1687            let len = text.len();
1688            self.clipboard.copy(text);
1689            self.active_window_mut().status_message =
1690                Some(t!("clipboard.yanked", count = len).to_string());
1691        }
1692    }
1693
1694    /// Yank (copy) from previous word start to cursor
1695    pub fn yank_word_backward(&mut self) {
1696        let cursor_positions: Vec<_> = self
1697            .active_cursors()
1698            .iter()
1699            .map(|(_, c)| c.position)
1700            .collect();
1701        let ranges: Vec<_> = {
1702            let state = self.active_state();
1703            cursor_positions
1704                .into_iter()
1705                .filter_map(|end| {
1706                    let start = find_word_start_left(&state.buffer, end);
1707                    if start < end {
1708                        Some(start..end)
1709                    } else {
1710                        None
1711                    }
1712                })
1713                .collect()
1714        };
1715
1716        if ranges.is_empty() {
1717            return;
1718        }
1719
1720        let mut text = String::new();
1721        let state = self.active_state_mut();
1722        for range in ranges {
1723            if !text.is_empty() {
1724                text.push('\n');
1725            }
1726            let range_text = state.get_text_range(range.start, range.end);
1727            text.push_str(&range_text);
1728        }
1729
1730        if !text.is_empty() {
1731            let len = text.len();
1732            self.clipboard.copy(text);
1733            self.active_window_mut().status_message =
1734                Some(t!("clipboard.yanked", count = len).to_string());
1735        }
1736    }
1737
1738    /// Yank (copy) from cursor to end of line
1739    pub fn yank_to_line_end(&mut self) {
1740        let estimated_line_length = 80;
1741
1742        // First collect cursor positions with immutable borrow
1743        let cursor_positions: Vec<_> = self
1744            .active_cursors()
1745            .iter()
1746            .map(|(_, cursor)| cursor.position)
1747            .collect();
1748
1749        // Now compute ranges with mutable borrow (line_iterator needs &mut self)
1750        let state = self.active_state_mut();
1751        let mut ranges = Vec::new();
1752        for pos in cursor_positions {
1753            let mut iter = state.buffer.line_iterator(pos, estimated_line_length);
1754            let line_start = iter.current_position();
1755            if let Some((_start, content)) = iter.next_line() {
1756                // Don't include the line ending in yank
1757                let content_len = content.trim_end_matches(&['\n', '\r'][..]).len();
1758                let line_end = line_start + content_len;
1759                if pos < line_end {
1760                    ranges.push(pos..line_end);
1761                }
1762            }
1763        }
1764
1765        if ranges.is_empty() {
1766            return;
1767        }
1768
1769        let mut text = String::new();
1770        for range in ranges {
1771            if !text.is_empty() {
1772                text.push('\n');
1773            }
1774            let range_text = state.get_text_range(range.start, range.end);
1775            text.push_str(&range_text);
1776        }
1777
1778        if !text.is_empty() {
1779            let len = text.len();
1780            self.clipboard.copy(text);
1781            self.active_window_mut().status_message =
1782                Some(t!("clipboard.yanked", count = len).to_string());
1783        }
1784    }
1785
1786    /// Yank (copy) from start of line to cursor
1787    pub fn yank_to_line_start(&mut self) {
1788        let estimated_line_length = 80;
1789
1790        // First collect cursor positions with immutable borrow
1791        let cursor_positions: Vec<_> = self
1792            .active_cursors()
1793            .iter()
1794            .map(|(_, cursor)| cursor.position)
1795            .collect();
1796
1797        // Now compute ranges with mutable borrow (line_iterator needs &mut self)
1798        let state = self.active_state_mut();
1799        let mut ranges = Vec::new();
1800        for pos in cursor_positions {
1801            let iter = state.buffer.line_iterator(pos, estimated_line_length);
1802            let line_start = iter.current_position();
1803            if pos > line_start {
1804                ranges.push(line_start..pos);
1805            }
1806        }
1807
1808        if ranges.is_empty() {
1809            return;
1810        }
1811
1812        let mut text = String::new();
1813        for range in ranges {
1814            if !text.is_empty() {
1815                text.push('\n');
1816            }
1817            let range_text = state.get_text_range(range.start, range.end);
1818            text.push_str(&range_text);
1819        }
1820
1821        if !text.is_empty() {
1822            let len = text.len();
1823            self.clipboard.copy(text);
1824            self.active_window_mut().status_message =
1825                Some(t!("clipboard.yanked", count = len).to_string());
1826        }
1827    }
1828}