move scope variable into nu variable (#4725)

This commit is contained in:
JT 2022-03-04 11:36:11 -05:00 committed by GitHub
parent eef3de2d05
commit e64ca97fe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 349 additions and 355 deletions

View File

@ -86,7 +86,7 @@ impl NuCompleter {
) -> Vec<(reedline::Span, String)> {
let mut output = vec![];
let builtins = ["$nu", "$scope", "$in", "$config", "$env", "$nothing"];
let builtins = ["$nu", "$in", "$config", "$env", "$nothing"];
for builtin in builtins {
if builtin.as_bytes().starts_with(prefix) {

View File

@ -672,83 +672,11 @@ pub fn eval_subexpression(
Ok(input)
}
pub fn eval_variable(
pub fn create_scope(
engine_state: &EngineState,
stack: &Stack,
var_id: VarId,
span: Span,
) -> Result<Value, ShellError> {
match var_id {
nu_protocol::NU_VARIABLE_ID => {
// $nu
let mut output_cols = vec![];
let mut output_vals = vec![];
if let Some(mut config_path) = nu_path::config_dir() {
config_path.push("nushell");
let mut history_path = config_path.clone();
history_path.push("history.txt");
output_cols.push("history-path".into());
output_vals.push(Value::String {
val: history_path.to_string_lossy().to_string(),
span,
});
config_path.push("config.nu");
output_cols.push("config-path".into());
output_vals.push(Value::String {
val: config_path.to_string_lossy().to_string(),
span,
});
}
#[cfg(feature = "plugin")]
if let Some(path) = &engine_state.plugin_signatures {
if let Some(path_str) = path.to_str() {
output_cols.push("plugin-path".into());
output_vals.push(Value::String {
val: path_str.into(),
span,
});
}
}
// since the env var PWD doesn't exist on all platforms
// lets just get the current directory
let cwd = current_dir_str(engine_state, stack)?;
output_cols.push("cwd".into());
output_vals.push(Value::String { val: cwd, span });
if let Some(home_path) = nu_path::home_dir() {
if let Some(home_path_str) = home_path.to_str() {
output_cols.push("home-path".into());
output_vals.push(Value::String {
val: home_path_str.into(),
span,
})
}
}
let temp = std::env::temp_dir();
if let Some(temp_path) = temp.to_str() {
output_cols.push("temp-path".into());
output_vals.push(Value::String {
val: temp_path.into(),
span,
})
}
Ok(Value::Record {
cols: output_cols,
vals: output_vals,
span,
})
}
nu_protocol::SCOPE_VARIABLE_ID => {
let mut output_cols = vec![];
let mut output_vals = vec![];
@ -1037,10 +965,9 @@ pub fn eval_variable(
// The names of the commands should be a value string
match (rec_a.get(0), rec_b.get(0)) {
(Some(val_a), Some(val_b)) => match (val_a, val_b) {
(
Value::String { val: str_a, .. },
Value::String { val: str_b, .. },
) => str_a.cmp(str_b),
(Value::String { val: str_a, .. }, Value::String { val: str_b, .. }) => {
str_a.cmp(str_b)
}
_ => Ordering::Equal,
},
_ => Ordering::Equal,
@ -1081,6 +1008,86 @@ pub fn eval_variable(
span,
})
}
pub fn eval_variable(
engine_state: &EngineState,
stack: &Stack,
var_id: VarId,
span: Span,
) -> Result<Value, ShellError> {
match var_id {
nu_protocol::NU_VARIABLE_ID => {
// $nu
let mut output_cols = vec![];
let mut output_vals = vec![];
if let Some(mut config_path) = nu_path::config_dir() {
config_path.push("nushell");
let mut history_path = config_path.clone();
history_path.push("history.txt");
output_cols.push("history-path".into());
output_vals.push(Value::String {
val: history_path.to_string_lossy().to_string(),
span,
});
config_path.push("config.nu");
output_cols.push("config-path".into());
output_vals.push(Value::String {
val: config_path.to_string_lossy().to_string(),
span,
});
}
#[cfg(feature = "plugin")]
if let Some(path) = &engine_state.plugin_signatures {
if let Some(path_str) = path.to_str() {
output_cols.push("plugin-path".into());
output_vals.push(Value::String {
val: path_str.into(),
span,
});
}
}
// since the env var PWD doesn't exist on all platforms
// lets just get the current directory
let cwd = current_dir_str(engine_state, stack)?;
output_cols.push("cwd".into());
output_vals.push(Value::String { val: cwd, span });
output_cols.push("scope".into());
output_vals.push(create_scope(engine_state, stack, span)?);
if let Some(home_path) = nu_path::home_dir() {
if let Some(home_path_str) = home_path.to_str() {
output_cols.push("home-path".into());
output_vals.push(Value::String {
val: home_path_str.into(),
span,
})
}
}
let temp = std::env::temp_dir();
if let Some(temp_path) = temp.to_str() {
output_cols.push("temp-path".into());
output_vals.push(Value::String {
val: temp_path.into(),
span,
})
}
Ok(Value::Record {
cols: output_cols,
vals: output_vals,
span,
})
}
ENV_VARIABLE_ID => {
let env_vars = stack.get_env_vars(engine_state);
let env_columns = env_vars.keys();

View File

@ -1556,16 +1556,6 @@ pub fn parse_variable_expr(
},
None,
);
} else if contents == b"$scope" {
return (
Expression {
expr: Expr::Var(nu_protocol::SCOPE_VARIABLE_ID),
span,
ty: Type::Unknown,
custom_completion: None,
},
None,
);
} else if contents == b"$in" {
return (
Expression {

View File

@ -175,10 +175,9 @@ pub struct EngineState {
}
pub const NU_VARIABLE_ID: usize = 0;
pub const SCOPE_VARIABLE_ID: usize = 1;
pub const IN_VARIABLE_ID: usize = 2;
pub const CONFIG_VARIABLE_ID: usize = 3;
pub const ENV_VARIABLE_ID: usize = 4;
pub const IN_VARIABLE_ID: usize = 1;
pub const CONFIG_VARIABLE_ID: usize = 2;
pub const ENV_VARIABLE_ID: usize = 3;
// NOTE: If you add more to this list, make sure to update the > checks based on the last in the list
impl EngineState {

View File

@ -15,9 +15,7 @@ mod value;
pub use value::Value;
pub use config::*;
pub use engine::{
CONFIG_VARIABLE_ID, ENV_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID, SCOPE_VARIABLE_ID,
};
pub use engine::{CONFIG_VARIABLE_ID, ENV_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID};
pub use example::*;
pub use exportable::*;
pub use id::*;

View File

@ -25,7 +25,7 @@ You can say that the module `greetings` exports an overlay which consists of two
By itself, the module does not do anything.
We can verify its existence by printing all available overlays:
```
> $scope.overlays
> $nu.scope.overlays
╭───┬───────────╮
│ 0 │ greetings │
╰───┴───────────╯
@ -306,7 +306,7 @@ It creates the `$config` variable using the module system.
## Known Issues
* It might be more appropriate to use `$scope.modules` instead of `$scope.overlays`
* It might be more appropriate to use `$nu.scope.modules` instead of `$nu.scope.overlays`
## Future Design Ideas

View File

@ -1,6 +1,6 @@
let vers = (version).version
for command in ($scope.commands | where is_custom == false && is_extern == false) {
for command in ($nu.scope.commands | where is_custom == false && is_extern == false) {
let top = $"---
title: ($command.command)
layout: command

View File

@ -78,7 +78,7 @@ fn help_works_with_missing_requirements() -> TestResult {
#[test]
fn scope_variable() -> TestResult {
run_test(
r#"let x = 3; $scope.vars | where name == "$x" | get type.0"#,
r#"let x = 3; $nu.scope.vars | where name == "$x" | get type.0"#,
"int",
)
}