From 310ecb79b670f04aaf52b6366a6fce2c130a5c75 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sat, 22 Jan 2022 16:18:31 -0500 Subject: [PATCH] Add flag completions (#817) --- crates/nu-cli/src/completions.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/nu-cli/src/completions.rs b/crates/nu-cli/src/completions.rs index 27e37fcb7d..2912b00122 100644 --- a/crates/nu-cli/src/completions.rs +++ b/crates/nu-cli/src/completions.rs @@ -1,7 +1,7 @@ use nu_engine::eval_block; use nu_parser::{flatten_expression, parse}; use nu_protocol::{ - ast::Statement, + ast::{Expr, Statement}, engine::{EngineState, Stack, StateWorkingSet}, PipelineData, Span, }; @@ -196,6 +196,31 @@ impl NuCompleter { offset, ); } + if prefix.starts_with(b"-") { + // this might be a flag, let's see + if let Expr::Call(call) = &expr.expr { + let decl = working_set.get_decl(call.decl_id); + let sig = decl.signature(); + + let mut output = vec![]; + + for named in &sig.named { + let mut named = named.long.as_bytes().to_vec(); + named.insert(0, b'-'); + named.insert(0, b'-'); + if named.starts_with(prefix) { + output.push(( + reedline::Span { + start: flat.0.start - offset, + end: flat.0.end - offset, + }, + String::from_utf8_lossy(&named).to_string(), + )); + } + } + return output; + } + } match &flat.1 { nu_parser::FlatShape::Custom(custom_completion) => {