salvation_cosmic_text/edit/
syntect.rs

1#[cfg(not(feature = "std"))]
2use alloc::{string::String, vec::Vec};
3#[cfg(feature = "std")]
4use std::{fs, io, path::Path};
5use syntect::highlighting::{
6    FontStyle, HighlightState, Highlighter, RangedHighlightIterator, ThemeSet,
7};
8use syntect::parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet};
9
10use crate::{
11    Action, AttrsList, BorrowedWithFontSystem, BufferRef, Change, Color, Cursor, Edit, Editor,
12    FontSystem, Selection, Shaping, Style, Weight,
13};
14
15pub use syntect::highlighting::Theme as SyntaxTheme;
16
17#[derive(Debug)]
18pub struct SyntaxSystem {
19    pub syntax_set: SyntaxSet,
20    pub theme_set: ThemeSet,
21}
22
23impl SyntaxSystem {
24    /// Create a new [`SyntaxSystem`]
25    pub fn new() -> Self {
26        Self {
27            //TODO: store newlines in buffer
28            syntax_set: SyntaxSet::load_defaults_nonewlines(),
29            theme_set: ThemeSet::load_defaults(),
30        }
31    }
32}
33
34/// A wrapper of [`Editor`] with syntax highlighting provided by [`SyntaxSystem`]
35#[derive(Debug)]
36pub struct SyntaxEditor<'syntax_system, 'buffer> {
37    editor: Editor<'buffer>,
38    syntax_system: &'syntax_system SyntaxSystem,
39    syntax: &'syntax_system SyntaxReference,
40    theme: &'syntax_system SyntaxTheme,
41    highlighter: Highlighter<'syntax_system>,
42    syntax_cache: Vec<(ParseState, ScopeStack)>,
43}
44
45impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> {
46    /// Create a new [`SyntaxEditor`] with the provided [`Buffer`], [`SyntaxSystem`], and theme name.
47    ///
48    /// A good default theme name is "base16-eighties.dark".
49    ///
50    /// Returns None if theme not found
51    pub fn new(
52        buffer: impl Into<BufferRef<'buffer>>,
53        syntax_system: &'syntax_system SyntaxSystem,
54        theme_name: &str,
55    ) -> Option<Self> {
56        let editor = Editor::new(buffer);
57        let syntax = syntax_system.syntax_set.find_syntax_plain_text();
58        let theme = syntax_system.theme_set.themes.get(theme_name)?;
59        let highlighter = Highlighter::new(theme);
60
61        Some(Self {
62            editor,
63            syntax_system,
64            syntax,
65            theme,
66            highlighter,
67            syntax_cache: Vec::new(),
68        })
69    }
70
71    /// Modifies the theme of the [`SyntaxEditor`], returning false if the theme is missing
72    pub fn update_theme(&mut self, theme_name: &str) -> bool {
73        if let Some(theme) = self.syntax_system.theme_set.themes.get(theme_name) {
74            if self.theme != theme {
75                self.theme = theme;
76                self.highlighter = Highlighter::new(theme);
77                self.syntax_cache.clear();
78
79                // Reset attrs to match default foreground and no highlighting
80                self.with_buffer_mut(|buffer| {
81                    for line in buffer.lines.iter_mut() {
82                        let mut attrs = line.attrs_list().defaults();
83                        if let Some(foreground) = self.theme.settings.foreground {
84                            attrs = attrs.color(Color::rgba(
85                                foreground.r,
86                                foreground.g,
87                                foreground.b,
88                                foreground.a,
89                            ));
90                        }
91                        line.set_attrs_list(AttrsList::new(attrs));
92                    }
93                });
94            }
95
96            true
97        } else {
98            false
99        }
100    }
101
102    /// Load text from a file, and also set syntax to the best option
103    ///
104    /// ## Errors
105    ///
106    /// Returns an [`io::Error`] if reading the file fails
107    #[cfg(feature = "std")]
108    pub fn load_text<P: AsRef<Path>>(
109        &mut self,
110        font_system: &mut FontSystem,
111        path: P,
112        mut attrs: crate::Attrs,
113    ) -> io::Result<()> {
114        let path = path.as_ref();
115
116        // Set attrs to match default foreground
117        if let Some(foreground) = self.theme.settings.foreground {
118            attrs = attrs.color(Color::rgba(
119                foreground.r,
120                foreground.g,
121                foreground.b,
122                foreground.a,
123            ));
124        }
125
126        let text = fs::read_to_string(path)?;
127        self.editor.with_buffer_mut(|buffer| {
128            buffer.set_text(font_system, &text, attrs, Shaping::Advanced)
129        });
130
131        //TODO: re-use text
132        self.syntax = match self.syntax_system.syntax_set.find_syntax_for_file(path) {
133            Ok(Some(some)) => some,
134            Ok(None) => {
135                log::warn!("no syntax found for {:?}", path);
136                self.syntax_system.syntax_set.find_syntax_plain_text()
137            }
138            Err(err) => {
139                log::warn!("failed to determine syntax for {:?}: {:?}", path, err);
140                self.syntax_system.syntax_set.find_syntax_plain_text()
141            }
142        };
143
144        // Clear syntax cache
145        self.syntax_cache.clear();
146
147        Ok(())
148    }
149
150    /// Get the default background color
151    pub fn background_color(&self) -> Color {
152        if let Some(background) = self.theme.settings.background {
153            Color::rgba(background.r, background.g, background.b, background.a)
154        } else {
155            Color::rgb(0, 0, 0)
156        }
157    }
158
159    /// Get the default foreground (text) color
160    pub fn foreground_color(&self) -> Color {
161        if let Some(foreground) = self.theme.settings.foreground {
162            Color::rgba(foreground.r, foreground.g, foreground.b, foreground.a)
163        } else {
164            Color::rgb(0xFF, 0xFF, 0xFF)
165        }
166    }
167
168    /// Get the default cursor color
169    pub fn cursor_color(&self) -> Color {
170        if let Some(some) = self.theme.settings.caret {
171            Color::rgba(some.r, some.g, some.b, some.a)
172        } else {
173            self.foreground_color()
174        }
175    }
176
177    /// Get the default selection color
178    pub fn selection_color(&self) -> Color {
179        if let Some(some) = self.theme.settings.selection {
180            Color::rgba(some.r, some.g, some.b, some.a)
181        } else {
182            let foreground_color = self.foreground_color();
183            Color::rgba(
184                foreground_color.r(),
185                foreground_color.g(),
186                foreground_color.b(),
187                0x33,
188            )
189        }
190    }
191
192    /// Get the current syntect theme
193    pub fn theme(&self) -> &SyntaxTheme {
194        self.theme
195    }
196
197    /// Draw the editor
198    #[cfg(feature = "swash")]
199    pub fn draw<F>(&self, font_system: &mut FontSystem, cache: &mut crate::SwashCache, mut f: F)
200    where
201        F: FnMut(i32, i32, u32, u32, Color),
202    {
203        let size = self.with_buffer(|buffer| buffer.size());
204        f(0, 0, size.0 as u32, size.1 as u32, self.background_color());
205        self.editor.draw(
206            font_system,
207            cache,
208            self.foreground_color(),
209            self.cursor_color(),
210            self.selection_color(),
211            self.foreground_color(),
212            f,
213        );
214    }
215}
216
217impl<'syntax_system, 'buffer> Edit<'buffer> for SyntaxEditor<'syntax_system, 'buffer> {
218    fn buffer_ref(&self) -> &BufferRef<'buffer> {
219        self.editor.buffer_ref()
220    }
221
222    fn buffer_ref_mut(&mut self) -> &mut BufferRef<'buffer> {
223        self.editor.buffer_ref_mut()
224    }
225
226    fn cursor(&self) -> Cursor {
227        self.editor.cursor()
228    }
229
230    fn set_cursor(&mut self, cursor: Cursor) {
231        self.editor.set_cursor(cursor);
232    }
233
234    fn selection(&self) -> Selection {
235        self.editor.selection()
236    }
237
238    fn set_cursor_hidden(&mut self, hidden: bool) {
239        self.editor.set_cursor_hidden(hidden);
240    }
241
242    fn set_selection(&mut self, selection: Selection) {
243        self.editor.set_selection(selection);
244    }
245
246    fn auto_indent(&self) -> bool {
247        self.editor.auto_indent()
248    }
249
250    fn set_auto_indent(&mut self, auto_indent: bool) {
251        self.editor.set_auto_indent(auto_indent);
252    }
253
254    fn tab_width(&self) -> u16 {
255        self.editor.tab_width()
256    }
257
258    fn set_tab_width(&mut self, tab_width: u16) {
259        self.editor.set_tab_width(tab_width);
260    }
261
262    fn shape_as_needed(&mut self, font_system: &mut FontSystem, prune: bool) {
263        #[cfg(feature = "std")]
264        let now = std::time::Instant::now();
265
266        let cursor = self.cursor();
267        self.editor.with_buffer_mut(|buffer| {
268            let visible_lines = buffer.visible_lines();
269            let scroll = buffer.scroll();
270            let scroll_end = scroll.layout + visible_lines;
271            let mut total_layout = 0;
272            let mut highlighted = 0;
273            for line_i in 0..buffer.lines.len() {
274                // Break out if we have reached the end of scroll and are past the cursor
275                if total_layout >= scroll_end && line_i > cursor.line {
276                    break;
277                }
278
279                let line = &mut buffer.lines[line_i];
280                if line.preedit_range().is_some() {
281                    continue;
282                }
283                if line.metadata().is_some() && line_i < self.syntax_cache.len() {
284                    //TODO: duplicated code!
285                    if line_i >= scroll.line && total_layout < scroll_end {
286                        // Perform shaping and layout of this line in order to count if we have reached scroll
287                        match buffer.line_layout(font_system, line_i) {
288                            Some(layout_lines) => {
289                                total_layout += layout_lines.len() as i32;
290                            }
291                            None => {
292                                //TODO: should this be possible?
293                            }
294                        }
295                    }
296                    continue;
297                }
298                highlighted += 1;
299
300                let (mut parse_state, scope_stack) =
301                    if line_i > 0 && line_i <= self.syntax_cache.len() {
302                        self.syntax_cache[line_i - 1].clone()
303                    } else {
304                        (ParseState::new(self.syntax), ScopeStack::new())
305                    };
306                let mut highlight_state = HighlightState::new(&self.highlighter, scope_stack);
307                let ops = parse_state
308                    .parse_line(line.text(), &self.syntax_system.syntax_set)
309                    .expect("failed to parse syntax");
310                let ranges = RangedHighlightIterator::new(
311                    &mut highlight_state,
312                    &ops,
313                    line.text(),
314                    &self.highlighter,
315                );
316
317                let attrs = line.attrs_list().defaults();
318                let mut attrs_list = AttrsList::new(attrs);
319                for (style, _, range) in ranges {
320                    let span_attrs = attrs
321                        .color(Color::rgba(
322                            style.foreground.r,
323                            style.foreground.g,
324                            style.foreground.b,
325                            style.foreground.a,
326                        ))
327                        //TODO: background
328                        .style(if style.font_style.contains(FontStyle::ITALIC) {
329                            Style::Italic
330                        } else {
331                            Style::Normal
332                        })
333                        .weight(if style.font_style.contains(FontStyle::BOLD) {
334                            Weight::BOLD
335                        } else {
336                            Weight::NORMAL
337                        }); //TODO: underline
338                    if span_attrs != attrs {
339                        attrs_list.add_span(range, span_attrs);
340                    }
341                }
342
343                // Update line attributes. This operation only resets if the line changes
344                line.set_attrs_list(attrs_list);
345
346                // Perform shaping and layout of this line in order to count if we have reached scroll
347                if line_i >= scroll.line && total_layout < scroll_end {
348                    match buffer.line_layout(font_system, line_i) {
349                        Some(layout_lines) => {
350                            total_layout += layout_lines.len() as i32;
351                        }
352                        None => {
353                            //TODO: should this be possible?
354                        }
355                    }
356                }
357
358                let cache_item = (parse_state.clone(), highlight_state.path.clone());
359                if line_i < self.syntax_cache.len() {
360                    if self.syntax_cache[line_i] != cache_item {
361                        self.syntax_cache[line_i] = cache_item;
362                        if line_i + 1 < buffer.lines.len() {
363                            buffer.lines[line_i + 1].reset();
364                        }
365                    }
366                } else {
367                    buffer.lines[line_i].set_metadata(self.syntax_cache.len());
368                    self.syntax_cache.push(cache_item);
369                }
370            }
371
372            if highlighted > 0 {
373                buffer.set_redraw(true);
374                #[cfg(feature = "std")]
375                log::debug!(
376                    "Syntax highlighted {} lines in {:?}",
377                    highlighted,
378                    now.elapsed()
379                );
380            }
381        });
382
383        self.editor.shape_as_needed(font_system, prune);
384    }
385
386    fn delete_range(&mut self, start: Cursor, end: Cursor) {
387        self.editor.delete_range(start, end);
388    }
389
390    fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option<AttrsList>) -> Cursor {
391        self.editor.insert_at(cursor, data, attrs_list)
392    }
393
394    fn copy_selection(&self) -> Option<String> {
395        self.editor.copy_selection()
396    }
397
398    fn delete_selection(&mut self) -> bool {
399        self.editor.delete_selection()
400    }
401
402    fn apply_change(&mut self, change: &Change) -> bool {
403        self.editor.apply_change(change)
404    }
405
406    fn start_change(&mut self) {
407        self.editor.start_change();
408    }
409
410    fn preedit_range(&self) -> Option<core::ops::Range<usize>> {
411        self.editor.preedit_range()
412    }
413
414    fn preedit_text(&self) -> Option<String> {
415        self.editor.preedit_text()
416    }
417
418    fn finish_change(&mut self) -> Option<Change> {
419        self.editor.finish_change()
420    }
421
422    fn action(&mut self, font_system: &mut FontSystem, action: Action) {
423        self.editor.action(font_system, action);
424    }
425
426    fn cursor_position(&self) -> Option<(i32, i32)> {
427        self.editor.cursor_position()
428    }
429}
430
431impl<'font_system, 'syntax_system, 'buffer>
432    BorrowedWithFontSystem<'font_system, SyntaxEditor<'syntax_system, 'buffer>>
433{
434    /// Load text from a file, and also set syntax to the best option
435    ///
436    /// ## Errors
437    ///
438    /// Returns an [`io::Error`] if reading the file fails
439    #[cfg(feature = "std")]
440    pub fn load_text<P: AsRef<Path>>(&mut self, path: P, attrs: crate::Attrs) -> io::Result<()> {
441        self.inner.load_text(self.font_system, path, attrs)
442    }
443
444    #[cfg(feature = "swash")]
445    pub fn draw<F>(&mut self, cache: &mut crate::SwashCache, f: F)
446    where
447        F: FnMut(i32, i32, u32, u32, Color),
448    {
449        self.inner.draw(self.font_system, cache, f);
450    }
451}