mirror of
https://github.com/nushell/nushell.git
synced 2025-08-18 20:38:31 +02:00
Track call arguments in a single list (#5125)
* Initial implementation of ordered call args * Run cargo fmt * Fix some clippy lints * Add positional len and nth * Cargo fmt * Remove more old nth calls * Good ole rustfmt * Add named len Co-authored-by: Hristo Filaretov <h.filaretov@protonmail.com>
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
use nu_protocol::ast::Expr;
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{ast::Call, engine::Command, ShellError, Signature};
|
||||
use nu_protocol::{PipelineData, Spanned};
|
||||
use nu_protocol::{
|
||||
ast::{Argument, Call, Expr, Expression},
|
||||
engine::Command,
|
||||
ShellError, Signature,
|
||||
};
|
||||
use nu_protocol::{PipelineData, Spanned, Type};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct KnownExternal {
|
||||
@@ -34,69 +37,56 @@ impl Command for KnownExternal {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
// FIXME: This is a bit of a hack, and it'd be nice for the parser/AST to be able to handle the original
|
||||
// order of the parameters. Until then, we need to recover the original order.
|
||||
|
||||
// FIXME: This is going to be a bit expensive, but we need to do it to ensure any new block/subexpression
|
||||
// we find when parsing the external call is handled properly.
|
||||
let mut engine_state = engine_state.clone();
|
||||
|
||||
let call_span = call.span();
|
||||
let contents = engine_state.get_span_contents(&call_span);
|
||||
let head_span = call.head;
|
||||
let decl_id = engine_state
|
||||
.find_decl("run-external".as_bytes())
|
||||
.ok_or(ShellError::ExternalNotSupported(head_span))?;
|
||||
|
||||
let redirect_stdout = call.redirect_stdout;
|
||||
let redirect_stderr = call.redirect_stderr;
|
||||
let command = engine_state.get_decl(decl_id);
|
||||
|
||||
let (lexed, _) = crate::lex(contents, call_span.start, &[], &[], true);
|
||||
let mut extern_call = Call::new(head_span);
|
||||
|
||||
let spans: Vec<_> = lexed.into_iter().map(|x| x.span).collect();
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
let (external_call, _) = crate::parse_external_call(&mut working_set, &spans, &[]);
|
||||
let delta = working_set.render();
|
||||
engine_state.merge_delta(delta, None, ".")?;
|
||||
let working_state = StateWorkingSet::new(engine_state);
|
||||
let extern_name = working_state.get_span_contents(call.head);
|
||||
let extern_name = String::from_utf8(extern_name.to_vec())
|
||||
.expect("this was already parsed as a command name");
|
||||
let arg_extern_name = Expression {
|
||||
expr: Expr::String(extern_name),
|
||||
span: call.head,
|
||||
ty: Type::String,
|
||||
custom_completion: None,
|
||||
};
|
||||
|
||||
match external_call.expr {
|
||||
Expr::ExternalCall(head, args) => {
|
||||
let decl_id = engine_state
|
||||
.find_decl("run-external".as_bytes())
|
||||
.ok_or(ShellError::ExternalNotSupported(head.span))?;
|
||||
extern_call.add_positional(arg_extern_name);
|
||||
|
||||
let command = engine_state.get_decl(decl_id);
|
||||
|
||||
let mut call = Call::new(head.span);
|
||||
|
||||
call.positional.push(*head);
|
||||
|
||||
for arg in args {
|
||||
call.positional.push(arg.clone())
|
||||
}
|
||||
|
||||
if redirect_stdout {
|
||||
call.named.push((
|
||||
Spanned {
|
||||
item: "redirect-stdout".into(),
|
||||
span: call_span,
|
||||
},
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
if redirect_stderr {
|
||||
call.named.push((
|
||||
Spanned {
|
||||
item: "redirect-stderr".into(),
|
||||
span: call_span,
|
||||
},
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
command.run(&engine_state, stack, &call, input)
|
||||
}
|
||||
x => {
|
||||
println!("{:?}", x);
|
||||
panic!("internal error: known external not actually external")
|
||||
for arg in &call.arguments {
|
||||
match arg {
|
||||
Argument::Positional(positional) => extern_call.add_positional(positional.clone()),
|
||||
Argument::Named(named) => extern_call.add_named(named.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
if call.redirect_stdout {
|
||||
extern_call.add_named((
|
||||
Spanned {
|
||||
item: "redirect-stdout".into(),
|
||||
span: call_span,
|
||||
},
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
if call.redirect_stderr {
|
||||
extern_call.add_named((
|
||||
Spanned {
|
||||
item: "redirect-stderr".into(),
|
||||
span: call_span,
|
||||
},
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
command.run(engine_state, stack, &extern_call, input)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user