Add config to NuCompleter (#4538)

This commit is contained in:
Jakub Žádník 2022-02-18 20:54:13 +02:00 committed by GitHub
parent e5bf56a7dd
commit c893cc1485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 11 deletions

View File

@ -12,11 +12,15 @@ const SEP: char = std::path::MAIN_SEPARATOR;
#[derive(Clone)]
pub struct NuCompleter {
engine_state: EngineState,
config: Option<Value>,
}
impl NuCompleter {
pub fn new(engine_state: EngineState) -> Self {
Self { engine_state }
pub fn new(engine_state: EngineState, config: Option<Value>) -> Self {
Self {
engine_state,
config,
}
}
fn external_command_completion(&self, prefix: &str) -> Vec<String> {
@ -235,14 +239,19 @@ impl NuCompleter {
let mut stack = Stack::new();
// Set up our initial config to start from
stack.vars.insert(
CONFIG_VARIABLE_ID,
Value::Record {
cols: vec![],
vals: vec![],
span: Span { start: 0, end: 0 },
},
);
if let Some(conf) = &self.config {
stack.vars.insert(CONFIG_VARIABLE_ID, conf.clone());
} else {
stack.vars.insert(
CONFIG_VARIABLE_ID,
Value::Record {
cols: vec![],
vals: vec![],
span: Span { start: 0, end: 0 },
},
);
}
let result = eval_block(
&self.engine_state,
&mut stack,

View File

@ -138,7 +138,10 @@ pub(crate) fn evaluate(engine_state: &mut EngineState) -> Result<()> {
.with_validator(Box::new(NuValidator {
engine_state: engine_state.clone(),
}))
.with_completer(Box::new(NuCompleter::new(engine_state.clone())))
.with_completer(Box::new(NuCompleter::new(
engine_state.clone(),
stack.vars.get(&CONFIG_VARIABLE_ID).cloned(),
)))
.with_quick_completions(config.quick_completions)
.with_ansi_colors(config.use_ansi_coloring);