use ratatui::{ crossterm::event::{KeyCode, KeyEvent}, style::{Style, Stylize}, text::{ToLine, ToSpan}, widgets::Widget, }; pub mod buffer; pub mod cursor; pub mod theme; pub mod treesitter; use buffer::Buffer; use tree_sitter_highlight::HighlightConfiguration; use self::cursor::CursorMove; use super::statusbar::StatusBar; pub struct LuaEditor { bar: StatusBar, scroll: usize, buffer: Buffer, highlight_config: HighlightConfiguration, } impl LuaEditor { pub fn new() -> Self { Self { bar: StatusBar::new() .left(" [No Name] ") .left_style(Style::default().on_magenta()), scroll: 0, buffer: Buffer::new(), highlight_config: treesitter::new_highlight_configuration(), } } pub fn handle_key_event(&mut self, event: KeyEvent) { match event.code { KeyCode::Char(c) => self.buffer.insert(c), KeyCode::Backspace => { self.buffer.delete(); } KeyCode::Enter => { self.buffer.insert('\n'); } KeyCode::Left => self.buffer.move_cursor(CursorMove::Left(1)), KeyCode::Right => self.buffer.move_cursor(CursorMove::Right(1)), KeyCode::Up => self.buffer.move_cursor(CursorMove::Up(1)), KeyCode::Down => self.buffer.move_cursor(CursorMove::Down(1)), KeyCode::Home => {} KeyCode::End => {} KeyCode::PageUp => {} KeyCode::PageDown => {} KeyCode::Tab => self.buffer.insert('\t'), KeyCode::BackTab => {} KeyCode::Delete => {} KeyCode::Insert => {} KeyCode::F(_) => {} KeyCode::Null => {} KeyCode::Esc => {} KeyCode::CapsLock => {} KeyCode::ScrollLock => {} KeyCode::NumLock => {} KeyCode::PrintScreen => {} KeyCode::Pause => {} KeyCode::Menu => {} KeyCode::KeypadBegin => {} KeyCode::Media(_) => {} KeyCode::Modifier(_) => {} } } pub fn set_text(&mut self, content: S) where S: AsRef, { self.buffer .set_lines(content.as_ref().lines().map(|s| s.to_string()).collect()) } pub fn text(&self) -> String { self.buffer.lines().join("\n") } } impl Widget for &mut LuaEditor { fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) where Self: Sized, { self.bar.render(area, buf); let inner_area = self.bar.area(area); let text = self.text(); let highlights = treesitter::highlighter_split(text.as_bytes(), &self.highlight_config); let nr_width = (self.buffer.lines().len().to_string().len() + 1).max(4) as u16; let mut text_area = inner_area.clone(); text_area.x += nr_width; let mut span_area = text_area.clone(); let mut current_line = 0; for (hl, group) in highlights.iter() { if *group == "\n" { if current_line >= self.scroll { span_area.y += 1; span_area.x = text_area.x; } current_line += 1; } else if current_line >= self.scroll { let group = group.replace("\t", " "); let span = group.to_span(); if inner_area.contains(span_area.into()) { theme::theme_highlight_group(*hl, span).render(span_area, buf); } span_area.x += group.len() as u16; } } for i in self.scroll..self.buffer.lines().len() { let mut nr_area = span_area.clone(); nr_area.x = inner_area.x; nr_area.width = nr_width - 1; nr_area.y = inner_area.y + (i - self.scroll) as u16; nr_area.height = 1; if !inner_area.contains(nr_area.into()) { break; } (i + 1).to_line().right_aligned().render(nr_area, buf); } let mut cursor_area = text_area.clone(); cursor_area.width = 1; cursor_area.height = 1; cursor_area.y += self.buffer.cursor().y() as u16; cursor_area.x += self.buffer.cursor().x() as u16; let (first, _) = self .buffer .current_line() .split_at(self.buffer.cursor().x() as usize); for c in first.chars() { if c == '\t' { cursor_area.x += 1; } } if self.scroll > self.buffer.cursor().y() { self.scroll = self.buffer.cursor().y(); } else if inner_area.height as usize + self.scroll - 1 < self.buffer.cursor().y() { self.scroll = self.buffer.cursor().y() - inner_area.height as usize + 1; } cursor_area.y -= self.scroll as u16; 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); } } }