mirror of
https://github.com/nushell/nushell.git
synced 2024-11-29 03:44:19 +01:00
make it-expansion work through blocks when necessary (#1681)
This commit is contained in:
parent
8d197e1b2f
commit
73d5310c9c
@ -848,6 +848,8 @@ async fn process_line(
|
|||||||
|
|
||||||
classified_block.block.expand_it_usage();
|
classified_block.block.expand_it_usage();
|
||||||
|
|
||||||
|
trace!("{:#?}", classified_block);
|
||||||
|
|
||||||
match run_block(&classified_block.block, ctx, input_stream, &Scope::empty()).await {
|
match run_block(&classified_block.block, ctx, input_stream, &Scope::empty()).await {
|
||||||
Ok(input) => {
|
Ok(input) => {
|
||||||
// Running a pipeline gives us back a stream that we can then
|
// Running a pipeline gives us back a stream that we can then
|
||||||
|
@ -105,6 +105,51 @@ impl ClassifiedCommand {
|
|||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn expand_it_usage(&mut self) {
|
||||||
|
match self {
|
||||||
|
ClassifiedCommand::Internal(command) => {
|
||||||
|
if let SpannedExpression {
|
||||||
|
expr: Expression::Literal(Literal::String(s)),
|
||||||
|
..
|
||||||
|
} = &*command.args.head
|
||||||
|
{
|
||||||
|
if s == "run_external" {
|
||||||
|
// For now, don't it-expand externals
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(positionals) = &mut command.args.positional {
|
||||||
|
for arg in positionals {
|
||||||
|
if let SpannedExpression {
|
||||||
|
expr: Expression::Block(block),
|
||||||
|
..
|
||||||
|
} = arg
|
||||||
|
{
|
||||||
|
block.expand_it_usage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ClassifiedCommand::Expr(expr) => {
|
||||||
|
if let SpannedExpression {
|
||||||
|
expr: Expression::Block(ref block),
|
||||||
|
span,
|
||||||
|
} = **expr
|
||||||
|
{
|
||||||
|
let mut block = block.clone();
|
||||||
|
block.expand_it_usage();
|
||||||
|
*expr = Box::new(SpannedExpression {
|
||||||
|
expr: Expression::Block(block),
|
||||||
|
span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
|
||||||
@ -125,6 +170,9 @@ impl Commands {
|
|||||||
/// Convert all shallow uses of $it to `each { use of $it }`, converting each to a per-row command
|
/// Convert all shallow uses of $it to `each { use of $it }`, converting each to a per-row command
|
||||||
pub fn expand_it_usage(&mut self) {
|
pub fn expand_it_usage(&mut self) {
|
||||||
for idx in 0..self.list.len() {
|
for idx in 0..self.list.len() {
|
||||||
|
self.list[idx].expand_it_usage();
|
||||||
|
}
|
||||||
|
for idx in 1..self.list.len() {
|
||||||
if self.list[idx].has_it_iteration() {
|
if self.list[idx].has_it_iteration() {
|
||||||
self.list[idx] = ClassifiedCommand::Internal(InternalCommand {
|
self.list[idx] = ClassifiedCommand::Internal(InternalCommand {
|
||||||
name: "each".to_string(),
|
name: "each".to_string(),
|
||||||
|
@ -107,7 +107,9 @@ mod parse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mod tilde_expansion {
|
mod tilde_expansion {
|
||||||
use super::nu;
|
use nu_test_support::fs::Stub::EmptyFile;
|
||||||
|
use nu_test_support::playground::Playground;
|
||||||
|
use nu_test_support::{nu, pipeline};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
@ -136,4 +138,28 @@ mod tilde_expansion {
|
|||||||
|
|
||||||
assert_eq!(actual, "1~1");
|
assert_eq!(actual, "1~1");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn proper_it_expansion() {
|
||||||
|
Playground::setup("ls_test_1", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![
|
||||||
|
EmptyFile("andres.txt"),
|
||||||
|
EmptyFile("gedge.txt"),
|
||||||
|
EmptyFile("jonathan.txt"),
|
||||||
|
EmptyFile("yehuda.txt"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: dirs.test(), pipeline(
|
||||||
|
r#"
|
||||||
|
ls | sort-by name | group-by type | each { get File.name | echo $it } | to-json
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
actual,
|
||||||
|
r#"["andres.txt","gedge.txt","jonathan.txt","yehuda.txt"]"#
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user