2022-11-18 21:46:48 +00:00
|
|
|
use nu_protocol::ast::{
|
2023-03-24 01:52:01 +00:00
|
|
|
Block, Expr, Expression, ImportPatternMember, MatchPattern, PathMember, Pattern, Pipeline,
|
|
|
|
PipelineElement,
|
2022-11-18 21:46:48 +00:00
|
|
|
};
|
2021-09-02 18:21:37 +00:00
|
|
|
use nu_protocol::{engine::StateWorkingSet, Span};
|
2023-04-05 19:34:47 +00:00
|
|
|
use nu_protocol::{DeclId, VarId};
|
2021-12-16 12:17:29 +00:00
|
|
|
use std::fmt::{Display, Formatter, Result};
|
2021-07-22 19:50:59 +00:00
|
|
|
|
2022-04-06 07:58:55 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq, Ord, Clone, PartialOrd)]
|
2021-07-22 19:50:59 +00:00
|
|
|
pub enum FlatShape {
|
2022-12-14 13:11:17 +00:00
|
|
|
And,
|
2022-02-28 23:31:53 +00:00
|
|
|
Binary,
|
2022-12-14 13:11:17 +00:00
|
|
|
Block,
|
|
|
|
Bool,
|
|
|
|
Custom(DeclId),
|
|
|
|
DateTime,
|
|
|
|
Directory,
|
2021-07-22 19:50:59 +00:00
|
|
|
External,
|
2021-09-10 08:07:18 +00:00
|
|
|
ExternalArg,
|
2022-12-14 13:11:17 +00:00
|
|
|
Filepath,
|
|
|
|
Flag,
|
|
|
|
Float,
|
|
|
|
Garbage,
|
|
|
|
GlobPattern,
|
|
|
|
Int,
|
2023-04-05 19:34:47 +00:00
|
|
|
InternalCall(DeclId),
|
|
|
|
Keyword,
|
2022-12-14 13:11:17 +00:00
|
|
|
List,
|
2021-07-22 19:50:59 +00:00
|
|
|
Literal,
|
2023-03-24 01:52:01 +00:00
|
|
|
MatchPattern,
|
2022-12-14 13:11:17 +00:00
|
|
|
Nothing,
|
2021-07-22 19:50:59 +00:00
|
|
|
Operator,
|
2022-12-14 13:11:17 +00:00
|
|
|
Or,
|
|
|
|
Pipe,
|
|
|
|
Range,
|
|
|
|
Record,
|
|
|
|
Redirection,
|
2021-07-22 19:50:59 +00:00
|
|
|
Signature,
|
|
|
|
String,
|
2021-12-25 20:50:02 +00:00
|
|
|
StringInterpolation,
|
2022-01-03 03:18:23 +00:00
|
|
|
Table,
|
2023-04-05 19:34:47 +00:00
|
|
|
Variable(VarId),
|
|
|
|
VarDecl(VarId),
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 12:17:29 +00:00
|
|
|
impl Display for FlatShape {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
|
|
|
match self {
|
2022-12-14 13:11:17 +00:00
|
|
|
FlatShape::And => write!(f, "shape_and"),
|
2022-02-28 23:31:53 +00:00
|
|
|
FlatShape::Binary => write!(f, "shape_binary"),
|
2022-12-14 13:11:17 +00:00
|
|
|
FlatShape::Block => write!(f, "shape_block"),
|
2022-02-21 18:27:21 +00:00
|
|
|
FlatShape::Bool => write!(f, "shape_bool"),
|
2022-12-14 13:11:17 +00:00
|
|
|
FlatShape::Custom(_) => write!(f, "shape_custom"),
|
|
|
|
FlatShape::DateTime => write!(f, "shape_datetime"),
|
|
|
|
FlatShape::Directory => write!(f, "shape_directory"),
|
2022-02-21 18:27:21 +00:00
|
|
|
FlatShape::External => write!(f, "shape_external"),
|
|
|
|
FlatShape::ExternalArg => write!(f, "shape_externalarg"),
|
2022-12-14 13:11:17 +00:00
|
|
|
FlatShape::Filepath => write!(f, "shape_filepath"),
|
|
|
|
FlatShape::Flag => write!(f, "shape_flag"),
|
|
|
|
FlatShape::Float => write!(f, "shape_float"),
|
|
|
|
FlatShape::Garbage => write!(f, "shape_garbage"),
|
|
|
|
FlatShape::GlobPattern => write!(f, "shape_globpattern"),
|
|
|
|
FlatShape::Int => write!(f, "shape_int"),
|
2023-04-05 19:34:47 +00:00
|
|
|
FlatShape::InternalCall(_) => write!(f, "shape_internalcall"),
|
|
|
|
FlatShape::Keyword => write!(f, "shape_keyword"),
|
2022-12-14 13:11:17 +00:00
|
|
|
FlatShape::List => write!(f, "shape_list"),
|
2022-02-21 18:27:21 +00:00
|
|
|
FlatShape::Literal => write!(f, "shape_literal"),
|
2023-03-24 01:52:01 +00:00
|
|
|
FlatShape::MatchPattern => write!(f, "shape_match_pattern"),
|
2022-12-14 13:11:17 +00:00
|
|
|
FlatShape::Nothing => write!(f, "shape_nothing"),
|
2022-02-21 18:27:21 +00:00
|
|
|
FlatShape::Operator => write!(f, "shape_operator"),
|
2022-12-14 13:11:17 +00:00
|
|
|
FlatShape::Or => write!(f, "shape_or"),
|
|
|
|
FlatShape::Pipe => write!(f, "shape_pipe"),
|
|
|
|
FlatShape::Range => write!(f, "shape_range"),
|
|
|
|
FlatShape::Record => write!(f, "shape_record"),
|
|
|
|
FlatShape::Redirection => write!(f, "shape_redirection"),
|
2022-02-21 18:27:21 +00:00
|
|
|
FlatShape::Signature => write!(f, "shape_signature"),
|
|
|
|
FlatShape::String => write!(f, "shape_string"),
|
|
|
|
FlatShape::StringInterpolation => write!(f, "shape_string_interpolation"),
|
|
|
|
FlatShape::Table => write!(f, "shape_table"),
|
2023-04-05 19:34:47 +00:00
|
|
|
FlatShape::Variable(_) => write!(f, "shape_variable"),
|
|
|
|
FlatShape::VarDecl(_) => write!(f, "shape_vardecl"),
|
2021-12-16 12:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
pub fn flatten_block(working_set: &StateWorkingSet, block: &Block) -> Vec<(Span, FlatShape)> {
|
|
|
|
let mut output = vec![];
|
2022-02-15 19:31:14 +00:00
|
|
|
for pipeline in &block.pipelines {
|
|
|
|
output.extend(flatten_pipeline(working_set, pipeline));
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
|
|
|
}
|
2021-07-22 19:50:59 +00:00
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
pub fn flatten_expression(
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
expr: &Expression,
|
|
|
|
) -> Vec<(Span, FlatShape)> {
|
2021-09-14 04:59:46 +00:00
|
|
|
if let Some(custom_completion) = &expr.custom_completion {
|
2022-03-10 07:49:02 +00:00
|
|
|
return vec![(expr.span, FlatShape::Custom(*custom_completion))];
|
2021-09-14 04:59:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
match &expr.expr {
|
|
|
|
Expr::BinaryOp(lhs, op, rhs) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
output.extend(flatten_expression(working_set, lhs));
|
|
|
|
output.extend(flatten_expression(working_set, op));
|
|
|
|
output.extend(flatten_expression(working_set, rhs));
|
|
|
|
output
|
|
|
|
}
|
2022-04-06 19:10:25 +00:00
|
|
|
Expr::UnaryNot(inner_expr) => {
|
|
|
|
let mut output = vec![(
|
2022-12-03 09:44:12 +00:00
|
|
|
Span::new(expr.span.start, expr.span.start + 3),
|
2022-04-06 19:10:25 +00:00
|
|
|
FlatShape::Operator,
|
|
|
|
)];
|
|
|
|
output.extend(flatten_expression(working_set, inner_expr));
|
|
|
|
output
|
|
|
|
}
|
2022-11-10 08:21:49 +00:00
|
|
|
Expr::Block(block_id)
|
|
|
|
| Expr::Closure(block_id)
|
|
|
|
| Expr::RowCondition(block_id)
|
|
|
|
| Expr::Subexpression(block_id) => {
|
2022-01-03 05:21:26 +00:00
|
|
|
let outer_span = expr.span;
|
|
|
|
|
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
let flattened = flatten_block(working_set, working_set.get_block(*block_id));
|
|
|
|
|
|
|
|
if let Some(first) = flattened.first() {
|
|
|
|
if first.0.start > outer_span.start {
|
2022-12-03 09:44:12 +00:00
|
|
|
output.push((Span::new(outer_span.start, first.0.start), FlatShape::Block));
|
2022-01-03 05:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let last = if let Some(last) = flattened.last() {
|
|
|
|
if last.0.end < outer_span.end {
|
2022-12-03 09:44:12 +00:00
|
|
|
Some((Span::new(last.0.end, outer_span.end), FlatShape::Block))
|
2022-01-03 05:21:26 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
output.extend(flattened);
|
|
|
|
if let Some(last) = last {
|
|
|
|
output.push(last)
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
Expr::Call(call) => {
|
2023-04-05 19:34:47 +00:00
|
|
|
let mut output = vec![(call.head, FlatShape::InternalCall(call.decl_id))];
|
2021-10-13 17:53:27 +00:00
|
|
|
|
|
|
|
let mut args = vec![];
|
2022-04-09 02:55:02 +00:00
|
|
|
for positional in call.positional_iter() {
|
2021-10-13 17:53:27 +00:00
|
|
|
args.extend(flatten_expression(working_set, positional));
|
2021-07-24 05:57:17 +00:00
|
|
|
}
|
2022-04-09 02:55:02 +00:00
|
|
|
for named in call.named_iter() {
|
2021-10-13 17:53:27 +00:00
|
|
|
args.push((named.0.span, FlatShape::Flag));
|
2022-04-09 05:17:48 +00:00
|
|
|
if let Some(expr) = &named.2 {
|
2021-10-13 17:53:27 +00:00
|
|
|
args.extend(flatten_expression(working_set, expr));
|
2021-10-11 21:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-13 17:53:27 +00:00
|
|
|
// sort these since flags and positional args can be intermixed
|
|
|
|
args.sort();
|
|
|
|
|
|
|
|
output.extend(args);
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
|
|
|
}
|
Make external command substitution works friendly(like fish shell, trailing ending newlines) (#7156)
# Description
As title, when execute external sub command, auto-trimming end
new-lines, like how fish shell does.
And if the command is executed directly like: `cat tmp`, the result
won't change.
Fixes: #6816
Fixes: #3980
Note that although nushell works correctly by directly replace output of
external command to variable(or other places like string interpolation),
it's not friendly to user, and users almost want to use `str trim` to
trim trailing newline, I think that's why fish shell do this
automatically.
If the pr is ok, as a result, no more `str trim -r` is required when
user is writing scripts which using external commands.
# User-Facing Changes
Before:
<img width="523" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468810-86b04dbb-c147-459a-96a5-e0095eeaab3d.png">
After:
<img width="505" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468599-7b537488-3d6b-458e-9d75-d85780826db0.png">
# 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 --features=extra -- -D warnings -D
clippy::unwrap_used -A clippy::needless_collect` to check that you're
using the standard code style
- `cargo test --workspace --features=extra` 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.
2022-11-23 03:51:57 +00:00
|
|
|
Expr::ExternalCall(head, args, _) => {
|
2022-01-13 08:17:45 +00:00
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
match **head {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::String(..),
|
|
|
|
span,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
output.push((span, FlatShape::External));
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
output.extend(flatten_expression(working_set, head));
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 08:07:18 +00:00
|
|
|
|
|
|
|
for arg in args {
|
2021-10-08 21:51:47 +00:00
|
|
|
//output.push((*arg, FlatShape::ExternalArg));
|
|
|
|
match arg {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::String(..),
|
|
|
|
span,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
output.push((*span, FlatShape::ExternalArg));
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
output.extend(flatten_expression(working_set, arg));
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 08:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output
|
2021-09-02 08:25:22 +00:00
|
|
|
}
|
|
|
|
Expr::Garbage => {
|
|
|
|
vec![(expr.span, FlatShape::Garbage)]
|
|
|
|
}
|
2021-12-20 01:05:33 +00:00
|
|
|
Expr::Nothing => {
|
|
|
|
vec![(expr.span, FlatShape::Nothing)]
|
|
|
|
}
|
2022-02-24 02:02:48 +00:00
|
|
|
Expr::DateTime(_) => {
|
|
|
|
vec![(expr.span, FlatShape::DateTime)]
|
|
|
|
}
|
2022-02-28 23:31:53 +00:00
|
|
|
Expr::Binary(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Binary)]
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
Expr::Int(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Int)]
|
|
|
|
}
|
|
|
|
Expr::Float(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Float)]
|
2021-09-04 21:52:57 +00:00
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
Expr::MatchPattern(pattern) => {
|
|
|
|
// FIXME: do nicer flattening later
|
|
|
|
flatten_pattern(pattern)
|
|
|
|
}
|
|
|
|
Expr::MatchBlock(matches) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
for match_ in matches {
|
|
|
|
output.extend(flatten_pattern(&match_.0));
|
|
|
|
output.extend(flatten_expression(working_set, &match_.1));
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
2021-10-05 02:27:39 +00:00
|
|
|
Expr::ValueWithUnit(x, unit) => {
|
|
|
|
let mut output = flatten_expression(working_set, x);
|
|
|
|
output.push((unit.span, FlatShape::String));
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
2021-10-02 02:59:11 +00:00
|
|
|
Expr::CellPath(cell_path) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for path_element in &cell_path.members {
|
|
|
|
match path_element {
|
|
|
|
PathMember::String { span, .. } => output.push((*span, FlatShape::String)),
|
|
|
|
PathMember::Int { span, .. } => output.push((*span, FlatShape::Int)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
2021-09-26 18:39:19 +00:00
|
|
|
Expr::FullCellPath(cell_path) => {
|
2021-09-06 22:02:24 +00:00
|
|
|
let mut output = vec![];
|
2021-09-26 18:39:19 +00:00
|
|
|
output.extend(flatten_expression(working_set, &cell_path.head));
|
|
|
|
for path_element in &cell_path.tail {
|
2021-09-06 22:02:24 +00:00
|
|
|
match path_element {
|
|
|
|
PathMember::String { span, .. } => output.push((*span, FlatShape::String)),
|
|
|
|
PathMember::Int { span, .. } => output.push((*span, FlatShape::Int)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
2021-11-15 23:16:06 +00:00
|
|
|
Expr::ImportPattern(import_pattern) => {
|
|
|
|
let mut output = vec![(import_pattern.head.span, FlatShape::String)];
|
|
|
|
|
|
|
|
for member in &import_pattern.members {
|
|
|
|
match member {
|
|
|
|
ImportPatternMember::Glob { span } => output.push((*span, FlatShape::String)),
|
|
|
|
ImportPatternMember::Name { span, .. } => {
|
|
|
|
output.push((*span, FlatShape::String))
|
|
|
|
}
|
|
|
|
ImportPatternMember::List { names } => {
|
|
|
|
for (_, span) in names {
|
|
|
|
output.push((*span, FlatShape::String));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
2022-09-04 15:36:42 +00:00
|
|
|
Expr::Overlay(_) => {
|
|
|
|
vec![(expr.span, FlatShape::String)]
|
|
|
|
}
|
2021-09-11 11:13:04 +00:00
|
|
|
Expr::Range(from, next, to, op) => {
|
2021-09-04 21:52:57 +00:00
|
|
|
let mut output = vec![];
|
|
|
|
if let Some(f) = from {
|
|
|
|
output.extend(flatten_expression(working_set, f));
|
|
|
|
}
|
2021-09-11 11:13:04 +00:00
|
|
|
if let Some(s) = next {
|
|
|
|
output.extend(vec![(op.next_op_span, FlatShape::Operator)]);
|
|
|
|
output.extend(flatten_expression(working_set, s));
|
|
|
|
}
|
|
|
|
output.extend(vec![(op.span, FlatShape::Operator)]);
|
2021-09-04 21:52:57 +00:00
|
|
|
if let Some(t) = to {
|
|
|
|
output.extend(flatten_expression(working_set, t));
|
|
|
|
}
|
|
|
|
output
|
2021-09-02 08:25:22 +00:00
|
|
|
}
|
|
|
|
Expr::Bool(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Bool)]
|
|
|
|
}
|
2021-10-04 19:21:31 +00:00
|
|
|
Expr::Filepath(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Filepath)]
|
|
|
|
}
|
2022-04-22 20:18:51 +00:00
|
|
|
Expr::Directory(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Directory)]
|
|
|
|
}
|
2021-10-04 19:21:31 +00:00
|
|
|
Expr::GlobPattern(_) => {
|
|
|
|
vec![(expr.span, FlatShape::GlobPattern)]
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
Expr::List(list) => {
|
2022-01-03 03:18:23 +00:00
|
|
|
let outer_span = expr.span;
|
|
|
|
let mut last_end = outer_span.start;
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
let mut output = vec![];
|
|
|
|
for l in list {
|
2022-01-03 03:18:23 +00:00
|
|
|
let flattened = flatten_expression(working_set, l);
|
|
|
|
|
|
|
|
if let Some(first) = flattened.first() {
|
|
|
|
if first.0.start > last_end {
|
2022-12-03 09:44:12 +00:00
|
|
|
output.push((Span::new(last_end, first.0.start), FlatShape::List));
|
2022-01-03 03:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(last) = flattened.last() {
|
|
|
|
last_end = last.0.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
output.extend(flattened);
|
|
|
|
}
|
|
|
|
|
|
|
|
if last_end < outer_span.end {
|
2022-12-03 09:44:12 +00:00
|
|
|
output.push((Span::new(last_end, outer_span.end), FlatShape::List));
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
|
|
|
}
|
2021-12-25 20:50:02 +00:00
|
|
|
Expr::StringInterpolation(exprs) => {
|
2022-04-25 23:44:44 +00:00
|
|
|
let mut output = vec![];
|
2021-12-25 20:50:02 +00:00
|
|
|
for expr in exprs {
|
|
|
|
output.extend(flatten_expression(working_set, expr));
|
|
|
|
}
|
2022-04-25 23:44:44 +00:00
|
|
|
|
|
|
|
if let Some(first) = output.first() {
|
|
|
|
if first.0.start != expr.span.start {
|
|
|
|
// If we aren't a bare word interpolation, also highlight the outer quotes
|
|
|
|
output.insert(
|
|
|
|
0,
|
|
|
|
(
|
2022-12-03 09:44:12 +00:00
|
|
|
Span::new(expr.span.start, expr.span.start + 2),
|
2022-04-25 23:44:44 +00:00
|
|
|
FlatShape::StringInterpolation,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
output.push((
|
2022-12-03 09:44:12 +00:00
|
|
|
Span::new(expr.span.end - 1, expr.span.end),
|
2022-04-25 23:44:44 +00:00
|
|
|
FlatShape::StringInterpolation,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 20:50:02 +00:00
|
|
|
output
|
|
|
|
}
|
2021-11-10 23:14:00 +00:00
|
|
|
Expr::Record(list) => {
|
2022-01-03 05:21:26 +00:00
|
|
|
let outer_span = expr.span;
|
|
|
|
let mut last_end = outer_span.start;
|
|
|
|
|
2021-11-10 23:14:00 +00:00
|
|
|
let mut output = vec![];
|
|
|
|
for l in list {
|
2022-01-03 05:21:26 +00:00
|
|
|
let flattened_lhs = flatten_expression(working_set, &l.0);
|
|
|
|
let flattened_rhs = flatten_expression(working_set, &l.1);
|
|
|
|
|
|
|
|
if let Some(first) = flattened_lhs.first() {
|
|
|
|
if first.0.start > last_end {
|
2022-12-03 09:44:12 +00:00
|
|
|
output.push((Span::new(last_end, first.0.start), FlatShape::Record));
|
2022-01-03 05:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(last) = flattened_lhs.last() {
|
|
|
|
last_end = last.0.end;
|
|
|
|
}
|
|
|
|
output.extend(flattened_lhs);
|
|
|
|
|
|
|
|
if let Some(first) = flattened_rhs.first() {
|
|
|
|
if first.0.start > last_end {
|
2022-12-03 09:44:12 +00:00
|
|
|
output.push((Span::new(last_end, first.0.start), FlatShape::Record));
|
2022-01-03 05:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(last) = flattened_rhs.last() {
|
|
|
|
last_end = last.0.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
output.extend(flattened_rhs);
|
|
|
|
}
|
|
|
|
if last_end < outer_span.end {
|
2022-12-03 09:44:12 +00:00
|
|
|
output.push((Span::new(last_end, outer_span.end), FlatShape::Record));
|
2021-11-10 23:14:00 +00:00
|
|
|
}
|
2022-01-03 05:21:26 +00:00
|
|
|
|
2021-11-10 23:14:00 +00:00
|
|
|
output
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
Expr::Keyword(_, span, expr) => {
|
2023-04-05 19:34:47 +00:00
|
|
|
let mut output = vec![(*span, FlatShape::Keyword)];
|
2021-09-02 08:25:22 +00:00
|
|
|
output.extend(flatten_expression(working_set, expr));
|
|
|
|
output
|
|
|
|
}
|
|
|
|
Expr::Operator(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Operator)]
|
|
|
|
}
|
|
|
|
Expr::Signature(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Signature)]
|
|
|
|
}
|
|
|
|
Expr::String(_) => {
|
|
|
|
vec![(expr.span, FlatShape::String)]
|
|
|
|
}
|
|
|
|
Expr::Table(headers, cells) => {
|
2022-01-03 03:18:23 +00:00
|
|
|
let outer_span = expr.span;
|
|
|
|
let mut last_end = outer_span.start;
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
let mut output = vec![];
|
|
|
|
for e in headers {
|
2022-01-03 03:18:23 +00:00
|
|
|
let flattened = flatten_expression(working_set, e);
|
|
|
|
if let Some(first) = flattened.first() {
|
|
|
|
if first.0.start > last_end {
|
2022-12-03 09:44:12 +00:00
|
|
|
output.push((Span::new(last_end, first.0.start), FlatShape::Table));
|
2022-01-03 03:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(last) = flattened.last() {
|
|
|
|
last_end = last.0.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
output.extend(flattened);
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
for row in cells {
|
|
|
|
for expr in row {
|
2022-01-03 03:18:23 +00:00
|
|
|
let flattened = flatten_expression(working_set, expr);
|
|
|
|
if let Some(first) = flattened.first() {
|
|
|
|
if first.0.start > last_end {
|
2022-12-03 09:44:12 +00:00
|
|
|
output.push((Span::new(last_end, first.0.start), FlatShape::Table));
|
2022-01-03 03:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(last) = flattened.last() {
|
|
|
|
last_end = last.0.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
output.extend(flattened);
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 03:18:23 +00:00
|
|
|
|
|
|
|
if last_end < outer_span.end {
|
2022-12-03 09:44:12 +00:00
|
|
|
output.push((Span::new(last_end, outer_span.end), FlatShape::Table));
|
2022-01-03 03:18:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
|
|
|
}
|
2023-04-05 19:34:47 +00:00
|
|
|
Expr::Var(var_id) => {
|
|
|
|
vec![(expr.span, FlatShape::Variable(*var_id))]
|
|
|
|
}
|
|
|
|
Expr::VarDecl(var_id) => {
|
|
|
|
vec![(expr.span, FlatShape::VarDecl(*var_id))]
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
}
|
2021-07-22 19:50:59 +00:00
|
|
|
|
2022-11-18 21:46:48 +00:00
|
|
|
pub fn flatten_pipeline_element(
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
pipeline_element: &PipelineElement,
|
|
|
|
) -> Vec<(Span, FlatShape)> {
|
|
|
|
match pipeline_element {
|
2022-11-22 18:26:13 +00:00
|
|
|
PipelineElement::Expression(span, expr) => {
|
|
|
|
if let Some(span) = span {
|
|
|
|
let mut output = vec![(*span, FlatShape::Pipe)];
|
|
|
|
output.append(&mut flatten_expression(working_set, expr));
|
|
|
|
output
|
|
|
|
} else {
|
|
|
|
flatten_expression(working_set, expr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PipelineElement::Redirection(span, _, expr) => {
|
|
|
|
let mut output = vec![(*span, FlatShape::Redirection)];
|
|
|
|
output.append(&mut flatten_expression(working_set, expr));
|
|
|
|
output
|
|
|
|
}
|
Support redirect `err` and `out` to different streams (#7685)
# Description
Closes: #7364
# User-Facing Changes
Given the following shell script:
```bash
x=$(printf '=%.0s' {1..100})
echo $x
echo $x 1>&2
```
It supports the following command:
```
bash test.sh out> out.txt err> err.txt
```
Then both `out.txt` and `err.txt` will contain `=`(100 times)
## About the change
The core idea is that when doing lite-parsing, introduce a new variant
`LiteElement::SeparateRedirection` if we meet two Redirection
token(which is generated by `lex` function),
During converting from lite block to block,
`LiteElement::SeparateRedirection` will be converted to
`PipelineElement::SeparateRedirection`.
Then in the block eval process, if we get
`PipelineElement::SeparateRedirection`, we invoke `save` command with
`--stderr` arguments to acthive our behavior.
## What happened internally?
Take the following command as example:
```
^ls out> out.txt err> err.txt
```
lex parsing result(`Tokens`) are not changed, but `LiteBlock` and
`Block` is changed after this pr.
### LiteBlock before
```rust
LiteBlock {
block: [
LitePipeline { commands: [
Command(None, LiteCommand { comments: [], parts: [Span { start: 39041, end: 39044 }] }),
// actually the span of first Redirection is wrong too..
Redirection(Span { start: 39058, end: 39062 }, Stdout, LiteCommand { comments: [], parts: [Span { start: 39050, end: 39057 }] }),
Redirection(Span { start: 39058, end: 39062 }, Stderr, LiteCommand { comments: [], parts: [Span { start: 39063, end: 39070 }] })
]
}]
}
```
### LiteBlock after
```rust
LiteBlock {
block: [
LitePipeline { commands: [
Command(
None,
LiteCommand { comments: [], parts: [Span { start: 38525, end: 38528 }] }),
// new one! two Redirection merged into one SeparateRedirection.
SeparateRedirection {
out: (Span { start: 38529, end: 38533 }, LiteCommand { comments: [], parts: [Span { start: 38534, end: 38541 }] }),
err: (Span { start: 38542, end: 38546 }, LiteCommand { comments: [], parts: [Span { start: 38547, end: 38554 }] })
}
]
}]
}
```
### Block before
```rust
Pipeline {
elements: [
Expression(None, Expression {
expr: ExternalCall(Expression { expr: String("ls"), span: Span { start: 39042, end: 39044 }, ty: String, custom_completion: None }, [], false),
span: Span { start: 39041, end: 39044 },
ty: Any, custom_completion: None
}),
Redirection(Span { start: 39058, end: 39062 }, Stdout, Expression { expr: String("out.txt"), span: Span { start: 39050, end: 39057 }, ty: String, custom_completion: None }),
Redirection(Span { start: 39058, end: 39062 }, Stderr, Expression { expr: String("err.txt"), span: Span { start: 39063, end: 39070 }, ty: String, custom_completion: None })] }
```
### Block after
```rust
Pipeline {
elements: [
Expression(None, Expression {
expr: ExternalCall(Expression { expr: String("ls"), span: Span { start: 38526, end: 38528 }, ty: String, custom_completion: None }, [], false),
span: Span { start: 38525, end: 38528 },
ty: Any,
custom_completion: None
}),
// new one! SeparateRedirection
SeparateRedirection {
out: (Span { start: 38529, end: 38533 }, Expression { expr: String("out.txt"), span: Span { start: 38534, end: 38541 }, ty: String, custom_completion: None }),
err: (Span { start: 38542, end: 38546 }, Expression { expr: String("err.txt"), span: Span { start: 38547, end: 38554 }, ty: String, custom_completion: None })
}
]
}
```
# 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.
2023-01-12 09:22:30 +00:00
|
|
|
PipelineElement::SeparateRedirection {
|
|
|
|
out: (out_span, out_expr),
|
|
|
|
err: (err_span, err_expr),
|
|
|
|
} => {
|
|
|
|
let mut output = vec![(*out_span, FlatShape::Redirection)];
|
|
|
|
output.append(&mut flatten_expression(working_set, out_expr));
|
|
|
|
output.push((*err_span, FlatShape::Redirection));
|
|
|
|
output.append(&mut flatten_expression(working_set, err_expr));
|
|
|
|
output
|
|
|
|
}
|
2022-12-13 03:36:13 +00:00
|
|
|
PipelineElement::And(span, expr) => {
|
|
|
|
let mut output = vec![(*span, FlatShape::And)];
|
|
|
|
output.append(&mut flatten_expression(working_set, expr));
|
|
|
|
output
|
|
|
|
}
|
2022-11-22 18:26:13 +00:00
|
|
|
PipelineElement::Or(span, expr) => {
|
2022-12-13 03:36:13 +00:00
|
|
|
let mut output = vec![(*span, FlatShape::Or)];
|
2022-11-22 18:26:13 +00:00
|
|
|
output.append(&mut flatten_expression(working_set, expr));
|
|
|
|
output
|
|
|
|
}
|
2022-11-18 21:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
pub fn flatten_pipeline(
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
pipeline: &Pipeline,
|
|
|
|
) -> Vec<(Span, FlatShape)> {
|
|
|
|
let mut output = vec![];
|
2022-11-18 21:46:48 +00:00
|
|
|
for expr in &pipeline.elements {
|
|
|
|
output.extend(flatten_pipeline_element(working_set, expr))
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
|
|
|
|
pub fn flatten_pattern(match_pattern: &MatchPattern) -> Vec<(Span, FlatShape)> {
|
|
|
|
let mut output = vec![];
|
|
|
|
match &match_pattern.pattern {
|
|
|
|
Pattern::Garbage => {
|
|
|
|
output.push((match_pattern.span, FlatShape::Garbage));
|
|
|
|
}
|
|
|
|
Pattern::IgnoreValue => {
|
|
|
|
output.push((match_pattern.span, FlatShape::Nothing));
|
|
|
|
}
|
2023-03-30 22:08:53 +00:00
|
|
|
Pattern::IgnoreRest => {
|
|
|
|
output.push((match_pattern.span, FlatShape::Nothing));
|
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
Pattern::List(items) => {
|
|
|
|
if let Some(first) = items.first() {
|
|
|
|
if let Some(last) = items.last() {
|
|
|
|
output.push((
|
|
|
|
Span::new(match_pattern.span.start, first.span.start),
|
|
|
|
FlatShape::MatchPattern,
|
|
|
|
));
|
|
|
|
for item in items {
|
|
|
|
output.extend(flatten_pattern(item));
|
|
|
|
}
|
|
|
|
output.push((
|
|
|
|
Span::new(last.span.end, match_pattern.span.end),
|
|
|
|
FlatShape::MatchPattern,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output.push((match_pattern.span, FlatShape::MatchPattern));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Pattern::Record(items) => {
|
|
|
|
if let Some(first) = items.first() {
|
|
|
|
if let Some(last) = items.last() {
|
|
|
|
output.push((
|
|
|
|
Span::new(match_pattern.span.start, first.1.span.start),
|
|
|
|
FlatShape::MatchPattern,
|
|
|
|
));
|
|
|
|
for item in items {
|
|
|
|
output.extend(flatten_pattern(&item.1));
|
|
|
|
}
|
|
|
|
output.push((
|
|
|
|
Span::new(last.1.span.end, match_pattern.span.end),
|
|
|
|
FlatShape::MatchPattern,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output.push((match_pattern.span, FlatShape::MatchPattern));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Pattern::Value(_) => {
|
|
|
|
output.push((match_pattern.span, FlatShape::MatchPattern));
|
|
|
|
}
|
2023-04-05 19:34:47 +00:00
|
|
|
Pattern::Variable(var_id) => {
|
|
|
|
output.push((match_pattern.span, FlatShape::VarDecl(*var_id)));
|
2023-03-24 01:52:01 +00:00
|
|
|
}
|
2023-04-05 19:34:47 +00:00
|
|
|
Pattern::Rest(var_id) => {
|
|
|
|
output.push((match_pattern.span, FlatShape::VarDecl(*var_id)));
|
2023-03-30 22:08:53 +00:00
|
|
|
}
|
2023-03-26 22:31:57 +00:00
|
|
|
Pattern::Or(patterns) => {
|
|
|
|
for pattern in patterns {
|
|
|
|
output.extend(flatten_pattern(pattern));
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|