Refactor scope commands (#10023)

This commit is contained in:
Jakub Žádník
2023-08-17 11:58:38 +03:00
committed by GitHub
parent 35f8d8548a
commit e88a51e930
14 changed files with 440 additions and 269 deletions

View File

@ -35,7 +35,7 @@ impl Command for ScopeAliases {
let ctrlc = engine_state.ctrlc.clone();
let mut scope_data = ScopeData::new(engine_state, stack);
scope_data.populate_all();
scope_data.populate_decls();
Ok(scope_data.collect_aliases(span).into_pipeline_data(ctrlc))
}

View File

@ -35,7 +35,7 @@ impl Command for ScopeCommands {
let ctrlc = engine_state.ctrlc.clone();
let mut scope_data = ScopeData::new(engine_state, stack);
scope_data.populate_all();
scope_data.populate_decls();
Ok(scope_data.collect_commands(span).into_pipeline_data(ctrlc))
}

View File

@ -31,8 +31,7 @@ impl Command for ScopeEngineStats {
) -> Result<PipelineData, ShellError> {
let span = call.head;
let mut scope_data = ScopeData::new(engine_state, stack);
scope_data.populate_all();
let scope_data = ScopeData::new(engine_state, stack);
Ok(scope_data.collect_engine_state(span).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 ScopeExterns;
impl Command for ScopeExterns {
fn name(&self) -> &str {
"scope externs"
}
fn signature(&self) -> Signature {
Signature::build("scope externs")
.input_output_types(vec![(Type::Nothing, Type::Any)])
.allow_variants_without_examples(true)
.category(Category::Filters)
}
fn usage(&self) -> &str {
"Output info on the known externals 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_decls();
Ok(scope_data.collect_externs(span).into_pipeline_data(ctrlc))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Show the known externals in the current scope",
example: "scope externs",
result: None,
}]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ScopeExterns {})
}
}

View File

@ -2,6 +2,7 @@ mod aliases;
mod command;
mod commands;
mod engine_stats;
mod externs;
mod modules;
mod variables;
@ -9,5 +10,6 @@ pub use aliases::*;
pub use command::*;
pub use commands::*;
pub use engine_stats::*;
pub use externs::*;
pub use modules::*;
pub use variables::*;

View File

@ -35,7 +35,7 @@ impl Command for ScopeVariables {
let ctrlc = engine_state.ctrlc.clone();
let mut scope_data = ScopeData::new(engine_state, stack);
scope_data.populate_all();
scope_data.populate_vars();
Ok(scope_data.collect_vars(span).into_pipeline_data(ctrlc))
}