refactor(completion, parser): move custom_completion info from Expression to Signature (#15613)

Restricts custom completion from universal to internal arguments only.

Pros:
1. Less memory
2. More flexible for later customizations, e.g. #14923 

Cons:
1. limited customization capabilities, but at least covers all currently
existing features in nushell.

# Description

Mostly vibe coded by [Zed AI](https://zed.dev/ai) with a single prompt.
LGTM, but I'm not so sure @ysthakur 

# User-Facing Changes

Hopefully none.

# Tests + Formatting

+3

# After Submitting

---------

Co-authored-by: Yash Thakur <45539777+ysthakur@users.noreply.github.com>
This commit is contained in:
zc he
2025-07-25 02:21:58 +08:00
committed by GitHub
parent 1b01625e1e
commit 71baeff287
12 changed files with 194 additions and 144 deletions

View File

@ -1,5 +1,5 @@
use crate::{
BlockId, DeclId, GetSpan, IN_VARIABLE_ID, Signature, Span, SpanId, Type, VarId,
BlockId, GetSpan, IN_VARIABLE_ID, Signature, Span, SpanId, Type, VarId,
ast::{Argument, Block, Expr, ExternalArgument, ImportPattern, MatchPattern, RecordItem},
engine::StateWorkingSet,
};
@ -15,7 +15,6 @@ pub struct Expression {
pub span: Span,
pub span_id: SpanId,
pub ty: Type,
pub custom_completion: Option<DeclId>,
}
impl Expression {
@ -26,7 +25,6 @@ impl Expression {
span,
span_id,
ty: Type::Any,
custom_completion: None,
}
}
@ -572,7 +570,6 @@ impl Expression {
span,
span_id,
ty,
custom_completion: None,
}
}
@ -582,7 +579,6 @@ impl Expression {
span,
span_id,
ty,
custom_completion: None,
}
}
@ -592,7 +588,6 @@ impl Expression {
span,
span_id: SpanId::new(0),
ty,
custom_completion: None,
}
}
@ -602,7 +597,6 @@ impl Expression {
span: self.span,
span_id,
ty: self.ty,
custom_completion: self.custom_completion,
}
}

View File

@ -1,6 +1,6 @@
use crate::{
BlockId, DeprecationEntry, Example, FromValue, PipelineData, ShellError, SyntaxShape, Type,
Value, VarId,
BlockId, DeclId, DeprecationEntry, Example, FromValue, PipelineData, ShellError, SyntaxShape,
Type, Value, VarId,
engine::{Call, Command, CommandType, EngineState, Stack},
};
use nu_derive_value::FromValue as DeriveFromValue;
@ -24,6 +24,7 @@ pub struct Flag {
// For custom commands
pub var_id: Option<VarId>,
pub default_value: Option<Value>,
pub custom_completion: Option<DeclId>,
}
/// The signature definition for a positional argument
@ -36,6 +37,7 @@ pub struct PositionalArg {
// For custom commands
pub var_id: Option<VarId>,
pub default_value: Option<Value>,
pub custom_completion: Option<DeclId>,
}
/// Command categories
@ -255,6 +257,7 @@ impl Signature {
required: false,
var_id: None,
default_value: None,
custom_completion: None,
};
self.named.push(flag);
self
@ -319,6 +322,7 @@ impl Signature {
shape: shape.into(),
var_id: None,
default_value: None,
custom_completion: None,
});
self
@ -337,6 +341,7 @@ impl Signature {
shape: shape.into(),
var_id: None,
default_value: None,
custom_completion: None,
});
self
@ -354,6 +359,7 @@ impl Signature {
shape: shape.into(),
var_id: None,
default_value: None,
custom_completion: None,
});
self
@ -393,6 +399,7 @@ impl Signature {
desc: desc.into(),
var_id: None,
default_value: None,
custom_completion: None,
});
self
@ -416,6 +423,7 @@ impl Signature {
desc: desc.into(),
var_id: None,
default_value: None,
custom_completion: None,
});
self
@ -438,6 +446,7 @@ impl Signature {
desc: desc.into(),
var_id: None,
default_value: None,
custom_completion: None,
});
self

View File

@ -1,4 +1,4 @@
use crate::{DeclId, Type};
use crate::Type;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
@ -29,9 +29,6 @@ pub enum SyntaxShape {
/// A closure is allowed, eg `{|| start this thing}`
Closure(Option<Vec<SyntaxShape>>),
/// A [`SyntaxShape`] with custom completion logic
CompleterWrapper(Box<SyntaxShape>, DeclId),
/// A datetime value, eg `2022-02-02` or `2019-10-12T07:20:50.52+00:00`
DateTime,
@ -147,7 +144,6 @@ impl SyntaxShape {
SyntaxShape::Closure(_) => Type::Closure,
SyntaxShape::Binary => Type::Binary,
SyntaxShape::CellPath => Type::Any,
SyntaxShape::CompleterWrapper(inner, _) => inner.to_type(),
SyntaxShape::DateTime => Type::Date,
SyntaxShape::Duration => Type::Duration,
SyntaxShape::Expression => Type::Any,
@ -252,7 +248,6 @@ impl Display for SyntaxShape {
SyntaxShape::ExternalArgument => write!(f, "external-argument"),
SyntaxShape::Boolean => write!(f, "bool"),
SyntaxShape::Error => write!(f, "error"),
SyntaxShape::CompleterWrapper(x, _) => write!(f, "completable<{x}>"),
SyntaxShape::OneOf(list) => {
write!(f, "oneof<")?;
if let Some((last, rest)) = list.split_last() {

View File

@ -45,6 +45,7 @@ fn test_signature_chained() {
shape: SyntaxShape::String,
var_id: None,
default_value: None,
custom_completion: None,
})
);
assert_eq!(
@ -55,6 +56,7 @@ fn test_signature_chained() {
shape: SyntaxShape::String,
var_id: None,
default_value: None,
custom_completion: None,
})
);
assert_eq!(
@ -65,6 +67,7 @@ fn test_signature_chained() {
shape: SyntaxShape::String,
var_id: None,
default_value: None,
custom_completion: None,
})
);
@ -78,6 +81,7 @@ fn test_signature_chained() {
desc: "required named description".to_string(),
var_id: None,
default_value: None,
custom_completion: None,
})
);
@ -91,6 +95,7 @@ fn test_signature_chained() {
desc: "required named description".to_string(),
var_id: None,
default_value: None,
custom_completion: None,
})
);
}