fix: set repl_buffer_state to the REPL buffer after the `pre_execut… (#8560)

…ion` hook


# Description

Previously when a `executehostcommand` shortcut calls `commandline`, to
get the current command line, it's incorrectly set to the value of
`executehostcommand` `cmd`.

This fixes the regression (due to #8207), so it's correctly set to
what's in the REPL buffer.

# User-Facing Changes

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
Steven Xu 2023-03-22 22:43:25 +11:00 committed by GitHub
parent cb1eefd24a
commit b9858ea8f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -460,32 +460,29 @@ pub fn evaluate_repl(
.into_diagnostic()?; // todo: don't stop repl if error here?
}
// Right before we start running the code the user gave us,
// fire the "pre_execution" hook
// Right before we start running the code the user gave us, fire the `pre_execution`
// hook
if let Some(hook) = config.hooks.pre_execution.clone() {
// Set the REPL buffer to the current command for the "pre_execution" hook
let mut repl_buffer = engine_state
.repl_buffer_state
.lock()
.expect("repl buffer state mutex");
let next_repl_buffer = repl_buffer.to_string();
*repl_buffer = s.to_string();
drop(repl_buffer);
if let Err(err) = eval_hook(engine_state, stack, None, vec![], &hook) {
report_error_new(engine_state, &err);
}
// Restore the REPL buffer state for the next command. It could've been edited
// by `commandline`.
let mut repl_buffer = engine_state
.repl_buffer_state
.lock()
.expect("repl buffer state mutex");
*repl_buffer = next_repl_buffer;
drop(repl_buffer);
}
let mut repl_buffer = engine_state
.repl_buffer_state
.lock()
.expect("repl buffer state mutex");
*repl_buffer = line_editor.current_buffer_contents().to_string();
drop(repl_buffer);
if shell_integration {
run_ansi_sequence(PRE_EXECUTE_MARKER)?;
}