mirror of https://github.com/helix-editor/helix
feat: implement basic highlighting (does not handle newlines yet)
parent
f4827c8abf
commit
fa2de3222f
|
@ -1,3 +1,11 @@
|
||||||
|
/// ```helix
|
||||||
|
/// #(|hello world)#
|
||||||
|
/// #(|hello world)#
|
||||||
|
/// #[hello world|]#
|
||||||
|
/// #(|hello world)#
|
||||||
|
/// ```
|
||||||
|
fn a() {}
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
commands::{self, OnKeyCallback, OnKeyCallbackKind},
|
commands::{self, OnKeyCallback, OnKeyCallbackKind},
|
||||||
compositor::{Component, Context, Event, EventResult},
|
compositor::{Component, Context, Event, EventResult},
|
||||||
|
|
|
@ -5,13 +5,14 @@ use tui::{
|
||||||
text::{Span, Spans, Text},
|
text::{Span, Spans, Text},
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::{cmp::Ordering, collections::HashSet, sync::Arc};
|
||||||
|
|
||||||
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};
|
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};
|
||||||
|
|
||||||
use helix_core::{
|
use helix_core::{
|
||||||
syntax::{self, HighlightEvent, InjectionLanguageMarker, Syntax},
|
syntax::{self, HighlightEvent, InjectionLanguageMarker, Syntax},
|
||||||
RopeSlice,
|
test::print,
|
||||||
|
Rope, RopeSlice,
|
||||||
};
|
};
|
||||||
use helix_view::{
|
use helix_view::{
|
||||||
graphics::{Margin, Rect, Style},
|
graphics::{Margin, Rect, Style},
|
||||||
|
@ -71,6 +72,52 @@ pub fn highlighted_code_block<'a>(
|
||||||
Box::new(highlight_iter)
|
Box::new(highlight_iter)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if language == "helix" {
|
||||||
|
let (text, selections) = print(text);
|
||||||
|
// let text = Rope::from(text).slice(..);
|
||||||
|
|
||||||
|
let style_cursor = get_theme("ui.cursor");
|
||||||
|
let style_cursor_primary = get_theme("ui.cursor.primary");
|
||||||
|
let style_selection = get_theme("ui.selection");
|
||||||
|
let style_selection_primary = get_theme("ui.selection.primary");
|
||||||
|
let style_text = get_theme("ui.text");
|
||||||
|
|
||||||
|
let mut ranges2 = HashSet::new();
|
||||||
|
let mut cursors = HashSet::new();
|
||||||
|
let primary_idx = selections.primary_index();
|
||||||
|
|
||||||
|
for range in selections.iter() {
|
||||||
|
ranges2.extend(range.from()..range.to());
|
||||||
|
cursors.insert(if range.head > range.anchor {
|
||||||
|
range.head.saturating_sub(1)
|
||||||
|
} else {
|
||||||
|
range.head
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (idx, ch) in text.chars().enumerate() {
|
||||||
|
let is_cursor = cursors.contains(&idx);
|
||||||
|
let is_selection = ranges2.contains(&idx);
|
||||||
|
|
||||||
|
let style = if is_cursor {
|
||||||
|
if idx == primary_idx {
|
||||||
|
style_cursor_primary
|
||||||
|
} else {
|
||||||
|
style_cursor
|
||||||
|
}
|
||||||
|
} else if is_selection {
|
||||||
|
if idx == primary_idx {
|
||||||
|
style_selection_primary
|
||||||
|
} else {
|
||||||
|
style_selection
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
style_text
|
||||||
|
};
|
||||||
|
|
||||||
|
spans.push(Span::styled(ch.to_string(), style));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
let mut highlights = Vec::new();
|
let mut highlights = Vec::new();
|
||||||
for event in highlight_iter {
|
for event in highlight_iter {
|
||||||
match event {
|
match event {
|
||||||
|
@ -111,6 +158,7 @@ pub fn highlighted_code_block<'a>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !spans.is_empty() {
|
if !spans.is_empty() {
|
||||||
let spans = std::mem::take(&mut spans);
|
let spans = std::mem::take(&mut spans);
|
||||||
|
|
Loading…
Reference in New Issue