nu-cli/completions: added completion for $nu (#5303)

This commit is contained in:
Herlon Aguiar 2022-04-23 01:49:17 +02:00 committed by GitHub
parent cc78446ffd
commit b9eb213f36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 1 deletions

View File

@ -1,8 +1,10 @@
use crate::completions::Completer;
use nu_engine::eval_variable;
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
Span, Value,
};
use reedline::Suggestion;
use std::sync::Arc;
@ -65,6 +67,48 @@ impl Completer for VariableCompletion {
return output;
}
// Completions for $nu.<tab>
if var_str.as_str() == "$nu" {
// Eval nu var
if let Ok(nuval) = eval_variable(
&self.engine_state,
&self.stack,
nu_protocol::NU_VARIABLE_ID,
nu_protocol::Span {
start: current_span.start,
end: current_span.end,
},
) {
// Find recursively the values for sublevels
// if no sublevels are set it returns the current value
let value = recursive_value(nuval, self.var_context.1.clone());
match value {
Value::Record {
cols,
vals: _,
span: _,
} => {
// Add all the columns as completion
for item in cols {
output.push(Suggestion {
value: item,
description: None,
extra: None,
span: current_span,
});
}
return output;
}
_ => {
return output;
}
}
}
}
// Completion other variable types
if let Some(var_id) = var_id {
// Extract the variable value from the stack

View File

@ -11,6 +11,6 @@ pub use documentation::get_full_help;
pub use env::*;
pub use eval::{
eval_block, eval_call, eval_expression, eval_expression_with_input, eval_operator,
eval_subexpression,
eval_subexpression, eval_variable,
};
pub use glob_from::glob_from;