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