nvim_api/opts/
set_extmark.rs

1use std::ops::{Deref, DerefMut};
2
3use nvim_types::{self as nvim, Array, Integer, Object};
4
5use crate::trait_utils::StringOrListOfStrings;
6use crate::types::{ExtmarkHlMode, ExtmarkVirtTextPosition};
7
8#[derive(Clone, Debug, Default)]
9#[allow(non_camel_case_types)]
10#[repr(C)]
11pub(crate) struct KeyDict_set_extmark {
12    id: Object,
13    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
14    spell: Object,
15    hl_eol: Object,
16    strict: Object,
17    end_col: Object,
18    conceal: Object,
19    hl_mode: Object,
20    end_row: Object,
21    /// The docs don't mention this but it's there.
22    end_line: Object,
23    hl_group: Object,
24    priority: Object,
25    ephemeral: Object,
26    sign_text: Object,
27    virt_text: Object,
28    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
29    ui_watched: Object,
30    virt_lines: Object,
31    line_hl_group: Object,
32    right_gravity: Object,
33    sign_hl_group: Object,
34    virt_text_pos: Object,
35    virt_text_hide: Object,
36    number_hl_group: Object,
37    virt_lines_above: Object,
38    end_right_gravity: Object,
39    virt_text_win_col: Object,
40    virt_lines_leftcol: Object,
41    cursorline_hl_group: Object,
42}
43
44#[derive(Clone, Debug, Default)]
45pub struct SetExtmarkOpts(pub(crate) KeyDict_set_extmark);
46
47impl SetExtmarkOpts {
48    #[inline(always)]
49    pub fn builder() -> SetExtmarkOptsBuilder {
50        SetExtmarkOptsBuilder::default()
51    }
52
53    #[inline(always)]
54    pub fn set_conceal(&mut self, conceal: Option<char>) {
55        let ch = conceal.map(nvim::String::from).unwrap_or_default();
56        self.0.conceal = ch.into();
57    }
58
59    #[inline(always)]
60    pub fn set_cursorline_hl_group(&mut self, cursorline_hl_group: &str) {
61        self.0.cursorline_hl_group =
62            nvim::String::from(cursorline_hl_group).into();
63    }
64
65    #[inline(always)]
66    pub fn set_end_col(&mut self, end_col: usize) {
67        self.0.end_col = (end_col as Integer).into();
68    }
69
70    #[inline(always)]
71    pub fn set_end_right_gravity(&mut self, end_right_gravity: bool) {
72        self.0.end_right_gravity = end_right_gravity.into();
73    }
74
75    #[inline(always)]
76    pub fn set_end_row(&mut self, end_row: usize) {
77        self.0.end_row = (end_row as Integer).into();
78    }
79
80    #[inline(always)]
81    pub fn set_ephemeral(&mut self, ephemeral: bool) {
82        self.0.ephemeral = ephemeral.into();
83    }
84
85    #[inline(always)]
86    pub fn set_hl_eol(&mut self, hl_eol: bool) {
87        self.0.hl_eol = hl_eol.into();
88    }
89
90    #[inline(always)]
91    pub fn set_hl_group(&mut self, hl_group: &str) {
92        self.0.hl_group = nvim::String::from(hl_group).into();
93    }
94
95    #[inline(always)]
96    pub fn set_hl_mode(&mut self, hl_mode: ExtmarkHlMode) {
97        self.0.hl_mode = nvim::String::from(hl_mode).into();
98    }
99
100    #[inline(always)]
101    pub fn set_id(&mut self, id: u32) {
102        self.0.id = id.into();
103    }
104
105    #[inline(always)]
106    pub fn set_line_hl_group(&mut self, line_hl_group: &str) {
107        self.0.line_hl_group = nvim::String::from(line_hl_group).into();
108    }
109
110    #[inline(always)]
111    pub fn set_number_hl_group(&mut self, number_hl_group: &str) {
112        self.0.number_hl_group = nvim::String::from(number_hl_group).into();
113    }
114
115    #[inline(always)]
116    pub fn set_priority(&mut self, priority: u32) {
117        self.0.priority = priority.into();
118    }
119
120    #[inline(always)]
121    pub fn set_right_gravity(&mut self, right_gravity: u32) {
122        self.0.right_gravity = right_gravity.into();
123    }
124
125    #[inline(always)]
126    pub fn set_sign_hl_group(&mut self, sign_hl_group: &str) {
127        self.0.sign_hl_group = nvim::String::from(sign_hl_group).into();
128    }
129
130    #[inline(always)]
131    pub fn set_sign_text(&mut self, sign_text: &str) {
132        self.0.sign_text = nvim::String::from(sign_text).into();
133    }
134
135    #[inline(always)]
136    pub fn set_strict(&mut self, strict: bool) {
137        self.0.strict = strict.into();
138    }
139
140    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
141    #[cfg_attr(
142        docsrs,
143        doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
144    )]
145    #[inline(always)]
146    pub fn set_ui_watched(&mut self, ui_watched: bool) {
147        self.0.ui_watched = ui_watched.into();
148    }
149
150    #[inline(always)]
151    pub fn set_virt_lines<Txt, Hl, Cnk, ChunkyCnk>(
152        &mut self,
153        virt_lines: ChunkyCnk,
154    ) where
155        ChunkyCnk: IntoIterator<Item = Cnk>,
156        Cnk: IntoIterator<Item = (Txt, Hl)>,
157        Txt: Into<nvim::String>,
158        Hl: StringOrListOfStrings,
159    {
160        self.0.virt_lines = virt_lines
161            .into_iter()
162            .map(|chnky| {
163                Array::from_iter(chnky.into_iter().map(|(txt, hl)| {
164                    Array::from_iter([txt.into().into(), hl.to_object()])
165                }))
166            })
167            .collect::<Array>()
168            .into();
169    }
170
171    #[inline(always)]
172    pub fn set_virt_lines_above(&mut self, virt_lines_above: bool) {
173        self.0.virt_lines_above = virt_lines_above.into();
174    }
175
176    #[inline(always)]
177    pub fn set_virt_lines_leftcol(&mut self, virt_lines_leftcol: bool) {
178        self.0.virt_lines_leftcol = virt_lines_leftcol.into();
179    }
180
181    #[inline(always)]
182    pub fn set_virt_text<Txt, Hl, Cnk>(&mut self, virt_text: Cnk)
183    where
184        Cnk: IntoIterator<Item = (Txt, Hl)>,
185        Txt: Into<nvim::String>,
186        Hl: StringOrListOfStrings,
187    {
188        self.0.virt_text = virt_text
189            .into_iter()
190            .map(|(txt, hl)| {
191                Array::from_iter([txt.into().into(), hl.to_object()])
192            })
193            .collect::<Array>()
194            .into();
195    }
196
197    #[inline(always)]
198    pub fn set_virt_text_hide(&mut self, virt_text_hide: bool) {
199        self.0.virt_text_hide = virt_text_hide.into();
200    }
201
202    pub fn set_virt_text_pos(
203        &mut self,
204        virt_text_pos: ExtmarkVirtTextPosition,
205    ) {
206        self.0.virt_text_pos = nvim::String::from(virt_text_pos).into();
207    }
208
209    #[inline(always)]
210    pub fn set_virt_text_win_col(&mut self, virt_text_win_col: u32) {
211        self.0.virt_text_win_col = virt_text_win_col.into();
212    }
213}
214
215#[derive(Clone, Debug)]
216pub struct SetExtmarkOptsBuilder(Option<SetExtmarkOpts>);
217
218impl SetExtmarkOptsBuilder {
219    /// Enable concealing symilar to `:syn-conceal`. If a character is supplied
220    /// it is used as `:syn-cchar`.
221    ///
222    /// [`hl_group`](SetExtmarkOptsBuilder::hl_group) is used to highlight the
223    /// character if provided, otherwise it defaults to `hl-Conceal`.
224    #[inline(always)]
225    pub fn conceal(&mut self, ch: Option<char>) -> &mut Self {
226        self.set_conceal(ch);
227        self
228    }
229
230    /// Name of the highlight group used to highlight the line when the cursor
231    /// is on the same line as the mark and `cursorline` is enabled.
232    #[inline(always)]
233    pub fn cursorline_hl_group(
234        &mut self,
235        cursorline_hl_group: &str,
236    ) -> &mut Self {
237        self.set_cursorline_hl_group(cursorline_hl_group);
238        self
239    }
240
241    /// Ending line of the mark. 0-indexed and exclusive.
242    #[inline(always)]
243    pub fn end_col(&mut self, end_col: usize) -> &mut Self {
244        self.set_end_col(end_col);
245        self
246    }
247
248    /// Indicates the direction the extmark's end position (if it exists) will
249    /// be shifted in when new text is inserted (`true` for right, `false` for
250    /// left). Defaults to left.
251    #[inline(always)]
252    pub fn end_right_gravity(&mut self, end_right_gravity: bool) -> &mut Self {
253        self.set_end_right_gravity(end_right_gravity);
254        self
255    }
256
257    /// Ending line of the mark. 0-indexed and inclusive.
258    #[inline(always)]
259    pub fn end_row(&mut self, end_row: usize) -> &mut Self {
260        self.set_end_row(end_row);
261        self
262    }
263
264    /// For use with
265    /// [`api::set_decoration_provider`](crate::set_decoration_provider)
266    /// callbacks. The mark will only be used for the current redraw cycle, and
267    /// not be permanently stored in the buffer.
268    #[inline(always)]
269    pub fn ephemeral(&mut self, ephemeral: bool) -> &mut Self {
270        self.set_ephemeral(ephemeral);
271        self
272    }
273
274    /// Whether to continue the highlight for the rest of the screen line for
275    /// multiline highlights covering the EOL of a line.
276    #[inline(always)]
277    pub fn hl_eol(&mut self, hl_eol: bool) -> &mut Self {
278        self.set_hl_eol(hl_eol);
279        self
280    }
281
282    /// Name of the highlight group used to highlight this mark.
283    #[inline(always)]
284    pub fn hl_group(&mut self, hl_group: &str) -> &mut Self {
285        self.set_hl_group(hl_group);
286        self
287    }
288
289    /// Controls how highlights are combined with the highlights of the text.
290    #[inline(always)]
291    pub fn hl_mode(&mut self, hl_mode: ExtmarkHlMode) -> &mut Self {
292        self.set_hl_mode(hl_mode);
293        self
294    }
295
296    /// Id of the extmark to edit.
297    #[inline(always)]
298    pub fn id(&mut self, id: u32) -> &mut Self {
299        self.set_id(id);
300        self
301    }
302
303    /// Name of the highlight group used to highlight the whole line.
304    #[inline(always)]
305    pub fn line_hl_group(&mut self, line_hl_group: &str) -> &mut Self {
306        self.set_line_hl_group(line_hl_group);
307        self
308    }
309
310    /// Name of the highlight group used to highlight the number column.
311    #[inline(always)]
312    pub fn number_hl_group(&mut self, number_hl_group: &str) -> &mut Self {
313        self.set_number_hl_group(number_hl_group);
314        self
315    }
316
317    /// A priority value for the highlight group. For example, treesitter
318    /// highlights use a value of 100.
319    #[inline(always)]
320    pub fn priority(&mut self, priority: u32) -> &mut Self {
321        self.set_priority(priority);
322        self
323    }
324
325    /// Indicates the direction the extmark will be shifted in when new text is
326    /// inserted (`true` for right, `false` for left). Defaults to right.
327    #[inline(always)]
328    pub fn right_gravity(&mut self, right_gravity: u32) -> &mut Self {
329        self.set_right_gravity(right_gravity);
330        self
331    }
332
333    /// Name of the highlight group used to highlight the sign column text.
334    #[inline(always)]
335    pub fn sign_hl_group(&mut self, sign_hl_group: &str) -> &mut Self {
336        self.set_sign_hl_group(sign_hl_group);
337        self
338    }
339
340    /// Text to display in the sign column. Should take up 1-2 display cells.
341    #[inline(always)]
342    pub fn sign_text(&mut self, sign_text: &str) -> &mut Self {
343        self.set_sign_text(sign_text);
344        self
345    }
346
347    /// Whether the extmark should not be placed if the line or column value is
348    /// past the end of the buffer or end of the line, respectively. Defaults
349    /// to `true`.
350    #[inline(always)]
351    pub fn strict(&mut self, strict: bool) -> &mut Self {
352        self.set_strict(strict);
353        self
354    }
355
356    /// Whether the mark should be drawn by an external UI. When `true` the UI
357    /// will receive `win_extmark` events.
358    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
359    #[cfg_attr(
360        docsrs,
361        doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
362    )]
363    #[inline(always)]
364    pub fn ui_watched(&mut self, ui_watched: bool) -> &mut Self {
365        self.set_ui_watched(ui_watched);
366        self
367    }
368
369    /// Virtual lines to add next to the mark.
370    #[inline(always)]
371    pub fn virt_lines<Txt, Hl, Cnk, ChunkyCnk>(
372        &mut self,
373        virt_lines: ChunkyCnk,
374    ) -> &mut Self
375    where
376        ChunkyCnk: IntoIterator<Item = Cnk>,
377        Cnk: IntoIterator<Item = (Txt, Hl)>,
378        Txt: Into<nvim::String>,
379        Hl: StringOrListOfStrings,
380    {
381        self.set_virt_lines(virt_lines);
382        self
383    }
384
385    /// Whether to place virtual lines above the buffer line containing the
386    /// mark.
387    #[inline(always)]
388    pub fn virt_lines_above(&mut self, virt_lines_above: bool) -> &mut Self {
389        self.set_virt_lines_above(virt_lines_above);
390        self
391    }
392
393    /// Whether to place extmarks in the leftmost column of the ewindow,
394    /// bypassing sign and number columns.
395    #[inline(always)]
396    pub fn virt_lines_leftcol(
397        &mut self,
398        virt_lines_leftcol: bool,
399    ) -> &mut Self {
400        self.set_virt_lines_leftcol(virt_lines_leftcol);
401        self
402    }
403
404    /// Virtual text to link to this mark. Every `(text, highlights)` tuple
405    /// represents a text chunk with a specified highlight. The highlights
406    /// specified in `highlights` will be combined together, with the highest
407    /// priority highlight beign applied last. Each highlight group can either
408    /// be a string or an integer, the latter obtained using
409    /// [`api::get_hl_id_by_name`](crate::get_hl_id_by_name).
410    #[inline(always)]
411    pub fn virt_text<Txt, Hl, Cnk>(&mut self, virt_text: Cnk) -> &mut Self
412    where
413        Cnk: IntoIterator<Item = (Txt, Hl)>,
414        Txt: Into<nvim::String>,
415        Hl: StringOrListOfStrings,
416    {
417        self.set_virt_text(virt_text);
418        self
419    }
420
421    /// Whether to hide the virtual text when the background text is selected
422    /// or hidden due to horizontal scroll.
423    #[inline(always)]
424    pub fn virt_text_hide(&mut self, virt_text_hide: bool) -> &mut Self {
425        self.set_virt_text_hide(virt_text_hide);
426        self
427    }
428
429    /// Position of the virtual text.
430    #[inline(always)]
431    pub fn virt_text_pos(
432        &mut self,
433        virt_text_pos: ExtmarkVirtTextPosition,
434    ) -> &mut Self {
435        self.set_virt_text_pos(virt_text_pos);
436        self
437    }
438
439    /// Position the virtual text at a fixed window column (starting from the
440    /// first text column).
441    #[inline(always)]
442    pub fn virt_text_win_col(&mut self, col: u32) -> &mut Self {
443        self.set_virt_text_win_col(col);
444        self
445    }
446
447    /// Builds the options.
448    #[inline(always)]
449    pub fn build(&mut self) -> SetExtmarkOpts {
450        self.0.take().unwrap()
451    }
452}
453
454impl Default for SetExtmarkOptsBuilder {
455    #[inline]
456    fn default() -> Self {
457        Self(Some(SetExtmarkOpts::default()))
458    }
459}
460
461impl Deref for SetExtmarkOptsBuilder {
462    type Target = SetExtmarkOpts;
463
464    fn deref(&self) -> &Self::Target {
465        self.0.as_ref().unwrap()
466    }
467}
468
469impl DerefMut for SetExtmarkOptsBuilder {
470    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
471        self.0.as_mut().unwrap()
472    }
473}