summaryrefslogtreecommitdiff
path: root/src/widgets/luaeditor/mod.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-07-25 17:09:09 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-07-25 17:09:09 +0200
commitdaa65dd89ec432522482729261d81e916e645ade (patch)
treecc16c222087de6f5545d9e6a3f300a66ad36f1ab /src/widgets/luaeditor/mod.rs
parent20e7569e2bf11d110d5afd2a99eacdbd65a7e055 (diff)
editor add scrolling
Diffstat (limited to 'src/widgets/luaeditor/mod.rs')
-rw-r--r--src/widgets/luaeditor/mod.rs44
1 files changed, 21 insertions, 23 deletions
diff --git a/src/widgets/luaeditor/mod.rs b/src/widgets/luaeditor/mod.rs
index 9a73e6e..63e3d65 100644
--- a/src/widgets/luaeditor/mod.rs
+++ b/src/widgets/luaeditor/mod.rs
@@ -96,35 +96,25 @@ impl Widget for &mut LuaEditor<'_> {
let highlights = treesitter::highlighter_split(text.as_bytes(), &self.highlight_config);
let mut span_area = inner_area.clone();
+ let mut current_line = 0;
- 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 {
+ for (hl, group) in highlights.iter() {
+ if *group == "\n" {
+ if current_line >= self.scroll {
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;
- }
+ 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);
+ }
- if group.ends_with("\n") {
- span_area.y += 1;
- span_area.x = inner_area.x;
+ span_area.x += group.len() as u16;
}
}
@@ -145,6 +135,14 @@ impl Widget for &mut LuaEditor<'_> {
}
}
+ 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()