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, Renderer, Selection, Shaping, Style, UnderlineStyle, 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 pub fn new() -> Self {
26 Self {
27 syntax_set: SyntaxSet::load_defaults_nonewlines(),
29 theme_set: ThemeSet::load_defaults(),
30 }
31 }
32}
33
34#[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 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 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 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 #[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 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 self.editor.with_buffer_mut(|buffer| {
128 buffer.set_text("", &attrs, Shaping::Advanced, None);
129 });
130
131 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 self.syntax_cache.clear();
146
147 let text = fs::read_to_string(path)?;
149 self.editor.with_buffer_mut(|buffer| {
150 buffer.set_text(&text, &attrs, Shaping::Advanced, None);
151 });
152
153 Ok(())
154 }
155
156 pub fn syntax_by_extension(&mut self, extension: &str) {
158 self.syntax = match self
159 .syntax_system
160 .syntax_set
161 .find_syntax_by_extension(extension)
162 {
163 Some(some) => some,
164 None => {
165 log::warn!("no syntax found for {extension:?}");
166 self.syntax_system.syntax_set.find_syntax_plain_text()
167 }
168 };
169
170 self.syntax_cache.clear();
171 }
172
173 pub fn background_color(&self) -> Color {
175 if let Some(background) = self.theme.settings.background {
176 Color::rgba(background.r, background.g, background.b, background.a)
177 } else {
178 Color::rgb(0, 0, 0)
179 }
180 }
181
182 pub fn foreground_color(&self) -> Color {
184 if let Some(foreground) = self.theme.settings.foreground {
185 Color::rgba(foreground.r, foreground.g, foreground.b, foreground.a)
186 } else {
187 Color::rgb(0xFF, 0xFF, 0xFF)
188 }
189 }
190
191 pub fn cursor_color(&self) -> Color {
193 if let Some(some) = self.theme.settings.caret {
194 Color::rgba(some.r, some.g, some.b, some.a)
195 } else {
196 self.foreground_color()
197 }
198 }
199
200 pub fn selection_color(&self) -> Color {
202 if let Some(some) = self.theme.settings.selection {
203 Color::rgba(some.r, some.g, some.b, some.a)
204 } else {
205 let foreground_color = self.foreground_color();
206 Color::rgba(
207 foreground_color.r(),
208 foreground_color.g(),
209 foreground_color.b(),
210 0x33,
211 )
212 }
213 }
214
215 pub fn theme(&self) -> &SyntaxTheme {
217 self.theme
218 }
219
220 #[cfg(feature = "swash")]
224 pub fn draw<F>(
225 &mut self,
226 font_system: &mut FontSystem,
227 cache: &mut crate::SwashCache,
228 callback: F,
229 ) where
230 F: FnMut(i32, i32, u32, u32, Color),
231 {
232 self.editor.draw(
233 font_system,
234 cache,
235 self.foreground_color(),
236 self.cursor_color(),
237 self.selection_color(),
238 self.foreground_color(),
239 callback,
240 );
241 }
242
243 pub fn render<R: Renderer>(&self, renderer: &mut R) {
248 let size = self.with_buffer(|buffer| buffer.size());
249 if let Some(width) = size.0 {
250 if let Some(height) = size.1 {
251 renderer.rectangle(0, 0, width as u32, height as u32, self.background_color());
252 }
253 }
254 self.editor.render(
255 renderer,
256 self.foreground_color(),
257 self.cursor_color(),
258 self.selection_color(),
259 self.foreground_color(),
260 );
261 }
262}
263
264impl<'buffer> Edit<'buffer> for SyntaxEditor<'_, 'buffer> {
265 fn buffer_ref(&self) -> &BufferRef<'buffer> {
266 self.editor.buffer_ref()
267 }
268
269 fn buffer_ref_mut(&mut self) -> &mut BufferRef<'buffer> {
270 self.editor.buffer_ref_mut()
271 }
272
273 fn cursor(&self) -> Cursor {
274 self.editor.cursor()
275 }
276
277 fn set_cursor(&mut self, cursor: Cursor) {
278 self.editor.set_cursor(cursor);
279 }
280
281 fn selection(&self) -> Selection {
282 self.editor.selection()
283 }
284
285 fn set_selection(&mut self, selection: Selection) {
286 self.editor.set_selection(selection);
287 }
288
289 fn auto_indent(&self) -> bool {
290 self.editor.auto_indent()
291 }
292
293 fn set_auto_indent(&mut self, auto_indent: bool) {
294 self.editor.set_auto_indent(auto_indent);
295 }
296
297 fn tab_width(&self) -> u16 {
298 self.editor.tab_width()
299 }
300
301 fn set_tab_width(&mut self, tab_width: u16) {
302 self.editor.set_tab_width(tab_width);
303 }
304
305 fn shape_as_needed(&mut self, font_system: &mut FontSystem, prune: bool) {
306 #[cfg(feature = "std")]
307 let now = std::time::Instant::now();
308
309 let cursor = self.cursor();
310 self.editor.with_buffer_mut(|buffer| {
311 let metrics = buffer.metrics();
312 let scroll = buffer.scroll();
313 let scroll_end = scroll.vertical + buffer.size().1.unwrap_or(f32::INFINITY);
314 let mut total_height = 0.0;
315 let mut highlighted = 0;
316 for line_i in 0..buffer.lines.len() {
317 if total_height > scroll_end && line_i > cursor.line {
319 break;
320 }
321
322 let line = &mut buffer.lines[line_i];
323 if line.metadata().is_some() && line_i < self.syntax_cache.len() {
324 if line_i >= scroll.line && total_height < scroll_end {
326 match buffer.line_layout(font_system, line_i) {
328 Some(layout_lines) => {
329 for layout_line in layout_lines.iter() {
330 total_height +=
331 layout_line.line_height_opt.unwrap_or(metrics.line_height);
332 }
333 }
334 None => {
335 }
337 }
338 }
339 continue;
340 }
341 highlighted += 1;
342
343 let (mut parse_state, scope_stack) =
344 if line_i > 0 && line_i <= self.syntax_cache.len() {
345 self.syntax_cache[line_i - 1].clone()
346 } else {
347 (ParseState::new(self.syntax), ScopeStack::new())
348 };
349 let mut highlight_state = HighlightState::new(&self.highlighter, scope_stack);
350 let ops = parse_state
351 .parse_line(line.text(), &self.syntax_system.syntax_set)
352 .expect("failed to parse syntax");
353 let ranges = RangedHighlightIterator::new(
354 &mut highlight_state,
355 &ops,
356 line.text(),
357 &self.highlighter,
358 );
359
360 let attrs = line.attrs_list().defaults();
361 let mut attrs_list = AttrsList::new(&attrs);
362 let original_attrs = attrs.clone(); for (style, _, range) in ranges {
364 let span_attrs = attrs
365 .clone() .color(Color::rgba(
367 style.foreground.r,
368 style.foreground.g,
369 style.foreground.b,
370 style.foreground.a,
371 ))
372 .style(if style.font_style.contains(FontStyle::ITALIC) {
374 Style::Italic
375 } else {
376 Style::Normal
377 })
378 .weight(if style.font_style.contains(FontStyle::BOLD) {
379 Weight::BOLD
380 } else {
381 Weight::NORMAL
382 })
383 .underline(if style.font_style.contains(FontStyle::UNDERLINE) {
384 UnderlineStyle::Single
385 } else {
386 UnderlineStyle::None
387 });
388 if span_attrs != original_attrs {
389 attrs_list.add_span(range, &span_attrs);
390 }
391 }
392
393 line.set_attrs_list(attrs_list);
395
396 if line_i >= scroll.line && total_height < scroll_end {
398 match buffer.line_layout(font_system, line_i) {
399 Some(layout_lines) => {
400 for layout_line in layout_lines.iter() {
401 total_height +=
402 layout_line.line_height_opt.unwrap_or(metrics.line_height);
403 }
404 }
405 None => {
406 }
408 }
409 }
410
411 let cache_item = (parse_state.clone(), highlight_state.path.clone());
412 if line_i < self.syntax_cache.len() {
413 if self.syntax_cache[line_i] != cache_item {
414 self.syntax_cache[line_i] = cache_item;
415 if line_i + 1 < buffer.lines.len() {
416 buffer.lines[line_i + 1].reset();
417 }
418 }
419 } else {
420 buffer.lines[line_i].set_metadata(self.syntax_cache.len());
421 self.syntax_cache.push(cache_item);
422 }
423 }
424
425 if highlighted > 0 {
426 buffer.set_redraw(true);
427 #[cfg(feature = "std")]
428 log::debug!(
429 "Syntax highlighted {} lines in {:?}",
430 highlighted,
431 now.elapsed()
432 );
433 }
434 });
435
436 self.editor.shape_as_needed(font_system, prune);
437 }
438
439 fn delete_range(&mut self, start: Cursor, end: Cursor) {
440 self.editor.delete_range(start, end);
441 }
442
443 fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option<AttrsList>) -> Cursor {
444 self.editor.insert_at(cursor, data, attrs_list)
445 }
446
447 fn copy_selection(&self) -> Option<String> {
448 self.editor.copy_selection()
449 }
450
451 fn delete_selection(&mut self) -> bool {
452 self.editor.delete_selection()
453 }
454
455 fn apply_change(&mut self, change: &Change) -> bool {
456 self.editor.apply_change(change)
457 }
458
459 fn start_change(&mut self) {
460 self.editor.start_change();
461 }
462
463 fn finish_change(&mut self) -> Option<Change> {
464 self.editor.finish_change()
465 }
466
467 fn action(&mut self, font_system: &mut FontSystem, action: Action) {
468 self.editor.action(font_system, action);
469 }
470
471 fn cursor_position(&self) -> Option<(i32, i32)> {
472 self.editor.cursor_position()
473 }
474}
475
476impl BorrowedWithFontSystem<'_, SyntaxEditor<'_, '_>> {
477 #[cfg(feature = "std")]
483 pub fn load_text<P: AsRef<Path>>(&mut self, path: P, attrs: crate::Attrs) -> io::Result<()> {
484 self.inner.load_text(self.font_system, path, attrs)
485 }
486
487 #[cfg(feature = "swash")]
488 pub fn draw<F>(&mut self, cache: &mut crate::SwashCache, f: F)
489 where
490 F: FnMut(i32, i32, u32, u32, Color),
491 {
492 self.inner.draw(self.font_system, cache, f);
493 }
494}