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::ContextMenuEntry;
6use crate::canvas_editor::features::vim::VimMode;
7use crate::canvas_editor::{CodeEditor, IndentStyle};
8use crate::theme::Style;
9
10impl CodeEditor {
11 /// Replaces the custom context-menu entries.
12 ///
13 /// Custom entries are shown in addition to the built-in editing actions;
14 /// see [`Self::set_default_context_menu_enabled`] to hide those.
15 ///
16 /// # Arguments
17 ///
18 /// * `entries` - The entries to display, in order
19 ///
20 /// # Example
21 ///
22 /// ```
23 /// use iced_code_editor::{CodeEditor, ContextMenuEntry};
24 ///
25 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
26 /// editor.set_custom_context_menu_entries(vec![
27 /// ContextMenuEntry::item("format", "Format Document")
28 /// .with_shortcut("Ctrl+Shift+F"),
29 /// ContextMenuEntry::separator(),
30 /// ContextMenuEntry::item("rename", "Rename Symbol").with_enabled(false),
31 /// ]);
32 /// assert_eq!(editor.custom_context_menu_entries().len(), 3);
33 /// ```
34 pub fn set_custom_context_menu_entries(
35 &mut self,
36 entries: Vec<ContextMenuEntry>,
37 ) {
38 self.custom_context_menu_entries = entries;
39 }
40
41 /// Replaces the custom context-menu entries using the builder pattern.
42 ///
43 /// # Arguments
44 ///
45 /// * `entries` - The entries to display, in order
46 ///
47 /// # Returns
48 ///
49 /// Self for method chaining
50 ///
51 /// # Example
52 ///
53 /// ```
54 /// use iced_code_editor::{CodeEditor, ContextMenuEntry};
55 ///
56 /// let editor = CodeEditor::new("fn main() {}", "rs")
57 /// .with_custom_context_menu_entries(vec![
58 /// ContextMenuEntry::item("format", "Format Document"),
59 /// ]);
60 /// assert_eq!(editor.custom_context_menu_entries().len(), 1);
61 /// ```
62 #[must_use]
63 pub fn with_custom_context_menu_entries(
64 mut self,
65 entries: Vec<ContextMenuEntry>,
66 ) -> Self {
67 self.set_custom_context_menu_entries(entries);
68 self
69 }
70
71 /// Returns the custom context-menu entries in display order.
72 ///
73 /// # Returns
74 ///
75 /// The entries previously set, or an empty slice if none were set
76 ///
77 /// # Example
78 ///
79 /// ```
80 /// use iced_code_editor::CodeEditor;
81 ///
82 /// let editor = CodeEditor::new("fn main() {}", "rs");
83 /// assert!(editor.custom_context_menu_entries().is_empty());
84 /// ```
85 pub fn custom_context_menu_entries(&self) -> &[ContextMenuEntry] {
86 &self.custom_context_menu_entries
87 }
88
89 /// Sets whether the built-in editing actions appear in the context menu.
90 ///
91 /// The built-in actions are undo/redo, cut/copy/paste, and select all.
92 /// Disabling them leaves only the custom entries, which is how a host
93 /// application replaces the menu wholesale rather than extending it.
94 ///
95 /// # Arguments
96 ///
97 /// * `enabled` - Whether to show the built-in actions
98 ///
99 /// # Example
100 ///
101 /// ```
102 /// use iced_code_editor::CodeEditor;
103 ///
104 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
105 /// editor.set_default_context_menu_enabled(false);
106 /// assert!(!editor.default_context_menu_enabled());
107 /// ```
108 pub fn set_default_context_menu_enabled(&mut self, enabled: bool) {
109 self.default_context_menu_enabled = enabled;
110 }
111
112 /// Sets built-in context-menu visibility using the builder pattern.
113 ///
114 /// # Arguments
115 ///
116 /// * `enabled` - Whether to show the built-in actions
117 ///
118 /// # Returns
119 ///
120 /// Self for method chaining
121 ///
122 /// # Example
123 ///
124 /// ```
125 /// use iced_code_editor::CodeEditor;
126 ///
127 /// let editor = CodeEditor::new("fn main() {}", "rs")
128 /// .with_default_context_menu_enabled(false);
129 /// assert!(!editor.default_context_menu_enabled());
130 /// ```
131 #[must_use]
132 pub fn with_default_context_menu_enabled(mut self, enabled: bool) -> Self {
133 self.set_default_context_menu_enabled(enabled);
134 self
135 }
136
137 /// Returns whether the built-in context-menu actions are enabled.
138 ///
139 /// # Returns
140 ///
141 /// `true` if the built-in actions are shown, `false` otherwise
142 ///
143 /// # Example
144 ///
145 /// ```
146 /// use iced_code_editor::CodeEditor;
147 ///
148 /// let editor = CodeEditor::new("fn main() {}", "rs");
149 /// // Shown by default.
150 /// assert!(editor.default_context_menu_enabled());
151 /// ```
152 pub fn default_context_menu_enabled(&self) -> bool {
153 self.default_context_menu_enabled
154 }
155
156 /// Sets whether the built-in reveal-in-file-manager action is shown.
157 ///
158 /// The action is only useful when the host application knows the file path
159 /// backing the editor, so it is off by default and the host opts in.
160 ///
161 /// # Arguments
162 ///
163 /// * `enabled` - Whether to show the reveal action
164 ///
165 /// # Example
166 ///
167 /// ```
168 /// use iced_code_editor::CodeEditor;
169 ///
170 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
171 /// editor.set_reveal_in_file_manager_enabled(true);
172 /// assert!(editor.reveal_in_file_manager_enabled());
173 /// ```
174 pub fn set_reveal_in_file_manager_enabled(&mut self, enabled: bool) {
175 self.reveal_in_file_manager_enabled = enabled;
176 }
177
178 /// Sets reveal-in-file-manager visibility using the builder pattern.
179 ///
180 /// # Arguments
181 ///
182 /// * `enabled` - Whether to show the reveal action
183 ///
184 /// # Returns
185 ///
186 /// Self for method chaining
187 ///
188 /// # Example
189 ///
190 /// ```
191 /// use iced_code_editor::CodeEditor;
192 ///
193 /// let editor = CodeEditor::new("fn main() {}", "rs")
194 /// .with_reveal_in_file_manager_enabled(true);
195 /// assert!(editor.reveal_in_file_manager_enabled());
196 /// ```
197 #[must_use]
198 pub fn with_reveal_in_file_manager_enabled(
199 mut self,
200 enabled: bool,
201 ) -> Self {
202 self.set_reveal_in_file_manager_enabled(enabled);
203 self
204 }
205
206 /// Returns whether the reveal-in-file-manager action is shown.
207 ///
208 /// # Returns
209 ///
210 /// `true` if the reveal action is shown, `false` otherwise
211 ///
212 /// # Example
213 ///
214 /// ```
215 /// use iced_code_editor::CodeEditor;
216 ///
217 /// let editor = CodeEditor::new("fn main() {}", "rs");
218 /// // Off by default: the editor doesn't know its own file path.
219 /// assert!(!editor.reveal_in_file_manager_enabled());
220 /// ```
221 pub fn reveal_in_file_manager_enabled(&self) -> bool {
222 self.reveal_in_file_manager_enabled
223 }
224
225 /// Enables or disables Vim behavior for this editor instance.
226 ///
227 /// Changing this setting enters a clean Normal mode without modifying the
228 /// buffer or command history. Any secondary cursors are collapsed onto the
229 /// primary one, since Vim owns its own selection model.
230 ///
231 /// # Arguments
232 ///
233 /// * `enabled` - Whether to enable Vim behavior
234 ///
235 /// # Example
236 ///
237 /// ```
238 /// use iced_code_editor::{CodeEditor, VimMode};
239 ///
240 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
241 /// editor.set_vim_enabled(true);
242 ///
243 /// // Vim always starts in Normal mode.
244 /// assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
245 /// ```
246 pub fn set_vim_enabled(&mut self, enabled: bool) {
247 if self.is_grouping {
248 self.history.end_group();
249 self.is_grouping = false;
250 }
251 self.vim_enabled = enabled;
252 self.vim_state.enter_clean_normal_mode();
253 self.cursors.remove_all_but_primary();
254 let position = if enabled {
255 self.vim_normal_position(self.cursors.primary_position())
256 } else {
257 self.cursors.primary_position()
258 };
259 self.cursors.set_single(position);
260 self.is_dragging = false;
261 self.overlay_cache.clear();
262 }
263
264 /// Sets whether Vim behavior is enabled using the builder pattern.
265 ///
266 /// # Arguments
267 ///
268 /// * `enabled` - Whether to enable Vim behavior
269 ///
270 /// # Returns
271 ///
272 /// Self for method chaining
273 ///
274 /// # Example
275 ///
276 /// ```
277 /// use iced_code_editor::CodeEditor;
278 ///
279 /// let editor = CodeEditor::new("fn main() {}", "rs")
280 /// .with_vim_enabled(true);
281 /// assert!(editor.vim_enabled());
282 /// ```
283 #[must_use]
284 pub fn with_vim_enabled(mut self, enabled: bool) -> Self {
285 self.set_vim_enabled(enabled);
286 self
287 }
288
289 /// Returns whether Vim behavior is enabled for this editor instance.
290 ///
291 /// # Returns
292 ///
293 /// `true` if Vim behavior is enabled, `false` otherwise
294 ///
295 /// # Example
296 ///
297 /// ```
298 /// use iced_code_editor::CodeEditor;
299 ///
300 /// let editor = CodeEditor::new("fn main() {}", "rs");
301 /// // Off by default.
302 /// assert!(!editor.vim_enabled());
303 /// ```
304 pub fn vim_enabled(&self) -> bool {
305 self.vim_enabled
306 }
307
308 /// Returns the active Vim mode, or `None` when Vim behavior is disabled.
309 ///
310 /// Use this to drive a mode indicator in the host application's status bar.
311 ///
312 /// # Returns
313 ///
314 /// `Some(mode)` when Vim is enabled, `None` otherwise
315 ///
316 /// # Example
317 ///
318 /// ```
319 /// use iced_code_editor::{CodeEditor, VimMode};
320 ///
321 /// let editor = CodeEditor::new("fn main() {}", "rs");
322 /// // `None` rather than a default mode, so a status bar can tell
323 /// // "Vim is off" apart from "Vim is in Normal mode".
324 /// assert_eq!(editor.vim_mode(), None);
325 ///
326 /// let editor = editor.with_vim_enabled(true);
327 /// assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
328 /// ```
329 pub fn vim_mode(&self) -> Option<VimMode> {
330 self.vim_enabled.then(|| self.vim_state.mode())
331 }
332
333 /// Sets the theme style for the editor.
334 ///
335 /// # Arguments
336 ///
337 /// * `style` - The style to apply to the editor
338 ///
339 /// # Example
340 ///
341 /// ```
342 /// use iced_code_editor::{CodeEditor, theme};
343 ///
344 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
345 /// editor.set_theme(theme::from_iced_theme(&iced::Theme::TokyoNightStorm));
346 /// ```
347 pub fn set_theme(&mut self, style: Style) {
348 self.style = style;
349 self.content_cache.clear();
350 self.overlay_cache.clear();
351 }
352
353 /// Sets the language for UI translations.
354 ///
355 /// This changes the language used for all UI text elements in the editor,
356 /// including search dialog tooltips, placeholders, and labels.
357 ///
358 /// # Arguments
359 ///
360 /// * `language` - The language to use for UI text
361 ///
362 /// # Example
363 ///
364 /// ```
365 /// use iced_code_editor::{CodeEditor, Language};
366 ///
367 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
368 /// editor.set_language(Language::French);
369 /// ```
370 pub fn set_language(&mut self, language: crate::i18n::Language) {
371 self.translations.set_language(language);
372 self.overlay_cache.clear();
373 }
374
375 /// Returns the current UI language.
376 ///
377 /// # Returns
378 ///
379 /// The currently active language for UI text
380 ///
381 /// # Example
382 ///
383 /// ```
384 /// use iced_code_editor::{CodeEditor, Language};
385 ///
386 /// let editor = CodeEditor::new("fn main() {}", "rs");
387 /// let current_lang = editor.language();
388 /// ```
389 pub fn language(&self) -> crate::i18n::Language {
390 self.translations.language()
391 }
392
393 /// Sets whether line wrapping is enabled.
394 ///
395 /// When enabled, long lines will wrap at the viewport width or at a
396 /// configured column width.
397 ///
398 /// # Arguments
399 ///
400 /// * `enabled` - Whether to enable line wrapping
401 ///
402 /// # Example
403 ///
404 /// ```
405 /// use iced_code_editor::CodeEditor;
406 ///
407 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
408 /// editor.set_wrap_enabled(false); // Disable wrapping
409 /// ```
410 pub fn set_wrap_enabled(&mut self, enabled: bool) {
411 if self.wrap_enabled != enabled {
412 self.wrap_enabled = enabled;
413 if enabled {
414 self.horizontal_scroll_offset = 0.0;
415 }
416 self.content_cache.clear();
417 self.overlay_cache.clear();
418 }
419 }
420
421 /// Returns whether line wrapping is enabled.
422 ///
423 /// # Returns
424 ///
425 /// `true` if line wrapping is enabled, `false` otherwise
426 ///
427 /// # Example
428 ///
429 /// ```
430 /// use iced_code_editor::CodeEditor;
431 ///
432 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
433 /// // Enabled by default.
434 /// assert!(editor.wrap_enabled());
435 ///
436 /// editor.set_wrap_enabled(false);
437 /// assert!(!editor.wrap_enabled());
438 /// ```
439 pub fn wrap_enabled(&self) -> bool {
440 self.wrap_enabled
441 }
442
443 /// Enables or disables visible whitespace rendering.
444 ///
445 /// When enabled, space characters are rendered as `·` and tab characters
446 /// as `→`, both drawn in a dimmed color to remain non-intrusive. Toggling
447 /// this setting clears the content cache to trigger an immediate redraw.
448 ///
449 /// # Arguments
450 ///
451 /// * `enabled` - Whether to show whitespace characters
452 ///
453 /// # Example
454 ///
455 /// ```
456 /// use iced_code_editor::CodeEditor;
457 ///
458 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
459 /// editor.set_show_whitespace(true);
460 /// ```
461 pub fn set_show_whitespace(&mut self, enabled: bool) {
462 if self.show_whitespace != enabled {
463 self.show_whitespace = enabled;
464 self.content_cache.clear();
465 }
466 }
467
468 /// Returns whether visible whitespace rendering is enabled.
469 ///
470 /// # Returns
471 ///
472 /// `true` if whitespace characters are rendered, `false` otherwise
473 ///
474 /// # Example
475 ///
476 /// ```
477 /// use iced_code_editor::CodeEditor;
478 ///
479 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
480 /// // Enabled by default.
481 /// assert!(editor.show_whitespace());
482 ///
483 /// editor.set_show_whitespace(false);
484 /// assert!(!editor.show_whitespace());
485 /// ```
486 pub fn show_whitespace(&self) -> bool {
487 self.show_whitespace
488 }
489
490 /// Enables or disables the matching-bracket/quote-pair highlight overlay.
491 ///
492 /// When enabled, placing the cursor next to a bracket (`(`, `)`, `[`,
493 /// `]`, `{`, `}`) or a quote (`"`, `'`) highlights it and its matching
494 /// pair. When disabled, no matching scan or highlight is performed.
495 ///
496 /// # Arguments
497 ///
498 /// * `enabled` - Whether to enable the bracket/quote-matching highlight
499 ///
500 /// # Example
501 ///
502 /// ```
503 /// use iced_code_editor::CodeEditor;
504 ///
505 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
506 /// editor.set_bracket_match_highlight_enabled(false); // Disable
507 /// ```
508 pub fn set_bracket_match_highlight_enabled(&mut self, enabled: bool) {
509 if self.bracket_match_highlight_enabled != enabled {
510 self.bracket_match_highlight_enabled = enabled;
511 self.overlay_cache.clear();
512 }
513 }
514
515 /// Returns whether the matching-bracket/quote-pair highlight overlay is enabled.
516 ///
517 /// # Returns
518 ///
519 /// `true` if the highlight is enabled, `false` otherwise
520 ///
521 /// # Example
522 ///
523 /// ```
524 /// use iced_code_editor::CodeEditor;
525 ///
526 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
527 /// // Enabled by default.
528 /// assert!(editor.bracket_match_highlight_enabled());
529 ///
530 /// editor.set_bracket_match_highlight_enabled(false);
531 /// assert!(!editor.bracket_match_highlight_enabled());
532 /// ```
533 pub fn bracket_match_highlight_enabled(&self) -> bool {
534 self.bracket_match_highlight_enabled
535 }
536
537 /// Enables or disables bracket-pair colorization (rainbow brackets).
538 ///
539 /// When enabled, each `( ) [ ] { }` is colored by its nesting depth, so a
540 /// matching pair always shares the same color, cycling through a fixed
541 /// palette as depth increases. When disabled, brackets render with their
542 /// normal syntax-highlight color.
543 ///
544 /// # Arguments
545 ///
546 /// * `enabled` - Whether to enable bracket-pair colorization
547 ///
548 /// # Example
549 ///
550 /// ```
551 /// use iced_code_editor::CodeEditor;
552 ///
553 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
554 /// editor.set_bracket_pair_colorization_enabled(false); // Disable
555 /// ```
556 pub fn set_bracket_pair_colorization_enabled(&mut self, enabled: bool) {
557 if self.bracket_pair_colorization_enabled != enabled {
558 self.bracket_pair_colorization_enabled = enabled;
559 self.content_cache.clear();
560 }
561 }
562
563 /// Returns whether bracket-pair colorization (rainbow brackets) is enabled.
564 ///
565 /// # Returns
566 ///
567 /// `true` if bracket-pair colorization is enabled, `false` otherwise
568 ///
569 /// # Example
570 ///
571 /// ```
572 /// use iced_code_editor::CodeEditor;
573 ///
574 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
575 /// // Enabled by default.
576 /// assert!(editor.bracket_pair_colorization_enabled());
577 ///
578 /// editor.set_bracket_pair_colorization_enabled(false);
579 /// assert!(!editor.bracket_pair_colorization_enabled());
580 /// ```
581 pub fn bracket_pair_colorization_enabled(&self) -> bool {
582 self.bracket_pair_colorization_enabled
583 }
584
585 /// Enables or disables code folding (collapse/expand blocks).
586 ///
587 /// When disabled, no fold chevrons are drawn and all lines are shown
588 /// regardless of the collapsed state (which is preserved, so re-enabling
589 /// restores the previously collapsed blocks).
590 ///
591 /// # Arguments
592 ///
593 /// * `enabled` - Whether to enable code folding
594 ///
595 /// # Example
596 ///
597 /// ```
598 /// use iced_code_editor::CodeEditor;
599 ///
600 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
601 /// editor.set_folding_enabled(true);
602 /// ```
603 pub fn set_folding_enabled(&mut self, enabled: bool) {
604 if self.folding_enabled != enabled {
605 self.folding_enabled = enabled;
606 self.bump_fold_revision();
607 }
608 }
609
610 /// Returns whether code folding is enabled.
611 ///
612 /// # Returns
613 ///
614 /// `true` if code folding is enabled, `false` otherwise
615 ///
616 /// # Example
617 ///
618 /// ```
619 /// use iced_code_editor::CodeEditor;
620 ///
621 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
622 /// // Enabled by default.
623 /// assert!(editor.folding_enabled());
624 ///
625 /// editor.set_folding_enabled(false);
626 /// assert!(!editor.folding_enabled());
627 /// ```
628 pub fn folding_enabled(&self) -> bool {
629 self.folding_enabled
630 }
631
632 /// Enables or disables automatic indentation on Enter.
633 ///
634 /// When enabled, pressing Enter copies the leading whitespace of the
635 /// current line to the new line. When disabled, the cursor is placed
636 /// at column 0 on the new line.
637 ///
638 /// # Arguments
639 ///
640 /// * `enabled` - `true` to enable auto-indentation, `false` to disable
641 ///
642 /// # Example
643 ///
644 /// ```
645 /// use iced_code_editor::CodeEditor;
646 ///
647 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
648 /// editor.set_auto_indent_enabled(false);
649 /// assert!(!editor.auto_indent_enabled());
650 /// ```
651 pub fn set_auto_indent_enabled(&mut self, enabled: bool) {
652 self.auto_indent_enabled = enabled;
653 }
654
655 /// Returns whether auto-indentation is enabled.
656 ///
657 /// # Returns
658 ///
659 /// `true` if auto-indentation is enabled, `false` otherwise
660 ///
661 /// # Example
662 ///
663 /// ```
664 /// use iced_code_editor::CodeEditor;
665 ///
666 /// let editor = CodeEditor::new("fn main() {}", "rs");
667 /// // Enabled by default.
668 /// assert!(editor.auto_indent_enabled());
669 /// ```
670 pub fn auto_indent_enabled(&self) -> bool {
671 self.auto_indent_enabled
672 }
673
674 /// Enables or disables auto-closing of brackets and quotes.
675 ///
676 /// When enabled, typing an opening bracket/quote (`(`, `[`, `{`, `"`,
677 /// `'`) auto-inserts its matching closing character with the cursor
678 /// placed between them, typing the closing character right before an
679 /// already-inserted match moves the cursor past it instead of
680 /// duplicating it, and typing an opening bracket/quote while text is
681 /// selected wraps the selection in the pair instead of replacing it.
682 ///
683 /// # Arguments
684 ///
685 /// * `enabled` - `true` to enable auto-closing, `false` to disable
686 ///
687 /// # Example
688 ///
689 /// ```
690 /// use iced_code_editor::CodeEditor;
691 ///
692 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
693 /// editor.set_auto_close_brackets(false); // Disable auto-closing
694 /// ```
695 pub fn set_auto_close_brackets(&mut self, enabled: bool) {
696 self.auto_close_brackets = enabled;
697 }
698
699 /// Returns whether auto-closing of brackets and quotes is enabled.
700 ///
701 /// # Returns
702 ///
703 /// `true` if auto-closing is enabled, `false` otherwise
704 ///
705 /// # Example
706 ///
707 /// ```
708 /// use iced_code_editor::CodeEditor;
709 ///
710 /// let editor = CodeEditor::new("fn main() {}", "rs");
711 /// // Enabled by default.
712 /// assert!(editor.auto_close_brackets());
713 /// ```
714 pub fn auto_close_brackets(&self) -> bool {
715 self.auto_close_brackets
716 }
717
718 /// Sets the indentation style used when pressing the Tab key.
719 ///
720 /// # Arguments
721 ///
722 /// * `style` - The indentation style (`IndentStyle::Spaces(n)` or `IndentStyle::Tab`)
723 ///
724 /// # Example
725 ///
726 /// ```
727 /// use iced_code_editor::{CodeEditor, IndentStyle};
728 ///
729 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
730 ///
731 /// // Indent with a real tab character instead of the default 4 spaces.
732 /// editor.set_indent_style(IndentStyle::Tab);
733 /// assert_eq!(editor.indent_style(), IndentStyle::Tab);
734 ///
735 /// // Or pick a different space width.
736 /// editor.set_indent_style(IndentStyle::Spaces(2));
737 /// assert_eq!(editor.indent_style(), IndentStyle::Spaces(2));
738 /// ```
739 pub fn set_indent_style(&mut self, style: IndentStyle) {
740 self.indent_style = style;
741 }
742
743 /// Returns the current indentation style.
744 ///
745 /// # Returns
746 ///
747 /// The current [`IndentStyle`] configured for this editor
748 ///
749 /// # Example
750 ///
751 /// ```
752 /// use iced_code_editor::{CodeEditor, IndentStyle};
753 ///
754 /// let editor = CodeEditor::new("fn main() {}", "rs");
755 /// assert_eq!(editor.indent_style(), IndentStyle::Spaces(4));
756 /// ```
757 pub fn indent_style(&self) -> IndentStyle {
758 self.indent_style
759 }
760
761 /// Enables or disables the search/replace functionality.
762 ///
763 /// When disabled, search/replace keyboard shortcuts (Ctrl+F, Ctrl+H, F3)
764 /// will be ignored. If the search dialog is currently open, it will be closed.
765 ///
766 /// # Arguments
767 ///
768 /// * `enabled` - Whether to enable search/replace functionality
769 ///
770 /// # Example
771 ///
772 /// ```
773 /// use iced_code_editor::CodeEditor;
774 ///
775 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
776 /// editor.set_search_replace_enabled(false); // Disable search/replace
777 /// ```
778 pub fn set_search_replace_enabled(&mut self, enabled: bool) {
779 self.search_replace_enabled = enabled;
780 if !enabled && self.search_state.is_open {
781 self.search_state.close();
782 }
783 }
784
785 /// Returns whether search/replace functionality is enabled.
786 ///
787 /// # Returns
788 ///
789 /// `true` if search/replace is enabled, `false` otherwise
790 ///
791 /// # Example
792 ///
793 /// ```
794 /// use iced_code_editor::CodeEditor;
795 ///
796 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
797 /// // Enabled by default.
798 /// assert!(editor.search_replace_enabled());
799 ///
800 /// editor.set_search_replace_enabled(false);
801 /// assert!(!editor.search_replace_enabled());
802 /// ```
803 pub fn search_replace_enabled(&self) -> bool {
804 self.search_replace_enabled
805 }
806
807 /// Returns the syntax highlighting language identifier for this editor.
808 ///
809 /// This is the language key passed at construction (e.g., `"lua"`, `"rs"`, `"py"`).
810 ///
811 /// # Examples
812 ///
813 /// ```
814 /// use iced_code_editor::CodeEditor;
815 /// let editor = CodeEditor::new("fn main() {}", "rs");
816 /// assert_eq!(editor.syntax(), "rs");
817 /// ```
818 pub fn syntax(&self) -> &str {
819 &self.syntax
820 }
821
822 /// Sets the line wrapping with builder pattern.
823 ///
824 /// # Arguments
825 ///
826 /// * `enabled` - Whether to enable line wrapping
827 ///
828 /// # Returns
829 ///
830 /// Self for method chaining
831 ///
832 /// # Example
833 ///
834 /// ```
835 /// use iced_code_editor::CodeEditor;
836 ///
837 /// let editor = CodeEditor::new("fn main() {}", "rs")
838 /// .with_wrap_enabled(false);
839 /// ```
840 #[must_use]
841 pub fn with_wrap_enabled(mut self, enabled: bool) -> Self {
842 self.wrap_enabled = enabled;
843 self
844 }
845
846 /// Enables or disables code folding using the builder pattern.
847 ///
848 /// # Arguments
849 ///
850 /// * `enabled` - Whether to enable code folding
851 ///
852 /// # Example
853 ///
854 /// ```
855 /// use iced_code_editor::CodeEditor;
856 ///
857 /// let editor = CodeEditor::new("fn main() {}", "rs")
858 /// .with_folding_enabled(true);
859 /// ```
860 #[must_use]
861 pub fn with_folding_enabled(mut self, enabled: bool) -> Self {
862 self.folding_enabled = enabled;
863 self
864 }
865
866 /// Sets the wrap column (fixed width wrapping).
867 ///
868 /// When set to `Some(n)`, lines will wrap at column `n`.
869 /// When set to `None`, lines will wrap at the viewport width.
870 ///
871 /// # Arguments
872 ///
873 /// * `column` - The column to wrap at, or None for viewport-based wrapping
874 ///
875 /// # Example
876 ///
877 /// ```
878 /// use iced_code_editor::CodeEditor;
879 ///
880 /// let editor = CodeEditor::new("fn main() {}", "rs")
881 /// .with_wrap_column(Some(80)); // Wrap at 80 characters
882 /// ```
883 #[must_use]
884 pub fn with_wrap_column(mut self, column: Option<usize>) -> Self {
885 self.wrap_column = column;
886 self
887 }
888
889 /// Sets whether line numbers are displayed.
890 ///
891 /// When disabled, the gutter is completely removed (0px width),
892 /// providing more space for code display.
893 ///
894 /// # Arguments
895 ///
896 /// * `enabled` - Whether to display line numbers
897 ///
898 /// # Example
899 ///
900 /// ```
901 /// use iced_code_editor::CodeEditor;
902 ///
903 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
904 /// editor.set_line_numbers_enabled(false); // Hide line numbers
905 /// ```
906 pub fn set_line_numbers_enabled(&mut self, enabled: bool) {
907 if self.line_numbers_enabled != enabled {
908 self.line_numbers_enabled = enabled;
909 self.content_cache.clear();
910 self.overlay_cache.clear();
911 }
912 }
913
914 /// Returns whether line numbers are displayed.
915 ///
916 /// # Returns
917 ///
918 /// `true` if line numbers are displayed, `false` otherwise
919 ///
920 /// # Example
921 ///
922 /// ```
923 /// use iced_code_editor::CodeEditor;
924 ///
925 /// let mut editor = CodeEditor::new("fn main() {}", "rs");
926 /// // Displayed by default.
927 /// assert!(editor.line_numbers_enabled());
928 ///
929 /// editor.set_line_numbers_enabled(false);
930 /// assert!(!editor.line_numbers_enabled());
931 /// ```
932 pub fn line_numbers_enabled(&self) -> bool {
933 self.line_numbers_enabled
934 }
935
936 /// Sets the line numbers display with builder pattern.
937 ///
938 /// # Arguments
939 ///
940 /// * `enabled` - Whether to display line numbers
941 ///
942 /// # Returns
943 ///
944 /// Self for method chaining
945 ///
946 /// # Example
947 ///
948 /// ```
949 /// use iced_code_editor::CodeEditor;
950 ///
951 /// let editor = CodeEditor::new("fn main() {}", "rs")
952 /// .with_line_numbers_enabled(false);
953 /// ```
954 #[must_use]
955 pub fn with_line_numbers_enabled(mut self, enabled: bool) -> Self {
956 self.line_numbers_enabled = enabled;
957 self
958 }
959}
960
961#[cfg(test)]
962mod tests {
963 use super::*;
964 use crate::canvas_editor::features::context_menu::ContextMenuItem;
965 use crate::canvas_editor::features::vim;
966
967 #[test]
968 fn test_custom_context_menu_configuration() {
969 let custom_entries = vec![
970 ContextMenuEntry::item("format", "Format document")
971 .with_shortcut("Shift+Alt+F"),
972 ContextMenuEntry::separator(),
973 ContextMenuEntry::Item(
974 ContextMenuItem::new("rename", "Rename symbol")
975 .with_enabled(false),
976 ),
977 ];
978
979 let editor = CodeEditor::new("", "rs")
980 .with_custom_context_menu_entries(custom_entries.clone())
981 .with_default_context_menu_enabled(false);
982
983 assert_eq!(editor.custom_context_menu_entries(), custom_entries);
984 assert!(!editor.default_context_menu_enabled());
985
986 let default_editor = CodeEditor::new("", "rs");
987 assert!(default_editor.custom_context_menu_entries().is_empty());
988 assert!(default_editor.default_context_menu_enabled());
989 }
990
991 #[test]
992 fn test_reveal_in_file_manager_configuration() {
993 let mut editor = CodeEditor::new("", "rs");
994 assert!(!editor.reveal_in_file_manager_enabled());
995
996 editor.set_reveal_in_file_manager_enabled(true);
997 assert!(editor.reveal_in_file_manager_enabled());
998
999 let editor =
1000 CodeEditor::new("", "rs").with_reveal_in_file_manager_enabled(true);
1001 assert!(editor.reveal_in_file_manager_enabled());
1002 }
1003
1004 #[test]
1005 fn test_auto_close_brackets_configuration() {
1006 let mut editor = CodeEditor::new("", "rs");
1007 assert!(editor.auto_close_brackets());
1008
1009 editor.set_auto_close_brackets(false);
1010 assert!(!editor.auto_close_brackets());
1011
1012 editor.set_auto_close_brackets(true);
1013 assert!(editor.auto_close_brackets());
1014 }
1015
1016 #[test]
1017 fn test_bracket_match_highlight_configuration() {
1018 let mut editor = CodeEditor::new("", "rs");
1019 assert!(editor.bracket_match_highlight_enabled());
1020
1021 editor.set_bracket_match_highlight_enabled(false);
1022 assert!(!editor.bracket_match_highlight_enabled());
1023
1024 editor.set_bracket_match_highlight_enabled(true);
1025 assert!(editor.bracket_match_highlight_enabled());
1026 }
1027
1028 #[test]
1029 fn test_bracket_pair_colorization_configuration() {
1030 let mut editor = CodeEditor::new("", "rs");
1031 assert!(editor.bracket_pair_colorization_enabled());
1032
1033 editor.set_bracket_pair_colorization_enabled(false);
1034 assert!(!editor.bracket_pair_colorization_enabled());
1035
1036 editor.set_bracket_pair_colorization_enabled(true);
1037 assert!(editor.bracket_pair_colorization_enabled());
1038 }
1039
1040 #[test]
1041 fn vim_disabled_by_default() {
1042 let editor = CodeEditor::new("unchanged", "rs");
1043
1044 assert!(!editor.vim_enabled());
1045 assert_eq!(editor.vim_mode(), None);
1046 assert_eq!(editor.content(), "unchanged");
1047 assert!(!editor.can_undo());
1048 assert!(!editor.can_redo());
1049 }
1050
1051 #[test]
1052 fn vim_enable_enters_clean_normal_mode() {
1053 let mut editor = CodeEditor::new("unchanged", "rs");
1054 assert_eq!(editor.vim_state.parse_key('9'), None);
1055 assert_eq!(editor.vim_state.parse_key('d'), None);
1056
1057 editor.set_vim_enabled(true);
1058
1059 assert!(editor.vim_enabled());
1060 assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
1061 assert_eq!(
1062 editor.vim_state.parse_key('l'),
1063 Some(vim::VimAction::Motion {
1064 motion: vim::VimMotion::Right,
1065 count: 1,
1066 explicit_count: false,
1067 })
1068 );
1069 assert_eq!(editor.content(), "unchanged");
1070 assert!(!editor.can_undo());
1071 assert!(!editor.can_redo());
1072 }
1073
1074 #[test]
1075 fn vim_disable_clears_pending_state() {
1076 let mut editor =
1077 CodeEditor::new("unchanged", "rs").with_vim_enabled(true);
1078 assert_eq!(editor.vim_state.parse_key('4'), None);
1079 assert_eq!(editor.vim_state.parse_key('d'), None);
1080
1081 editor.set_vim_enabled(false);
1082 assert!(!editor.vim_enabled());
1083 assert_eq!(editor.vim_mode(), None);
1084
1085 editor.set_vim_enabled(true);
1086 assert_eq!(
1087 editor.vim_state.parse_key('w'),
1088 Some(vim::VimAction::Motion {
1089 motion: vim::VimMotion::WordForward,
1090 count: 1,
1091 explicit_count: false,
1092 })
1093 );
1094 assert_eq!(editor.content(), "unchanged");
1095 assert!(!editor.can_undo());
1096 assert!(!editor.can_redo());
1097 }
1098
1099 #[test]
1100 fn vim_reset_clears_pending_state() {
1101 let mut editor = CodeEditor::new("before", "rs").with_vim_enabled(true);
1102 assert_eq!(editor.vim_state.parse_key('3'), None);
1103 assert_eq!(editor.vim_state.parse_key('g'), None);
1104
1105 let _ = editor.reset("after");
1106
1107 assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
1108 assert_eq!(editor.vim_state.parse_key('g'), None);
1109 assert_eq!(
1110 editor.vim_state.parse_key('g'),
1111 Some(vim::VimAction::Motion {
1112 motion: vim::VimMotion::DocumentStart,
1113 count: 1,
1114 explicit_count: false,
1115 })
1116 );
1117 assert_eq!(editor.content(), "after");
1118 assert!(!editor.can_undo());
1119 assert!(!editor.can_redo());
1120 }
1121
1122 #[test]
1123 fn test_syntax_getter() {
1124 let editor = CodeEditor::new("", "lua");
1125 assert_eq!(editor.syntax(), "lua");
1126 }
1127
1128 #[test]
1129 fn test_folding_enabled_by_default() {
1130 let editor = CodeEditor::new("fn main() {}", "rs");
1131 assert!(editor.folding_enabled());
1132 }
1133}