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 let original_attrs = attrs.clone(); for (style, _, range) in ranges {
338 let span_attrs = attrs
339 .clone() .color(Color::rgba(
341 style.foreground.r,
342 style.foreground.g,
343 style.foreground.b,
344 style.foreground.a,
345 ))
346 .style(if style.font_style.contains(FontStyle::ITALIC) {
348 Style::Italic
349 } else {
350 Style::Normal
351 })
352 .weight(if style.font_style.contains(FontStyle::BOLD) {
353 Weight::BOLD
354 } else {
355 Weight::NORMAL
356 }); if span_attrs != original_attrs {
358 attrs_list.add_span(range, &span_attrs);
359 }
360 }
361
362 line.set_attrs_list(attrs_list);
364
365 if line_i >= scroll.line && total_height < scroll_end {
367 match buffer.line_layout(font_system, line_i) {
368 Some(layout_lines) => {
369 for layout_line in layout_lines.iter() {
370 total_height +=
371 layout_line.line_height_opt.unwrap_or(metrics.line_height);
372 }
373 }
374 None => {
375 }
377 }
378 }
379
380 let cache_item = (parse_state.clone(), highlight_state.path.clone());
381 if line_i < self.syntax_cache.len() {
382 if self.syntax_cache[line_i] != cache_item {
383 self.syntax_cache[line_i] = cache_item;
384 if line_i + 1 < buffer.lines.len() {
385 buffer.lines[line_i + 1].reset();
386 }
387 }
388 } else {
389 buffer.lines[line_i].set_metadata(self.syntax_cache.len());
390 self.syntax_cache.push(cache_item);
391 }
392 }
393
394 if highlighted > 0 {
395 buffer.set_redraw(true);
396 #[cfg(feature = "std")]
397 log::debug!(
398 "Syntax highlighted {} lines in {:?}",
399 highlighted,
400 now.elapsed()
401 );
402 }
403 });
404
405 self.editor.shape_as_needed(font_system, prune);
406 }
407
408 fn delete_range(&mut self, start: Cursor, end: Cursor) {
409 self.editor.delete_range(start, end);
410 }
411
412 fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option<AttrsList>) -> Cursor {
413 self.editor.insert_at(cursor, data, attrs_list)
414 }
415
416 fn copy_selection(&self) -> Option<String> {
417 self.editor.copy_selection()
418 }
419
420 fn delete_selection(&mut self) -> bool {
421 self.editor.delete_selection()
422 }
423
424 fn apply_change(&mut self, change: &Change) -> bool {
425 self.editor.apply_change(change)
426 }
427
428 fn start_change(&mut self) {
429 self.editor.start_change();
430 }
431
432 fn finish_change(&mut self) -> Option<Change> {
433 self.editor.finish_change()
434 }
435
436 fn action(&mut self, font_system: &mut FontSystem, action: Action) {
437 self.editor.action(font_system, action);
438 }
439
440 fn cursor_position(&self) -> Option<(i32, i32)> {
441 self.editor.cursor_position()
442 }
443}
444
445impl BorrowedWithFontSystem<'_, SyntaxEditor<'_, '_>> {
446 #[cfg(feature = "std")]
452 pub fn load_text<P: AsRef<Path>>(&mut self, path: P, attrs: crate::Attrs) -> io::Result<()> {
453 self.inner.load_text(self.font_system, path, attrs)
454 }
455
456 #[cfg(feature = "swash")]
457 pub fn draw<F>(&mut self, cache: &mut crate::SwashCache, f: F)
458 where
459 F: FnMut(i32, i32, u32, u32, Color),
460 {
461 self.inner.draw(self.font_system, cache, f);
462 }
463}