From f4d4b784a0880f74a09cd8c55947e9ff11c52524 Mon Sep 17 00:00:00 2001 From: JT Date: Wed, 9 Mar 2022 18:36:24 -0500 Subject: [PATCH 1/3] Move completions to DeclId --- 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 0f1c372ab487..8d69053de3f7 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 50b679c6f824..b87a36dd60d6 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 f43b780d0196..6299eb71dbe9 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 0bee3b396e60..07f89948cb21 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 7742948af22b..99af7e60e654 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 2aaa247328ee..1977cafb558d 100644 --- a/crates/nu-protocol/src/ast/expression.rs +++ b/crates/nu-protocol/src/ast/expression.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use super::{Expr, Operator}; +use crate::DeclId; use crate::ast::ImportPattern; use crate::{engine::StateWorkingSet, BlockId, Signature, Span, Type, VarId, IN_VARIABLE_ID}; @@ -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 3855030d4e35..8d458b5c0781 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::{Type, DeclId}; /// 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 b9e8ecb55790..8ce822d7a064 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, } } From f07b947de272750702ed34a18166a24f1c7eddc0 Mon Sep 17 00:00:00 2001 From: JT Date: Wed, 9 Mar 2022 20:10:10 -0500 Subject: [PATCH 2/3] fmt --- crates/nu-protocol/src/ast/expression.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nu-protocol/src/ast/expression.rs b/crates/nu-protocol/src/ast/expression.rs index 1977cafb558d..ee28b120c36a 100644 --- a/crates/nu-protocol/src/ast/expression.rs +++ b/crates/nu-protocol/src/ast/expression.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; use super::{Expr, Operator}; -use crate::DeclId; 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)] From 12dd953fa53167c1190af2d35324b1c7331ecb50 Mon Sep 17 00:00:00 2001 From: JT Date: Wed, 9 Mar 2022 20:21:44 -0500 Subject: [PATCH 3/3] fmt --- crates/nu-protocol/src/syntax_shape.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nu-protocol/src/syntax_shape.rs b/crates/nu-protocol/src/syntax_shape.rs index 8d458b5c0781..ebc8d5c51d0f 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, DeclId}; +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)]