mirror of
https://github.com/nushell/nushell
synced 2024-12-26 04:53:09 +00:00
Add more multiline pipeline forms (#4740)
This commit is contained in:
parent
32601bb352
commit
a4a8f5df54
8 changed files with 85 additions and 7 deletions
|
@ -243,7 +243,6 @@ pub fn lex(
|
|||
let c = *c;
|
||||
if c == b'|' {
|
||||
// If the next character is `|`, it's either `|` or `||`.
|
||||
|
||||
let idx = curr_offset;
|
||||
let prev_idx = idx;
|
||||
curr_offset += 1;
|
||||
|
@ -262,10 +261,31 @@ pub fn lex(
|
|||
}
|
||||
|
||||
// Otherwise, it's just a regular `|` token.
|
||||
output.push(Token::new(
|
||||
TokenContents::Pipe,
|
||||
Span::new(span_offset + idx, span_offset + idx + 1),
|
||||
));
|
||||
|
||||
// Before we push, check to see if the previous character was a newline.
|
||||
// If so, then this is a continuation of the previous line
|
||||
if let Some(prev) = output.last_mut() {
|
||||
match prev.contents {
|
||||
TokenContents::Eol => {
|
||||
*prev = Token::new(
|
||||
TokenContents::Pipe,
|
||||
Span::new(span_offset + idx, span_offset + idx + 1),
|
||||
)
|
||||
}
|
||||
_ => {
|
||||
output.push(Token::new(
|
||||
TokenContents::Pipe,
|
||||
Span::new(span_offset + idx, span_offset + idx + 1),
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
output.push(Token::new(
|
||||
TokenContents::Pipe,
|
||||
Span::new(span_offset + idx, span_offset + idx + 1),
|
||||
));
|
||||
}
|
||||
|
||||
is_complete = false;
|
||||
} else if c == b';' {
|
||||
// If the next character is a `;`, we're looking at a semicolon token.
|
||||
|
@ -282,9 +302,11 @@ pub fn lex(
|
|||
TokenContents::Semicolon,
|
||||
Span::new(span_offset + idx, span_offset + idx + 1),
|
||||
));
|
||||
} else if c == b'\n' || c == b'\r' {
|
||||
} else if c == b'\r' {
|
||||
// Ignore a stand-alone carriage return
|
||||
curr_offset += 1;
|
||||
} else if c == b'\n' {
|
||||
// If the next character is a newline, we're looking at an EOL (end of line) token.
|
||||
|
||||
let idx = curr_offset;
|
||||
curr_offset += 1;
|
||||
if !additional_whitespace.contains(&c) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
extern crate nu_test_support;
|
||||
|
||||
mod parsing;
|
||||
mod path;
|
||||
mod plugins;
|
||||
mod shell;
|
||||
|
|
46
tests/parsing/mod.rs
Normal file
46
tests/parsing/mod.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use nu_test_support::nu;
|
||||
|
||||
#[test]
|
||||
fn run_nu_script_single_line() {
|
||||
let actual = nu!(cwd: "tests/parsing/samples", r#"
|
||||
nu single_line.nu
|
||||
"#);
|
||||
|
||||
assert_eq!(actual.out, "5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_nu_script_multiline_start_pipe() {
|
||||
let actual = nu!(cwd: "tests/parsing/samples", r#"
|
||||
nu multiline_start_pipe.nu
|
||||
"#);
|
||||
|
||||
assert_eq!(actual.out, "4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_nu_script_multiline_start_pipe_win() {
|
||||
let actual = nu!(cwd: "tests/parsing/samples", r#"
|
||||
nu multiline_start_pipe_win.nu
|
||||
"#);
|
||||
|
||||
assert_eq!(actual.out, "3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_nu_script_multiline_end_pipe() {
|
||||
let actual = nu!(cwd: "tests/parsing/samples", r#"
|
||||
nu multiline_end_pipe.nu
|
||||
"#);
|
||||
|
||||
assert_eq!(actual.out, "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_nu_script_multiline_end_pipe_win() {
|
||||
let actual = nu!(cwd: "tests/parsing/samples", r#"
|
||||
nu multiline_end_pipe_win.nu
|
||||
"#);
|
||||
|
||||
assert_eq!(actual.out, "3");
|
||||
}
|
2
tests/parsing/samples/multiline_end_pipe.nu
Normal file
2
tests/parsing/samples/multiline_end_pipe.nu
Normal file
|
@ -0,0 +1,2 @@
|
|||
echo "hi" |
|
||||
str length
|
2
tests/parsing/samples/multiline_end_pipe_win.nu
Normal file
2
tests/parsing/samples/multiline_end_pipe_win.nu
Normal file
|
@ -0,0 +1,2 @@
|
|||
echo "how" |
|
||||
str length
|
2
tests/parsing/samples/multiline_start_pipe.nu
Normal file
2
tests/parsing/samples/multiline_start_pipe.nu
Normal file
|
@ -0,0 +1,2 @@
|
|||
echo "four"
|
||||
| str length
|
2
tests/parsing/samples/multiline_start_pipe_win.nu
Normal file
2
tests/parsing/samples/multiline_start_pipe_win.nu
Normal file
|
@ -0,0 +1,2 @@
|
|||
echo "one"
|
||||
| str length
|
1
tests/parsing/samples/single_line.nu
Normal file
1
tests/parsing/samples/single_line.nu
Normal file
|
@ -0,0 +1 @@
|
|||
echo "hello" | str length
|
Loading…
Reference in a new issue