From 9535e2c309a070abf8f1d67be4461ac6596d8d18 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Mon, 3 Jan 2022 14:18:23 +1100 Subject: [PATCH] Fix list and table print (#652) * Fix list printing * Fix list and table highlighting --- crates/nu-cli/src/syntax_highlight.rs | 14 ++++ crates/nu-color-config/src/shape_color.rs | 2 + crates/nu-parser/src/flatten.rs | 89 ++++++++++++++++++++++- crates/nu-parser/src/parser.rs | 14 ++-- 4 files changed, 109 insertions(+), 10 deletions(-) diff --git a/crates/nu-cli/src/syntax_highlight.rs b/crates/nu-cli/src/syntax_highlight.rs index e1a6a032e..b9c36d074 100644 --- a/crates/nu-cli/src/syntax_highlight.rs +++ b/crates/nu-cli/src/syntax_highlight.rs @@ -128,6 +128,20 @@ impl Highlighter for NuHighlighter { next_token, )) } + FlatShape::List => { + // nushell ??? + output.push(( + get_shape_color(shape.1.to_string(), &self.config), + next_token, + )) + } + FlatShape::Table => { + // nushell ??? + output.push(( + get_shape_color(shape.1.to_string(), &self.config), + next_token, + )) + } FlatShape::Filepath => output.push(( // nushell Path get_shape_color(shape.1.to_string(), &self.config), diff --git a/crates/nu-color-config/src/shape_color.rs b/crates/nu-color-config/src/shape_color.rs index f6fabe71a..6d1eb3c3f 100644 --- a/crates/nu-color-config/src/shape_color.rs +++ b/crates/nu-color-config/src/shape_color.rs @@ -19,6 +19,8 @@ pub fn get_shape_color(shape: String, conf: &Config) -> Style { "flatshape_signature" => Style::new().fg(Color::Green).bold(), "flatshape_string" => Style::new().fg(Color::Green), "flatshape_string_interpolation" => Style::new().fg(Color::Cyan).bold(), + "flatshape_list" => Style::new().fg(Color::Cyan).bold(), + "flatshape_table" => Style::new().fg(Color::Blue).bold(), "flatshape_filepath" => Style::new().fg(Color::Cyan), "flatshape_globpattern" => Style::new().fg(Color::Cyan).bold(), "flatshape_variable" => Style::new().fg(Color::Purple), diff --git a/crates/nu-parser/src/flatten.rs b/crates/nu-parser/src/flatten.rs index 1fc2f2e3a..d5ef2458f 100644 --- a/crates/nu-parser/src/flatten.rs +++ b/crates/nu-parser/src/flatten.rs @@ -20,6 +20,8 @@ pub enum FlatShape { Signature, String, StringInterpolation, + List, + Table, Filepath, GlobPattern, Variable, @@ -44,6 +46,8 @@ impl Display for FlatShape { FlatShape::Signature => write!(f, "flatshape_signature"), FlatShape::String => write!(f, "flatshape_string"), FlatShape::StringInterpolation => write!(f, "flatshape_string_interpolation"), + FlatShape::List => write!(f, "flatshape_string_interpolation"), + FlatShape::Table => write!(f, "flatshape_table"), FlatShape::Filepath => write!(f, "flatshape_filepath"), FlatShape::GlobPattern => write!(f, "flatshape_globpattern"), FlatShape::Variable => write!(f, "flatshape_variable"), @@ -211,9 +215,40 @@ pub fn flatten_expression( vec![(expr.span, FlatShape::GlobPattern)] } Expr::List(list) => { + let outer_span = expr.span; + let mut last_end = outer_span.start; + let mut output = vec![]; for l in list { - output.extend(flatten_expression(working_set, l)); + let flattened = flatten_expression(working_set, l); + + if let Some(first) = flattened.first() { + if first.0.start > last_end { + output.push(( + Span { + start: last_end, + end: first.0.start, + }, + FlatShape::List, + )); + } + } + + if let Some(last) = flattened.last() { + last_end = last.0.end; + } + + output.extend(flattened); + } + + if last_end < outer_span.end { + output.push(( + Span { + start: last_end, + end: outer_span.end, + }, + FlatShape::List, + )); } output } @@ -263,15 +298,63 @@ pub fn flatten_expression( flatten_block(working_set, working_set.get_block(*block_id)) } Expr::Table(headers, cells) => { + let outer_span = expr.span; + let mut last_end = outer_span.start; + let mut output = vec![]; for e in headers { - output.extend(flatten_expression(working_set, e)); + let flattened = flatten_expression(working_set, e); + if let Some(first) = flattened.first() { + if first.0.start > last_end { + output.push(( + Span { + start: last_end, + end: first.0.start, + }, + FlatShape::Table, + )); + } + } + + if let Some(last) = flattened.last() { + last_end = last.0.end; + } + + output.extend(flattened); } for row in cells { for expr in row { - output.extend(flatten_expression(working_set, expr)); + let flattened = flatten_expression(working_set, expr); + if let Some(first) = flattened.first() { + if first.0.start > last_end { + output.push(( + Span { + start: last_end, + end: first.0.start, + }, + FlatShape::Table, + )); + } + } + + if let Some(last) = flattened.last() { + last_end = last.0.end; + } + + output.extend(flattened); } } + + if last_end < outer_span.end { + output.push(( + Span { + start: last_end, + end: outer_span.end, + }, + FlatShape::Table, + )); + } + output } Expr::Var(_) | Expr::VarDecl(_) => { diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 65c73d0d6..046f91c4b 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2514,10 +2514,10 @@ pub fn parse_list_expression( error = error.or_else(|| Some(ParseError::Unclosed("]".into(), Span { start: end, end }))); } - let span = Span { start, end }; - let source = working_set.get_span_contents(span); + let inner_span = Span { start, end }; + let source = working_set.get_span_contents(inner_span); - let (output, err) = lex(source, span.start, &[b'\n', b'\r', b','], &[], true); + let (output, err) = lex(source, inner_span.start, &[b'\n', b'\r', b','], &[], true); error = error.or(err); let (output, err) = lite_parse(&output); @@ -2585,9 +2585,9 @@ pub fn parse_table_expression( error = error.or_else(|| Some(ParseError::Unclosed("]".into(), Span { start: end, end }))); } - let span = Span { start, end }; + let inner_span = Span { start, end }; - let source = working_set.get_span_contents(span); + let source = working_set.get_span_contents(inner_span); let (output, err) = lex(source, start, &[b'\n', b'\r', b','], &[], true); error = error.or(err); @@ -2599,7 +2599,7 @@ pub fn parse_table_expression( 0 => ( Expression { expr: Expr::List(vec![]), - span, + span: original_span, ty: Type::List(Box::new(Type::Unknown)), custom_completion: None, }, @@ -2665,7 +2665,7 @@ pub fn parse_table_expression( ( Expression { expr: Expr::Table(table_headers, rows), - span, + span: original_span, ty: Type::Table, custom_completion: None, },