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,9 +1,4 @@
use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::{command_prelude::*, get_full_help};
#[derive(Clone)]
pub struct ConfigMeta;

View File

@ -1,12 +1,6 @@
use nu_engine::{env_to_strings, CallExt};
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use super::utils::gen_command;
use nu_cmd_base::util::get_editor;
use nu_engine::{command_prelude::*, env_to_strings};
#[derive(Clone)]
pub struct ConfigEnv;

View File

@ -1,12 +1,6 @@
use nu_engine::{env_to_strings, CallExt};
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use super::utils::gen_command;
use nu_cmd_base::util::get_editor;
use nu_engine::{command_prelude::*, env_to_strings};
#[derive(Clone)]
pub struct ConfigNu;

View File

@ -1,10 +1,6 @@
use chrono::Local;
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Type,
};
use nu_engine::command_prelude::*;
use nu_utils::{get_default_config, get_default_env};
use std::io::Write;

View File

@ -1,9 +1,6 @@
use std::collections::HashMap;
use std::path::PathBuf;
use nu_protocol::{IoStream, Span, Spanned};
use crate::ExternalCommand;
use nu_protocol::{IoStream, Span, Spanned};
use std::{collections::HashMap, path::PathBuf};
pub(crate) fn gen_command(
span: Span,

View File

@ -1,10 +1,5 @@
use nu_engine::{get_eval_block, redirect_env, CallExt};
use nu_protocol::{
ast::Call,
engine::{Closure, Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use nu_engine::{command_prelude::*, get_eval_block, redirect_env};
use nu_protocol::engine::Closure;
#[derive(Clone)]
pub struct ExportEnv;

View File

@ -1,9 +1,4 @@
use nu_engine::{current_dir, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::{command_prelude::*, current_dir};
#[derive(Clone)]
pub struct LoadEnv;

View File

@ -1,15 +1,8 @@
use std::path::PathBuf;
use nu_engine::{
find_in_dirs_env, get_dirs_var_from_call, get_eval_block_with_early_return, redirect_env,
CallExt,
};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block_with_early_return,
redirect_env,
};
use std::path::PathBuf;
/// Source a file for environment variables.
#[derive(Clone)]

View File

@ -1,13 +1,7 @@
use nu_engine::{command_prelude::*, eval_block};
use nu_protocol::{debugger::WithoutDebug, engine::Closure};
use std::collections::HashMap;
use nu_engine::{eval_block, CallExt};
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::{
ast::Call,
engine::{Closure, Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct WithEnv;