summaryrefslogtreecommitdiff
path: root/src/widgets/luaeditor/mod.rs
blob: 4fb8a3bba277523a389b21943fc7698286438f85 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use ratatui::{
    crossterm::event::{KeyCode, KeyEvent},
    layout::Rect,
    text::{ToLine, ToSpan},
    widgets::{Paragraph, Widget},
};
use tree_sitter_highlight::HighlightConfiguration;

use crate::{config::keymap::EditorKeyMap, lua};
use crate::{
    config::{theme::editor::bar::EditorBarTheme, GlobalConfig},
    state::{editor::bar::EditorBarState, GlobalState},
    tuicursor::TuiCursor,
};

use super::statusbar::StatusBar;

pub mod theme;
pub mod treesitter;

pub struct LuaEditor {
    bar: StatusBar,
    scroll: usize,
    highlight_config: HighlightConfiguration,
}

impl LuaEditor {
    pub fn new() -> Self {
        Self {
            bar: StatusBar::new(),
            scroll: 0,
            highlight_config: treesitter::new_highlight_configuration(),
        }
    }

    pub fn handle_key_event(&mut self, event: KeyEvent) {
        let r = EditorKeyMap::handle(event);

        if r.unwrap_or(true) {
            match event.code {
                KeyCode::Char(c) => GlobalState::instance_mut().editor.buffer.insert(c),
                KeyCode::Backspace => {
                    GlobalState::instance_mut().editor.buffer.delete();
                }
                KeyCode::Enter => {
                    GlobalState::instance_mut().editor.buffer.insert('\n');
                }
                _ => {}
            }
        }
    }

    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;
        let x = buffer.cursor().x()
            + buffer.current_line()[..buffer.cursor().x()]
                .chars()
                .filter(|c| *c == '\t')
                .count();

        Some(TuiCursor {
            position: (buffer.cursor().y() as u16, x as u16 + nr_width),
            style: state.editor.cursor_shape.into(),
        })
    }
}

impl Widget for &mut LuaEditor {
    fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
    where
        Self: Sized,
    {
        {
            let state = GlobalState::instance();
            let buffer = &state.editor.buffer;

            let config = GlobalConfig::instance();
            let theme = &config.theme.editor;

            let inner_area = self.bar.area(area);

            let text = buffer.as_string();
            let highlights = treesitter::highlighter_split(text.as_bytes(), &self.highlight_config);

            let nr_width = (buffer.lines().len().to_string().len() + 1).max(4) as u16;
            let mut text_area = inner_area;
            text_area.x += nr_width;

            let mut span_area = text_area;
            let mut current_line = 0;

            theme
                .background
                .get((), &lua::get())
                .unwrap_or_default()
                .apply(Paragraph::default())
                .render(area, buf);

            theme
                .cursor_line
                .get((), &lua::get())
                .unwrap_or_default()
                .apply(Paragraph::default())
                .render(
                    Rect::new(
                        text_area.x,
                        text_area.y + buffer.cursor().y() as u16,
                        text_area.width,
                        1,
                    ),
                    buf,
                );

            for (hl, group) in highlights.iter() {
                if *group == "\n" {
                    if current_line >= self.scroll {
                        span_area.y += 1;
                        span_area.x = text_area.x;
                    }

                    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.highlight.highlight(*hl, span).render(span_area, buf);
                    }

                    span_area.x += group.len() as u16;
                }
            }

            for i in self.scroll..buffer.lines().len() {
                let mut nr_area = span_area;
                nr_area.x = inner_area.x;
                nr_area.width = nr_width;
                nr_area.y = inner_area.y + (i - self.scroll) as u16;
                nr_area.height = 1;

                if !inner_area.contains(nr_area.into()) {
                    break;
                }

                if buffer.cursor().y() == i {
                    theme
                        .active_line_number
                        .get((), &lua::get())
                        .unwrap_or_default()
                } else {
                    theme.line_number.get((), &lua::get()).unwrap_or_default()
                }
                .apply(((i + 1).to_string() + " ").to_line())
                .right_aligned()
                .render(nr_area, buf);
            }
        }

        EditorBarState::apply(&mut self.bar, (), &lua::get());
        EditorBarTheme::apply(&mut self.bar, (), &lua::get());
        self.bar.render(area, buf);
    }
}

impl Default for LuaEditor {
    fn default() -> Self {
        Self {
            highlight_config: treesitter::new_highlight_configuration(),
            bar: StatusBar::default(),
            scroll: 0,
        }
    }
}