add custom_completion field to .scope command (#5227)

This commit is contained in:
Sygmei 2022-04-19 00:02:03 +02:00 committed by GitHub
parent 88ec4186ec
commit e206555d9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ use nu_protocol::ast::{Block, Call, Expr, Expression, Operator};
use nu_protocol::engine::{EngineState, Stack, Visibility}; use nu_protocol::engine::{EngineState, Stack, Visibility};
use nu_protocol::{ use nu_protocol::{
IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Range, ShellError, Span, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Range, ShellError, Span,
Spanned, Unit, Value, VarId, ENV_VARIABLE_ID, Spanned, SyntaxShape, Unit, Value, VarId, ENV_VARIABLE_ID,
}; };
use crate::{current_dir_str, get_full_help}; use crate::{current_dir_str, get_full_help};
@ -760,6 +760,17 @@ pub fn eval_subexpression(
Ok(input) Ok(input)
} }
fn extract_custom_completion_from_arg(engine_state: &EngineState, shape: &SyntaxShape) -> String {
return match shape {
SyntaxShape::Custom(_, custom_completion_decl_id) => {
let custom_completion_command = engine_state.get_decl(*custom_completion_decl_id);
let custom_completion_command_name: &str = &*custom_completion_command.name();
custom_completion_command_name.to_string()
}
_ => "".to_string(),
};
}
pub fn create_scope( pub fn create_scope(
engine_state: &EngineState, engine_state: &EngineState,
stack: &Stack, stack: &Stack,
@ -852,6 +863,7 @@ pub fn create_scope(
"is_optional".to_string(), "is_optional".to_string(),
"short_flag".to_string(), "short_flag".to_string(),
"description".to_string(), "description".to_string(),
"custom_completion".to_string(),
]; ];
// required_positional // required_positional
@ -864,6 +876,10 @@ pub fn create_scope(
Value::boolean(false, span), Value::boolean(false, span),
Value::nothing(span), Value::nothing(span),
Value::string(req.desc, span), Value::string(req.desc, span),
Value::string(
extract_custom_completion_from_arg(engine_state, &req.shape),
span,
),
]; ];
sig_records.push(Value::Record { sig_records.push(Value::Record {
@ -883,6 +899,10 @@ pub fn create_scope(
Value::boolean(true, span), Value::boolean(true, span),
Value::nothing(span), Value::nothing(span),
Value::string(opt.desc, span), Value::string(opt.desc, span),
Value::string(
extract_custom_completion_from_arg(engine_state, &opt.shape),
span,
),
]; ];
sig_records.push(Value::Record { sig_records.push(Value::Record {
@ -903,6 +923,10 @@ pub fn create_scope(
Value::boolean(true, span), Value::boolean(true, span),
Value::nothing(span), Value::nothing(span),
Value::string(rest.desc, span), Value::string(rest.desc, span),
Value::string(
extract_custom_completion_from_arg(engine_state, &rest.shape),
span,
),
]; ];
sig_records.push(Value::Record { sig_records.push(Value::Record {
@ -922,8 +946,11 @@ pub fn create_scope(
continue; continue;
} }
let mut custom_completion_command_name: String = "".to_string();
let shape = if let Some(arg) = named.arg { let shape = if let Some(arg) = named.arg {
flag_type = Value::string("named", span); flag_type = Value::string("named", span);
custom_completion_command_name =
extract_custom_completion_from_arg(engine_state, &arg);
Value::string(arg.to_string(), span) Value::string(arg.to_string(), span)
} else { } else {
flag_type = Value::string("switch", span); flag_type = Value::string("switch", span);
@ -944,6 +971,7 @@ pub fn create_scope(
Value::boolean(!named.required, span), Value::boolean(!named.required, span),
short_flag, short_flag,
Value::string(named.desc, span), Value::string(named.desc, span),
Value::string(custom_completion_command_name, span),
]; ];
sig_records.push(Value::Record { sig_records.push(Value::Record {