From 12bf23faa690fcec8bbd19b5b320f8c1a7d1c944 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Thu, 10 Mar 2022 02:49:02 -0500 Subject: [PATCH] Move completions to DeclId (#4801) * Move completions to DeclId * fmt * fmt --- crates/nu-cli/src/completions.rs | 26 +++++++++++------------- crates/nu-engine/src/eval.rs | 2 +- crates/nu-engine/src/lib.rs | 3 ++- crates/nu-parser/src/flatten.rs | 5 +++-- crates/nu-parser/src/parser.rs | 22 ++++++++++++-------- crates/nu-protocol/src/ast/expression.rs | 3 ++- crates/nu-protocol/src/syntax_shape.rs | 4 ++-- crates/nu-protocol/src/ty.rs | 2 +- 8 files changed, 37 insertions(+), 30 deletions(-) diff --git a/crates/nu-cli/src/completions.rs b/crates/nu-cli/src/completions.rs index 0f1c372ab..8d69053de 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_engine::eval_call; use nu_parser::{flatten_expression, parse, trim_quotes, FlatShape}; use nu_protocol::{ - ast::Expr, + ast::{Call, Expr}, engine::{EngineState, Stack, StateWorkingSet}, PipelineData, Span, Value, CONFIG_VARIABLE_ID, }; @@ -275,16 +275,9 @@ impl NuCompleter { } match &flat.1 { - FlatShape::Custom(custom_completion) => { + FlatShape::Custom(decl_id) => { //let prefix = working_set.get_span_contents(flat.0).to_vec(); - let (block, ..) = parse( - &mut working_set, - None, - custom_completion.as_bytes(), - false, - ); - let mut stack = Stack::new(); // Set up our initial config to start from if let Some(conf) = &self.config { @@ -300,13 +293,18 @@ impl NuCompleter { ); } - let result = eval_block( + let result = eval_call( &self.engine_state, &mut stack, - &block, + &Call { + decl_id: *decl_id, + head: new_span, + positional: vec![], + named: vec![], + redirect_stdout: true, + redirect_stderr: true, + }, PipelineData::new(new_span), - true, - true, ); fn map_completions<'a>( diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 50b679c6f..b87a36dd6 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -24,7 +24,7 @@ pub fn eval_operator(op: &Expression) -> Result { } } -fn eval_call( +pub fn eval_call( engine_state: &EngineState, caller_stack: &mut Stack, call: &Call, diff --git a/crates/nu-engine/src/lib.rs b/crates/nu-engine/src/lib.rs index f43b780d0..6299eb71d 100644 --- a/crates/nu-engine/src/lib.rs +++ b/crates/nu-engine/src/lib.rs @@ -10,6 +10,7 @@ pub use column::get_columns; pub use documentation::{generate_docs, get_brief_help, get_documentation, get_full_help}; pub use env::*; pub use eval::{ - eval_block, eval_expression, eval_expression_with_input, eval_operator, eval_subexpression, + eval_block, eval_call, eval_expression, eval_expression_with_input, eval_operator, + eval_subexpression, }; pub use glob_from::glob_from; diff --git a/crates/nu-parser/src/flatten.rs b/crates/nu-parser/src/flatten.rs index 0bee3b396..07f89948c 100644 --- a/crates/nu-parser/src/flatten.rs +++ b/crates/nu-parser/src/flatten.rs @@ -1,4 +1,5 @@ use nu_protocol::ast::{Block, Expr, Expression, ImportPatternMember, PathMember, Pipeline}; +use nu_protocol::DeclId; use nu_protocol::{engine::StateWorkingSet, Span}; use std::fmt::{Display, Formatter, Result}; @@ -28,7 +29,7 @@ pub enum FlatShape { GlobPattern, Variable, Flag, - Custom(String), + Custom(DeclId), } impl Display for FlatShape { @@ -76,7 +77,7 @@ pub fn flatten_expression( expr: &Expression, ) -> Vec<(Span, FlatShape)> { if let Some(custom_completion) = &expr.custom_completion { - return vec![(expr.span, FlatShape::Custom(custom_completion.clone()))]; + return vec![(expr.span, FlatShape::Custom(*custom_completion))]; } match &expr.expr { diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 7742948af..99af7e60e 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2451,13 +2451,19 @@ pub fn parse_shape_name( ); let command_name = trim_quotes(split[1].as_bytes()); - return ( - SyntaxShape::Custom( - Box::new(shape), - String::from_utf8_lossy(command_name).to_string(), - ), - err, - ); + let decl_id = working_set.find_decl(command_name); + + if let Some(decl_id) = decl_id { + return (SyntaxShape::Custom(Box::new(shape), decl_id), err); + } else { + return ( + shape, + Some(ParseError::UnknownCommand(Span { + start: span.start + split[0].len() + 1, + end: span.end, + })), + ); + } } else { return (SyntaxShape::Any, Some(ParseError::UnknownType(span))); } @@ -3713,7 +3719,7 @@ pub fn parse_value( match shape { SyntaxShape::Custom(shape, custom_completion) => { let (mut expression, err) = parse_value(working_set, span, shape); - expression.custom_completion = Some(custom_completion.clone()); + expression.custom_completion = Some(*custom_completion); (expression, err) } SyntaxShape::Number => parse_number(bytes, span), diff --git a/crates/nu-protocol/src/ast/expression.rs b/crates/nu-protocol/src/ast/expression.rs index 2aaa24732..ee28b120c 100644 --- a/crates/nu-protocol/src/ast/expression.rs +++ b/crates/nu-protocol/src/ast/expression.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use super::{Expr, Operator}; use crate::ast::ImportPattern; +use crate::DeclId; use crate::{engine::StateWorkingSet, BlockId, Signature, Span, Type, VarId, IN_VARIABLE_ID}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -9,7 +10,7 @@ pub struct Expression { pub expr: Expr, pub span: Span, pub ty: Type, - pub custom_completion: Option, + pub custom_completion: Option, } impl Expression { diff --git a/crates/nu-protocol/src/syntax_shape.rs b/crates/nu-protocol/src/syntax_shape.rs index 3855030d4..ebc8d5c51 100644 --- a/crates/nu-protocol/src/syntax_shape.rs +++ b/crates/nu-protocol/src/syntax_shape.rs @@ -2,7 +2,7 @@ use std::fmt::Display; use serde::{Deserialize, Serialize}; -use crate::Type; +use crate::{DeclId, Type}; /// The syntactic shapes that values must match to be passed into a command. You can think of this as the type-checking that occurs when you call a function. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -90,7 +90,7 @@ pub enum SyntaxShape { Record, /// A custom shape with custom completion logic - Custom(Box, String), + Custom(Box, DeclId), } impl SyntaxShape { diff --git a/crates/nu-protocol/src/ty.rs b/crates/nu-protocol/src/ty.rs index b9e8ecb55..8ce822d7a 100644 --- a/crates/nu-protocol/src/ty.rs +++ b/crates/nu-protocol/src/ty.rs @@ -51,7 +51,7 @@ impl Type { Type::Unknown => SyntaxShape::Any, Type::Error => SyntaxShape::Any, Type::Binary => SyntaxShape::Binary, - Type::Custom => SyntaxShape::Custom(Box::new(SyntaxShape::Any), String::new()), + Type::Custom => SyntaxShape::Any, Type::Signature => SyntaxShape::Signature, } }