Update config directly at assignment (#13332)

# Description

Allows `Stack` to have a modified local `Config`, which is updated
immediately when `$env.config` is assigned to. This means that even
within a script, commands that come after `$env.config` changes will
always see those changes in `Stack::get_config()`.

Also fixed a lot of cases where `engine_state.get_config()` was used
even when `Stack` was available.

Closes #13324.

# User-Facing Changes
- Config changes apply immediately after the assignment is executed,
rather than whenever config is read by a command that needs it.
- Potentially slower performance when executing a lot of lines that
change `$env.config` one after another. Recommended to get `$env.config`
into a `mut` variable first and do modifications, then assign it back.
- Much faster performance when executing a script that made
modifications to `$env.config`, as the changes are only parsed once.

# Tests + Formatting
All passing.

# After Submitting
- [ ] release notes
This commit is contained in:
Devyn Cairns
2024-07-11 06:09:33 -07:00
committed by GitHub
parent deaa711ca6
commit f65bc97a54
46 changed files with 327 additions and 222 deletions

View File

@@ -653,7 +653,7 @@ Operating system commands:
let list: bool = call.has_flag(engine_state, stack, "list")?;
let escape: bool = call.has_flag(engine_state, stack, "escape")?;
let osc: bool = call.has_flag(engine_state, stack, "osc")?;
let use_ansi_coloring = engine_state.get_config().use_ansi_coloring;
let use_ansi_coloring = stack.get_config(engine_state).use_ansi_coloring;
if list {
return Ok(generate_ansi_code_list(

View File

@@ -1,10 +1,12 @@
use std::sync::Arc;
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::command_prelude::*;
use nu_protocol::Config;
pub struct Arguments {
cell_paths: Option<Vec<CellPath>>,
config: Config,
config: Arc<Config>,
}
impl CmdArgument for Arguments {
@@ -51,11 +53,8 @@ impl Command for SubCommand {
) -> Result<PipelineData, ShellError> {
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
let config = engine_state.get_config();
let args = Arguments {
cell_paths,
config: config.clone(),
};
let config = stack.get_config(engine_state);
let args = Arguments { cell_paths, config };
operate(action, args, input, call.head, engine_state.signals())
}

View File

@@ -79,6 +79,7 @@ impl Command for InputList {
let fuzzy = call.has_flag(engine_state, stack, "fuzzy")?;
let index = call.has_flag(engine_state, stack, "index")?;
let display_path: Option<CellPath> = call.get_flag(engine_state, stack, "display")?;
let config = stack.get_config(engine_state);
let options: Vec<Options> = match input {
PipelineData::Value(Value::Range { .. }, ..)
@@ -89,9 +90,9 @@ impl Command for InputList {
let display_value = if let Some(ref cellpath) = display_path {
val.clone()
.follow_cell_path(&cellpath.members, false)?
.to_expanded_string(", ", engine_state.get_config())
.to_expanded_string(", ", &config)
} else {
val.to_expanded_string(", ", engine_state.get_config())
val.to_expanded_string(", ", &config)
};
Ok(Options {
name: display_value,