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