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