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, 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(font_system, "", &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(font_system, &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")]
222 pub fn draw<F>(&self, font_system: &mut FontSystem, cache: &mut crate::SwashCache, callback: F)
223 where
224 F: FnMut(i32, i32, u32, u32, Color),
225 {
226 let mut renderer = crate::LegacyRenderer {
227 font_system,
228 cache,
229 callback,
230 };
231 self.render(&mut renderer);
232 }
233
234 pub fn render<R: Renderer>(&self, renderer: &mut R) {
235 let size = self.with_buffer(|buffer| buffer.size());
236 if let Some(width) = size.0 {
237 if let Some(height) = size.1 {
238 renderer.rectangle(0, 0, width as u32, height as u32, self.background_color());
239 }
240 }
241 self.editor.render(
242 renderer,
243 self.foreground_color(),
244 self.cursor_color(),
245 self.selection_color(),
246 self.foreground_color(),
247 );
248 }
249}
250
251impl<'buffer> Edit<'buffer> for SyntaxEditor<'_, 'buffer> {
252 fn buffer_ref(&self) -> &BufferRef<'buffer> {
253 self.editor.buffer_ref()
254 }
255
256 fn buffer_ref_mut(&mut self) -> &mut BufferRef<'buffer> {
257 self.editor.buffer_ref_mut()
258 }
259
260 fn cursor(&self) -> Cursor {
261 self.editor.cursor()
262 }
263
264 fn set_cursor(&mut self, cursor: Cursor) {
265 self.editor.set_cursor(cursor);
266 }
267
268 fn selection(&self) -> Selection {
269 self.editor.selection()
270 }
271
272 fn set_selection(&mut self, selection: Selection) {
273 self.editor.set_selection(selection);
274 }
275
276 fn auto_indent(&self) -> bool {
277 self.editor.auto_indent()
278 }
279
280 fn set_auto_indent(&mut self, auto_indent: bool) {
281 self.editor.set_auto_indent(auto_indent);
282 }
283
284 fn tab_width(&self) -> u16 {
285 self.editor.tab_width()
286 }
287
288 fn set_tab_width(&mut self, font_system: &mut FontSystem, tab_width: u16) {
289 self.editor.set_tab_width(font_system, tab_width);
290 }
291
292 fn shape_as_needed(&mut self, font_system: &mut FontSystem, prune: bool) {
293 #[cfg(feature = "std")]
294 let now = std::time::Instant::now();
295
296 let cursor = self.cursor();
297 self.editor.with_buffer_mut(|buffer| {
298 let metrics = buffer.metrics();
299 let scroll = buffer.scroll();
300 let scroll_end = scroll.vertical + buffer.size().1.unwrap_or(f32::INFINITY);
301 let mut total_height = 0.0;
302 let mut highlighted = 0;
303 for line_i in 0..buffer.lines.len() {
304 if total_height > scroll_end && line_i > cursor.line {
306 break;
307 }
308
309 let line = &mut buffer.lines[line_i];
310 if line.metadata().is_some() && line_i < self.syntax_cache.len() {
311 if line_i >= scroll.line && total_height < scroll_end {
313 match buffer.line_layout(font_system, line_i) {
315 Some(layout_lines) => {
316 for layout_line in layout_lines.iter() {
317 total_height +=
318 layout_line.line_height_opt.unwrap_or(metrics.line_height);
319 }
320 }
321 None => {
322 }
324 }
325 }
326 continue;
327 }
328 highlighted += 1;
329
330 let (mut parse_state, scope_stack) =
331 if line_i > 0 && line_i <= self.syntax_cache.len() {
332 self.syntax_cache[line_i - 1].clone()
333 } else {
334 (ParseState::new(self.syntax), ScopeStack::new())
335 };
336 let mut highlight_state = HighlightState::new(&self.highlighter, scope_stack);
337 let ops = parse_state
338 .parse_line(line.text(), &self.syntax_system.syntax_set)
339 .expect("failed to parse syntax");
340 let ranges = RangedHighlightIterator::new(
341 &mut highlight_state,
342 &ops,
343 line.text(),
344 &self.highlighter,
345 );
346
347 let attrs = line.attrs_list().defaults();
348 let mut attrs_list = AttrsList::new(&attrs);
349 let original_attrs = attrs.clone(); for (style, _, range) in ranges {
351 let span_attrs = attrs
352 .clone() .color(Color::rgba(
354 style.foreground.r,
355 style.foreground.g,
356 style.foreground.b,
357 style.foreground.a,
358 ))
359 .style(if style.font_style.contains(FontStyle::ITALIC) {
361 Style::Italic
362 } else {
363 Style::Normal
364 })
365 .weight(if style.font_style.contains(FontStyle::BOLD) {
366 Weight::BOLD
367 } else {
368 Weight::NORMAL
369 }); if span_attrs != original_attrs {
371 attrs_list.add_span(range, &span_attrs);
372 }
373 }
374
375 line.set_attrs_list(attrs_list);
377
378 if line_i >= scroll.line && total_height < scroll_end {
380 match buffer.line_layout(font_system, line_i) {
381 Some(layout_lines) => {
382 for layout_line in layout_lines.iter() {
383 total_height +=
384 layout_line.line_height_opt.unwrap_or(metrics.line_height);
385 }
386 }
387 None => {
388 }
390 }
391 }
392
393 let cache_item = (parse_state.clone(), highlight_state.path.clone());
394 if line_i < self.syntax_cache.len() {
395 if self.syntax_cache[line_i] != cache_item {
396 self.syntax_cache[line_i] = cache_item;
397 if line_i + 1 < buffer.lines.len() {
398 buffer.lines[line_i + 1].reset();
399 }
400 }
401 } else {
402 buffer.lines[line_i].set_metadata(self.syntax_cache.len());
403 self.syntax_cache.push(cache_item);
404 }
405 }
406
407 if highlighted > 0 {
408 buffer.set_redraw(true);
409 #[cfg(feature = "std")]
410 log::debug!(
411 "Syntax highlighted {} lines in {:?}",
412 highlighted,
413 now.elapsed()
414 );
415 }
416 });
417
418 self.editor.shape_as_needed(font_system, prune);
419 }
420
421 fn delete_range(&mut self, start: Cursor, end: Cursor) {
422 self.editor.delete_range(start, end);
423 }
424
425 fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option<AttrsList>) -> Cursor {
426 self.editor.insert_at(cursor, data, attrs_list)
427 }
428
429 fn copy_selection(&self) -> Option<String> {
430 self.editor.copy_selection()
431 }
432
433 fn delete_selection(&mut self) -> bool {
434 self.editor.delete_selection()
435 }
436
437 fn apply_change(&mut self, change: &Change) -> bool {
438 self.editor.apply_change(change)
439 }
440
441 fn start_change(&mut self) {
442 self.editor.start_change();
443 }
444
445 fn finish_change(&mut self) -> Option<Change> {
446 self.editor.finish_change()
447 }
448
449 fn action(&mut self, font_system: &mut FontSystem, action: Action) {
450 self.editor.action(font_system, action);
451 }
452
453 fn cursor_position(&self) -> Option<(i32, i32)> {
454 self.editor.cursor_position()
455 }
456}
457
458impl BorrowedWithFontSystem<'_, SyntaxEditor<'_, '_>> {
459 #[cfg(feature = "std")]
465 pub fn load_text<P: AsRef<Path>>(&mut self, path: P, attrs: crate::Attrs) -> io::Result<()> {
466 self.inner.load_text(self.font_system, path, attrs)
467 }
468
469 #[cfg(feature = "swash")]
470 pub fn draw<F>(&mut self, cache: &mut crate::SwashCache, f: F)
471 where
472 F: FnMut(i32, i32, u32, u32, Color),
473 {
474 self.inner.draw(self.font_system, cache, f);
475 }
476}