Fix list and table print (#652)

* Fix list printing

* Fix list and table highlighting
This commit is contained in:
JT 2022-01-03 14:18:23 +11:00 committed by GitHub
parent 850f66aa9d
commit 9535e2c309
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 109 additions and 10 deletions

View File

@ -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),

View File

@ -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),

View File

@ -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(_) => {

View File

@ -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,
},