Move completions to DeclId (#4801)

* Move completions to DeclId

* fmt

* fmt
This commit is contained in:
JT 2022-03-10 02:49:02 -05:00 committed by GitHub
parent 643cce8a6f
commit 12bf23faa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 37 additions and 30 deletions

View File

@ -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>(

View File

@ -24,7 +24,7 @@ pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
}
}
fn eval_call(
pub fn eval_call(
engine_state: &EngineState,
caller_stack: &mut Stack,
call: &Call,

View File

@ -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;

View File

@ -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 {

View File

@ -2451,13 +2451,19 @@ pub fn parse_shape_name(
);
let command_name = trim_quotes(split[1].as_bytes());
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 (
SyntaxShape::Custom(
Box::new(shape),
String::from_utf8_lossy(command_name).to_string(),
),
err,
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),

View File

@ -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<String>,
pub custom_completion: Option<DeclId>,
}
impl Expression {

View File

@ -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<SyntaxShape>, String),
Custom(Box<SyntaxShape>, DeclId),
}
impl SyntaxShape {

View File

@ -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,
}
}