From 2a9c29c18f3141a35e008a0a1f8991125c7db9c4 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 8 Jun 2020 22:37:12 -0700 Subject: [PATCH] Strip leading spaces after line continuation (#635) Make line continuations strip leading spaces on the next line. This changes what is passed to the shell, so this is a breaking change. However, I don't think that this will break any recipes. --- src/evaluator.rs | 15 ++++++++++++--- src/recipe.rs | 6 ++++-- tests/integration.rs | 8 ++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/evaluator.rs b/src/evaluator.rs index 603aec5..dddbeee 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -145,11 +145,20 @@ impl<'src, 'run> Evaluator<'src, 'run> { }) } - pub(crate) fn evaluate_line(&mut self, line: &Line<'src>) -> RunResult<'src, String> { + pub(crate) fn evaluate_line( + &mut self, + line: &Line<'src>, + continued: bool, + ) -> RunResult<'src, String> { let mut evaluated = String::new(); - for fragment in &line.fragments { + for (i, fragment) in line.fragments.iter().enumerate() { match fragment { - Fragment::Text { token } => evaluated += token.lexeme(), + Fragment::Text { token } => + if i == 0 && continued { + evaluated += token.lexeme().trim_start(); + } else { + evaluated += token.lexeme(); + }, Fragment::Interpolation { expression } => { evaluated += &self.evaluate_expression(expression)?; }, diff --git a/src/recipe.rs b/src/recipe.rs index fb266a5..43aece3 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -89,7 +89,7 @@ impl<'src, D> Recipe<'src, D> { if self.shebang { let mut evaluated_lines = vec![]; for line in &self.body { - evaluated_lines.push(evaluator.evaluate_line(line)?); + evaluated_lines.push(evaluator.evaluate_line(line, false)?); } if config.dry_run || self.quiet { @@ -205,14 +205,16 @@ impl<'src, D> Recipe<'src, D> { break; } let mut evaluated = String::new(); + let mut continued = false; loop { if lines.peek().is_none() { break; } let line = lines.next().unwrap(); line_number += 1; - evaluated += &evaluator.evaluate_line(line)?; + evaluated += &evaluator.evaluate_line(line, continued)?; if line.is_continuation() { + continued = true; evaluated.pop(); } else { break; diff --git a/tests/integration.rs b/tests/integration.rs index bc32967..c73b354 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1292,8 +1292,8 @@ foo: b \ c "#, - stdout: "a b c\n", - stderr: "echo a b c\n", + stdout: "ab c\n", + stderr: "echo ab c\n", } test! { @@ -1304,8 +1304,8 @@ foo: b \ c' "#, - stdout: "a b c\n", - stderr: "echo 'a b c'\n", + stdout: "ab c\n", + stderr: "echo 'ab c'\n", } test! {