nushell/crates/nu-cli/src/syntax_highlight.rs

219 lines
8.1 KiB
Rust
Raw Normal View History

2022-01-01 22:42:50 +01:00
use log::trace;
2021-07-22 21:50:59 +02:00
use nu_ansi_term::Style;
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};
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,
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 {
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);
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 {
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((
// nushell Garbage
get_shape_color(shape.1.to_string(), &self.config),
2021-07-22 21:50:59 +02:00
next_token,
)),
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,
))
}
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 => {
// nushell Int
output.push((
get_shape_color(shape.1.to_string(), &self.config),
next_token,
))
2021-07-22 21:50:59 +02:00
}
2021-08-08 22:21:21 +02:00
FlatShape::Float => {
// nushell Decimal
output.push((
get_shape_color(shape.1.to_string(), &self.config),
next_token,
))
2021-08-08 22:21:21 +02:00
}
FlatShape::Range => output.push((
// nushell DotDot ?
get_shape_color(shape.1.to_string(), &self.config),
next_token,
)),
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 => {
// nushell ?
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((
// nushell Operator
get_shape_color(shape.1.to_string(), &self.config),
2021-07-22 21:50:59 +02:00
next_token,
)),
FlatShape::Signature => output.push((
// nushell ?
get_shape_color(shape.1.to_string(), &self.config),
2021-07-22 21:50:59 +02:00
next_token,
)),
FlatShape::String => {
// nushell String
output.push((
get_shape_color(shape.1.to_string(), &self.config),
next_token,
))
2021-10-11 23:17:45 +02:00
}
FlatShape::StringInterpolation => {
// nushell ???
output.push((
get_shape_color(shape.1.to_string(), &self.config),
next_token,
))
}
FlatShape::DateTime => {
// nushell ???
output.push((
get_shape_color(shape.1.to_string(), &self.config),
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,
))
}
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((
// nushell Path
get_shape_color(shape.1.to_string(), &self.config),
2021-10-04 21:21:31 +02:00
next_token,
)),
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((
// nushell GlobPattern
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((
// nushell Variable
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
}