feat: implement basic highlighting (does not handle newlines yet)

pull/12706/head
Nikita Revenco 2025-01-27 21:55:35 +00:00
parent f4827c8abf
commit fa2de3222f
3 changed files with 93 additions and 33 deletions

View File

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

View File

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

4
lol.helix 100644
View File

@ -0,0 +1,4 @@
hello world #[|lol]#
hello world #[lol|]#
hello wor#(| hello world )#
#(lol|)#