diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-07-25 17:09:09 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-07-25 17:09:09 +0200 |
| commit | daa65dd89ec432522482729261d81e916e645ade (patch) | |
| tree | cc16c222087de6f5545d9e6a3f300a66ad36f1ab /src/widgets/luaeditor | |
| parent | 20e7569e2bf11d110d5afd2a99eacdbd65a7e055 (diff) | |
editor add scrolling
Diffstat (limited to 'src/widgets/luaeditor')
| -rw-r--r-- | src/widgets/luaeditor/mod.rs | 44 | ||||
| -rw-r--r-- | src/widgets/luaeditor/treesitter.rs | 11 |
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, |