summaryrefslogtreecommitdiff
path: root/src/widgets/luaeditor/mod.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-08-02 18:09:23 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-08-02 18:09:23 +0200
commitde9ad07b2a4737713f1473641fe195d7e3023928 (patch)
tree38f3bdb122b6e94fe45dbfa2264314c7a7f26be8 /src/widgets/luaeditor/mod.rs
parent29ab8b40dc6976687ffb8bfbf663314b0ec3c46e (diff)
add tui-cursor handling
Diffstat (limited to 'src/widgets/luaeditor/mod.rs')
-rw-r--r--src/widgets/luaeditor/mod.rs52
1 files changed, 13 insertions, 39 deletions
diff --git a/src/widgets/luaeditor/mod.rs b/src/widgets/luaeditor/mod.rs
index 3ce9ab5..0bcffc8 100644
--- a/src/widgets/luaeditor/mod.rs
+++ b/src/widgets/luaeditor/mod.rs
@@ -1,12 +1,9 @@
use ratatui::{
- crossterm::event::{KeyCode, KeyEvent, KeyModifiers},
- style::Stylize,
- text::{ToLine, ToSpan},
- widgets::Widget,
+ crossterm::event::{KeyCode, KeyEvent, KeyModifiers}, text::{ToLine, ToSpan}, widgets::Widget
};
use tree_sitter_highlight::HighlightConfiguration;
-use crate::state::{editor::EditorState, window::Window, GlobalState};
+use crate::{state::{editor::EditorState, window::Window, GlobalState}, tuicursor::TuiCursor};
use crate::{cursor::CursorMove, lua};
use super::statusbar::StatusBar;
@@ -83,6 +80,17 @@ impl LuaEditor {
KeyCode::Modifier(_) => {}
}
}
+
+ pub fn render_cursor(&self) -> Option<TuiCursor> {
+ let state = GlobalState::instance();
+ let buffer = &state.editor.buffer;
+ let nr_width = (buffer.lines().len().to_string().len() + 1).max(4) as u16;
+
+ Some(TuiCursor {
+ position: (buffer.cursor().y() as u16, buffer.cursor().x() as u16 + nr_width),
+ style: ratatui::crossterm::cursor::SetCursorStyle::SteadyBar,
+ })
+ }
}
impl Widget for &mut LuaEditor {
@@ -139,40 +147,6 @@ impl Widget for &mut LuaEditor {
(i + 1).to_line().right_aligned().render(nr_area, buf);
}
-
- let mut cursor_area = text_area;
- cursor_area.width = 1;
- cursor_area.height = 1;
- cursor_area.y += buffer.cursor().y() as u16;
- cursor_area.x += buffer.cursor().x() as u16;
-
- let (first, _) = buffer.current_line().split_at(buffer.cursor().x());
-
- for c in first.chars() {
- if c == '\t' {
- cursor_area.x += 1;
- }
- }
-
- if self.scroll > buffer.cursor().y() {
- self.scroll = buffer.cursor().y();
- } else if inner_area.height as usize + self.scroll - 1 < buffer.cursor().y() {
- self.scroll = buffer.cursor().y() - inner_area.height as usize + 1;
- }
-
- cursor_area.y -= self.scroll as u16;
-
- if inner_area.contains(cursor_area.into()) {
- buffer
- .current_line()
- .chars()
- .nth(buffer.cursor().x())
- .map(|c| if c == '\t' { ' ' } else { c })
- .unwrap_or(' ')
- .to_span()
- .reversed()
- .render(cursor_area, buf);
- }
}
}