mirror of
https://github.com/nushell/nushell.git
synced 2025-08-23 09:30:31 +02:00
Move 'where' to parser keywords; Add 'filter' command (#7365)
# Description This PR moves the `where` command to a parser keyword. While it still uses the shape-directed parsing dictated by the signature, we're free to change the parsing code now to a custom one once we remove the syntax shapes. As a side effect, the `where -b` flag was removed and its functionality has moved to the new `filter` command. Just FYI, other commands that take row conditions: - `take until` - `take while` - `skip until` - `skip while` - `any` - `all` We can either move these to the parser as well or make them accept a closure instead of row condition. # User-Facing Changes New `filter` command which replaces `where -b` functionality. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use log::trace;
|
||||
use nu_path::canonicalize_with;
|
||||
use nu_protocol::{
|
||||
ast::{
|
||||
@@ -3130,6 +3131,97 @@ pub fn parse_source(
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse_where_expr(
|
||||
working_set: &mut StateWorkingSet,
|
||||
spans: &[Span],
|
||||
expand_aliases_denylist: &[usize],
|
||||
) -> (Expression, Option<ParseError>) {
|
||||
trace!("parsing: where");
|
||||
|
||||
if !spans.is_empty() && working_set.get_span_contents(spans[0]) != b"where" {
|
||||
return (
|
||||
garbage(span(spans)),
|
||||
Some(ParseError::UnknownState(
|
||||
"internal error: Wrong call name for 'where' command".into(),
|
||||
span(spans),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
if spans.len() < 2 {
|
||||
return (
|
||||
garbage(span(spans)),
|
||||
Some(ParseError::MissingPositional(
|
||||
"row condition".into(),
|
||||
span(spans),
|
||||
"where <row_condition>".into(),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
let call = match working_set.find_decl(b"where", &Type::Any) {
|
||||
Some(decl_id) => {
|
||||
let ParsedInternalCall {
|
||||
call,
|
||||
error: mut err,
|
||||
output,
|
||||
} = parse_internal_call(
|
||||
working_set,
|
||||
spans[0],
|
||||
&spans[1..],
|
||||
decl_id,
|
||||
expand_aliases_denylist,
|
||||
);
|
||||
let decl = working_set.get_decl(decl_id);
|
||||
|
||||
let call_span = span(spans);
|
||||
|
||||
err = check_call(call_span, &decl.signature(), &call).or(err);
|
||||
if err.is_some() || call.has_flag("help") {
|
||||
return (
|
||||
Expression {
|
||||
expr: Expr::Call(call),
|
||||
span: call_span,
|
||||
ty: output,
|
||||
custom_completion: None,
|
||||
},
|
||||
err,
|
||||
);
|
||||
}
|
||||
|
||||
call
|
||||
}
|
||||
None => {
|
||||
return (
|
||||
garbage(span(spans)),
|
||||
Some(ParseError::UnknownState(
|
||||
"internal error: 'where' declaration not found".into(),
|
||||
span(spans),
|
||||
)),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
Expression {
|
||||
expr: Expr::Call(call),
|
||||
span: span(spans),
|
||||
ty: Type::Any,
|
||||
custom_completion: None,
|
||||
},
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse_where(
|
||||
working_set: &mut StateWorkingSet,
|
||||
spans: &[Span],
|
||||
expand_aliases_denylist: &[usize],
|
||||
) -> (Pipeline, Option<ParseError>) {
|
||||
let (expression, err) = parse_where_expr(working_set, spans, expand_aliases_denylist);
|
||||
(Pipeline::from_vec(vec![expression]), err)
|
||||
}
|
||||
|
||||
#[cfg(feature = "plugin")]
|
||||
pub fn parse_register(
|
||||
working_set: &mut StateWorkingSet,
|
||||
|
Reference in New Issue
Block a user