Add command_prelude module (#12291)

# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
    ShellError, Signature, Span, Type, Value,
};
```

This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
    ast::{Call, CellPath},
    engine::{Command, EngineState, Stack},
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
    PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```

This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
This commit is contained in:
Ian Manske
2024-03-26 21:17:30 +00:00
committed by GitHub
parent f8c1e3ac61
commit c747ec75c9
660 changed files with 1634 additions and 4332 deletions

View File

@ -1,12 +1,9 @@
use nu_engine::{get_full_help, CallExt};
use nu_parser::parse;
use nu_parser::{escape_for_script_arg, escape_quote_string};
use nu_protocol::report_error;
use nu_engine::{command_prelude::*, get_full_help};
use nu_parser::{escape_for_script_arg, escape_quote_string, parse};
use nu_protocol::{
ast::{Call, Expr, Expression},
engine::{Command, EngineState, Stack, StateWorkingSet},
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Spanned, SyntaxShape,
Value,
ast::{Expr, Expression},
engine::StateWorkingSet,
report_error,
};
use nu_utils::stdout_write_all_and_flush;

View File

@ -3,15 +3,18 @@ use log::info;
use nu_cli::read_plugin_file;
use nu_cli::{eval_config_contents, eval_source};
use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{report_error, Config};
use nu_protocol::{ParseError, PipelineData, Spanned};
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
report_error, Config, ParseError, PipelineData, Spanned,
};
use nu_utils::{get_default_config, get_default_env};
use std::fs::File;
use std::io::Write;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::path::Path;
use std::sync::Arc;
use std::{
fs::File,
io::Write,
panic::{catch_unwind, AssertUnwindSafe},
path::Path,
sync::Arc,
};
pub(crate) const NUSHELL_FOLDER: &str = "nushell";
const CONFIG_FILE: &str = "config.nu";

View File

@ -7,8 +7,7 @@ use crate::{
#[cfg(feature = "plugin")]
use nu_cli::read_plugin_file;
use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl};
use nu_protocol::eval_const::create_nu_constant;
use nu_protocol::{PipelineData, Span, NU_VARIABLE_ID};
use nu_protocol::{eval_const::create_nu_constant, PipelineData, Span, NU_VARIABLE_ID};
use nu_utils::utils::perf;
pub(crate) fn run_commands(

View File

@ -1,10 +1,9 @@
use nu_protocol::engine::EngineState;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use nu_protocol::engine::EngineState;
pub(crate) fn ctrlc_protection(engine_state: &mut EngineState, ctrlc: &Arc<AtomicBool>) {
let handler_ctrlc = ctrlc.clone();
let engine_state_ctrlc = ctrlc.clone();

View File

@ -1,9 +1,12 @@
use nu_cmd_base::hook::{eval_env_change_hook, eval_hook};
use nu_engine::eval_block;
use nu_parser::parse;
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{cli_error::CliError, PipelineData, Value};
use nu_protocol::{
cli_error::CliError,
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet},
PipelineData, Value,
};
use nu_std::load_standard_library;
use std::io::{self, BufRead, Read, Write};