Repository: nushell/nushell
File: crates/nu-cli/src/repl.rs (Rust)

Code before:
            run_ansi_sequence(&format!(
                "{VSCODE_COMMANDLINE_MARKER_PREFIX}{replaced_cmd_text}{VSCODE_COMMANDLINE_MARKER_SUFFIX}"
            ));
        }
    }
}

fn run_shell_integration_reset_application_mode() {
    run_ansi_sequence(RESET_APPLICATION_MODE);
}

///
/// Clear the screen and output anything remaining in the EngineState buffer.
///
fn flush_engine_state_repl_buffer(engine_state: &mut EngineState, line_editor: &mut Reedline) {
    let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
    line_editor.run_edit_commands(&[
        EditCommand::Clear,
        EditCommand::InsertString(repl.buffer.to_string()),
        EditCommand::MoveToPosition {
            position: repl.cursor_pos,
            select: false,
        },
    ]);
    engine_state.immediately_accept = repl.accept;
    repl.accept = false;
    repl.buffer = "".to_string();
    repl.cursor_pos = 0;
}

///
/// Setup history management for Reedline
///
fn setup_history(
    engine_state: &mut EngineState,
    line_editor: Reedline,
    history: HistoryConfig,
) -> Result<Reedline> {
    // Setup history_isolation aka "history per session"
    let history_session_id = if history.isolation {
        Reedline::create_history_session_id()
    } else {
        None
    };

    if let Some(path) = history.file_path() {
        return update_line_editor_history(
            engine_state,
            path,
            history,
            line_editor,

Review comment (bug):
If `flush_engine_state_repl_buffer` is the only place we alter the buffer, I think it would make sense to place the `.with_immediately_accept` there as well, reading directly from `repl_state.accept`. Then it would be guaranteed that any alterations being observed through the whole chain of hooks etc. would be consistent, and no bugs could be introduced by someone in the future unintentionally modifying `engine_state.immediately_accept`

Code after:
fn run_shell_integration_reset_application_mode() {
    run_ansi_sequence(RESET_APPLICATION_MODE);
}

///
/// Clear the screen and output anything remaining in the EngineState buffer.
///
fn flush_engine_state_repl_buffer(
    engine_state: &mut EngineState,
    mut line_editor: Reedline,
) -> Reedline {
    let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
    line_editor.run_edit_commands(&[
        EditCommand::Clear,
        EditCommand::InsertString(repl.buffer.to_string()),
        EditCommand::MoveToPosition {
            position: repl.cursor_pos,
            select: false,
        },
    ]);
    if repl.accept {
        line_editor = line_editor.with_immediately_accept(true)
    }
    repl.accept = false;
    repl.buffer = "".to_string();
    repl.cursor_pos = 0;
    line_editor
}

///
/// Setup history management for Reedline
///
fn setup_history(
    engine_state: &mut EngineState,
    line_editor: Reedline,
    history: HistoryConfig,
) -> Result<Reedline> {
    // Setup history_isolation aka "history per session"
    let history_session_id = if history.isolation {
        Reedline::create_history_session_id()
    } else {
        None
    };

    if let Some(path) = history.file_path() {
        return update_line_editor_history(
            engine_state,
            path,
            history,
            line_editor,
            history_session_id,