From 525ed7653f641b55093e863f3dcd84c6fffdf52f Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Mon, 24 Jan 2022 09:19:38 -0500 Subject: [PATCH] Add var vals and alias expansions to scope var (#837) * Add var vals and alias expansions to scope var * Fix test --- crates/nu-engine/src/eval.rs | 55 ++++++++++++++++++++++++++---------- src/tests/test_engine.rs | 5 +++- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index c627a7ac10..49aa704d9e 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -620,18 +620,29 @@ pub fn eval_variable( let mut output_cols = vec![]; let mut output_vals = vec![]; - let mut var_names = vec![]; - let mut var_types = vec![]; + let mut vars = vec![]; + let mut commands = vec![]; let mut aliases = vec![]; let mut overlays = vec![]; for frame in &engine_state.scope { for var in &frame.vars { - var_names.push(String::from_utf8_lossy(var.0).to_string()); + let var_name = Value::string(String::from_utf8_lossy(var.0).to_string(), span); - let var = engine_state.get_var(*var.1); - var_types.push(Value::string(var.to_string(), span)); + let var_type = Value::string(engine_state.get_var(*var.1).to_string(), span); + + let var_value = if let Ok(val) = stack.get_var(*var.1) { + val + } else { + Value::nothing(span) + }; + + vars.push(Value::Record { + cols: vec!["name".to_string(), "type".to_string(), "value".to_string()], + vals: vec![var_name, var_type, var_value], + span, + }) } for command in &frame.decls { @@ -829,10 +840,21 @@ pub fn eval_variable( } for alias in &frame.aliases { - aliases.push(Value::String { - val: String::from_utf8_lossy(alias.0).to_string(), - span, - }); + let mut alias_text = String::new(); + for span in alias.1 { + let contents = engine_state.get_span_contents(span); + if !alias_text.is_empty() { + alias_text.push(' '); + } + alias_text.push_str(&String::from_utf8_lossy(contents).to_string()); + } + aliases.push(( + Value::String { + val: String::from_utf8_lossy(alias.0).to_string(), + span, + }, + Value::string(alias_text, span), + )); } for overlay in &frame.overlays { @@ -844,11 +866,7 @@ pub fn eval_variable( } output_cols.push("vars".to_string()); - output_vals.push(Value::Record { - cols: var_names, - vals: var_types, - span, - }); + output_vals.push(Value::List { vals: vars, span }); commands.sort_by(|a, b| match (a, b) { (Value::Record { vals: rec_a, .. }, Value::Record { vals: rec_b, .. }) => { @@ -876,7 +894,14 @@ pub fn eval_variable( aliases.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal)); output_cols.push("aliases".to_string()); output_vals.push(Value::List { - vals: aliases, + vals: aliases + .into_iter() + .map(|(alias, value)| Value::Record { + cols: vec!["alias".into(), "expansion".into()], + vals: vec![alias, value], + span, + }) + .collect(), span, }); diff --git a/src/tests/test_engine.rs b/src/tests/test_engine.rs index 88eaa3eb69..a17e7821ba 100644 --- a/src/tests/test_engine.rs +++ b/src/tests/test_engine.rs @@ -77,7 +77,10 @@ fn help_works_with_missing_requirements() -> TestResult { #[test] fn scope_variable() -> TestResult { - run_test(r#"let x = 3; $scope.vars.'$x'"#, "int") + run_test( + r#"let x = 3; $scope.vars | where name == "$x" | get type.0"#, + "int", + ) } #[test]