diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index a2b26a8ae..ddea60394 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -1,6 +1,6 @@ use crate::commands::classified::block::run_block; use crate::commands::classified::maybe_text_codec::{MaybeTextCodec, StringOrBinary}; -use crate::context::Context; +use crate::evaluation_context::EvaluationContext; use crate::path::canonicalize; use crate::prelude::*; #[cfg(feature = "rustyline-support")] @@ -56,8 +56,8 @@ pub fn search_paths() -> Vec { search_paths } -pub fn create_default_context(interactive: bool) -> Result> { - let mut context = Context::basic()?; +pub fn create_default_context(interactive: bool) -> Result> { + let mut context = EvaluationContext::basic()?; { use crate::commands::*; @@ -325,7 +325,7 @@ fn convert_rustyline_result_to_string(input: Result) -> L /// The entry point for the CLI. Will register all known internal commands, load experimental commands, load plugins, then prepare the prompt and line reader for input. #[cfg(feature = "rustyline-support")] -pub async fn cli(mut context: Context) -> Result<(), Box> { +pub async fn cli(mut context: EvaluationContext) -> Result<(), Box> { let mut syncer = EnvironmentSyncer::new(); let configuration = syncer.get_config(); @@ -538,7 +538,7 @@ pub async fn cli(mut context: Context) -> Result<(), Box> { Ok(()) } -pub fn register_plugins(context: &mut Context) -> Result<(), ShellError> { +pub fn register_plugins(context: &mut EvaluationContext) -> Result<(), ShellError> { if let Ok(plugins) = crate::plugin::scan(search_paths()) { context.add_commands( plugins @@ -551,7 +551,7 @@ pub fn register_plugins(context: &mut Context) -> Result<(), ShellError> { Ok(()) } -fn configure_ctrl_c(_context: &mut Context) -> Result<(), Box> { +fn configure_ctrl_c(_context: &mut EvaluationContext) -> Result<(), Box> { #[cfg(feature = "ctrlc")] { let cc = _context.ctrl_c.clone(); @@ -569,7 +569,7 @@ fn configure_ctrl_c(_context: &mut Context) -> Result<(), Box> { } async fn run_startup_commands( - context: &mut Context, + context: &mut EvaluationContext, config: &dyn nu_data::config::Conf, ) -> Result<(), ShellError> { if let Some(commands) = config.var("startup") { @@ -599,7 +599,7 @@ async fn run_startup_commands( pub async fn run_pipeline_standalone( pipeline: String, redirect_stdin: bool, - context: &mut Context, + context: &mut EvaluationContext, exit_on_error: bool, ) -> Result<(), Box> { let line = process_line(&pipeline, context, redirect_stdin, false).await; @@ -798,7 +798,7 @@ fn configure_rustyline_editor( #[cfg(feature = "rustyline-support")] fn nu_line_editor_helper( - context: &mut Context, + context: &mut EvaluationContext, config: &dyn nu_data::config::Conf, ) -> crate::shell::Helper { let hinter = rustyline_hinter(config); @@ -834,7 +834,7 @@ pub enum LineResult { Break, } -pub async fn parse_and_eval(line: &str, ctx: &mut Context) -> Result { +pub async fn parse_and_eval(line: &str, ctx: &mut EvaluationContext) -> Result { let line = if line.ends_with('\n') { &line[..line.len() - 1] } else { @@ -867,7 +867,7 @@ pub async fn parse_and_eval(line: &str, ctx: &mut Context) -> Result LineResult { @@ -1085,7 +1085,7 @@ mod tests { #[quickcheck] fn quickcheck_parse(data: String) -> bool { if let Ok(lite_block) = nu_parser::lite_parse(&data, 0) { - let context = crate::context::Context::basic().unwrap(); + let context = crate::evaluation_context::EvaluationContext::basic().unwrap(); let _ = nu_parser::classify_block(&lite_block, context.registry()); } true diff --git a/crates/nu-cli/src/command_registry.rs b/crates/nu-cli/src/command_registry.rs new file mode 100644 index 000000000..d9d01d415 --- /dev/null +++ b/crates/nu-cli/src/command_registry.rs @@ -0,0 +1,64 @@ +use crate::commands::Command; +use indexmap::IndexMap; +use nu_errors::ShellError; +use nu_parser::SignatureRegistry; +use nu_protocol::Signature; +use parking_lot::Mutex; +use std::sync::Arc; + +#[derive(Debug, Clone, Default)] +pub struct CommandRegistry { + registry: Arc>>, +} + +impl SignatureRegistry for CommandRegistry { + fn has(&self, name: &str) -> bool { + let registry = self.registry.lock(); + registry.contains_key(name) + } + fn get(&self, name: &str) -> Option { + let registry = self.registry.lock(); + registry.get(name).map(|command| command.signature()) + } + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } +} + +impl CommandRegistry { + pub fn new() -> CommandRegistry { + CommandRegistry { + registry: Arc::new(Mutex::new(IndexMap::default())), + } + } +} + +impl CommandRegistry { + pub fn get_command(&self, name: &str) -> Option { + let registry = self.registry.lock(); + + registry.get(name).cloned() + } + + pub fn expect_command(&self, name: &str) -> Result { + self.get_command(name).ok_or_else(|| { + ShellError::untagged_runtime_error(format!("Could not load command: {}", name)) + }) + } + + pub fn has(&self, name: &str) -> bool { + let registry = self.registry.lock(); + + registry.contains_key(name) + } + + pub fn insert(&mut self, name: impl Into, command: Command) { + let mut registry = self.registry.lock(); + registry.insert(name.into(), command); + } + + pub fn names(&self) -> Vec { + let registry = self.registry.lock(); + registry.keys().cloned().collect() + } +} diff --git a/crates/nu-cli/src/commands/alias.rs b/crates/nu-cli/src/commands/alias.rs index 34e32c956..6735bed49 100644 --- a/crates/nu-cli/src/commands/alias.rs +++ b/crates/nu-cli/src/commands/alias.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_data::config; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/commands/append.rs b/crates/nu-cli/src/commands/append.rs index e78a0de49..a73ef487a 100644 --- a/crates/nu-cli/src/commands/append.rs +++ b/crates/nu-cli/src/commands/append.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/benchmark.rs b/crates/nu-cli/src/commands/benchmark.rs index 1a6c40886..6a71120b9 100644 --- a/crates/nu-cli/src/commands/benchmark.rs +++ b/crates/nu-cli/src/commands/benchmark.rs @@ -58,7 +58,7 @@ async fn benchmark( let registry = registry.clone(); let tag = raw_args.call_info.args.span; - let mut context = Context::from_raw(&raw_args, ®istry); + let mut context = EvaluationContext::from_raw(&raw_args, ®istry); let scope = raw_args.call_info.scope.clone(); let (BenchmarkArgs { block }, input) = raw_args.process(®istry).await?; @@ -114,7 +114,7 @@ async fn benchmark( let registry = registry.clone(); let tag = raw_args.call_info.args.span; - let mut context = Context::from_raw(&raw_args, ®istry); + let mut context = EvaluationContext::from_raw(&raw_args, ®istry); let scope = raw_args.call_info.scope.clone(); let (BenchmarkArgs { block }, input) = raw_args.process(®istry).await?; diff --git a/crates/nu-cli/src/commands/classified/block.rs b/crates/nu-cli/src/commands/classified/block.rs index 999b7fdfc..be0722a67 100644 --- a/crates/nu-cli/src/commands/classified/block.rs +++ b/crates/nu-cli/src/commands/classified/block.rs @@ -1,6 +1,6 @@ use crate::commands::classified::expr::run_expression_block; use crate::commands::classified::internal::run_internal_command; -use crate::context::Context; +use crate::evaluation_context::EvaluationContext; use crate::prelude::*; use crate::stream::InputStream; use futures::stream::TryStreamExt; @@ -11,7 +11,7 @@ use std::sync::atomic::Ordering; pub(crate) async fn run_block( block: &Block, - ctx: &mut Context, + ctx: &mut EvaluationContext, mut input: InputStream, it: &Value, vars: &IndexMap, @@ -64,7 +64,7 @@ pub(crate) async fn run_block( async fn run_pipeline( commands: &Commands, - ctx: &mut Context, + ctx: &mut EvaluationContext, mut input: InputStream, it: &Value, vars: &IndexMap, diff --git a/crates/nu-cli/src/commands/classified/expr.rs b/crates/nu-cli/src/commands/classified/expr.rs index 3c8f5b3d7..bdeaa211d 100644 --- a/crates/nu-cli/src/commands/classified/expr.rs +++ b/crates/nu-cli/src/commands/classified/expr.rs @@ -10,7 +10,7 @@ use nu_protocol::Value; pub(crate) async fn run_expression_block( expr: SpannedExpression, - context: &mut Context, + context: &mut EvaluationContext, it: &Value, vars: &IndexMap, env: &IndexMap, diff --git a/crates/nu-cli/src/commands/classified/external.rs b/crates/nu-cli/src/commands/classified/external.rs index f7bc01971..6d7e2c876 100644 --- a/crates/nu-cli/src/commands/classified/external.rs +++ b/crates/nu-cli/src/commands/classified/external.rs @@ -19,7 +19,7 @@ use nu_source::Tag; pub(crate) async fn run_external_command( command: ExternalCommand, - context: &mut Context, + context: &mut EvaluationContext, input: InputStream, scope: &Scope, external_redirection: ExternalRedirection, @@ -39,7 +39,7 @@ pub(crate) async fn run_external_command( async fn run_with_stdin( command: ExternalCommand, - context: &mut Context, + context: &mut EvaluationContext, input: InputStream, scope: &Scope, external_redirection: ExternalRedirection, @@ -543,7 +543,7 @@ mod tests { add_quotes, argument_contains_whitespace, argument_is_quoted, expand_tilde, remove_quotes, }; #[cfg(feature = "which")] - use super::{run_external_command, Context, InputStream}; + use super::{run_external_command, EvaluationContext, InputStream}; #[cfg(feature = "which")] use futures::executor::block_on; @@ -573,7 +573,8 @@ mod tests { let cmd = ExternalBuilder::for_name("i_dont_exist.exe").build(); let input = InputStream::empty(); - let mut ctx = Context::basic().expect("There was a problem creating a basic context."); + let mut ctx = + EvaluationContext::basic().expect("There was a problem creating a basic context."); assert!(run_external_command( cmd, @@ -591,7 +592,7 @@ mod tests { // async fn failure_run() -> Result<(), ShellError> { // let cmd = ExternalBuilder::for_name("fail").build(); - // let mut ctx = Context::basic().expect("There was a problem creating a basic context."); + // let mut ctx = EvaluationContext::basic().expect("There was a problem creating a basic context."); // let stream = run_external_command(cmd, &mut ctx, None, false) // .await? // .expect("There was a problem running the external command."); diff --git a/crates/nu-cli/src/commands/classified/internal.rs b/crates/nu-cli/src/commands/classified/internal.rs index 5bce7fdb0..014525636 100644 --- a/crates/nu-cli/src/commands/classified/internal.rs +++ b/crates/nu-cli/src/commands/classified/internal.rs @@ -9,7 +9,7 @@ use nu_protocol::{CommandAction, Primitive, ReturnSuccess, Scope, UntaggedValue, pub(crate) async fn run_internal_command( command: InternalCommand, - context: &mut Context, + context: &mut EvaluationContext, input: InputStream, it: &Value, vars: &IndexMap, diff --git a/crates/nu-cli/src/commands/clip.rs b/crates/nu-cli/src/commands/clip.rs index 3b0f466ab..86eed7daf 100644 --- a/crates/nu-cli/src/commands/clip.rs +++ b/crates/nu-cli/src/commands/clip.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use futures::stream::StreamExt; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/commands/command.rs b/crates/nu-cli/src/commands/command.rs index 18a44f3a4..06a0d15d6 100644 --- a/crates/nu-cli/src/commands/command.rs +++ b/crates/nu-cli/src/commands/command.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::help::get_help; -use crate::context::CommandRegistry; use crate::deserializer::ConfigDeserializer; use crate::evaluate::evaluate_args::evaluate_args; use crate::prelude::*; diff --git a/crates/nu-cli/src/commands/compact.rs b/crates/nu-cli/src/commands/compact.rs index 24dff5b55..fd8f5d10c 100644 --- a/crates/nu-cli/src/commands/compact.rs +++ b/crates/nu-cli/src/commands/compact.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use futures::future; use futures::stream::StreamExt; diff --git a/crates/nu-cli/src/commands/config/clear.rs b/crates/nu-cli/src/commands/config/clear.rs index b37523602..d594ad120 100644 --- a/crates/nu-cli/src/commands/config/clear.rs +++ b/crates/nu-cli/src/commands/config/clear.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/config/get.rs b/crates/nu-cli/src/commands/config/get.rs index 8c8202621..c2f19b26c 100644 --- a/crates/nu-cli/src/commands/config/get.rs +++ b/crates/nu-cli/src/commands/config/get.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/config/load.rs b/crates/nu-cli/src/commands/config/load.rs index d09acec30..0fb21a52c 100644 --- a/crates/nu-cli/src/commands/config/load.rs +++ b/crates/nu-cli/src/commands/config/load.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/config/path.rs b/crates/nu-cli/src/commands/config/path.rs index 0df3abf35..bec194a41 100644 --- a/crates/nu-cli/src/commands/config/path.rs +++ b/crates/nu-cli/src/commands/config/path.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/config/remove.rs b/crates/nu-cli/src/commands/config/remove.rs index 602580d78..a5022595f 100644 --- a/crates/nu-cli/src/commands/config/remove.rs +++ b/crates/nu-cli/src/commands/config/remove.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/config/set.rs b/crates/nu-cli/src/commands/config/set.rs index 10f936cad..7d534c1f5 100644 --- a/crates/nu-cli/src/commands/config/set.rs +++ b/crates/nu-cli/src/commands/config/set.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/config/set_into.rs b/crates/nu-cli/src/commands/config/set_into.rs index da8fe4f3e..2c542b763 100644 --- a/crates/nu-cli/src/commands/config/set_into.rs +++ b/crates/nu-cli/src/commands/config/set_into.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/count.rs b/crates/nu-cli/src/commands/count.rs index 95dc2da11..456ac4cce 100644 --- a/crates/nu-cli/src/commands/count.rs +++ b/crates/nu-cli/src/commands/count.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use futures::stream::StreamExt; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/commands/cp.rs b/crates/nu-cli/src/commands/cp.rs index 778f032a8..1f51ba05f 100644 --- a/crates/nu-cli/src/commands/cp.rs +++ b/crates/nu-cli/src/commands/cp.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/default.rs b/crates/nu-cli/src/commands/default.rs index ffe0671da..ea5210672 100644 --- a/crates/nu-cli/src/commands/default.rs +++ b/crates/nu-cli/src/commands/default.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/do_.rs b/crates/nu-cli/src/commands/do_.rs index 4ddb0368c..8e2868028 100644 --- a/crates/nu-cli/src/commands/do_.rs +++ b/crates/nu-cli/src/commands/do_.rs @@ -65,7 +65,7 @@ async fn do_( let registry = registry.clone(); let external_redirection = raw_args.call_info.args.external_redirection; - let mut context = Context::from_raw(&raw_args, ®istry); + let mut context = EvaluationContext::from_raw(&raw_args, ®istry); let scope = raw_args.call_info.scope.clone(); let ( DoArgs { diff --git a/crates/nu-cli/src/commands/drop.rs b/crates/nu-cli/src/commands/drop.rs index 7820419ee..b7d6de779 100644 --- a/crates/nu-cli/src/commands/drop.rs +++ b/crates/nu-cli/src/commands/drop.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/each/command.rs b/crates/nu-cli/src/commands/each/command.rs index 806528932..22c44dd0b 100644 --- a/crates/nu-cli/src/commands/each/command.rs +++ b/crates/nu-cli/src/commands/each/command.rs @@ -1,6 +1,6 @@ +use crate::command_registry::CommandRegistry; use crate::commands::classified::block::run_block; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use futures::stream::once; @@ -84,7 +84,7 @@ pub async fn process_row( block: Arc, scope: Arc, head: Arc>, - mut context: Arc, + mut context: Arc, input: Value, ) -> Result { let input_clone = input.clone(); @@ -120,7 +120,7 @@ async fn each( let registry = registry.clone(); let head = Arc::new(raw_args.call_info.args.head.clone()); let scope = Arc::new(raw_args.call_info.scope.clone()); - let context = Arc::new(Context::from_raw(&raw_args, ®istry)); + let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry)); let (each_args, input): (EachArgs, _) = raw_args.process(®istry).await?; let block = Arc::new(each_args.block); diff --git a/crates/nu-cli/src/commands/each/group.rs b/crates/nu-cli/src/commands/each/group.rs index cb3678c6c..9d7a97b88 100644 --- a/crates/nu-cli/src/commands/each/group.rs +++ b/crates/nu-cli/src/commands/each/group.rs @@ -54,7 +54,7 @@ impl WholeStreamCommand for EachGroup { let registry = registry.clone(); let head = Arc::new(raw_args.call_info.args.head.clone()); let scope = Arc::new(raw_args.call_info.scope.clone()); - let context = Arc::new(Context::from_raw(&raw_args, ®istry)); + let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry)); let (each_args, input): (EachGroupArgs, _) = raw_args.process(®istry).await?; let block = Arc::new(each_args.block); @@ -79,7 +79,7 @@ pub(crate) fn run_block_on_vec( block: Arc, scope: Arc, head: Arc>, - context: Arc, + context: Arc, ) -> impl Future { let value = Value { value: UntaggedValue::Table(input), diff --git a/crates/nu-cli/src/commands/each/window.rs b/crates/nu-cli/src/commands/each/window.rs index a633f39db..167003d32 100644 --- a/crates/nu-cli/src/commands/each/window.rs +++ b/crates/nu-cli/src/commands/each/window.rs @@ -58,7 +58,7 @@ impl WholeStreamCommand for EachWindow { let registry = registry.clone(); let head = Arc::new(raw_args.call_info.args.head.clone()); let scope = Arc::new(raw_args.call_info.scope.clone()); - let context = Arc::new(Context::from_raw(&raw_args, ®istry)); + let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry)); let (each_args, mut input): (EachWindowArgs, _) = raw_args.process(®istry).await?; let block = Arc::new(each_args.block); diff --git a/crates/nu-cli/src/commands/enter.rs b/crates/nu-cli/src/commands/enter.rs index 69df7dd91..f84d4d018 100644 --- a/crates/nu-cli/src/commands/enter.rs +++ b/crates/nu-cli/src/commands/enter.rs @@ -1,6 +1,6 @@ +use crate::command_registry::CommandRegistry; use crate::commands::UnevaluatedCallInfo; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::hir::ExternalRedirection; diff --git a/crates/nu-cli/src/commands/every.rs b/crates/nu-cli/src/commands/every.rs index ff9508c5a..1cc47aca2 100644 --- a/crates/nu-cli/src/commands/every.rs +++ b/crates/nu-cli/src/commands/every.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/exec.rs b/crates/nu-cli/src/commands/exec.rs index e3e24b8b4..67b846670 100644 --- a/crates/nu-cli/src/commands/exec.rs +++ b/crates/nu-cli/src/commands/exec.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/exit.rs b/crates/nu-cli/src/commands/exit.rs index 1d451addc..eff1cf5f4 100644 --- a/crates/nu-cli/src/commands/exit.rs +++ b/crates/nu-cli/src/commands/exit.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::command::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{CommandAction, ReturnSuccess, Signature}; diff --git a/crates/nu-cli/src/commands/first.rs b/crates/nu-cli/src/commands/first.rs index b96cfc4a4..344ea9acd 100644 --- a/crates/nu-cli/src/commands/first.rs +++ b/crates/nu-cli/src/commands/first.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/format.rs b/crates/nu-cli/src/commands/format.rs index a03d2b7b9..942b13e4f 100644 --- a/crates/nu-cli/src/commands/format.rs +++ b/crates/nu-cli/src/commands/format.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/commands/group_by.rs b/crates/nu-cli/src/commands/group_by.rs index 72063fe83..fba2d4b27 100644 --- a/crates/nu-cli/src/commands/group_by.rs +++ b/crates/nu-cli/src/commands/group_by.rs @@ -96,7 +96,7 @@ pub async fn group_by( let registry = registry.clone(); let head = Arc::new(args.call_info.args.head.clone()); let scope = Arc::new(args.call_info.scope.clone()); - let context = Arc::new(Context::from_raw(&args, ®istry)); + let context = Arc::new(EvaluationContext::from_raw(&args, ®istry)); let (GroupByArgs { grouper }, input) = args.process(®istry).await?; let values: Vec = input.collect().await; diff --git a/crates/nu-cli/src/commands/headers.rs b/crates/nu-cli/src/commands/headers.rs index 102a0581a..fc3e14d0c 100644 --- a/crates/nu-cli/src/commands/headers.rs +++ b/crates/nu-cli/src/commands/headers.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use futures::stream::StreamExt; use indexmap::IndexMap; diff --git a/crates/nu-cli/src/commands/if_.rs b/crates/nu-cli/src/commands/if_.rs index 44acc6a95..a75743911 100644 --- a/crates/nu-cli/src/commands/if_.rs +++ b/crates/nu-cli/src/commands/if_.rs @@ -1,6 +1,6 @@ +use crate::command_registry::CommandRegistry; use crate::commands::classified::block::run_block; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; use nu_errors::ShellError; @@ -74,7 +74,7 @@ async fn if_command( let registry = Arc::new(registry.clone()); let scope = Arc::new(raw_args.call_info.scope.clone()); let tag = raw_args.call_info.name_tag.clone(); - let context = Arc::new(Context::from_raw(&raw_args, ®istry)); + let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry)); let ( IfArgs { diff --git a/crates/nu-cli/src/commands/insert.rs b/crates/nu-cli/src/commands/insert.rs index 44d9eb85a..7754bd6f1 100644 --- a/crates/nu-cli/src/commands/insert.rs +++ b/crates/nu-cli/src/commands/insert.rs @@ -1,6 +1,6 @@ +use crate::command_registry::CommandRegistry; use crate::commands::classified::block::run_block; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ @@ -48,7 +48,7 @@ impl WholeStreamCommand for Insert { async fn process_row( scope: Arc, - mut context: Arc, + mut context: Arc, input: Value, mut value: Arc, field: Arc, @@ -136,7 +136,7 @@ async fn insert( ) -> Result { let registry = registry.clone(); let scope = Arc::new(raw_args.call_info.scope.clone()); - let context = Arc::new(Context::from_raw(&raw_args, ®istry)); + let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry)); let (InsertArgs { column, value }, input) = raw_args.process(®istry).await?; let value = Arc::new(value); let column = Arc::new(column); diff --git a/crates/nu-cli/src/commands/is_empty.rs b/crates/nu-cli/src/commands/is_empty.rs index 32a0e1024..0beef1c80 100644 --- a/crates/nu-cli/src/commands/is_empty.rs +++ b/crates/nu-cli/src/commands/is_empty.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/keep/command.rs b/crates/nu-cli/src/commands/keep/command.rs index ec3484c5c..fc8a6ff2d 100644 --- a/crates/nu-cli/src/commands/keep/command.rs +++ b/crates/nu-cli/src/commands/keep/command.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/kill.rs b/crates/nu-cli/src/commands/kill.rs index 8ea29665f..de12ebb64 100644 --- a/crates/nu-cli/src/commands/kill.rs +++ b/crates/nu-cli/src/commands/kill.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/last.rs b/crates/nu-cli/src/commands/last.rs index 46630f7e1..cf44e870b 100644 --- a/crates/nu-cli/src/commands/last.rs +++ b/crates/nu-cli/src/commands/last.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/merge.rs b/crates/nu-cli/src/commands/merge.rs index 97db41301..9eee098bb 100644 --- a/crates/nu-cli/src/commands/merge.rs +++ b/crates/nu-cli/src/commands/merge.rs @@ -1,6 +1,6 @@ +use crate::command_registry::CommandRegistry; use crate::commands::classified::block::run_block; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_data::value::merge_values; @@ -55,7 +55,7 @@ async fn merge( ) -> Result { let registry = registry.clone(); let scope = raw_args.call_info.scope.clone(); - let mut context = Context::from_raw(&raw_args, ®istry); + let mut context = EvaluationContext::from_raw(&raw_args, ®istry); let name_tag = raw_args.call_info.name_tag.clone(); let (merge_args, input): (MergeArgs, _) = raw_args.process(®istry).await?; let block = merge_args.block; diff --git a/crates/nu-cli/src/commands/mkdir.rs b/crates/nu-cli/src/commands/mkdir.rs index 34f2e0c02..b003a36e5 100644 --- a/crates/nu-cli/src/commands/mkdir.rs +++ b/crates/nu-cli/src/commands/mkdir.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/move_/column.rs b/crates/nu-cli/src/commands/move_/column.rs index dd4ff45b4..1d91d9e4f 100644 --- a/crates/nu-cli/src/commands/move_/column.rs +++ b/crates/nu-cli/src/commands/move_/column.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_data::base::select_fields; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/commands/move_/mv.rs b/crates/nu-cli/src/commands/move_/mv.rs index 47850efc7..b442a6cb5 100644 --- a/crates/nu-cli/src/commands/move_/mv.rs +++ b/crates/nu-cli/src/commands/move_/mv.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/nth.rs b/crates/nu-cli/src/commands/nth.rs index a7415eac3..8941af9c8 100644 --- a/crates/nu-cli/src/commands/nth.rs +++ b/crates/nu-cli/src/commands/nth.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/nu/plugin.rs b/crates/nu-cli/src/commands/nu/plugin.rs index 32b9bbbde..ecf38b5cf 100644 --- a/crates/nu-cli/src/commands/nu/plugin.rs +++ b/crates/nu-cli/src/commands/nu/plugin.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::path::canonicalize; use crate::prelude::*; diff --git a/crates/nu-cli/src/commands/prepend.rs b/crates/nu-cli/src/commands/prepend.rs index 6d7cdbe89..c7ef5118c 100644 --- a/crates/nu-cli/src/commands/prepend.rs +++ b/crates/nu-cli/src/commands/prepend.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/range.rs b/crates/nu-cli/src/commands/range.rs index 0e7f1981e..bb65d4a7a 100644 --- a/crates/nu-cli/src/commands/range.rs +++ b/crates/nu-cli/src/commands/range.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::deserializer::NumericRange; use crate::prelude::*; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/commands/reduce.rs b/crates/nu-cli/src/commands/reduce.rs index 514a26a1c..bca002573 100644 --- a/crates/nu-cli/src/commands/reduce.rs +++ b/crates/nu-cli/src/commands/reduce.rs @@ -81,7 +81,7 @@ impl WholeStreamCommand for Reduce { async fn process_row( block: Arc, scope: Arc, - mut context: Arc, + mut context: Arc, row: Value, ) -> Result { let row_clone = row.clone(); @@ -104,7 +104,7 @@ async fn reduce( ) -> Result { let registry = registry.clone(); let base_scope = raw_args.call_info.scope.clone(); - let context = Arc::new(Context::from_raw(&raw_args, ®istry)); + let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry)); let (reduce_args, mut input): (ReduceArgs, _) = raw_args.process(®istry).await?; let block = Arc::new(reduce_args.block); let (ioffset, start) = match reduce_args.fold { diff --git a/crates/nu-cli/src/commands/reverse.rs b/crates/nu-cli/src/commands/reverse.rs index 69294769c..0f01650ba 100644 --- a/crates/nu-cli/src/commands/reverse.rs +++ b/crates/nu-cli/src/commands/reverse.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/rm.rs b/crates/nu-cli/src/commands/rm.rs index c5b3cf4f6..66d1b2566 100644 --- a/crates/nu-cli/src/commands/rm.rs +++ b/crates/nu-cli/src/commands/rm.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/run_alias.rs b/crates/nu-cli/src/commands/run_alias.rs index 4d161c8c0..3c5b9bd56 100644 --- a/crates/nu-cli/src/commands/run_alias.rs +++ b/crates/nu-cli/src/commands/run_alias.rs @@ -44,7 +44,7 @@ impl WholeStreamCommand for AliasCommand { block.set_redirect(call_info.args.external_redirection); let alias_command = self.clone(); - let mut context = Context::from_args(&args, ®istry); + let mut context = EvaluationContext::from_args(&args, ®istry); let input = args.input; let mut scope = call_info.scope.clone(); diff --git a/crates/nu-cli/src/commands/run_external.rs b/crates/nu-cli/src/commands/run_external.rs index 7fd29a6bf..e6c004007 100644 --- a/crates/nu-cli/src/commands/run_external.rs +++ b/crates/nu-cli/src/commands/run_external.rs @@ -84,32 +84,15 @@ impl WholeStreamCommand for RunExternalCommand { .and_then(spanned_expression_to_string)?; let mut external_context = { - #[cfg(windows)] - { - Context { - registry: registry.clone(), - host: args.host.clone(), - user_recently_used_autoenv_untrust: false, - shell_manager: args.shell_manager.clone(), - ctrl_c: args.ctrl_c.clone(), - current_errors: Arc::new(Mutex::new(vec![])), - windows_drives_previous_cwd: Arc::new(Mutex::new( - std::collections::HashMap::new(), - )), - raw_input: String::default(), - } - } - #[cfg(not(windows))] - { - Context { - registry: registry.clone(), - user_recently_used_autoenv_untrust: false, - host: args.host.clone(), - shell_manager: args.shell_manager.clone(), - ctrl_c: args.ctrl_c.clone(), - current_errors: Arc::new(Mutex::new(vec![])), - raw_input: String::default(), - } + EvaluationContext { + registry: registry.clone(), + host: args.host.clone(), + user_recently_used_autoenv_untrust: false, + shell_manager: args.shell_manager.clone(), + ctrl_c: args.ctrl_c.clone(), + current_errors: Arc::new(Mutex::new(vec![])), + windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), + raw_input: String::default(), } }; @@ -160,7 +143,10 @@ impl WholeStreamCommand for RunExternalCommand { } #[allow(unused_variables)] -async fn maybe_autocd_dir<'a>(cmd: &ExternalCommand, ctx: &mut Context) -> Option { +async fn maybe_autocd_dir<'a>( + cmd: &ExternalCommand, + ctx: &mut EvaluationContext, +) -> Option { // We will "auto cd" if // - the command name ends in a path separator, or // - it's not a command on the path and no arguments were given. diff --git a/crates/nu-cli/src/commands/select.rs b/crates/nu-cli/src/commands/select.rs index 05616906d..f801aa4b4 100644 --- a/crates/nu-cli/src/commands/select.rs +++ b/crates/nu-cli/src/commands/select.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/shuffle.rs b/crates/nu-cli/src/commands/shuffle.rs index d1a00809f..76cfb3313 100644 --- a/crates/nu-cli/src/commands/shuffle.rs +++ b/crates/nu-cli/src/commands/shuffle.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Value}; diff --git a/crates/nu-cli/src/commands/skip/command.rs b/crates/nu-cli/src/commands/skip/command.rs index 9450d8d0b..68adf21bd 100644 --- a/crates/nu-cli/src/commands/skip/command.rs +++ b/crates/nu-cli/src/commands/skip/command.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/sleep.rs b/crates/nu-cli/src/commands/sleep.rs index 360fb9b44..48ae88599 100644 --- a/crates/nu-cli/src/commands/sleep.rs +++ b/crates/nu-cli/src/commands/sleep.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/uniq.rs b/crates/nu-cli/src/commands/uniq.rs index 00c5842e3..6c4158c8d 100644 --- a/crates/nu-cli/src/commands/uniq.rs +++ b/crates/nu-cli/src/commands/uniq.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use indexmap::indexmap; use indexmap::map::IndexMap; diff --git a/crates/nu-cli/src/commands/update.rs b/crates/nu-cli/src/commands/update.rs index 127c0edbf..9a5a1e2dc 100644 --- a/crates/nu-cli/src/commands/update.rs +++ b/crates/nu-cli/src/commands/update.rs @@ -1,6 +1,6 @@ +use crate::command_registry::CommandRegistry; use crate::commands::classified::block::run_block; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ @@ -53,7 +53,7 @@ impl WholeStreamCommand for Update { async fn process_row( scope: Arc, - mut context: Arc, + mut context: Arc, input: Value, mut replacement: Arc, field: Arc, @@ -161,7 +161,7 @@ async fn update( let registry = registry.clone(); let name_tag = Arc::new(raw_args.call_info.name_tag.clone()); let scope = Arc::new(raw_args.call_info.scope.clone()); - let context = Arc::new(Context::from_raw(&raw_args, ®istry)); + let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry)); let (UpdateArgs { field, replacement }, input) = raw_args.process(®istry).await?; let replacement = Arc::new(replacement); let field = Arc::new(field); diff --git a/crates/nu-cli/src/commands/where_.rs b/crates/nu-cli/src/commands/where_.rs index 57145232e..e3e02c38a 100644 --- a/crates/nu-cli/src/commands/where_.rs +++ b/crates/nu-cli/src/commands/where_.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/commands/with_env.rs b/crates/nu-cli/src/commands/with_env.rs index 8add3eabb..556ca7fb1 100644 --- a/crates/nu-cli/src/commands/with_env.rs +++ b/crates/nu-cli/src/commands/with_env.rs @@ -76,7 +76,7 @@ async fn with_env( ) -> Result { let registry = registry.clone(); - let mut context = Context::from_raw(&raw_args, ®istry); + let mut context = EvaluationContext::from_raw(&raw_args, ®istry); let mut scope = raw_args.call_info.scope.clone(); let (WithEnvArgs { variable, block }, input) = raw_args.process(®istry).await?; diff --git a/crates/nu-cli/src/commands/wrap.rs b/crates/nu-cli/src/commands/wrap.rs index 27ed89ebb..d679d4ad3 100644 --- a/crates/nu-cli/src/commands/wrap.rs +++ b/crates/nu-cli/src/commands/wrap.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; -use crate::context::CommandRegistry; use crate::prelude::*; use indexmap::{indexmap, IndexMap}; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/completion/command.rs b/crates/nu-cli/src/completion/command.rs index c87a82402..6b609eb9b 100644 --- a/crates/nu-cli/src/completion/command.rs +++ b/crates/nu-cli/src/completion/command.rs @@ -4,14 +4,19 @@ use std::path::Path; use indexmap::set::IndexSet; use super::matchers::Matcher; -use crate::completion::{Completer, Context, Suggestion}; -use crate::context; +use crate::completion::{Completer, CompletionContext, Suggestion}; +use crate::evaluation_context::EvaluationContext; pub struct CommandCompleter; impl Completer for CommandCompleter { - fn complete(&self, ctx: &Context<'_>, partial: &str, matcher: &dyn Matcher) -> Vec { - let context: &context::Context = ctx.as_ref(); + fn complete( + &self, + ctx: &CompletionContext<'_>, + partial: &str, + matcher: &dyn Matcher, + ) -> Vec { + let context: &EvaluationContext = ctx.as_ref(); let mut commands: IndexSet = IndexSet::from_iter(context.registry.names()); // Command suggestions can come from three possible sets: diff --git a/crates/nu-cli/src/completion/flag.rs b/crates/nu-cli/src/completion/flag.rs index 25c0ab6fa..e068480e0 100644 --- a/crates/nu-cli/src/completion/flag.rs +++ b/crates/nu-cli/src/completion/flag.rs @@ -1,14 +1,19 @@ use super::matchers::Matcher; -use crate::completion::{Completer, Context, Suggestion}; -use crate::context; +use crate::completion::{Completer, CompletionContext, Suggestion}; +use crate::evaluation_context::EvaluationContext; pub struct FlagCompleter { pub(crate) cmd: String, } impl Completer for FlagCompleter { - fn complete(&self, ctx: &Context<'_>, partial: &str, matcher: &dyn Matcher) -> Vec { - let context: &context::Context = ctx.as_ref(); + fn complete( + &self, + ctx: &CompletionContext<'_>, + partial: &str, + matcher: &dyn Matcher, + ) -> Vec { + let context: &EvaluationContext = ctx.as_ref(); if let Some(cmd) = context.registry.get_command(&self.cmd) { let sig = cmd.signature(); diff --git a/crates/nu-cli/src/completion/mod.rs b/crates/nu-cli/src/completion/mod.rs index 2836f9018..145339be4 100644 --- a/crates/nu-cli/src/completion/mod.rs +++ b/crates/nu-cli/src/completion/mod.rs @@ -4,7 +4,7 @@ pub(crate) mod flag; pub(crate) mod matchers; pub(crate) mod path; -use crate::context; +use crate::evaluation_context::EvaluationContext; use matchers::Matcher; #[derive(Debug, Eq, PartialEq)] @@ -13,20 +13,25 @@ pub struct Suggestion { pub replacement: String, } -pub struct Context<'a>(&'a context::Context); +pub struct CompletionContext<'a>(&'a EvaluationContext); -impl<'a> Context<'a> { - pub fn new(a: &'a context::Context) -> Context<'a> { - Context(a) +impl<'a> CompletionContext<'a> { + pub fn new(a: &'a EvaluationContext) -> CompletionContext<'a> { + CompletionContext(a) } } -impl<'a> AsRef for Context<'a> { - fn as_ref(&self) -> &context::Context { +impl<'a> AsRef for CompletionContext<'a> { + fn as_ref(&self) -> &EvaluationContext { self.0 } } pub trait Completer { - fn complete(&self, ctx: &Context<'_>, partial: &str, matcher: &dyn Matcher) -> Vec; + fn complete( + &self, + ctx: &CompletionContext<'_>, + partial: &str, + matcher: &dyn Matcher, + ) -> Vec; } diff --git a/crates/nu-cli/src/completion/path.rs b/crates/nu-cli/src/completion/path.rs index c7c47d77e..af6a3d925 100644 --- a/crates/nu-cli/src/completion/path.rs +++ b/crates/nu-cli/src/completion/path.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use super::matchers::Matcher; -use crate::completion::{Completer, Context, Suggestion}; +use crate::completion::{Completer, CompletionContext, Suggestion}; const SEP: char = std::path::MAIN_SEPARATOR; @@ -76,7 +76,7 @@ impl PathCompleter { impl Completer for PathCompleter { fn complete( &self, - _ctx: &Context<'_>, + _ctx: &CompletionContext<'_>, partial: &str, matcher: &dyn Matcher, ) -> Vec { diff --git a/crates/nu-cli/src/context.rs b/crates/nu-cli/src/context.rs deleted file mode 100644 index 8125ee080..000000000 --- a/crates/nu-cli/src/context.rs +++ /dev/null @@ -1,297 +0,0 @@ -use crate::commands::{command::CommandArgs, Command, UnevaluatedCallInfo}; -use crate::env::host::Host; -use crate::shell::shell_manager::ShellManager; -use crate::stream::{InputStream, OutputStream}; -use indexmap::IndexMap; -use nu_errors::ShellError; -use nu_parser::SignatureRegistry; -use nu_protocol::{hir, Scope, Signature}; -use nu_source::{Tag, Text}; -use parking_lot::Mutex; -use std::error::Error; -use std::sync::atomic::AtomicBool; -use std::sync::Arc; - -#[derive(Debug, Clone, Default)] -pub struct CommandRegistry { - registry: Arc>>, -} - -impl SignatureRegistry for CommandRegistry { - fn has(&self, name: &str) -> bool { - let registry = self.registry.lock(); - registry.contains_key(name) - } - fn get(&self, name: &str) -> Option { - let registry = self.registry.lock(); - registry.get(name).map(|command| command.signature()) - } - fn clone_box(&self) -> Box { - Box::new(self.clone()) - } -} - -impl CommandRegistry { - pub fn new() -> CommandRegistry { - CommandRegistry { - registry: Arc::new(Mutex::new(IndexMap::default())), - } - } -} - -impl CommandRegistry { - pub fn get_command(&self, name: &str) -> Option { - let registry = self.registry.lock(); - - registry.get(name).cloned() - } - - pub fn expect_command(&self, name: &str) -> Result { - self.get_command(name).ok_or_else(|| { - ShellError::untagged_runtime_error(format!("Could not load command: {}", name)) - }) - } - - pub fn has(&self, name: &str) -> bool { - let registry = self.registry.lock(); - - registry.contains_key(name) - } - - pub fn insert(&mut self, name: impl Into, command: Command) { - let mut registry = self.registry.lock(); - registry.insert(name.into(), command); - } - - pub fn names(&self) -> Vec { - let registry = self.registry.lock(); - registry.keys().cloned().collect() - } -} - -#[derive(Clone)] -pub struct Context { - pub registry: CommandRegistry, - pub host: Arc>>, - pub current_errors: Arc>>, - pub ctrl_c: Arc, - pub raw_input: String, - pub user_recently_used_autoenv_untrust: bool, - pub(crate) shell_manager: ShellManager, - - #[cfg(windows)] - pub windows_drives_previous_cwd: Arc>>, -} - -impl Context { - pub(crate) fn registry(&self) -> &CommandRegistry { - &self.registry - } - - pub(crate) fn from_raw(raw_args: &CommandArgs, registry: &CommandRegistry) -> Context { - #[cfg(windows)] - { - Context { - registry: registry.clone(), - host: raw_args.host.clone(), - current_errors: raw_args.current_errors.clone(), - ctrl_c: raw_args.ctrl_c.clone(), - shell_manager: raw_args.shell_manager.clone(), - user_recently_used_autoenv_untrust: false, - windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), - raw_input: String::default(), - } - } - #[cfg(not(windows))] - { - Context { - registry: registry.clone(), - host: raw_args.host.clone(), - current_errors: raw_args.current_errors.clone(), - ctrl_c: raw_args.ctrl_c.clone(), - shell_manager: raw_args.shell_manager.clone(), - user_recently_used_autoenv_untrust: false, - raw_input: String::default(), - } - } - } - - pub(crate) fn from_args(args: &CommandArgs, registry: &CommandRegistry) -> Context { - #[cfg(windows)] - { - Context { - registry: registry.clone(), - host: args.host.clone(), - current_errors: args.current_errors.clone(), - ctrl_c: args.ctrl_c.clone(), - shell_manager: args.shell_manager.clone(), - user_recently_used_autoenv_untrust: false, - windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), - raw_input: String::default(), - } - } - #[cfg(not(windows))] - { - Context { - registry: registry.clone(), - host: args.host.clone(), - current_errors: args.current_errors.clone(), - ctrl_c: args.ctrl_c.clone(), - user_recently_used_autoenv_untrust: false, - shell_manager: args.shell_manager.clone(), - raw_input: String::default(), - } - } - } - - pub fn basic() -> Result> { - let registry = CommandRegistry::new(); - - #[cfg(windows)] - { - Ok(Context { - registry, - host: Arc::new(parking_lot::Mutex::new(Box::new( - crate::env::host::BasicHost, - ))), - current_errors: Arc::new(Mutex::new(vec![])), - ctrl_c: Arc::new(AtomicBool::new(false)), - user_recently_used_autoenv_untrust: false, - shell_manager: ShellManager::basic()?, - windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), - raw_input: String::default(), - }) - } - - #[cfg(not(windows))] - { - Ok(Context { - registry, - host: Arc::new(parking_lot::Mutex::new(Box::new( - crate::env::host::BasicHost, - ))), - current_errors: Arc::new(Mutex::new(vec![])), - ctrl_c: Arc::new(AtomicBool::new(false)), - user_recently_used_autoenv_untrust: false, - shell_manager: ShellManager::basic()?, - raw_input: String::default(), - }) - } - } - - pub(crate) fn error(&mut self, error: ShellError) { - self.with_errors(|errors| errors.push(error)) - } - - pub(crate) fn clear_errors(&mut self) { - self.current_errors.lock().clear() - } - - pub(crate) fn get_errors(&self) -> Vec { - self.current_errors.lock().clone() - } - - pub(crate) fn add_error(&self, err: ShellError) { - self.current_errors.lock().push(err); - } - - pub(crate) fn maybe_print_errors(&mut self, source: Text) -> bool { - let errors = self.current_errors.clone(); - let mut errors = errors.lock(); - - if errors.len() > 0 { - let error = errors[0].clone(); - *errors = vec![]; - - crate::cli::print_err(error, &source); - true - } else { - false - } - } - - pub(crate) fn configure( - &mut self, - config: &dyn nu_data::config::Conf, - block: impl FnOnce(&dyn nu_data::config::Conf, &mut Self) -> T, - ) { - block(config, &mut *self); - } - - pub(crate) fn with_host(&mut self, block: impl FnOnce(&mut dyn Host) -> T) -> T { - let mut host = self.host.lock(); - - block(&mut *host) - } - - pub(crate) fn with_errors(&mut self, block: impl FnOnce(&mut Vec) -> T) -> T { - let mut errors = self.current_errors.lock(); - - block(&mut *errors) - } - - pub fn add_commands(&mut self, commands: Vec) { - for command in commands { - self.registry.insert(command.name().to_string(), command); - } - } - - #[allow(unused)] - pub(crate) fn get_command(&self, name: &str) -> Option { - self.registry.get_command(name) - } - - pub(crate) fn is_command_registered(&self, name: &str) -> bool { - self.registry.has(name) - } - - pub(crate) fn expect_command(&self, name: &str) -> Result { - self.registry.expect_command(name) - } - - pub(crate) async fn run_command( - &mut self, - command: Command, - name_tag: Tag, - args: hir::Call, - scope: &Scope, - input: InputStream, - ) -> Result { - let command_args = self.command_args(args, input, name_tag, scope); - command.run(command_args, self.registry()).await - } - - fn call_info(&self, args: hir::Call, name_tag: Tag, scope: &Scope) -> UnevaluatedCallInfo { - UnevaluatedCallInfo { - args, - name_tag, - scope: scope.clone(), - } - } - - fn command_args( - &self, - args: hir::Call, - input: InputStream, - name_tag: Tag, - scope: &Scope, - ) -> CommandArgs { - CommandArgs { - host: self.host.clone(), - ctrl_c: self.ctrl_c.clone(), - current_errors: self.current_errors.clone(), - shell_manager: self.shell_manager.clone(), - call_info: self.call_info(args, name_tag, scope), - input, - raw_input: self.raw_input.clone(), - } - } - - pub fn get_env(&self) -> IndexMap { - let mut output = IndexMap::new(); - for (var, value) in self.host.lock().vars() { - output.insert(var, value); - } - output - } -} diff --git a/crates/nu-cli/src/env/environment_syncer.rs b/crates/nu-cli/src/env/environment_syncer.rs index fcc77958c..88509fca6 100644 --- a/crates/nu-cli/src/env/environment_syncer.rs +++ b/crates/nu-cli/src/env/environment_syncer.rs @@ -1,5 +1,5 @@ -use crate::context::Context; use crate::env::environment::{Env, Environment}; +use crate::evaluation_context::EvaluationContext; use nu_data::config::{Conf, NuConfig}; use nu_errors::ShellError; use parking_lot::Mutex; @@ -61,14 +61,14 @@ impl EnvironmentSyncer { environment.morph(&*config); } - pub fn autoenv(&self, ctx: &mut Context) -> Result<(), ShellError> { + pub fn autoenv(&self, ctx: &mut EvaluationContext) -> Result<(), ShellError> { let mut environment = self.env.lock(); let auto = environment.autoenv(ctx.user_recently_used_autoenv_untrust); ctx.user_recently_used_autoenv_untrust = false; auto } - pub fn sync_env_vars(&mut self, ctx: &mut Context) { + pub fn sync_env_vars(&mut self, ctx: &mut EvaluationContext) { let mut environment = self.env.lock(); if environment.env().is_some() { @@ -99,7 +99,7 @@ impl EnvironmentSyncer { } } - pub fn sync_path_vars(&mut self, ctx: &mut Context) { + pub fn sync_path_vars(&mut self, ctx: &mut EvaluationContext) { let mut environment = self.env.lock(); if environment.path().is_some() { @@ -131,7 +131,7 @@ impl EnvironmentSyncer { } #[cfg(test)] - pub fn clear_env_vars(&mut self, ctx: &mut Context) { + pub fn clear_env_vars(&mut self, ctx: &mut EvaluationContext) { for (key, _value) in ctx.with_host(|host| host.vars()) { if key != "path" && key != "PATH" { ctx.with_host(|host| host.env_rm(std::ffi::OsString::from(key))); @@ -140,7 +140,7 @@ impl EnvironmentSyncer { } #[cfg(test)] - pub fn clear_path_var(&mut self, ctx: &mut Context) { + pub fn clear_path_var(&mut self, ctx: &mut EvaluationContext) { ctx.with_host(|host| host.env_rm(std::ffi::OsString::from("PATH"))); } } @@ -148,8 +148,8 @@ impl EnvironmentSyncer { #[cfg(test)] mod tests { use super::EnvironmentSyncer; - use crate::context::Context; use crate::env::environment::Env; + use crate::evaluation_context::EvaluationContext; use indexmap::IndexMap; use nu_data::config::tests::FakeConfig; use nu_errors::ShellError; @@ -166,7 +166,7 @@ mod tests { #[test] fn syncs_env_if_new_env_entry_is_added_to_an_existing_configuration() -> Result<(), ShellError> { - let mut ctx = Context::basic()?; + let mut ctx = EvaluationContext::basic()?; ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); let mut expected = IndexMap::new(); @@ -269,7 +269,7 @@ mod tests { #[test] fn syncs_env_if_new_env_entry_in_session_is_not_in_configuration_file() -> Result<(), ShellError> { - let mut ctx = Context::basic()?; + let mut ctx = EvaluationContext::basic()?; ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); let mut expected = IndexMap::new(); @@ -368,7 +368,7 @@ mod tests { #[test] fn nu_envs_have_higher_priority_and_does_not_get_overwritten() -> Result<(), ShellError> { - let mut ctx = Context::basic()?; + let mut ctx = EvaluationContext::basic()?; ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); let mut expected = IndexMap::new(); @@ -444,7 +444,7 @@ mod tests { #[test] fn syncs_path_if_new_path_entry_in_session_is_not_in_configuration_file( ) -> Result<(), ShellError> { - let mut ctx = Context::basic()?; + let mut ctx = EvaluationContext::basic()?; ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); let expected = std::env::join_paths(vec![ @@ -531,7 +531,7 @@ mod tests { #[test] fn nu_paths_have_higher_priority_and_new_paths_get_appended_to_the_end( ) -> Result<(), ShellError> { - let mut ctx = Context::basic()?; + let mut ctx = EvaluationContext::basic()?; ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); let expected = std::env::join_paths(vec![ diff --git a/crates/nu-cli/src/evaluate/evaluate_args.rs b/crates/nu-cli/src/evaluate/evaluate_args.rs index 7da6f7000..cd8fe4278 100644 --- a/crates/nu-cli/src/evaluate/evaluate_args.rs +++ b/crates/nu-cli/src/evaluate/evaluate_args.rs @@ -1,5 +1,5 @@ // TODO: Temporary redirect -use crate::context::CommandRegistry; +use crate::command_registry::CommandRegistry; use crate::evaluate::evaluate_baseline_expr; use indexmap::IndexMap; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/evaluate/evaluator.rs b/crates/nu-cli/src/evaluate/evaluator.rs index 9d117e7d9..b57ec497b 100644 --- a/crates/nu-cli/src/evaluate/evaluator.rs +++ b/crates/nu-cli/src/evaluate/evaluator.rs @@ -1,5 +1,5 @@ +use crate::command_registry::CommandRegistry; use crate::commands::classified::block::run_block; -use crate::context::CommandRegistry; use crate::evaluate::operator::apply_operator; use crate::prelude::*; use async_recursion::async_recursion; @@ -241,7 +241,7 @@ async fn evaluate_invocation( env: &IndexMap, ) -> Result { // FIXME: we should use a real context here - let mut context = Context::basic()?; + let mut context = EvaluationContext::basic()?; context.registry = registry.clone(); let input = InputStream::one(it.clone()); diff --git a/crates/nu-cli/src/evaluation_context.rs b/crates/nu-cli/src/evaluation_context.rs new file mode 100644 index 000000000..49cc0231f --- /dev/null +++ b/crates/nu-cli/src/evaluation_context.rs @@ -0,0 +1,195 @@ +use crate::command_registry::CommandRegistry; +use crate::commands::{command::CommandArgs, Command, UnevaluatedCallInfo}; +use crate::env::host::Host; +use crate::shell::shell_manager::ShellManager; +use crate::stream::{InputStream, OutputStream}; +use indexmap::IndexMap; +use nu_errors::ShellError; +use nu_protocol::{hir, Scope}; +use nu_source::{Tag, Text}; +use parking_lot::Mutex; +use std::error::Error; +use std::sync::atomic::AtomicBool; +use std::sync::Arc; + +#[derive(Clone)] +pub struct EvaluationContext { + pub registry: CommandRegistry, + pub host: Arc>>, + pub current_errors: Arc>>, + pub ctrl_c: Arc, + pub raw_input: String, + pub user_recently_used_autoenv_untrust: bool, + pub(crate) shell_manager: ShellManager, + + /// Windows-specific: keep track of previous cwd on each drive + pub windows_drives_previous_cwd: Arc>>, +} + +impl EvaluationContext { + pub(crate) fn registry(&self) -> &CommandRegistry { + &self.registry + } + + pub(crate) fn from_raw( + raw_args: &CommandArgs, + registry: &CommandRegistry, + ) -> EvaluationContext { + EvaluationContext { + registry: registry.clone(), + host: raw_args.host.clone(), + current_errors: raw_args.current_errors.clone(), + ctrl_c: raw_args.ctrl_c.clone(), + shell_manager: raw_args.shell_manager.clone(), + user_recently_used_autoenv_untrust: false, + windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), + raw_input: String::default(), + } + } + + pub(crate) fn from_args(args: &CommandArgs, registry: &CommandRegistry) -> EvaluationContext { + EvaluationContext { + registry: registry.clone(), + host: args.host.clone(), + current_errors: args.current_errors.clone(), + ctrl_c: args.ctrl_c.clone(), + shell_manager: args.shell_manager.clone(), + user_recently_used_autoenv_untrust: false, + windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), + raw_input: String::default(), + } + } + + pub fn basic() -> Result> { + let registry = CommandRegistry::new(); + + Ok(EvaluationContext { + registry, + host: Arc::new(parking_lot::Mutex::new(Box::new( + crate::env::host::BasicHost, + ))), + current_errors: Arc::new(Mutex::new(vec![])), + ctrl_c: Arc::new(AtomicBool::new(false)), + user_recently_used_autoenv_untrust: false, + shell_manager: ShellManager::basic()?, + windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), + raw_input: String::default(), + }) + } + + pub(crate) fn error(&mut self, error: ShellError) { + self.with_errors(|errors| errors.push(error)) + } + + pub(crate) fn clear_errors(&mut self) { + self.current_errors.lock().clear() + } + + pub(crate) fn get_errors(&self) -> Vec { + self.current_errors.lock().clone() + } + + pub(crate) fn add_error(&self, err: ShellError) { + self.current_errors.lock().push(err); + } + + pub(crate) fn maybe_print_errors(&mut self, source: Text) -> bool { + let errors = self.current_errors.clone(); + let mut errors = errors.lock(); + + if errors.len() > 0 { + let error = errors[0].clone(); + *errors = vec![]; + + crate::cli::print_err(error, &source); + true + } else { + false + } + } + + pub(crate) fn configure( + &mut self, + config: &dyn nu_data::config::Conf, + block: impl FnOnce(&dyn nu_data::config::Conf, &mut Self) -> T, + ) { + block(config, &mut *self); + } + + pub(crate) fn with_host(&mut self, block: impl FnOnce(&mut dyn Host) -> T) -> T { + let mut host = self.host.lock(); + + block(&mut *host) + } + + pub(crate) fn with_errors(&mut self, block: impl FnOnce(&mut Vec) -> T) -> T { + let mut errors = self.current_errors.lock(); + + block(&mut *errors) + } + + pub fn add_commands(&mut self, commands: Vec) { + for command in commands { + self.registry.insert(command.name().to_string(), command); + } + } + + #[allow(unused)] + pub(crate) fn get_command(&self, name: &str) -> Option { + self.registry.get_command(name) + } + + pub(crate) fn is_command_registered(&self, name: &str) -> bool { + self.registry.has(name) + } + + pub(crate) fn expect_command(&self, name: &str) -> Result { + self.registry.expect_command(name) + } + + pub(crate) async fn run_command( + &mut self, + command: Command, + name_tag: Tag, + args: hir::Call, + scope: &Scope, + input: InputStream, + ) -> Result { + let command_args = self.command_args(args, input, name_tag, scope); + command.run(command_args, self.registry()).await + } + + fn call_info(&self, args: hir::Call, name_tag: Tag, scope: &Scope) -> UnevaluatedCallInfo { + UnevaluatedCallInfo { + args, + name_tag, + scope: scope.clone(), + } + } + + fn command_args( + &self, + args: hir::Call, + input: InputStream, + name_tag: Tag, + scope: &Scope, + ) -> CommandArgs { + CommandArgs { + host: self.host.clone(), + ctrl_c: self.ctrl_c.clone(), + current_errors: self.current_errors.clone(), + shell_manager: self.shell_manager.clone(), + call_info: self.call_info(args, name_tag, scope), + input, + raw_input: self.raw_input.clone(), + } + } + + pub fn get_env(&self) -> IndexMap { + let mut output = IndexMap::new(); + for (var, value) in self.host.lock().vars() { + output.insert(var, value); + } + output + } +} diff --git a/crates/nu-cli/src/examples.rs b/crates/nu-cli/src/examples.rs index dc86007f0..ea349f990 100644 --- a/crates/nu-cli/src/examples.rs +++ b/crates/nu-cli/src/examples.rs @@ -7,13 +7,13 @@ use nu_protocol::{ShellTypeName, Value}; use crate::commands::classified::block::run_block; use crate::commands::{whole_stream_command, BuildString, Each, Echo, StrCollect}; -use crate::context::Context; +use crate::evaluation_context::EvaluationContext; use crate::stream::InputStream; use crate::WholeStreamCommand; pub fn test(cmd: impl WholeStreamCommand + 'static) { let examples = cmd.examples(); - let mut base_context = Context::basic().expect("could not create basic context"); + let mut base_context = EvaluationContext::basic().expect("could not create basic context"); base_context.add_commands(vec![ whole_stream_command(Echo {}), @@ -58,7 +58,10 @@ pub fn test(cmd: impl WholeStreamCommand + 'static) { } /// Parse and run a nushell pipeline -fn parse_line(line: &'static str, ctx: &mut Context) -> Result { +fn parse_line( + line: &'static str, + ctx: &mut EvaluationContext, +) -> Result { let line = if line.ends_with('\n') { &line[..line.len() - 1] } else { @@ -75,7 +78,7 @@ fn parse_line(line: &'static str, ctx: &mut Context) -> Result Result, ShellError> { let input_stream = InputStream::empty(); let env = ctx.get_env(); diff --git a/crates/nu-cli/src/lib.rs b/crates/nu-cli/src/lib.rs index 47a8d1441..b8ca2941a 100644 --- a/crates/nu-cli/src/lib.rs +++ b/crates/nu-cli/src/lib.rs @@ -14,14 +14,15 @@ extern crate quickcheck; extern crate quickcheck_macros; mod cli; +mod command_registry; mod commands; #[cfg(feature = "rustyline-support")] mod completion; -mod context; mod deserializer; mod documentation; mod env; mod evaluate; +mod evaluation_context; mod format; mod futures; #[cfg(feature = "rustyline-support")] @@ -44,13 +45,14 @@ pub use crate::cli::{ create_default_context, parse_and_eval, process_line, register_plugins, run_pipeline_standalone, run_vec_of_pipelines, LineResult, }; +pub use crate::command_registry::CommandRegistry; pub use crate::commands::command::{ whole_stream_command, CommandArgs, EvaluatedWholeStreamCommandArgs, Example, WholeStreamCommand, }; pub use crate::commands::help::get_help; -pub use crate::context::{CommandRegistry, Context}; pub use crate::env::environment_syncer::EnvironmentSyncer; pub use crate::env::host::BasicHost; +pub use crate::evaluation_context::EvaluationContext; pub use crate::prelude::ToOutputStream; pub use crate::stream::{InputStream, InterruptibleStream, OutputStream}; pub use nu_data::config; diff --git a/crates/nu-cli/src/prelude.rs b/crates/nu-cli/src/prelude.rs index 601e71f7d..7c8d21f6b 100644 --- a/crates/nu-cli/src/prelude.rs +++ b/crates/nu-cli/src/prelude.rs @@ -71,10 +71,10 @@ macro_rules! trace_out_stream { pub(crate) use nu_protocol::{errln, out, outln}; use nu_source::HasFallibleSpan; +pub(crate) use crate::command_registry::CommandRegistry; pub(crate) use crate::commands::command::{CommandArgs, RawCommandArgs, RunnableContext}; pub(crate) use crate::commands::Example; -pub(crate) use crate::context::CommandRegistry; -pub(crate) use crate::context::Context; +pub(crate) use crate::evaluation_context::EvaluationContext; pub(crate) use nu_data::config; pub(crate) use nu_data::value; // pub(crate) use crate::env::host::handle_unexpected; diff --git a/crates/nu-cli/src/shell/completer.rs b/crates/nu-cli/src/shell/completer.rs index 95fe6f369..26a300c0a 100644 --- a/crates/nu-cli/src/shell/completer.rs +++ b/crates/nu-cli/src/shell/completer.rs @@ -4,7 +4,7 @@ use crate::completion::matchers; use crate::completion::matchers::Matcher; use crate::completion::path::{PathCompleter, PathSuggestion}; use crate::completion::{self, Completer, Suggestion}; -use crate::context; +use crate::evaluation_context::EvaluationContext; use nu_source::Tag; use std::borrow::Cow; @@ -18,11 +18,11 @@ impl NuCompleter { &self, line: &str, pos: usize, - context: &completion::Context, + context: &completion::CompletionContext, ) -> (usize, Vec) { use completion::engine::LocationType; - let nu_context: &context::Context = context.as_ref(); + let nu_context: &EvaluationContext = context.as_ref(); let lite_block = match nu_parser::lite_parse(line, 0) { Ok(block) => Some(block), Err(result) => result.partial, diff --git a/crates/nu-cli/src/shell/helper.rs b/crates/nu-cli/src/shell/helper.rs index 0fb49b135..5235397ce 100644 --- a/crates/nu-cli/src/shell/helper.rs +++ b/crates/nu-cli/src/shell/helper.rs @@ -4,7 +4,7 @@ use nu_parser::SignatureRegistry; use nu_source::{Tag, Tagged}; use crate::completion; -use crate::context::Context; +use crate::evaluation_context::EvaluationContext; use crate::shell::completer::NuCompleter; use crate::shell::painter::Painter; use crate::shell::palette::DefaultPalette; @@ -12,13 +12,16 @@ use crate::shell::palette::DefaultPalette; pub struct Helper { completer: NuCompleter, hinter: Option, - context: Context, + context: EvaluationContext, pub colored_prompt: String, validator: NuValidator, } impl Helper { - pub(crate) fn new(context: Context, hinter: Option) -> Helper { + pub(crate) fn new( + context: EvaluationContext, + hinter: Option, + ) -> Helper { Helper { completer: NuCompleter {}, hinter, @@ -48,7 +51,7 @@ impl rustyline::completion::Completer for Helper { pos: usize, _ctx: &rustyline::Context<'_>, ) -> Result<(usize, Vec), rustyline::error::ReadlineError> { - let ctx = completion::Context::new(&self.context); + let ctx = completion::CompletionContext::new(&self.context); Ok(self.completer.complete(line, pos, &ctx)) }