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,13 +1,12 @@
use nu_protocol::debugger::WithoutDebug;
use crate::eval_expression;
use nu_protocol::{
ast::Call,
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet},
eval_const::eval_constant,
FromValue, ShellError, Value,
};
use crate::eval_expression;
pub trait CallExt {
/// Check if a boolean flag is set (i.e. `--bool` or `--bool=true`)
fn has_flag(

View File

@ -0,0 +1,7 @@
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,
};

View File

@ -1,15 +1,13 @@
use nu_protocol::ast::{Argument, Expr, Expression, RecordItem};
use nu_protocol::debugger::WithoutDebug;
use crate::eval_call;
use nu_protocol::{
ast::Call,
ast::{Argument, Call, Expr, Expression, RecordItem},
debugger::WithoutDebug,
engine::{EngineState, Stack},
record, Category, Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Type,
Value,
};
use std::{collections::HashMap, fmt::Write};
use crate::eval_call;
pub fn get_full_help(
sig: &Signature,
examples: &[Example],

View File

@ -1,15 +1,16 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use nu_protocol::ast::{Call, Expr};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet, PWD_ENV};
use nu_protocol::{Config, PipelineData, ShellError, Span, Value, VarId};
use nu_path::canonicalize_with;
use nu_protocol::debugger::WithoutDebug;
use crate::eval_block;
use nu_path::canonicalize_with;
use nu_protocol::{
ast::{Call, Expr},
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet, PWD_ENV},
Config, PipelineData, ShellError, Span, Value, VarId,
};
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::Arc,
};
#[cfg(windows)]
const ENV_PATH_NAME: &str = "Path";

View File

@ -1,18 +1,17 @@
use std::{borrow::Cow, fs::OpenOptions, path::PathBuf};
use crate::{current_dir, current_dir_str, get_config, get_full_help};
use nu_path::expand_path_with;
use nu_protocol::debugger::DebugContext;
use nu_protocol::{
ast::{
Assignment, Block, Call, Expr, Expression, ExternalArgument, PathMember, PipelineElement,
PipelineRedirection, RedirectionSource, RedirectionTarget,
},
debugger::DebugContext,
engine::{Closure, EngineState, Redirection, Stack},
eval_base::Eval,
Config, FromValue, IntoPipelineData, IoStream, PipelineData, ShellError, Span, Spanned, Type,
Value, VarId, ENV_VARIABLE_ID,
};
use std::{borrow::Cow, fs::OpenOptions, path::PathBuf};
pub fn eval_call<D: DebugContext>(
engine_state: &EngineState,

View File

@ -2,10 +2,12 @@ use crate::{
eval_block, eval_block_with_early_return, eval_expression, eval_expression_with_input,
eval_subexpression,
};
use nu_protocol::ast::{Block, Expression};
use nu_protocol::debugger::{WithDebug, WithoutDebug};
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{PipelineData, ShellError, Value};
use nu_protocol::{
ast::{Block, Expression},
debugger::{WithDebug, WithoutDebug},
engine::{EngineState, Stack},
PipelineData, ShellError, Value,
};
/// Type of eval_block() function
pub type EvalBlockFn =

View File

@ -1,12 +1,11 @@
use nu_glob::MatchOptions;
use nu_path::{canonicalize_with, expand_path_with};
use nu_protocol::{NuGlob, ShellError, Span, Spanned};
use std::{
fs,
path::{Component, Path, PathBuf},
};
use nu_glob::MatchOptions;
use nu_path::{canonicalize_with, expand_path_with};
use nu_protocol::{NuGlob, ShellError, Span, Spanned};
const GLOB_CHARS: &[char] = &['*', '?', '['];
/// This function is like `nu_glob::glob` from the `glob` crate, except it is relative to a given cwd.

View File

@ -1,5 +1,6 @@
mod call_ext;
pub mod column;
pub mod command_prelude;
pub mod documentation;
pub mod env;
mod eval;

View File

@ -3,8 +3,7 @@ use nu_protocol::{
engine::{Command, EngineState, Stack, Visibility},
record, ModuleId, Signature, Span, SyntaxShape, Type, Value,
};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::{cmp::Ordering, collections::HashMap};
pub struct ScopeData<'e, 's> {
engine_state: &'e EngineState,