Skip to main content

iced_code_editor/canvas_editor/
bool_options.rs

1//! The mechanical half of the boolean editor options.
2//!
3//! Each option is up to three methods — `set_x`, `x`, `with_x` — and only the
4//! setter carries information: what the feature is, and which cache toggling it
5//! has to invalidate. Those stay hand-written in [`super::config`]. The getter
6//! and the builder are pure boilerplate, about fifty lines apiece once the
7//! mandatory `# Returns` / `# Arguments` / `# Example` sections are written
8//! out, and are generated here from a table instead.
9//!
10//! Two things this buys beyond the line count. The builder bodies were split
11//! between assigning the field directly and delegating to the setter, and the
12//! difference was accidental: a builder that assigns silently skips whatever
13//! the setter does besides assigning, which is a trap for the next option that
14//! grows a side effect. Every generated builder delegates. And the doc shape is
15//! now stated once, so an option cannot arrive with a section missing.
16//!
17//! Each row also carries the option's default, which the generated example
18//! *asserts* rather than merely stating in a comment the way the hand-written
19//! getters did — so a default that changes fails a doctest instead of quietly
20//! contradicting its own documentation.
21
22/// Generates the getter and the optional builder for a boolean editor option.
23///
24/// Each row names the field (which is also the getter's name), its setter, and
25/// optionally its builder, followed by the wording rustdoc needs. See the
26/// module documentation for why only these two are generated.
27macro_rules! bool_options {
28    ($(
29        $get:ident, $set:ident $(, $with:ident)?,
30            $get_summary:literal,
31            $get_returns:literal,
32            default: $default_note:literal, $default_negation:literal
33            $(builder: $with_summary:literal,
34              $with_argument:literal)?;
35    )*) => {
36        impl CodeEditor {
37            $(
38                #[doc = $get_summary]
39                #[doc = ""]
40                #[doc = "# Returns"]
41                #[doc = ""]
42                #[doc = $get_returns]
43                #[doc = ""]
44                #[doc = "# Example"]
45                #[doc = ""]
46                #[doc = "```"]
47                #[doc = "use iced_code_editor::CodeEditor;"]
48                #[doc = ""]
49                #[doc = "let mut editor = CodeEditor::new(\"fn main() {}\", \"rs\");"]
50                #[doc = concat!("// ", $default_note)]
51                #[doc = concat!(
52                    "assert!(",
53                    $default_negation,
54                    "editor.",
55                    stringify!($get),
56                    "());"
57                )]
58                #[doc = ""]
59                #[doc = concat!("editor.", stringify!($set), "(true);")]
60                #[doc = concat!("assert!(editor.", stringify!($get), "());")]
61                #[doc = ""]
62                #[doc = concat!("editor.", stringify!($set), "(false);")]
63                #[doc = concat!("assert!(!editor.", stringify!($get), "());")]
64                #[doc = "```"]
65                #[must_use]
66                pub fn $get(&self) -> bool {
67                    self.$get
68                }
69
70                $(
71                    #[doc = $with_summary]
72                    #[doc = ""]
73                    #[doc = "# Arguments"]
74                    #[doc = ""]
75                    #[doc = concat!("* `enabled` - ", $with_argument)]
76                    #[doc = ""]
77                    #[doc = "# Returns"]
78                    #[doc = ""]
79                    #[doc = "Self for method chaining"]
80                    #[doc = ""]
81                    #[doc = "# Example"]
82                    #[doc = ""]
83                    #[doc = "```"]
84                    #[doc = "use iced_code_editor::CodeEditor;"]
85                    #[doc = ""]
86                    #[doc = concat!(
87                        "let editor = CodeEditor::new(\"fn main() {}\", \"rs\")"
88                    )]
89                    #[doc = concat!("    .", stringify!($with), "(false);")]
90                    #[doc = concat!("assert!(!editor.", stringify!($get), "());")]
91                    #[doc = "```"]
92                    #[must_use]
93                    pub fn $with(mut self, enabled: bool) -> Self {
94                        // Through the setter, never a bare assignment: the
95                        // builder must not be the path that forgets a side
96                        // effect the setter has.
97                        self.$set(enabled);
98                        self
99                    }
100                )?
101            )*
102        }
103    };
104}
105
106use super::CodeEditor;
107
108bool_options! {
109    auto_close_brackets, set_auto_close_brackets,
110        "Returns whether auto-closing of brackets and quotes is enabled.",
111        "`true` if auto-closing is enabled, `false` otherwise",
112        default: "Enabled by default.", "";
113    auto_indent_enabled, set_auto_indent_enabled,
114        "Returns whether auto-indentation is enabled.",
115        "`true` if auto-indentation is enabled, `false` otherwise",
116        default: "Enabled by default.", "";
117    bracket_match_highlight_enabled, set_bracket_match_highlight_enabled,
118        "Returns whether the matching-bracket/quote-pair highlight overlay is enabled.",
119        "`true` if the highlight is enabled, `false` otherwise",
120        default: "Enabled by default.", "";
121    bracket_pair_colorization_enabled, set_bracket_pair_colorization_enabled,
122        "Returns whether bracket-pair colorization (rainbow brackets) is enabled.",
123        "`true` if bracket-pair colorization is enabled, `false` otherwise",
124        default: "Enabled by default.", "";
125    command_palette_enabled, set_command_palette_enabled, with_command_palette_enabled,
126        "Returns whether the command palette can be opened.",
127        "`true` if the palette can be opened, `false` otherwise",
128        default: "Available by default.", ""
129        builder: "Sets command-palette availability using the builder pattern.",
130        "Whether the palette can be opened";
131    default_command_palette_enabled, set_default_command_palette_enabled,
132        "Returns whether the built-in commands are listed in the palette.",
133        "`true` if the built-in commands are listed, `false` otherwise",
134        default: "Listed by default.", "";
135    default_context_menu_enabled, set_default_context_menu_enabled, with_default_context_menu_enabled,
136        "Returns whether the built-in context-menu actions are enabled.",
137        "`true` if the built-in actions are shown, `false` otherwise",
138        default: "Shown by default.", ""
139        builder: "Sets built-in context-menu visibility using the builder pattern.",
140        "Whether to show the built-in actions";
141    folding_enabled, set_folding_enabled, with_folding_enabled,
142        "Returns whether code folding is enabled.",
143        "`true` if code folding is enabled, `false` otherwise",
144        default: "Enabled by default.", ""
145        builder: "Enables or disables code folding using the builder pattern.",
146        "Whether to enable code folding";
147    line_numbers_enabled, set_line_numbers_enabled, with_line_numbers_enabled,
148        "Returns whether line numbers are displayed.",
149        "`true` if line numbers are displayed, `false` otherwise",
150        default: "Displayed by default.", ""
151        builder: "Sets the line numbers display with builder pattern.",
152        "Whether to display line numbers";
153    reveal_in_file_manager_enabled, set_reveal_in_file_manager_enabled,
154        "Returns whether the reveal-in-file-manager action is shown.",
155        "`true` if the reveal action is shown, `false` otherwise",
156        default: "Off by default: the editor doesn't know its own file path.", "!";
157    search_replace_enabled, set_search_replace_enabled,
158        "Returns whether search/replace functionality is enabled.",
159        "`true` if search/replace is enabled, `false` otherwise",
160        default: "Enabled by default.", "";
161    show_color_previews, set_show_color_previews, with_show_color_previews,
162        "Returns whether inline color previews are drawn.",
163        "`true` if color-preview swatches are rendered, `false` otherwise",
164        default: "Enabled by default.", ""
165        builder: "Sets the inline color-preview display with builder pattern.",
166        "Whether to draw color-preview swatches";
167    show_indent_guides, set_show_indent_guides, with_show_indent_guides,
168        "Returns whether indentation guides are drawn.",
169        "`true` if indentation guides are rendered, `false` otherwise",
170        default: "Enabled by default.", ""
171        builder: "Sets the indentation-guide display with builder pattern.",
172        "Whether to draw indentation guides";
173    show_whitespace, set_show_whitespace,
174        "Returns whether visible whitespace rendering is enabled.",
175        "`true` if whitespace characters are rendered, `false` otherwise",
176        default: "Enabled by default.", "";
177    sticky_scroll_enabled, set_sticky_scroll_enabled, with_sticky_scroll_enabled,
178        "Returns whether sticky scroll is enabled.",
179        "`true` if enclosing block headers are pinned, `false` otherwise",
180        default: "Enabled by default.", ""
181        builder: "Sets sticky scroll with builder pattern.",
182        "Whether to pin enclosing block headers above the viewport";
183    vim_enabled, set_vim_enabled, with_vim_enabled,
184        "Returns whether Vim behavior is enabled for this editor instance.",
185        "`true` if Vim behavior is enabled, `false` otherwise",
186        default: "Off by default.", "!"
187        builder: "Sets whether Vim behavior is enabled using the builder pattern.",
188        "Whether to enable Vim behavior";
189    wrap_enabled, set_wrap_enabled, with_wrap_enabled,
190        "Returns whether line wrapping is enabled.",
191        "`true` if line wrapping is enabled, `false` otherwise",
192        default: "Enabled by default.", ""
193        builder: "Sets the line wrapping with builder pattern.",
194        "Whether to enable line wrapping";
195}