iced_code_editor/canvas_editor/config.rs
1//! Builder-style configuration: context menu, theme/language, Vim toggle,
2//! wrap/whitespace/bracket/folding-enabled flags, auto-indent, auto-close
3//! brackets, indent style, search/replace enablement, and line numbers.
4
5use crate::canvas_editor::features::context_menu::{
6 ContextMenuEntry, ContextMenuItem,
7};
8use crate::canvas_editor::features::folding::{
9 FoldMarkerPosition, FoldMarkers,
10};
11use crate::canvas_editor::features::vim::VimMode;
12use crate::canvas_editor::{CodeEditor, IndentStyle};
13use crate::theme::Style;
14
15impl CodeEditor {
16 /// Replaces the custom context-menu entries.
17 ///
18 /// Custom entries are shown in addition to the built-in editing actions;
19 /// see [`Self::set_default_context_menu_enabled`] to hide those.
20 ///
21 /// # Arguments
22 ///
23 /// * `entries` - The entries to display, in order
24 ///
25 /// # Example
26 ///
27 /// ```
28 /// use iced_code_editor::{CodeEditor, ContextMenuEntry};
29 ///
30 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
31 /// editor.set_custom_context_menu_entries(vec![
32 /// ContextMenuEntry::item("format", "Format Document")
33 /// .with_shortcut("Ctrl+Shift+F"),
34 /// ContextMenuEntry::separator(),
35 /// ContextMenuEntry::item("rename", "Rename Symbol").with_enabled(false),
36 /// ]);
37 /// assert_eq!(editor.custom_context_menu_entries().len(), 3);
38 /// ```
39 pub fn set_custom_context_menu_entries(
40 &mut self,
41 entries: Vec<ContextMenuEntry>,
42 ) {
43 self.custom_context_menu_entries = entries;
44 }
45
46 /// Replaces the custom context-menu entries using the builder pattern.
47 ///
48 /// # Arguments
49 ///
50 /// * `entries` - The entries to display, in order
51 ///
52 /// # Returns
53 ///
54 /// Self for method chaining
55 ///
56 /// # Example
57 ///
58 /// ```
59 /// use iced_code_editor::{CodeEditor, ContextMenuEntry};
60 ///
61 /// let editor = CodeEditor::new("fn main() {}", "rs")
62 /// .with_custom_context_menu_entries(vec![
63 /// ContextMenuEntry::item("format", "Format Document"),
64 /// ]);
65 /// assert_eq!(editor.custom_context_menu_entries().len(), 1);
66 /// ```
67 #[must_use]
68 pub fn with_custom_context_menu_entries(
69 mut self,
70 entries: Vec<ContextMenuEntry>,
71 ) -> Self {
72 self.set_custom_context_menu_entries(entries);
73 self
74 }
75
76 /// Returns the custom context-menu entries in display order.
77 ///
78 /// # Returns
79 ///
80 /// The entries previously set, or an empty slice if none were set
81 ///
82 /// # Example
83 ///
84 /// ```
85 /// use iced_code_editor::CodeEditor;
86 ///
87 /// let editor = CodeEditor::new("fn main() {}", "rs");
88 /// assert!(editor.custom_context_menu_entries().is_empty());
89 /// ```
90 pub fn custom_context_menu_entries(&self) -> &[ContextMenuEntry] {
91 &self.custom_context_menu_entries
92 }
93
94 /// Sets whether the built-in editing actions appear in the context menu.
95 ///
96 /// The built-in actions are undo/redo, cut/copy/paste, and select all.
97 /// Disabling them leaves only the custom entries, which is how a host
98 /// application replaces the menu wholesale rather than extending it.
99 ///
100 /// # Arguments
101 ///
102 /// * `enabled` - Whether to show the built-in actions
103 ///
104 /// # Example
105 ///
106 /// ```
107 /// use iced_code_editor::CodeEditor;
108 ///
109 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
110 /// editor.set_default_context_menu_enabled(false);
111 /// assert!(!editor.default_context_menu_enabled());
112 /// ```
113 pub fn set_default_context_menu_enabled(&mut self, enabled: bool) {
114 self.default_context_menu_enabled = enabled;
115 }
116
117 /// Replaces the custom command-palette entries.
118 ///
119 /// Custom commands are listed before the built-in editor commands; see
120 /// [`Self::set_default_command_palette_enabled`] to hide those. Running
121 /// one emits [`Message::CommandPaletteAction`] carrying the entry's `id`,
122 /// which the host application handles — the editor never acts on it.
123 ///
124 /// Entries created with `with_enabled(false)` are left out of the list
125 /// entirely rather than dimmed: the palette is a search result list, so
126 /// every row it offers should be runnable.
127 ///
128 /// # Arguments
129 ///
130 /// * `entries` - The commands to list, in order
131 ///
132 /// # Example
133 ///
134 /// ```
135 /// use iced_code_editor::{CodeEditor, ContextMenuItem};
136 ///
137 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
138 /// editor.set_custom_command_palette_entries(vec![
139 /// ContextMenuItem::new("app.open_file", "Open File")
140 /// .with_shortcut("Ctrl+O"),
141 /// ContextMenuItem::new("app.new_tab", "New Tab"),
142 /// ]);
143 /// assert_eq!(editor.custom_command_palette_entries().len(), 2);
144 /// ```
145 ///
146 /// [`Message::CommandPaletteAction`]: crate::Message::CommandPaletteAction
147 pub fn set_custom_command_palette_entries(
148 &mut self,
149 entries: Vec<ContextMenuItem>,
150 ) {
151 self.custom_command_palette_entries = entries;
152 }
153
154 /// Replaces the custom command-palette entries using the builder pattern.
155 ///
156 /// # Arguments
157 ///
158 /// * `entries` - The commands to list, in order
159 ///
160 /// # Returns
161 ///
162 /// Self for method chaining
163 ///
164 /// # Example
165 ///
166 /// ```
167 /// use iced_code_editor::{CodeEditor, ContextMenuItem};
168 ///
169 /// let editor = CodeEditor::new("fn main() {}", "rs")
170 /// .with_custom_command_palette_entries(vec![
171 /// ContextMenuItem::new("app.open_file", "Open File"),
172 /// ]);
173 /// assert_eq!(editor.custom_command_palette_entries().len(), 1);
174 /// ```
175 #[must_use]
176 pub fn with_custom_command_palette_entries(
177 mut self,
178 entries: Vec<ContextMenuItem>,
179 ) -> Self {
180 self.set_custom_command_palette_entries(entries);
181 self
182 }
183
184 /// Returns the custom command-palette entries in display order.
185 ///
186 /// # Returns
187 ///
188 /// The entries previously set, or an empty slice if none were set
189 ///
190 /// # Example
191 ///
192 /// ```
193 /// use iced_code_editor::CodeEditor;
194 ///
195 /// let editor = CodeEditor::new("fn main() {}", "rs");
196 /// assert!(editor.custom_command_palette_entries().is_empty());
197 /// ```
198 pub fn custom_command_palette_entries(&self) -> &[ContextMenuItem] {
199 &self.custom_command_palette_entries
200 }
201
202 /// Sets whether the built-in editor commands are listed in the palette.
203 ///
204 /// Disabling them leaves only the custom entries, which is how a host
205 /// application takes the palette over rather than extending it.
206 ///
207 /// # Arguments
208 ///
209 /// * `enabled` - Whether to list the built-in commands
210 ///
211 /// # Example
212 ///
213 /// ```
214 /// use iced_code_editor::CodeEditor;
215 ///
216 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
217 /// editor.set_default_command_palette_enabled(false);
218 /// assert!(!editor.default_command_palette_enabled());
219 /// ```
220 pub fn set_default_command_palette_enabled(&mut self, enabled: bool) {
221 self.default_command_palette_enabled = enabled;
222 }
223
224 /// Sets built-in command visibility using the builder pattern.
225 ///
226 /// # Arguments
227 ///
228 /// * `enabled` - Whether to list the built-in commands
229 ///
230 /// # Returns
231 ///
232 /// Self for method chaining
233 ///
234 /// # Example
235 ///
236 /// ```
237 /// use iced_code_editor::CodeEditor;
238 ///
239 /// let editor = CodeEditor::new("fn main() {}", "rs")
240 /// .with_default_command_palette_enabled(false);
241 /// assert!(!editor.default_command_palette_enabled());
242 /// ```
243 #[must_use]
244 pub fn with_default_command_palette_enabled(
245 mut self,
246 enabled: bool,
247 ) -> Self {
248 self.set_default_command_palette_enabled(enabled);
249 self
250 }
251
252 /// Sets whether the command palette can be opened.
253 ///
254 /// Turning it off makes `Ctrl/Cmd+Shift+P` and
255 /// [`CodeEditor::open_command_palette`] no-ops, freeing the shortcut for
256 /// a host application that provides its own palette.
257 ///
258 /// # Arguments
259 ///
260 /// * `enabled` - Whether the palette can be opened
261 ///
262 /// # Example
263 ///
264 /// ```
265 /// use iced_code_editor::CodeEditor;
266 ///
267 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
268 /// editor.set_command_palette_enabled(false);
269 /// assert!(!editor.command_palette_enabled());
270 /// ```
271 pub fn set_command_palette_enabled(&mut self, enabled: bool) {
272 self.command_palette_enabled = enabled;
273 if !enabled {
274 self.command_palette_state.close();
275 }
276 }
277
278 /// Sets whether the built-in reveal-in-file-manager action is shown.
279 ///
280 /// The action is only useful when the host application knows the file path
281 /// backing the editor, so it is off by default and the host opts in.
282 ///
283 /// # Arguments
284 ///
285 /// * `enabled` - Whether to show the reveal action
286 ///
287 /// # Example
288 ///
289 /// ```
290 /// use iced_code_editor::CodeEditor;
291 ///
292 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
293 /// editor.set_reveal_in_file_manager_enabled(true);
294 /// assert!(editor.reveal_in_file_manager_enabled());
295 /// ```
296 pub fn set_reveal_in_file_manager_enabled(&mut self, enabled: bool) {
297 self.reveal_in_file_manager_enabled = enabled;
298 }
299
300 /// Sets reveal-in-file-manager visibility using the builder pattern.
301 ///
302 /// # Arguments
303 ///
304 /// * `enabled` - Whether to show the reveal action
305 ///
306 /// # Returns
307 ///
308 /// Self for method chaining
309 ///
310 /// # Example
311 ///
312 /// ```
313 /// use iced_code_editor::CodeEditor;
314 ///
315 /// let editor = CodeEditor::new("fn main() {}", "rs")
316 /// .with_reveal_in_file_manager_enabled(true);
317 /// assert!(editor.reveal_in_file_manager_enabled());
318 /// ```
319 #[must_use]
320 pub fn with_reveal_in_file_manager_enabled(
321 mut self,
322 enabled: bool,
323 ) -> Self {
324 self.set_reveal_in_file_manager_enabled(enabled);
325 self
326 }
327
328 /// Enables or disables Vim behavior for this editor instance.
329 ///
330 /// Changing this setting enters a clean Normal mode without modifying the
331 /// buffer or command history. Any secondary cursors are collapsed onto the
332 /// primary one, since Vim owns its own selection model.
333 ///
334 /// # Arguments
335 ///
336 /// * `enabled` - Whether to enable Vim behavior
337 ///
338 /// # Example
339 ///
340 /// ```
341 /// use iced_code_editor::{CodeEditor, VimMode};
342 ///
343 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
344 /// editor.set_vim_enabled(true);
345 ///
346 /// // Vim always starts in Normal mode.
347 /// assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
348 /// ```
349 pub fn set_vim_enabled(&mut self, enabled: bool) {
350 if self.is_grouping {
351 self.history.end_group();
352 self.is_grouping = false;
353 }
354 self.vim_enabled = enabled;
355 self.vim_state.enter_clean_normal_mode();
356 self.cursors.remove_all_but_primary();
357 let position = if enabled {
358 self.vim_normal_position(self.cursors.primary_position())
359 } else {
360 self.cursors.primary_position()
361 };
362 self.cursors.set_single(position);
363 self.is_dragging = false;
364 self.overlay_cache.clear();
365 }
366
367 /// Returns the active Vim mode, or `None` when Vim behavior is disabled.
368 ///
369 /// Use this to drive a mode indicator in the host application's status bar.
370 ///
371 /// # Returns
372 ///
373 /// `Some(mode)` when Vim is enabled, `None` otherwise
374 ///
375 /// # Example
376 ///
377 /// ```
378 /// use iced_code_editor::{CodeEditor, VimMode};
379 ///
380 /// let editor = CodeEditor::new("fn main() {}", "rs");
381 /// // `None` rather than a default mode, so a status bar can tell
382 /// // "Vim is off" apart from "Vim is in Normal mode".
383 /// assert_eq!(editor.vim_mode(), None);
384 ///
385 /// let editor = editor.with_vim_enabled(true);
386 /// assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
387 /// ```
388 pub fn vim_mode(&self) -> Option<VimMode> {
389 self.vim_enabled.then(|| self.vim_state.mode())
390 }
391
392 /// Sets the theme style for the editor.
393 ///
394 /// # Arguments
395 ///
396 /// * `style` - The style to apply to the editor
397 ///
398 /// # Example
399 ///
400 /// ```
401 /// use iced_code_editor::{CodeEditor, theme};
402 ///
403 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
404 /// editor.set_theme(theme::from_iced_theme(&iced::Theme::TokyoNightStorm));
405 /// ```
406 pub fn set_theme(&mut self, style: Style) {
407 self.style = style;
408 // The highlight cache stores resolved colors, not scopes, and the
409 // syntect palette is picked from the style's background lightness
410 // (see `CodeEditor::resolve_syntax`). A theme change can therefore
411 // flip light/dark and invalidate every cached span.
412 self.invalidate_highlight_from(0);
413 self.content_cache.clear();
414 self.overlay_cache.clear();
415 }
416
417 /// Sets the language for UI translations.
418 ///
419 /// This changes the language used for all UI text elements in the editor,
420 /// including search dialog tooltips, placeholders, and labels.
421 ///
422 /// # Arguments
423 ///
424 /// * `language` - The language to use for UI text
425 ///
426 /// # Example
427 ///
428 /// ```
429 /// use iced_code_editor::{CodeEditor, Language};
430 ///
431 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
432 /// editor.set_language(Language::French);
433 /// ```
434 pub fn set_language(&mut self, language: crate::i18n::Language) {
435 self.translations.set_language(language);
436 self.overlay_cache.clear();
437 }
438
439 /// Returns the current UI language.
440 ///
441 /// # Returns
442 ///
443 /// The currently active language for UI text
444 ///
445 /// # Example
446 ///
447 /// ```
448 /// use iced_code_editor::{CodeEditor, Language};
449 ///
450 /// let editor = CodeEditor::new("fn main() {}", "rs");
451 /// let current_lang = editor.language();
452 /// ```
453 pub fn language(&self) -> crate::i18n::Language {
454 self.translations.language()
455 }
456
457 /// Sets whether line wrapping is enabled.
458 ///
459 /// When enabled, long lines will wrap at the viewport width or at a
460 /// configured column width.
461 ///
462 /// # Arguments
463 ///
464 /// * `enabled` - Whether to enable line wrapping
465 ///
466 /// # Example
467 ///
468 /// ```
469 /// use iced_code_editor::CodeEditor;
470 ///
471 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
472 /// editor.set_wrap_enabled(false); // Disable wrapping
473 /// ```
474 pub fn set_wrap_enabled(&mut self, enabled: bool) {
475 if self.wrap_enabled != enabled {
476 self.wrap_enabled = enabled;
477 if enabled {
478 self.horizontal_scroll_offset = 0.0;
479 }
480 self.content_cache.clear();
481 self.overlay_cache.clear();
482 }
483 }
484
485 /// Enables or disables visible whitespace rendering.
486 ///
487 /// When enabled, space characters are rendered as `·` and tab characters
488 /// as `→`, both drawn in a dimmed color to remain non-intrusive. Toggling
489 /// this setting clears the content cache to trigger an immediate redraw.
490 ///
491 /// # Arguments
492 ///
493 /// * `enabled` - Whether to show whitespace characters
494 ///
495 /// # Example
496 ///
497 /// ```
498 /// use iced_code_editor::CodeEditor;
499 ///
500 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
501 /// editor.set_show_whitespace(true);
502 /// ```
503 pub fn set_show_whitespace(&mut self, enabled: bool) {
504 if self.show_whitespace != enabled {
505 self.show_whitespace = enabled;
506 self.content_cache.clear();
507 }
508 }
509
510 /// Enables or disables indentation guides.
511 ///
512 /// Indentation guides are faint vertical lines drawn at every indentation
513 /// level, making the nesting of a block visible at a glance. Their spacing
514 /// follows [`CodeEditor::indent_style`]. Toggling this setting clears the
515 /// content cache to trigger an immediate redraw.
516 ///
517 /// # Arguments
518 ///
519 /// * `enabled` - Whether to draw indentation guides
520 ///
521 /// # Example
522 ///
523 /// ```
524 /// use iced_code_editor::CodeEditor;
525 ///
526 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
527 /// editor.set_show_indent_guides(false);
528 /// ```
529 pub fn set_show_indent_guides(&mut self, enabled: bool) {
530 if self.show_indent_guides != enabled {
531 self.show_indent_guides = enabled;
532 self.content_cache.clear();
533 }
534 }
535
536 /// Enables or disables inline color previews.
537 ///
538 /// An inline color preview is a small square drawn just after a color
539 /// literal — `#1e1e2e`, `0xFF6B6B`, `rgb(58, 123, 213)` — filled with the
540 /// color the literal denotes, so a palette can be read without decoding
541 /// hexadecimal by eye. Toggling this setting clears the content cache to
542 /// trigger an immediate redraw.
543 ///
544 /// # Arguments
545 ///
546 /// * `enabled` - Whether to draw color-preview swatches
547 ///
548 /// # Example
549 ///
550 /// ```
551 /// use iced_code_editor::CodeEditor;
552 ///
553 /// let mut editor = CodeEditor::new("body { color: #f0c; }", "css");
554 /// editor.set_show_color_previews(false);
555 /// ```
556 pub fn set_show_color_previews(&mut self, enabled: bool) {
557 if self.show_color_previews != enabled {
558 self.show_color_previews = enabled;
559 self.content_cache.clear();
560 }
561 }
562
563 /// Enables or disables diagnostics.
564 ///
565 /// While disabled, no diagnostic is underlined and
566 /// [`CodeEditor::diagnostics_at`] returns nothing, so a host shows no
567 /// message on hover. The list passed to [`CodeEditor::set_diagnostics`] is
568 /// still stored and kept up to date, so enabling diagnostics again shows
569 /// the current list at once, without waiting for the source to publish
570 /// again. Toggling this setting clears the overlay cache to trigger an
571 /// immediate redraw.
572 ///
573 /// # Arguments
574 ///
575 /// * `enabled` - Whether to underline and report diagnostics
576 ///
577 /// # Example
578 ///
579 /// ```
580 /// use iced_code_editor::{
581 /// CodeEditor, Diagnostic, DiagnosticSeverity, LspPosition, LspRange,
582 /// };
583 ///
584 /// let mut editor = CodeEditor::new("let x = 1;", "rs");
585 /// let at = LspPosition { line: 0, character: 4 };
586 /// editor.set_diagnostics(vec![Diagnostic::new(
587 /// LspRange { start: at, end: at },
588 /// DiagnosticSeverity::Warning,
589 /// "unused variable",
590 /// )]);
591 ///
592 /// editor.set_diagnostics_enabled(false);
593 /// assert!(editor.diagnostics_at(at).is_empty());
594 /// assert_eq!(editor.diagnostics().len(), 1); // still stored
595 ///
596 /// editor.set_diagnostics_enabled(true);
597 /// assert_eq!(editor.diagnostics_at(at).len(), 1);
598 /// ```
599 pub fn set_diagnostics_enabled(&mut self, enabled: bool) {
600 if self.diagnostics_enabled != enabled {
601 self.diagnostics_enabled = enabled;
602 self.overlay_cache.clear();
603 }
604 }
605
606 /// Enables or disables sticky scroll.
607 ///
608 /// Sticky scroll pins the header lines of the blocks enclosing the topmost
609 /// visible line to the top of the viewport, so the structural context stays
610 /// readable while scrolling deep inside a long block. Clicking a pinned
611 /// header scrolls back to it. Enclosing blocks are detected from
612 /// indentation, exactly like code folding, so the feature is
613 /// language-agnostic but follows the file's indentation rather than its
614 /// syntax tree.
615 ///
616 /// Independent of [`CodeEditor::set_folding_enabled`], despite sharing that
617 /// detection: turning code folding off removes the fold chevrons and leaves
618 /// the pinned headers alone.
619 ///
620 /// # Arguments
621 ///
622 /// * `enabled` - Whether to pin enclosing block headers above the viewport
623 ///
624 /// # Example
625 ///
626 /// ```
627 /// use iced_code_editor::CodeEditor;
628 ///
629 /// let mut editor = CodeEditor::new("fn main() {\n let x = 1;\n}", "rs");
630 /// editor.set_sticky_scroll_enabled(false);
631 /// ```
632 pub fn set_sticky_scroll_enabled(&mut self, enabled: bool) {
633 self.sticky_scroll_enabled = enabled;
634 }
635
636 /// Enables or disables the matching-bracket/quote-pair highlight overlay.
637 ///
638 /// When enabled, placing the cursor next to a bracket (`(`, `)`, `[`,
639 /// `]`, `{`, `}`) or a quote (`"`, `'`) highlights it and its matching
640 /// pair. When disabled, no matching scan or highlight is performed.
641 ///
642 /// # Arguments
643 ///
644 /// * `enabled` - Whether to enable the bracket/quote-matching highlight
645 ///
646 /// # Example
647 ///
648 /// ```
649 /// use iced_code_editor::CodeEditor;
650 ///
651 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
652 /// editor.set_bracket_match_highlight_enabled(false); // Disable
653 /// ```
654 pub fn set_bracket_match_highlight_enabled(&mut self, enabled: bool) {
655 if self.bracket_match_highlight_enabled != enabled {
656 self.bracket_match_highlight_enabled = enabled;
657 self.overlay_cache.clear();
658 }
659 }
660
661 /// Enables or disables bracket-pair colorization (rainbow brackets).
662 ///
663 /// When enabled, each `( ) [ ] { }` is colored by its nesting depth, so a
664 /// matching pair always shares the same color, cycling through a fixed
665 /// palette as depth increases. When disabled, brackets render with their
666 /// normal syntax-highlight color.
667 ///
668 /// # Arguments
669 ///
670 /// * `enabled` - Whether to enable bracket-pair colorization
671 ///
672 /// # Example
673 ///
674 /// ```
675 /// use iced_code_editor::CodeEditor;
676 ///
677 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
678 /// editor.set_bracket_pair_colorization_enabled(false); // Disable
679 /// ```
680 pub fn set_bracket_pair_colorization_enabled(&mut self, enabled: bool) {
681 if self.bracket_pair_colorization_enabled != enabled {
682 self.bracket_pair_colorization_enabled = enabled;
683 self.content_cache.clear();
684 }
685 }
686
687 /// Enables or disables code folding (collapse/expand blocks).
688 ///
689 /// When disabled, no fold chevrons are drawn and all lines are shown
690 /// regardless of the collapsed state (which is preserved, so re-enabling
691 /// restores the previously collapsed blocks).
692 ///
693 /// # Arguments
694 ///
695 /// * `enabled` - Whether to enable code folding
696 ///
697 /// # Example
698 ///
699 /// ```
700 /// use iced_code_editor::CodeEditor;
701 ///
702 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
703 /// editor.set_folding_enabled(true);
704 /// ```
705 pub fn set_folding_enabled(&mut self, enabled: bool) {
706 if self.folding_enabled != enabled {
707 self.folding_enabled = enabled;
708 self.bump_fold_revision();
709 }
710 }
711
712 /// Sets when fold markers (the `▼`/`▶` chevrons) are drawn in the gutter.
713 ///
714 /// [`FoldMarkers::Never`] also frees the fold-marker column, so the text
715 /// moves closer to the line numbers; blocks can still be folded with the
716 /// keyboard shortcuts and the API. The fold state is kept across changes.
717 ///
718 /// # Arguments
719 ///
720 /// * `markers` - When fold markers are drawn
721 ///
722 /// # Example
723 ///
724 /// ```
725 /// use iced_code_editor::{CodeEditor, FoldMarkers};
726 ///
727 /// let mut editor = CodeEditor::new("fn main() {\n body();\n}", "rs");
728 /// editor.set_fold_markers(FoldMarkers::Never);
729 /// assert_eq!(editor.fold_markers(), FoldMarkers::Never);
730 /// ```
731 pub fn set_fold_markers(&mut self, markers: FoldMarkers) {
732 if self.fold_markers != markers {
733 self.fold_markers = markers;
734 // `Never` changes the gutter width, hence wrapping and hit tests.
735 self.bump_fold_revision();
736 }
737 }
738
739 /// Returns when fold markers are drawn in the gutter.
740 ///
741 /// # Example
742 ///
743 /// ```
744 /// use iced_code_editor::{CodeEditor, FoldMarkers};
745 ///
746 /// let editor = CodeEditor::new("fn main() {}", "rs");
747 /// assert_eq!(editor.fold_markers(), FoldMarkers::Always);
748 /// ```
749 pub fn fold_markers(&self) -> FoldMarkers {
750 self.fold_markers
751 }
752
753 /// Sets which side of the line numbers the fold-marker column sits on.
754 ///
755 /// # Arguments
756 ///
757 /// * `position` - Where the fold-marker column goes
758 ///
759 /// # Example
760 ///
761 /// ```
762 /// use iced_code_editor::{CodeEditor, FoldMarkerPosition};
763 ///
764 /// let mut editor = CodeEditor::new("fn main() {\n body();\n}", "rs");
765 /// editor.set_fold_marker_position(FoldMarkerPosition::BeforeLineNumbers);
766 /// ```
767 pub fn set_fold_marker_position(&mut self, position: FoldMarkerPosition) {
768 if self.fold_marker_position != position {
769 self.fold_marker_position = position;
770 self.content_cache.clear();
771 self.overlay_cache.clear();
772 }
773 }
774
775 /// Returns which side of the line numbers the fold-marker column sits on.
776 ///
777 /// # Example
778 ///
779 /// ```
780 /// use iced_code_editor::{CodeEditor, FoldMarkerPosition};
781 ///
782 /// let editor = CodeEditor::new("fn main() {}", "rs");
783 /// assert_eq!(
784 /// editor.fold_marker_position(),
785 /// FoldMarkerPosition::AfterLineNumbers
786 /// );
787 /// ```
788 pub fn fold_marker_position(&self) -> FoldMarkerPosition {
789 self.fold_marker_position
790 }
791
792 /// Enables or disables automatic indentation on Enter.
793 ///
794 /// When enabled, pressing Enter copies the leading whitespace of the
795 /// current line to the new line. When disabled, the cursor is placed
796 /// at column 0 on the new line.
797 ///
798 /// # Arguments
799 ///
800 /// * `enabled` - `true` to enable auto-indentation, `false` to disable
801 ///
802 /// # Example
803 ///
804 /// ```
805 /// use iced_code_editor::CodeEditor;
806 ///
807 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
808 /// editor.set_auto_indent_enabled(false);
809 /// assert!(!editor.auto_indent_enabled());
810 /// ```
811 pub fn set_auto_indent_enabled(&mut self, enabled: bool) {
812 self.auto_indent_enabled = enabled;
813 }
814
815 /// Enables or disables auto-closing of brackets and quotes.
816 ///
817 /// When enabled, typing an opening bracket/quote (`(`, `[`, `{`, `"`,
818 /// `'`) auto-inserts its matching closing character with the cursor
819 /// placed between them, typing the closing character right before an
820 /// already-inserted match moves the cursor past it instead of
821 /// duplicating it, and typing an opening bracket/quote while text is
822 /// selected wraps the selection in the pair instead of replacing it.
823 ///
824 /// # Arguments
825 ///
826 /// * `enabled` - `true` to enable auto-closing, `false` to disable
827 ///
828 /// # Example
829 ///
830 /// ```
831 /// use iced_code_editor::CodeEditor;
832 ///
833 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
834 /// editor.set_auto_close_brackets(false); // Disable auto-closing
835 /// ```
836 pub fn set_auto_close_brackets(&mut self, enabled: bool) {
837 self.auto_close_brackets = enabled;
838 }
839
840 /// Sets the indentation style used when pressing the Tab key.
841 ///
842 /// # Arguments
843 ///
844 /// * `style` - The indentation style (`IndentStyle::Spaces(n)` or `IndentStyle::Tab`)
845 ///
846 /// # Example
847 ///
848 /// ```
849 /// use iced_code_editor::{CodeEditor, IndentStyle};
850 ///
851 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
852 ///
853 /// // Indent with a real tab character instead of the default 4 spaces.
854 /// editor.set_indent_style(IndentStyle::Tab);
855 /// assert_eq!(editor.indent_style(), IndentStyle::Tab);
856 ///
857 /// // Or pick a different space width.
858 /// editor.set_indent_style(IndentStyle::Spaces(2));
859 /// assert_eq!(editor.indent_style(), IndentStyle::Spaces(2));
860 /// ```
861 pub fn set_indent_style(&mut self, style: IndentStyle) {
862 if self.indent_style != style {
863 self.indent_style = style;
864 self.content_cache.clear();
865 }
866 }
867
868 /// Returns the current indentation style.
869 ///
870 /// # Returns
871 ///
872 /// The current [`IndentStyle`] configured for this editor
873 ///
874 /// # Example
875 ///
876 /// ```
877 /// use iced_code_editor::{CodeEditor, IndentStyle};
878 ///
879 /// let editor = CodeEditor::new("fn main() {}", "rs");
880 /// assert_eq!(editor.indent_style(), IndentStyle::Spaces(4));
881 /// ```
882 pub fn indent_style(&self) -> IndentStyle {
883 self.indent_style
884 }
885
886 /// Enables or disables the search/replace functionality.
887 ///
888 /// When disabled, search/replace keyboard shortcuts (Ctrl+F, Ctrl+H, F3)
889 /// will be ignored. If the search dialog is currently open, it will be closed.
890 ///
891 /// # Arguments
892 ///
893 /// * `enabled` - Whether to enable search/replace functionality
894 ///
895 /// # Example
896 ///
897 /// ```
898 /// use iced_code_editor::CodeEditor;
899 ///
900 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
901 /// editor.set_search_replace_enabled(false); // Disable search/replace
902 /// ```
903 pub fn set_search_replace_enabled(&mut self, enabled: bool) {
904 self.search_replace_enabled = enabled;
905 if !enabled && self.search_state.is_open {
906 self.search_state.close();
907 }
908 }
909
910 /// Returns the syntax highlighting language identifier for this editor.
911 ///
912 /// This is the language key passed at construction (e.g., `"lua"`, `"rs"`, `"py"`).
913 ///
914 /// # Examples
915 ///
916 /// ```
917 /// use iced_code_editor::CodeEditor;
918 /// let editor = CodeEditor::new("fn main() {}", "rs");
919 /// assert_eq!(editor.syntax(), "rs");
920 /// ```
921 pub fn syntax(&self) -> &str {
922 &self.syntax
923 }
924
925 /// Returns the display name of the grammar actually used to highlight
926 /// this editor's content.
927 ///
928 /// [`CodeEditor::syntax`] returns the raw identifier the host set; this
929 /// returns what the highlighter resolved it to, so it also reports the
930 /// plain-text fallback when no grammar matches. It is the value to show in
931 /// a status bar: it tells the user which language the editor is really
932 /// coloring with, not which one was requested.
933 ///
934 /// # Returns
935 ///
936 /// The grammar's name (e.g. `"Rust"`, `"Lua"`, `"Plain Text"`).
937 ///
938 /// # Example
939 ///
940 /// ```
941 /// use iced_code_editor::CodeEditor;
942 ///
943 /// let editor = CodeEditor::new("fn main() {}", "rs");
944 /// assert_eq!(editor.syntax_name(), "Rust");
945 ///
946 /// // An identifier no bundled grammar matches degrades to plain text.
947 /// let unknown = CodeEditor::new("...", "no-such-language");
948 /// assert_eq!(unknown.syntax_name(), "Plain Text");
949 /// ```
950 pub fn syntax_name(&self) -> &'static str {
951 let (_, syntax, _) = self.resolve_syntax();
952 syntax.map_or("Plain Text", |syntax| syntax.name.as_str())
953 }
954
955 /// Sets the syntax highlighting language identifier for this editor.
956 ///
957 /// Use this when the content a single editor shows changes language --
958 /// typically after opening or saving a file under a different extension.
959 /// The identifier is the same key [`CodeEditor::new`] takes: a file
960 /// extension (`"rs"`, `"py"`, `"toml"`) or one of the aliases
961 /// `resolve_syntax` normalizes (`"rust"`, `"python"`, `"markdown"`, ...).
962 /// An unknown identifier falls back to plain text rather than losing the
963 /// content.
964 ///
965 /// Changing the identifier drops the cached per-line highlighting, so the
966 /// visible lines are re-tokenized on the next render. Setting the current
967 /// identifier again does nothing.
968 ///
969 /// # Arguments
970 ///
971 /// * `syntax` - Syntax highlighting language identifier (e.g. `"rs"`)
972 ///
973 /// # Example
974 ///
975 /// ```
976 /// use iced_code_editor::CodeEditor;
977 ///
978 /// let mut editor = CodeEditor::new("-- comment", "lua");
979 /// assert_eq!(editor.syntax(), "lua");
980 ///
981 /// editor.set_syntax("rs");
982 /// assert_eq!(editor.syntax(), "rs");
983 /// ```
984 pub fn set_syntax(&mut self, syntax: &str) {
985 if self.syntax == syntax {
986 return;
987 }
988
989 self.syntax.clear();
990 self.syntax.push_str(syntax);
991 // `highlighted_line_cached` rebuilds `HighlightCache` on its own when
992 // the active syntax no longer matches, so only the canvas needs an
993 // explicit redraw here.
994 self.content_cache.clear();
995 }
996
997 /// Sets the wrap column (fixed width wrapping).
998 ///
999 /// When set to `Some(n)`, lines will wrap at column `n`.
1000 /// When set to `None`, lines will wrap at the viewport width.
1001 /// Zero is normalized to one narrow-character column.
1002 ///
1003 /// # Arguments
1004 ///
1005 /// * `column` - The column to wrap at, or None for viewport-based wrapping
1006 ///
1007 /// # Example
1008 ///
1009 /// ```
1010 /// use iced_code_editor::CodeEditor;
1011 ///
1012 /// let editor = CodeEditor::new("fn main() {}", "rs")
1013 /// .with_wrap_column(Some(80)); // Wrap at 80 characters
1014 /// ```
1015 #[must_use]
1016 pub fn with_wrap_column(mut self, column: Option<usize>) -> Self {
1017 self.set_wrap_column(column);
1018 self
1019 }
1020
1021 /// Sets the soft-wrap width in narrow-character columns.
1022 ///
1023 /// `None` uses the viewport width; `Some(0)` is normalized to `Some(1)`.
1024 /// This does not modify document text or enable wrapping. Changing the
1025 /// width invalidates text and overlay geometry. While wrapping is enabled,
1026 /// horizontal scrolling is reset so the new visual segments stay visible.
1027 ///
1028 /// # Arguments
1029 ///
1030 /// * `column` - Wrap width in columns, or `None` for the viewport width
1031 ///
1032 /// # Example
1033 ///
1034 /// ```
1035 /// use iced_code_editor::CodeEditor;
1036 ///
1037 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
1038 /// editor.set_wrap_column(Some(80));
1039 /// assert_eq!(editor.wrap_column(), Some(80));
1040 ///
1041 /// editor.set_wrap_column(Some(0)); // Normalized to one column
1042 /// assert_eq!(editor.wrap_column(), Some(1));
1043 ///
1044 /// editor.set_wrap_column(None); // Back to viewport-based wrapping
1045 /// assert_eq!(editor.wrap_column(), None);
1046 /// ```
1047 pub fn set_wrap_column(&mut self, column: Option<usize>) {
1048 let column = column.map(|value| value.max(1));
1049 if self.wrap_column != column {
1050 self.wrap_column = column;
1051 if self.wrap_enabled {
1052 self.horizontal_scroll_offset = 0.0;
1053 }
1054 self.content_cache.clear();
1055 self.overlay_cache.clear();
1056 }
1057 }
1058
1059 /// Returns the soft-wrap width in narrow-character columns.
1060 ///
1061 /// `None` means viewport-based wrapping. The value is retained while
1062 /// wrapping is disabled; zero is normalized to one by the setter.
1063 ///
1064 /// # Returns
1065 ///
1066 /// The fixed wrap width, or `None` when wrapping follows the viewport
1067 ///
1068 /// # Example
1069 ///
1070 /// ```
1071 /// use iced_code_editor::CodeEditor;
1072 ///
1073 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
1074 /// assert_eq!(editor.wrap_column(), None); // Viewport width by default
1075 ///
1076 /// editor.set_wrap_column(Some(100));
1077 /// editor.set_wrap_enabled(false);
1078 /// assert_eq!(editor.wrap_column(), Some(100)); // Kept while disabled
1079 /// ```
1080 pub fn wrap_column(&self) -> Option<usize> {
1081 self.wrap_column
1082 }
1083
1084 /// Sets whether line numbers are displayed.
1085 ///
1086 /// When disabled, the gutter is completely removed (0px width),
1087 /// providing more space for code display.
1088 ///
1089 /// # Arguments
1090 ///
1091 /// * `enabled` - Whether to display line numbers
1092 ///
1093 /// # Example
1094 ///
1095 /// ```
1096 /// use iced_code_editor::CodeEditor;
1097 ///
1098 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
1099 /// editor.set_line_numbers_enabled(false); // Hide line numbers
1100 /// ```
1101 pub fn set_line_numbers_enabled(&mut self, enabled: bool) {
1102 if self.line_numbers_enabled != enabled {
1103 self.line_numbers_enabled = enabled;
1104 self.content_cache.clear();
1105 self.overlay_cache.clear();
1106 }
1107 }
1108}
1109
1110#[cfg(test)]
1111mod tests {
1112 use super::*;
1113 use crate::canvas_editor::features::context_menu::ContextMenuItem;
1114 use crate::canvas_editor::features::vim;
1115
1116 #[test]
1117 fn test_custom_context_menu_configuration() {
1118 let custom_entries = vec![
1119 ContextMenuEntry::item("format", "Format document")
1120 .with_shortcut("Shift+Alt+F"),
1121 ContextMenuEntry::separator(),
1122 ContextMenuEntry::Item(
1123 ContextMenuItem::new("rename", "Rename symbol")
1124 .with_enabled(false),
1125 ),
1126 ];
1127
1128 let editor = CodeEditor::new("", "rs")
1129 .with_custom_context_menu_entries(custom_entries.clone())
1130 .with_default_context_menu_enabled(false);
1131
1132 assert_eq!(editor.custom_context_menu_entries(), custom_entries);
1133 assert!(!editor.default_context_menu_enabled());
1134
1135 let default_editor = CodeEditor::new("", "rs");
1136 assert!(default_editor.custom_context_menu_entries().is_empty());
1137 assert!(default_editor.default_context_menu_enabled());
1138 }
1139
1140 #[test]
1141 fn test_reveal_in_file_manager_configuration() {
1142 let mut editor = CodeEditor::new("", "rs");
1143 assert!(!editor.reveal_in_file_manager_enabled());
1144
1145 editor.set_reveal_in_file_manager_enabled(true);
1146 assert!(editor.reveal_in_file_manager_enabled());
1147
1148 let editor =
1149 CodeEditor::new("", "rs").with_reveal_in_file_manager_enabled(true);
1150 assert!(editor.reveal_in_file_manager_enabled());
1151 }
1152
1153 #[test]
1154 fn test_auto_close_brackets_configuration() {
1155 let mut editor = CodeEditor::new("", "rs");
1156 assert!(editor.auto_close_brackets());
1157
1158 editor.set_auto_close_brackets(false);
1159 assert!(!editor.auto_close_brackets());
1160
1161 editor.set_auto_close_brackets(true);
1162 assert!(editor.auto_close_brackets());
1163 }
1164
1165 #[test]
1166 fn test_bracket_match_highlight_configuration() {
1167 let mut editor = CodeEditor::new("", "rs");
1168 assert!(editor.bracket_match_highlight_enabled());
1169
1170 editor.set_bracket_match_highlight_enabled(false);
1171 assert!(!editor.bracket_match_highlight_enabled());
1172
1173 editor.set_bracket_match_highlight_enabled(true);
1174 assert!(editor.bracket_match_highlight_enabled());
1175 }
1176
1177 #[test]
1178 fn test_bracket_pair_colorization_configuration() {
1179 let mut editor = CodeEditor::new("", "rs");
1180 assert!(editor.bracket_pair_colorization_enabled());
1181
1182 editor.set_bracket_pair_colorization_enabled(false);
1183 assert!(!editor.bracket_pair_colorization_enabled());
1184
1185 editor.set_bracket_pair_colorization_enabled(true);
1186 assert!(editor.bracket_pair_colorization_enabled());
1187 }
1188
1189 #[test]
1190 fn test_show_indent_guides_configuration() {
1191 let mut editor = CodeEditor::new("", "rs");
1192 assert!(editor.show_indent_guides());
1193
1194 editor.set_show_indent_guides(false);
1195 assert!(!editor.show_indent_guides());
1196
1197 editor.set_show_indent_guides(true);
1198 assert!(editor.show_indent_guides());
1199
1200 let editor = CodeEditor::new("", "rs").with_show_indent_guides(false);
1201 assert!(!editor.show_indent_guides());
1202 }
1203
1204 #[test]
1205 fn test_show_color_previews_configuration() {
1206 let mut editor = CodeEditor::new("", "rs");
1207 assert!(editor.show_color_previews());
1208
1209 editor.set_show_color_previews(false);
1210 assert!(!editor.show_color_previews());
1211
1212 editor.set_show_color_previews(true);
1213 assert!(editor.show_color_previews());
1214
1215 let editor = CodeEditor::new("", "rs").with_show_color_previews(false);
1216 assert!(!editor.show_color_previews());
1217 }
1218
1219 #[test]
1220 fn vim_disabled_by_default() {
1221 let editor = CodeEditor::new("unchanged", "rs");
1222
1223 assert!(!editor.vim_enabled());
1224 assert_eq!(editor.vim_mode(), None);
1225 assert_eq!(editor.content(), "unchanged");
1226 assert!(!editor.can_undo());
1227 assert!(!editor.can_redo());
1228 }
1229
1230 #[test]
1231 fn vim_enable_enters_clean_normal_mode() {
1232 let mut editor = CodeEditor::new("unchanged", "rs");
1233 assert_eq!(editor.vim_state.parse_key('9'), None);
1234 assert_eq!(editor.vim_state.parse_key('d'), None);
1235
1236 editor.set_vim_enabled(true);
1237
1238 assert!(editor.vim_enabled());
1239 assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
1240 assert_eq!(
1241 editor.vim_state.parse_key('l'),
1242 Some(vim::VimAction::Motion {
1243 motion: vim::VimMotion::Right,
1244 count: 1,
1245 explicit_count: false,
1246 })
1247 );
1248 assert_eq!(editor.content(), "unchanged");
1249 assert!(!editor.can_undo());
1250 assert!(!editor.can_redo());
1251 }
1252
1253 #[test]
1254 fn vim_disable_clears_pending_state() {
1255 let mut editor =
1256 CodeEditor::new("unchanged", "rs").with_vim_enabled(true);
1257 assert_eq!(editor.vim_state.parse_key('4'), None);
1258 assert_eq!(editor.vim_state.parse_key('d'), None);
1259
1260 editor.set_vim_enabled(false);
1261 assert!(!editor.vim_enabled());
1262 assert_eq!(editor.vim_mode(), None);
1263
1264 editor.set_vim_enabled(true);
1265 assert_eq!(
1266 editor.vim_state.parse_key('w'),
1267 Some(vim::VimAction::Motion {
1268 motion: vim::VimMotion::WordForward,
1269 count: 1,
1270 explicit_count: false,
1271 })
1272 );
1273 assert_eq!(editor.content(), "unchanged");
1274 assert!(!editor.can_undo());
1275 assert!(!editor.can_redo());
1276 }
1277
1278 #[test]
1279 fn vim_reset_clears_pending_state() {
1280 let mut editor = CodeEditor::new("before", "rs").with_vim_enabled(true);
1281 assert_eq!(editor.vim_state.parse_key('3'), None);
1282 assert_eq!(editor.vim_state.parse_key('g'), None);
1283
1284 let _ = editor.reset("after");
1285
1286 assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
1287 assert_eq!(editor.vim_state.parse_key('g'), None);
1288 assert_eq!(
1289 editor.vim_state.parse_key('g'),
1290 Some(vim::VimAction::Motion {
1291 motion: vim::VimMotion::DocumentStart,
1292 count: 1,
1293 explicit_count: false,
1294 })
1295 );
1296 assert_eq!(editor.content(), "after");
1297 assert!(!editor.can_undo());
1298 assert!(!editor.can_redo());
1299 }
1300
1301 #[test]
1302 fn test_syntax_getter() {
1303 let editor = CodeEditor::new("", "lua");
1304 assert_eq!(editor.syntax(), "lua");
1305 }
1306
1307 #[test]
1308 fn test_syntax_name_resolves_grammar_and_fallback() {
1309 assert_eq!(CodeEditor::new("", "rs").syntax_name(), "Rust");
1310 assert_eq!(CodeEditor::new("", "lua").syntax_name(), "Lua");
1311 // Aliases normalized by `resolve_syntax` resolve to the same grammar.
1312 assert_eq!(CodeEditor::new("", "rust").syntax_name(), "Rust");
1313 // Unknown identifiers degrade rather than losing the content.
1314 assert_eq!(
1315 CodeEditor::new("", "no-such-language").syntax_name(),
1316 "Plain Text"
1317 );
1318 }
1319
1320 #[test]
1321 fn test_syntax_name_follows_set_syntax() {
1322 let mut editor = CodeEditor::new("", "lua");
1323 assert_eq!(editor.syntax_name(), "Lua");
1324
1325 editor.set_syntax("py");
1326 assert_eq!(editor.syntax_name(), "Python");
1327 }
1328
1329 #[test]
1330 fn test_set_syntax_updates_identifier() {
1331 let mut editor = CodeEditor::new("-- comment", "lua");
1332
1333 editor.set_syntax("rs");
1334 assert_eq!(editor.syntax(), "rs");
1335
1336 // Setting the same identifier again is a no-op, not an error.
1337 editor.set_syntax("rs");
1338 assert_eq!(editor.syntax(), "rs");
1339 }
1340
1341 /// Color of the first highlighted span of the editor's first line, or
1342 /// `None` when syntect ships no definition for the active syntax.
1343 fn first_span_color(editor: &CodeEditor) -> Option<iced::Color> {
1344 let (syntax_set, syntax, theme) = editor.resolve_syntax();
1345 editor
1346 .highlighted_line_cached(0, syntax?, theme?, syntax_set)
1347 .first()
1348 .map(|(color, _)| *color)
1349 }
1350
1351 #[test]
1352 fn test_set_syntax_rehighlights_with_the_new_language() {
1353 // A Rust doc comment: a comment under `rs`, plain code under `lua`.
1354 let mut editor = CodeEditor::new("/// doc", "lua");
1355
1356 let as_lua = first_span_color(&editor);
1357 assert!(as_lua.is_some(), "syntect ships a Lua definition");
1358
1359 editor.set_syntax("rs");
1360 let as_rust = first_span_color(&editor);
1361 assert!(as_rust.is_some(), "syntect ships a Rust definition");
1362
1363 assert_ne!(
1364 as_lua, as_rust,
1365 "`/// doc` must not keep its Lua colors after switching to Rust"
1366 );
1367 }
1368
1369 #[test]
1370 fn test_set_theme_rehighlights_for_the_new_palette() {
1371 let mut editor = CodeEditor::new("// comment", "rs");
1372
1373 editor.set_theme(crate::theme::from_iced_theme(&iced::Theme::Dark));
1374 let dark = first_span_color(&editor);
1375 assert!(dark.is_some(), "syntect ships a Rust definition");
1376
1377 editor.set_theme(crate::theme::from_iced_theme(&iced::Theme::Light));
1378 let light = first_span_color(&editor);
1379
1380 assert_ne!(
1381 dark, light,
1382 "the cached dark colors must be dropped when the theme turns light"
1383 );
1384 }
1385
1386 #[test]
1387 fn test_folding_enabled_by_default() {
1388 let editor = CodeEditor::new("fn main() {}", "rs");
1389 assert!(editor.folding_enabled());
1390 }
1391}