diff options
Diffstat (limited to 'src/widgets/luaeditor/mod.rs')
| -rw-r--r-- | src/widgets/luaeditor/mod.rs | 62 |
1 files changed, 45 insertions, 17 deletions
diff --git a/src/widgets/luaeditor/mod.rs b/src/widgets/luaeditor/mod.rs index 723d8b5..9a73e6e 100644 --- a/src/widgets/luaeditor/mod.rs +++ b/src/widgets/luaeditor/mod.rs @@ -10,16 +10,19 @@ use ratatui::{ pub mod buffer; pub mod cursor; +pub mod theme; +pub mod treesitter; use buffer::Buffer; +use tree_sitter_highlight::HighlightConfiguration; use self::cursor::CursorMove; -#[derive(Debug)] pub struct LuaEditor<'a> { block: Option<Block<'a>>, scroll: usize, buffer: Buffer, + highlight_config: HighlightConfiguration, } impl<'a> LuaEditor<'a> { @@ -31,6 +34,7 @@ impl<'a> LuaEditor<'a> { block: None, scroll: 0, buffer: Buffer::from_str(content.as_ref()).unwrap(), + highlight_config: treesitter::new_highlight_configuration(), } } @@ -88,18 +92,40 @@ impl Widget for &mut LuaEditor<'_> { self.block.render(area, buf); let inner_area = self.block.inner_if_some(area); - for (i, line) in self.buffer.lines().iter().enumerate().skip(self.scroll) { - let replace = &line.replace('\t', " "); - let span = replace.to_span(); - let mut span_area = inner_area.clone(); - span_area.height = 1; - span_area.y += i as u16; + let text = self.text(); + let highlights = treesitter::highlighter_split(text.as_bytes(), &self.highlight_config); + + let mut span_area = inner_area.clone(); + + for (hl, group) in highlights.iter().skip(self.scroll) { + let lines: Vec<_> = group.lines().collect(); + + for (i, line) in lines.iter().enumerate() { + let line = line.replace("\t", " "); + let span = line.to_span(); + + if !inner_area.contains(span_area.into()) { + break; + } + + theme::theme_highlight_group(*hl, span).render(span_area, buf); + + if i < lines.len() - 1 { + span_area.y += 1; + span_area.x = inner_area.x; + } else { + span_area.x += line.len() as u16; + } + } if !inner_area.contains(span_area.into()) { break; } - span.render(span_area, buf) + if group.ends_with("\n") { + span_area.y += 1; + span_area.x = inner_area.x; + } } let mut cursor_area = inner_area.clone(); @@ -119,14 +145,16 @@ impl Widget for &mut LuaEditor<'_> { } } - self.buffer - .current_line() - .chars() - .nth(self.buffer.cursor().x() as usize) - .map(|c| if c == '\t' { ' ' } else { c }) - .unwrap_or(' ') - .to_span() - .reversed() - .render(cursor_area, buf); + if inner_area.contains(cursor_area.into()) { + self.buffer + .current_line() + .chars() + .nth(self.buffer.cursor().x() as usize) + .map(|c| if c == '\t' { ' ' } else { c }) + .unwrap_or(' ') + .to_span() + .reversed() + .render(cursor_area, buf); + } } } |