Skip to main content

ratatui_code_editor/
render.rs

1use ratatui_core::{widgets::Widget};
2use ratatui_core::buffer::Buffer;
3use ratatui_core::layout::Rect;
4use ratatui_core::style::{Color, Style};
5use crate::editor::Editor;
6use crate::code::{
7    RopeGraphemes, grapheme_width_and_chars_len, grapheme_width_and_bytes_len
8};
9
10/// Draws the main editor view in the provided area using the ratatui rendering buffer.
11///
12/// Renders the main editor view in four distinct layers:
13/// 1. Line numbers and text content are drawn in the visible viewport.
14/// 2. Syntax highlighting is overlaid on top of the text.
15/// 3. The selection highlight is rendered above the syntax layer.
16/// 4. User marks are rendered as the final uppermost overlay.
17///
18/// # Arguments
19///
20/// * `self` - The `Editor` instance (as reference) to render.
21/// * `area` - The rectangular area on the terminal to draw within.
22/// * `buf` - The ratatui `Buffer` that represents the screen cells to draw to.
23/// 
24impl Widget for &Editor {
25    fn render(self, area: Rect, buf: &mut Buffer) {
26        let code = self.code_ref();
27        let total_chars = code.len_chars();
28
29        let total_lines = code.len_lines();
30        let max_line_number = total_lines.max(1);
31        let line_number_digits = max_line_number.to_string().len().max(5);
32        let line_number_width = self.get_line_number_width();
33
34        let mut draw_y = area.top();
35        
36        let line_number_style = Style::default().fg(Color::DarkGray);
37        let default_text_style = Style::default().fg(Color::White);
38
39        // draw line numbers and text
40        for line_idx in self.offset_y..total_lines {
41            if draw_y >= area.bottom() { break }
42            if self.show_line_numbers {
43                let line_number = format!("{:>width$}", line_idx + 1, width = line_number_digits);
44                buf.set_string(area.left(), draw_y, &line_number, line_number_style);
45            }
46            let line_len = code.line_len(line_idx);
47            let max_x = (area.width as usize).saturating_sub(line_number_width);
48        
49            let start_col = self.offset_x.min(line_len);
50            let end_col = (start_col + max_x).min(line_len);
51        
52            let line_start_char = code.line_to_char(line_idx);
53            let char_start = line_start_char + start_col;
54            let char_end = line_start_char + end_col;
55        
56            let visible_chars = code.char_slice(char_start, char_end);
57
58            let displayed_line = visible_chars.to_string().replace("\t", &" ");
59        
60            let text_x = area.left() + line_number_width as u16;
61            if text_x < area.left() + area.width && draw_y < area.top() + area.height {
62                buf.set_string(text_x, draw_y, &displayed_line, default_text_style);
63            }
64        
65            draw_y += 1;
66        }
67
68        // draw syntax highlighting
69        if code.is_highlight() {
70            
71            // Render syntax highlighting for the visible portion of the text buffer.
72            // For each visible line within the viewport, limit the highlighting to the
73            // visible columns to avoid expensive processing of long lines outside the view.
74            // This improves performance by only querying Tree-sitter for the visible slice,
75            // then applying styles per character based on byte ranges returned by the syntax query.
76
77            for screen_y in 0..(area.height as usize) {
78                let line_idx = self.offset_y + screen_y;
79                if line_idx >= total_lines { break }
80            
81                let line_len = code.line_len(line_idx);
82                let max_x = (area.width as usize).saturating_sub(line_number_width);
83            
84                let line_start_char = code.line_to_char(line_idx);
85                let start_char = line_start_char + self.offset_x;
86                let visible_len = line_len.saturating_sub(self.offset_x);
87                let end = max_x.min(visible_len);
88                let end_char = start_char + end;
89
90                if start_char > total_chars || end_char > total_chars {
91                    continue; // last line offset case 
92                }
93
94                let chars = code.char_slice(start_char, end_char);
95
96                let start_byte = code.char_to_byte(start_char);
97                let end_byte = code.char_to_byte(end_char);
98            
99                let highlights = self.highlight_interval(
100                    start_byte, end_byte, &self.theme
101                );
102            
103                let mut x = 0;
104                let mut byte_idx_in_rope = start_byte;
105            
106                for g in RopeGraphemes::new(&chars) {
107                    let (g_width, g_bytes) = grapheme_width_and_bytes_len(g);
108                
109                    if x >= max_x { break; }
110                
111                    let start_x = area.left() + line_number_width as u16 + x as u16;
112                    let draw_y = area.top() + screen_y as u16;
113                
114                    for dx in 0..g_width {
115                        if x + dx >= max_x { break; }
116                        let draw_x = start_x + dx as u16;
117                        for &(start, end, s) in &highlights {
118                            if start <= byte_idx_in_rope && byte_idx_in_rope < end {
119                                buf[(draw_x, draw_y)].set_style(s);
120                                break;
121                            }
122                        }
123                    }
124                
125                    x = x.saturating_add(g_width);
126                    byte_idx_in_rope += g_bytes;
127                }
128            }
129        }
130
131        // draw selection
132        if let Some(selection) = self.selection && !selection.is_empty() {
133            let start = selection.start.min(selection.end);
134            let end = selection.start.max(selection.end);
135        
136            let start_line = code.char_to_line(start);
137            let end_line = code.char_to_line(end);
138        
139            for line_idx in start_line..=end_line {
140                if line_idx < self.offset_y { continue }
141                if line_idx >= self.offset_y + area.height as usize { break }
142        
143                let line_start_char = code.line_to_char(line_idx);
144                let line_len = code.line_len(line_idx);
145                let line_end_char = line_start_char + line_len;
146        
147                let sel_start = start.max(line_start_char);
148                let sel_end = end.min(line_end_char);
149        
150                let rel_start = sel_start - line_start_char;
151                let rel_end = sel_end - line_start_char;
152        
153                let start_col = self.offset_x.min(line_len);
154                let max_text_width = (area.width as usize).saturating_sub(line_number_width);
155                let end_col = (start_col + max_text_width).min(line_len);
156        
157                let char_slice_start = line_start_char + start_col;
158                let char_slice_end = line_start_char + end_col;
159        
160                let visible_chars = code.char_slice(char_slice_start, char_slice_end);
161
162                let draw_y = area.top() + (line_idx - self.offset_y) as u16;
163                let mut visual_x: u16 = 0;
164                let mut char_col = start_col;
165
166                for g in RopeGraphemes::new(&visible_chars) {
167                    let (g_width, g_chars) = grapheme_width_and_chars_len(g);
168                
169                    if char_col < rel_end && char_col + g_chars > rel_start {
170                        let start_x = area.left() + line_number_width as u16 + visual_x;
171                        for dx in 0..g_width as u16 {
172                            let draw_x = start_x + dx;
173                            if draw_x < area.right() && draw_y < area.bottom() {
174                                buf[(draw_x, draw_y)].set_style(Style::default().bg(Color::DarkGray));
175                            }
176                        }
177                    }
178                
179                    visual_x = visual_x.saturating_add(g_width as u16);
180                    char_col += g_chars;
181                }
182            }
183        }
184
185        // draw marks
186        if let Some(ref marks) = self.marks {
187            for &(start, end, color) in marks {
188                if start >= end || end > total_chars { continue }
189
190                let start_line = code.char_to_line(start);
191                let end_line = code.char_to_line(end);
192
193                for line_idx in start_line..=end_line {
194                    if line_idx < self.offset_y || line_idx >= self.offset_y + area.height as usize {
195                        continue;
196                    }
197
198                    let line_start_char = code.line_to_char(line_idx);
199                    let line_len = code.line_len(line_idx);
200                    let line_end_char = line_start_char + line_len;
201
202                    let highlight_start = start.max(line_start_char);
203                    let highlight_end = end.min(line_end_char);
204
205                    let rel_start = highlight_start - line_start_char;
206                    let rel_end = highlight_end - line_start_char;
207
208                    let start_col = self.offset_x.min(line_len);
209                    let max_text_width = (area.width as usize).saturating_sub(line_number_width);
210                    let end_col = (start_col + max_text_width).min(line_len);
211
212                    let char_slice_start = line_start_char + start_col;
213                    let char_slice_end = line_start_char + end_col;
214
215                    let visible_chars = code.char_slice(char_slice_start, char_slice_end);
216
217                    let draw_y = area.top() + (line_idx - self.offset_y) as u16;
218                    let mut visual_x: u16 = 0;
219                    let mut char_col = start_col;
220
221                    for g in RopeGraphemes::new(&visible_chars) {
222                        let (g_width, g_chars) = grapheme_width_and_chars_len(g);
223                    
224                        if char_col < rel_end && char_col + g_chars > rel_start {
225                            let start_x = area.left() + line_number_width as u16 + visual_x;
226                            for dx in 0..g_width as u16 {
227                                let draw_x = start_x + dx;
228                                if draw_x < area.right() && draw_y < area.bottom() {
229                                    buf[(draw_x, draw_y)].set_bg(color);
230                                }
231                            }
232                        }
233                    
234                        visual_x = visual_x.saturating_add(g_width as u16);
235                        char_col += g_chars;
236                    }
237                }
238            }
239        }
240    }
241}