2022-01-01 22:42:50 +01:00
|
|
|
use log::trace;
|
2021-07-22 21:50:59 +02:00
|
|
|
use nu_ansi_term::Style;
|
2021-12-16 13:17:29 +01:00
|
|
|
use nu_color_config::get_shape_color;
|
2021-09-06 22:41:30 +02:00
|
|
|
use nu_parser::{flatten_block, parse, FlatShape};
|
2021-09-02 20:21:37 +02:00
|
|
|
use nu_protocol::engine::{EngineState, StateWorkingSet};
|
2021-12-16 13:17:29 +01:00
|
|
|
use nu_protocol::Config;
|
2021-07-22 21:50:59 +02:00
|
|
|
use reedline::{Highlighter, StyledText};
|
2021-07-17 08:31:34 +02:00
|
|
|
|
2021-07-22 21:50:59 +02:00
|
|
|
pub struct NuHighlighter {
|
2021-10-25 08:31:39 +02:00
|
|
|
pub engine_state: EngineState,
|
2021-12-16 13:17:29 +01:00
|
|
|
pub config: Config,
|
2021-07-22 09:48:45 +02:00
|
|
|
}
|
2021-07-17 08:31:34 +02:00
|
|
|
|
2021-07-22 21:50:59 +02:00
|
|
|
impl Highlighter for NuHighlighter {
|
2022-02-26 18:23:05 +01:00
|
|
|
fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
|
2022-01-01 22:42:50 +01:00
|
|
|
trace!("highlighting: {}", line);
|
|
|
|
|
2021-07-22 21:50:59 +02:00
|
|
|
let (shapes, global_span_offset) = {
|
2021-10-25 08:31:39 +02:00
|
|
|
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
2022-03-18 20:03:57 +01:00
|
|
|
let (block, _) = parse(&mut working_set, None, line.as_bytes(), false, &[]);
|
2021-07-17 08:31:34 +02:00
|
|
|
|
2021-09-02 20:21:37 +02:00
|
|
|
let shapes = flatten_block(&working_set, &block);
|
2021-10-25 08:31:39 +02:00
|
|
|
(shapes, self.engine_state.next_span_start())
|
2021-07-22 09:48:45 +02:00
|
|
|
};
|
2021-07-17 08:31:34 +02:00
|
|
|
|
2021-07-22 21:50:59 +02:00
|
|
|
let mut output = StyledText::default();
|
|
|
|
let mut last_seen_span = global_span_offset;
|
2021-07-22 09:48:45 +02:00
|
|
|
|
2021-07-22 21:50:59 +02:00
|
|
|
for shape in &shapes {
|
2022-01-28 13:29:45 +01:00
|
|
|
if shape.0.end <= last_seen_span
|
|
|
|
|| last_seen_span < global_span_offset
|
|
|
|
|| shape.0.start < global_span_offset
|
|
|
|
{
|
2021-07-22 21:50:59 +02:00
|
|
|
// We've already output something for this span
|
|
|
|
// so just skip this one
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if shape.0.start > last_seen_span {
|
|
|
|
let gap = line
|
|
|
|
[(last_seen_span - global_span_offset)..(shape.0.start - global_span_offset)]
|
|
|
|
.to_string();
|
|
|
|
output.push((Style::new(), gap));
|
|
|
|
}
|
|
|
|
let next_token = line
|
|
|
|
[(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)]
|
|
|
|
.to_string();
|
|
|
|
match shape.1 {
|
|
|
|
FlatShape::Garbage => output.push((
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell Garbage
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-07-22 21:50:59 +02:00
|
|
|
next_token,
|
|
|
|
)),
|
2021-12-20 02:05:33 +01:00
|
|
|
FlatShape::Nothing => output.push((
|
|
|
|
// nushell Nothing
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
)),
|
2022-03-01 00:31:53 +01:00
|
|
|
FlatShape::Binary => {
|
|
|
|
// nushell ?
|
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
|
|
|
}
|
2021-12-16 13:17:29 +01:00
|
|
|
FlatShape::Bool => {
|
|
|
|
// nushell ?
|
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
|
|
|
}
|
2021-07-22 21:50:59 +02:00
|
|
|
FlatShape::Int => {
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell Int
|
|
|
|
output.push((
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-11-10 18:55:10 +01:00
|
|
|
next_token,
|
|
|
|
))
|
2021-07-22 21:50:59 +02:00
|
|
|
}
|
2021-08-08 22:21:21 +02:00
|
|
|
FlatShape::Float => {
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell Decimal
|
|
|
|
output.push((
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-11-10 18:55:10 +01:00
|
|
|
next_token,
|
|
|
|
))
|
2021-08-08 22:21:21 +02:00
|
|
|
}
|
2021-09-04 23:52:57 +02:00
|
|
|
FlatShape::Range => output.push((
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell DotDot ?
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-09-04 23:52:57 +02:00
|
|
|
next_token,
|
|
|
|
)),
|
2021-12-16 13:17:29 +01:00
|
|
|
FlatShape::InternalCall => output.push((
|
|
|
|
// nushell InternalCommand
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
)),
|
|
|
|
FlatShape::External => {
|
|
|
|
// nushell ExternalCommand
|
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
FlatShape::ExternalArg => {
|
|
|
|
// nushell ExternalWord
|
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
2021-07-24 07:57:17 +02:00
|
|
|
}
|
2021-07-22 21:50:59 +02:00
|
|
|
FlatShape::Literal => {
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell ?
|
2021-12-16 13:17:29 +01:00
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
2021-07-22 21:50:59 +02:00
|
|
|
}
|
|
|
|
FlatShape::Operator => output.push((
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell Operator
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-07-22 21:50:59 +02:00
|
|
|
next_token,
|
|
|
|
)),
|
|
|
|
FlatShape::Signature => output.push((
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell ?
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-07-22 21:50:59 +02:00
|
|
|
next_token,
|
|
|
|
)),
|
2021-11-10 18:55:10 +01:00
|
|
|
FlatShape::String => {
|
|
|
|
// nushell String
|
|
|
|
output.push((
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-11-10 18:55:10 +01:00
|
|
|
next_token,
|
|
|
|
))
|
2021-10-11 23:17:45 +02:00
|
|
|
}
|
2021-12-25 21:50:02 +01:00
|
|
|
FlatShape::StringInterpolation => {
|
|
|
|
// nushell ???
|
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
|
|
|
}
|
2022-02-24 03:02:48 +01:00
|
|
|
FlatShape::DateTime => {
|
|
|
|
// nushell ???
|
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
|
|
|
}
|
2022-01-03 04:18:23 +01:00
|
|
|
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,
|
|
|
|
))
|
|
|
|
}
|
2022-01-03 06:21:26 +01:00
|
|
|
FlatShape::Record => {
|
|
|
|
// nushell ???
|
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
FlatShape::Block => {
|
|
|
|
// nushell ???
|
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
|
|
|
}
|
2021-10-04 21:21:31 +02:00
|
|
|
FlatShape::Filepath => output.push((
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell Path
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-10-04 21:21:31 +02:00
|
|
|
next_token,
|
|
|
|
)),
|
2022-04-22 22:18:51 +02:00
|
|
|
FlatShape::Directory => output.push((
|
|
|
|
// nushell Directory
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
)),
|
2021-10-04 21:21:31 +02:00
|
|
|
FlatShape::GlobPattern => output.push((
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell GlobPattern
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-10-04 21:21:31 +02:00
|
|
|
next_token,
|
|
|
|
)),
|
2021-07-22 21:50:59 +02:00
|
|
|
FlatShape::Variable => output.push((
|
2021-11-10 18:55:10 +01:00
|
|
|
// nushell Variable
|
2021-12-16 13:17:29 +01:00
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
)),
|
|
|
|
FlatShape::Flag => {
|
|
|
|
// nushell Flag
|
|
|
|
output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
|
|
|
next_token,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
FlatShape::Custom(..) => output.push((
|
|
|
|
get_shape_color(shape.1.to_string(), &self.config),
|
2021-07-22 21:50:59 +02:00
|
|
|
next_token,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
last_seen_span = shape.0.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
let remainder = line[(last_seen_span - global_span_offset)..].to_string();
|
|
|
|
if !remainder.is_empty() {
|
|
|
|
output.push((Style::new(), remainder));
|
|
|
|
}
|
2021-07-17 08:31:34 +02:00
|
|
|
|
2021-07-22 21:50:59 +02:00
|
|
|
output
|
2021-07-22 09:48:45 +02:00
|
|
|
}
|
2021-07-17 08:31:34 +02:00
|
|
|
}
|