Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move completions to DeclId #4801

Merged
merged 3 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move completions to DeclId
  • Loading branch information
sophiajt committed Mar 9, 2022
commit f4d4b784a0880f74a09cd8c55947e9ff11c52524
26 changes: 12 additions & 14 deletions crates/nu-cli/src/completions.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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 {
Expand All @@ -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>(
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-engine/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion crates/nu-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 3 additions & 2 deletions crates/nu-parser/src/flatten.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -28,7 +29,7 @@ pub enum FlatShape {
GlobPattern,
Variable,
Flag,
Custom(String),
Custom(DeclId),
}

impl Display for FlatShape {
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 14 additions & 8 deletions crates/nu-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion crates/nu-protocol/src/ast/expression.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-protocol/src/syntax_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-protocol/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down