split $nu variable into scope commands and simpler $nu (#9487)

# Description

This splits off `scope` from `$nu`, creating a set of `scope` commands
for the various types of scope you might be interested in.

This also simplifies the `$nu` variable a bit.

# User-Facing Changes

This changes `$nu` to be a bit simpler and introduces a set of `scope`
subcommands.

# 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 -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **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:
JT
2023-06-21 09:33:01 +12:00
committed by GitHub
parent aaa21c1619
commit fbf3f7cf1c
21 changed files with 620 additions and 387 deletions

View File

@ -30,6 +30,7 @@ mod module;
mod mut_;
pub(crate) mod overlay;
mod return_;
mod scope;
mod try_;
mod use_;
mod version;
@ -67,6 +68,7 @@ pub use module::Module;
pub use mut_::Mut;
pub use overlay::*;
pub use return_::Return;
pub use scope::*;
pub use try_::Try;
pub use use_::Use;
pub use version::Version;

View File

@ -0,0 +1,62 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
};
#[derive(Clone)]
pub struct ScopeAliases;
impl Command for ScopeAliases {
fn name(&self) -> &str {
"scope aliases"
}
fn signature(&self) -> Signature {
Signature::build("scope aliases")
.input_output_types(vec![(Type::Nothing, Type::Any)])
.allow_variants_without_examples(true)
.category(Category::Filters)
}
fn usage(&self) -> &str {
"Output info on the aliases in the current scope."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let ctrlc = engine_state.ctrlc.clone();
let mut scope_data = ScopeData::new(engine_state, stack);
scope_data.populate_all();
Ok(scope_data.collect_aliases(span).into_pipeline_data(ctrlc))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Show the aliases in the current scope",
example: "scope aliases",
result: None,
}]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ScopeAliases {})
}
}

View File

@ -0,0 +1,50 @@
use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
#[derive(Clone)]
pub struct Scope;
impl Command for Scope {
fn name(&self) -> &str {
"scope"
}
fn signature(&self) -> Signature {
Signature::build("scope")
.category(Category::Core)
.input_output_types(vec![(Type::Nothing, Type::String)])
.allow_variants_without_examples(true)
}
fn usage(&self) -> &str {
"Commands for getting info about what is in scope."
}
fn is_parser_keyword(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(Value::String {
val: get_full_help(
&Scope.signature(),
&[],
engine_state,
stack,
self.is_parser_keyword(),
),
span: call.head,
}
.into_pipeline_data())
}
}

View File

@ -0,0 +1,62 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
};
#[derive(Clone)]
pub struct ScopeCommands;
impl Command for ScopeCommands {
fn name(&self) -> &str {
"scope commands"
}
fn signature(&self) -> Signature {
Signature::build("scope commands")
.input_output_types(vec![(Type::Nothing, Type::Any)])
.allow_variants_without_examples(true)
.category(Category::Filters)
}
fn usage(&self) -> &str {
"Output info on the commands in the current scope."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let ctrlc = engine_state.ctrlc.clone();
let mut scope_data = ScopeData::new(engine_state, stack);
scope_data.populate_all();
Ok(scope_data.collect_commands(span).into_pipeline_data(ctrlc))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Show the commands in the current scope",
example: "scope commands",
result: None,
}]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ScopeCommands {})
}
}

View File

@ -0,0 +1,59 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type};
#[derive(Clone)]
pub struct ScopeEngineStats;
impl Command for ScopeEngineStats {
fn name(&self) -> &str {
"scope engine-stats"
}
fn signature(&self) -> Signature {
Signature::build("scope engine-stats")
.input_output_types(vec![(Type::Nothing, Type::Any)])
.allow_variants_without_examples(true)
.category(Category::Filters)
}
fn usage(&self) -> &str {
"Output stats on the engine in the current state."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let mut scope_data = ScopeData::new(engine_state, stack);
scope_data.populate_all();
Ok(scope_data.collect_engine_state(span).into_pipeline_data())
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Show the stats on the current engine state",
example: "scope engine-stats",
result: None,
}]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ScopeEngineStats {})
}
}

View File

@ -0,0 +1,13 @@
mod aliases;
mod command;
mod commands;
mod engine_stats;
mod modules;
mod variables;
pub use aliases::*;
pub use command::*;
pub use commands::*;
pub use engine_stats::*;
pub use modules::*;
pub use variables::*;

View File

@ -0,0 +1,62 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
};
#[derive(Clone)]
pub struct ScopeModules;
impl Command for ScopeModules {
fn name(&self) -> &str {
"scope modules"
}
fn signature(&self) -> Signature {
Signature::build("scope modules")
.input_output_types(vec![(Type::Nothing, Type::Any)])
.allow_variants_without_examples(true)
.category(Category::Filters)
}
fn usage(&self) -> &str {
"Output info on the modules in the current scope."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let ctrlc = engine_state.ctrlc.clone();
let mut scope_data = ScopeData::new(engine_state, stack);
scope_data.populate_modules();
Ok(scope_data.collect_modules(span).into_pipeline_data(ctrlc))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Show the modules in the current scope",
example: "scope modules",
result: None,
}]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ScopeModules {})
}
}

View File

@ -0,0 +1,62 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
};
#[derive(Clone)]
pub struct ScopeVariables;
impl Command for ScopeVariables {
fn name(&self) -> &str {
"scope variables"
}
fn signature(&self) -> Signature {
Signature::build("scope variables")
.input_output_types(vec![(Type::Nothing, Type::Any)])
.allow_variants_without_examples(true)
.category(Category::Filters)
}
fn usage(&self) -> &str {
"Output info on the variables in the current scope."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let ctrlc = engine_state.ctrlc.clone();
let mut scope_data = ScopeData::new(engine_state, stack);
scope_data.populate_all();
Ok(scope_data.collect_vars(span).into_pipeline_data(ctrlc))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Show the variables in the current scope",
example: "scope variables",
result: None,
}]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ScopeVariables {})
}
}

View File

@ -52,6 +52,12 @@ pub fn create_default_context() -> EngineState {
Module,
Mut,
Return,
Scope,
ScopeAliases,
ScopeCommands,
ScopeEngineStats,
ScopeModules,
ScopeVariables,
Try,
Use,
Version,