summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/widgets/luaeditor/mod.rs44
-rw-r--r--src/widgets/luaeditor/treesitter.rs11
2 files changed, 31 insertions, 24 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()
diff --git a/src/widgets/luaeditor/treesitter.rs b/src/widgets/luaeditor/treesitter.rs
index 57013f9..1a4dc04 100644
--- a/src/widgets/luaeditor/treesitter.rs
+++ b/src/widgets/luaeditor/treesitter.rs
@@ -64,7 +64,16 @@ pub fn highlighter_split<'a>(
for event in highlights {
match event.unwrap() {
HighlightEvent::Source { start, end } => {
- splits.push((current, std::str::from_utf8(&s[start..end]).unwrap()));
+ let group = std::str::from_utf8(&s[start..end]).unwrap();
+
+ for line in group.lines() {
+ splits.push((current, line));
+ splits.push((current, "\n"));
+ }
+
+ if !group.ends_with("\n") {
+ splits.pop();
+ }
}
HighlightEvent::HighlightStart(s) => current = Some(s),
HighlightEvent::HighlightEnd => current = None,