Skip to content

Commit

Permalink
split $nu variable into scope commands and simpler $nu (#9487)
Browse files Browse the repository at this point in the history
# 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.
-->
  • Loading branch information
sophiajt committed Jun 20, 2023
1 parent aaa21c1 commit fbf3f7c
Show file tree
Hide file tree
Showing 21 changed files with 620 additions and 387 deletions.
84 changes: 1 addition & 83 deletions crates/nu-cli/tests/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ fn variables_completions() {
"loginshell-path".into(),
"os-info".into(),
"pid".into(),
"scope".into(),
"plugin-path".into(),
"startup-time".into(),
"temp-path".into(),
];
Expand Down Expand Up @@ -568,88 +568,6 @@ fn variables_completions() {
// Match results
match_suggestions(expected, suggestions);

// Test completions for $nu.scope
let suggestions = completer.complete("$nu.scope.", 10);
assert_eq!(5, suggestions.len());
let expected: Vec<String> = vec![
"aliases".into(),
"commands".into(),
"engine_state".into(),
"modules".into(),
"vars".into(),
];
// Match results
match_suggestions(expected, suggestions);

// Test completions for $nu.scope.commands
let suggestions = completer.complete("$nu.scope.commands.", 19);
assert_eq!(15, suggestions.len());
let expected: Vec<String> = vec![
"category".into(),
"creates_scope".into(),
"examples".into(),
"extra_usage".into(),
"is_builtin".into(),
"is_custom".into(),
"is_extern".into(),
"is_keyword".into(),
"is_plugin".into(),
"is_sub".into(),
"module_name".into(),
"name".into(),
"search_terms".into(),
"signatures".into(),
"usage".into(),
];
// Match results
match_suggestions(expected, suggestions);

// Test completions for $nu.scope.commands.signatures
let suggestions = completer.complete("$nu.scope.commands.signatures.", 30);
assert_eq!(17, suggestions.len());
let expected: Vec<String> = vec![
"any".into(),
"binary".into(),
"bool".into(),
"datetime".into(),
"duration".into(),
"filesize".into(),
"int".into(),
"list<any>".into(),
"list<binary>".into(),
"list<number>".into(),
"list<string>".into(),
"nothing".into(),
"number".into(),
"range".into(),
"record".into(),
"string".into(),
"table".into(),
];
// Match results
match_suggestions(expected, suggestions);

// Test completions for $nu.scope.engine_state
let suggestions = completer.complete("$nu.scope.engine_state.", 23);
assert_eq!(6, suggestions.len());
let expected: Vec<String> = vec![
"num_blocks".into(),
"num_decls".into(),
"num_env_vars".into(),
"num_modules".into(),
"num_vars".into(),
"source_bytes".into(),
];
// Match results
match_suggestions(expected, suggestions);

// Test completions for $nu.scope.vars
let suggestions = completer.complete("$nu.scope.vars.", 15);
assert_eq!(3, suggestions.len());
let expected: Vec<String> = vec!["name".into(), "type".into(), "value".into()];
// Match results
match_suggestions(expected, suggestions);

// Test completions for custom var
let suggestions = completer.complete("$actor.", 7);

Expand Down
2 changes: 2 additions & 0 deletions crates/nu-cmd-lang/src/core_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod module;
mod mut_;
pub(crate) mod overlay;
mod return_;
mod scope;
mod try_;
mod use_;
mod version;
Expand Down Expand Up @@ -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;
Expand Down
62 changes: 62 additions & 0 deletions crates/nu-cmd-lang/src/core_commands/scope/aliases.rs
Original file line number Diff line number Diff line change
@@ -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 {})
}
}
50 changes: 50 additions & 0 deletions crates/nu-cmd-lang/src/core_commands/scope/command.rs
Original file line number Diff line number Diff line change
@@ -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())
}
}
62 changes: 62 additions & 0 deletions crates/nu-cmd-lang/src/core_commands/scope/commands.rs
Original file line number Diff line number Diff line change
@@ -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 {})
}
}
59 changes: 59 additions & 0 deletions crates/nu-cmd-lang/src/core_commands/scope/engine_stats.rs
Original file line number Diff line number Diff line change
@@ -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 {})
}
}
13 changes: 13 additions & 0 deletions crates/nu-cmd-lang/src/core_commands/scope/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;

0 comments on commit fbf3f7c

Please sign in to comment.