magi-code 0.96.1

Repository-aware CLI coding agent for terminal work
Documentation
use super::*;

fn replace_prompt_burst_insert(display: &mut input::DisplayCommand, text: &str) -> bool {
    match display {
        input::DisplayCommand::Batch(commands) => commands
            .iter_mut()
            .any(|command| replace_prompt_burst_insert(command, text)),
        input::DisplayCommand::Prompt(input::PromptCommand::Edit {
            operation: input::PromptEditCommand::InsertChar(_),
            viewport,
            refresh_autocomplete,
        }) => {
            *display = input::DisplayCommand::Prompt(input::PromptCommand::InsertText {
                text: text.to_string(),
                viewport: *viewport,
                refresh_autocomplete: *refresh_autocomplete,
            });
            true
        }
        _ => false,
    }
}

pub(super) fn classify_prompt_key_burst_event(
    event: TerminalInputEvent,
    state: &state::MissionControlState,
    terminal_area: ratatui::layout::Rect,
    autocomplete_candidates: &[state::AutocompleteCandidate],
) -> Option<input::InputCommand> {
    match event {
        TerminalInputEvent::KeyBurst(text) => {
            if let Some(first) = text
                .chars()
                .next()
                .filter(|ch| input::char_autofocuses_prompt(*ch))
            {
                // Let ordinary typing decide focus and modal ownership, but insert the
                // entire burst together so no characters or undo grouping are lost.
                let command = input::classify_key_with_all_viewports(
                    crossterm::event::KeyEvent::new(
                        crossterm::event::KeyCode::Char(first),
                        crossterm::event::KeyModifiers::NONE,
                    ),
                    state,
                    viewports::key_viewports(terminal_area, state),
                    autocomplete_candidates,
                );
                let (action, display) = command.into_parts();
                if let Some(mut display) = display
                    && replace_prompt_burst_insert(&mut display, &text)
                {
                    return Some(input::InputCommand::action_with_display(action, display));
                }
            }
            if !input::input_accepts_paste(state) {
                return None;
            }
            Some(input::classify_paste_with_all_viewports(
                &text,
                state,
                viewports::paste_viewports(terminal_area, state),
                autocomplete_candidates,
            ))
        }
        TerminalInputEvent::KeyBurstTooLarge => {
            let command = input::classify_oversized_paste(state);
            (!matches!(command, input::InputCommand::None)).then_some(command)
        }
        TerminalInputEvent::Event(_) => None,
    }
}