1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
use std::str::FromStr;
use ratatui::{
crossterm::event::{KeyCode, KeyEvent},
prelude::BlockExt,
style::Stylize,
text::ToSpan,
widgets::{Block, 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;
pub struct LuaEditor<'a> {
block: Option<Block<'a>>,
scroll: usize,
buffer: Buffer,
highlight_config: HighlightConfiguration,
}
impl<'a> LuaEditor<'a> {
pub fn new<S>(content: S) -> Self
where
S: AsRef<str>,
{
Self {
block: None,
scroll: 0,
buffer: Buffer::from_str(content.as_ref()).unwrap(),
highlight_config: treesitter::new_highlight_configuration(),
}
}
pub fn block(mut self, block: Option<Block<'a>>) -> Self {
self.block = block;
self
}
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 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.block.render(area, buf);
let inner_area = self.block.inner_if_some(area);
let text = self.text();
let highlights = treesitter::highlighter_split(text.as_bytes(), &self.highlight_config);
let mut span_area = inner_area.clone();
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 {
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;
}
if group.ends_with("\n") {
span_area.y += 1;
span_area.x = inner_area.x;
}
}
let mut cursor_area = inner_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 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);
}
}
}
|