1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::{ffi, RegionIter};
use glib::{prelude::*, translate::*};

glib::wrapper! {
    /// Region utility.
    ///
    /// A [`Region`][crate::Region] permits to store a group of subregions of a
    /// [`gtk::TextBuffer`][crate::gtk::TextBuffer]. [`Region`][crate::Region] stores the subregions with pairs of
    /// [`gtk::TextMark`][crate::gtk::TextMark]'s, so the region is still valid after insertions and deletions
    /// in the [`gtk::TextBuffer`][crate::gtk::TextBuffer].
    ///
    /// The [`gtk::TextMark`][crate::gtk::TextMark] for the start of a subregion has a left gravity, while the
    /// [`gtk::TextMark`][crate::gtk::TextMark] for the end of a subregion has a right gravity.
    ///
    /// The typical use-case of [`Region`][crate::Region] is to scan a [`gtk::TextBuffer`][crate::gtk::TextBuffer] chunk by
    /// chunk, not the whole buffer at once to not block the user interface. The
    /// [`Region`][crate::Region] represents in that case the remaining region to scan. You
    /// can listen to the [`insert-text`][struct@crate::gtk::TextBuffer#insert-text] and
    /// [`delete-range`][struct@crate::gtk::TextBuffer#delete-range] signals to update the [`Region`][crate::Region]
    /// accordingly.
    ///
    /// To iterate through the subregions, you need to use a [`RegionIter`][crate::RegionIter],
    /// for example:
    /// **⚠️ The following code is in c ⚠️**
    ///
    /// ```c
    /// GtkSourceRegion *region;
    /// GtkSourceRegionIter region_iter;
    ///
    /// gtk_source_region_get_start_region_iter (region, &region_iter);
    ///
    /// while (!gtk_source_region_iter_is_end (&region_iter))
    /// {
    ///         GtkTextIter subregion_start;
    ///         GtkTextIter subregion_end;
    ///
    ///         if (!gtk_source_region_iter_get_subregion (&region_iter,
    ///                                                    &subregion_start,
    ///                                                    &subregion_end))
    ///         {
    ///                 break;
    ///         }
    ///
    ///         // Do something useful with the subregion.
    ///
    ///         gtk_source_region_iter_next (&region_iter);
    /// }
    /// ```
    ///
    /// ## Properties
    ///
    ///
    /// #### `buffer`
    ///  The [`gtk::TextBuffer`][crate::gtk::TextBuffer]. The #GtkSourceRegion has a weak reference to the
    /// buffer.
    ///
    /// Readable | Writeable | Construct Only
    ///
    /// # Implements
    ///
    /// [`RegionExt`][trait@crate::prelude::RegionExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GtkSourceRegion")]
    pub struct Region(Object<ffi::GtkSourceRegion, ffi::GtkSourceRegionClass>);

    match fn {
        type_ => || ffi::gtk_source_region_get_type(),
    }
}

impl Region {
    pub const NONE: Option<&'static Region> = None;

    /// ## `buffer`
    /// a #GtkTextBuffer.
    ///
    /// # Returns
    ///
    /// a new #GtkSourceRegion object for @buffer.
    #[doc(alias = "gtk_source_region_new")]
    pub fn new(buffer: &impl IsA<gtk::TextBuffer>) -> Region {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gtk_source_region_new(buffer.as_ref().to_glib_none().0)) }
    }

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`Region`] objects.
    ///
    /// This method returns an instance of [`RegionBuilder`](crate::builders::RegionBuilder) which can be used to create [`Region`] objects.
    pub fn builder() -> RegionBuilder {
        RegionBuilder::new()
    }
}

impl Default for Region {
    fn default() -> Self {
        glib::object::Object::new::<Self>()
    }
}

// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`Region`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct RegionBuilder {
    builder: glib::object::ObjectBuilder<'static, Region>,
}

impl RegionBuilder {
    fn new() -> Self {
        Self {
            builder: glib::object::Object::builder(),
        }
    }

    /// The [`gtk::TextBuffer`][crate::gtk::TextBuffer]. The #GtkSourceRegion has a weak reference to the
    /// buffer.
    pub fn buffer(self, buffer: &impl IsA<gtk::TextBuffer>) -> Self {
        Self {
            builder: self.builder.property("buffer", buffer.clone().upcast()),
        }
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`Region`].
    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
    pub fn build(self) -> Region {
        self.builder.build()
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::Region>> Sealed for T {}
}

/// Trait containing all [`struct@Region`] methods.
///
/// # Implementors
///
/// [`Region`][struct@crate::Region]
pub trait RegionExt: IsA<Region> + sealed::Sealed + 'static {
    /// Adds @region_to_add to @self.
    ///
    /// @region_to_add is not modified.
    /// ## `region_to_add`
    /// the #GtkSourceRegion to add to @self, or [`None`].
    #[doc(alias = "gtk_source_region_add_region")]
    fn add_region(&self, region_to_add: Option<&impl IsA<Region>>) {
        unsafe {
            ffi::gtk_source_region_add_region(
                self.as_ref().to_glib_none().0,
                region_to_add.map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    /// Adds the subregion delimited by @_start and @_end to @self.
    /// ## `_start`
    /// the start of the subregion.
    /// ## `_end`
    /// the end of the subregion.
    #[doc(alias = "gtk_source_region_add_subregion")]
    fn add_subregion(&self, _start: &gtk::TextIter, _end: &gtk::TextIter) {
        unsafe {
            ffi::gtk_source_region_add_subregion(
                self.as_ref().to_glib_none().0,
                _start.to_glib_none().0,
                _end.to_glib_none().0,
            );
        }
    }

    /// Gets the @start and @end bounds of the @self.
    ///
    /// # Returns
    ///
    /// [`true`] if @start and @end have been set successfully (if non-[`None`]),
    ///   or [`false`] if the @self is empty.
    ///
    /// ## `start`
    /// iterator to initialize with the start of @self,
    ///   or [`None`].
    ///
    /// ## `end`
    /// iterator to initialize with the end of @self,
    ///   or [`None`].
    #[doc(alias = "gtk_source_region_get_bounds")]
    #[doc(alias = "get_bounds")]
    fn bounds(&self) -> Option<(gtk::TextIter, gtk::TextIter)> {
        unsafe {
            let mut start = gtk::TextIter::uninitialized();
            let mut end = gtk::TextIter::uninitialized();
            let ret = from_glib(ffi::gtk_source_region_get_bounds(
                self.as_ref().to_glib_none().0,
                start.to_glib_none_mut().0,
                end.to_glib_none_mut().0,
            ));
            if ret {
                Some((start, end))
            } else {
                None
            }
        }
    }

    ///
    /// # Returns
    ///
    /// the #GtkTextBuffer.
    #[doc(alias = "gtk_source_region_get_buffer")]
    #[doc(alias = "get_buffer")]
    fn buffer(&self) -> Option<gtk::TextBuffer> {
        unsafe {
            from_glib_none(ffi::gtk_source_region_get_buffer(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Initializes a [`RegionIter`][crate::RegionIter] to the first subregion of @self.
    ///
    /// If @self is empty, @iter will be initialized to the end iterator.
    ///
    /// # Returns
    ///
    ///
    /// ## `iter`
    /// iterator to initialize to the first subregion.
    #[doc(alias = "gtk_source_region_get_start_region_iter")]
    #[doc(alias = "get_start_region_iter")]
    fn start_region_iter(&self) -> RegionIter {
        unsafe {
            let mut iter = RegionIter::uninitialized();
            ffi::gtk_source_region_get_start_region_iter(
                self.as_ref().to_glib_none().0,
                iter.to_glib_none_mut().0,
            );
            iter
        }
    }

    /// Returns the intersection between @self and @region2.
    ///
    /// @self and @region2 are not modified.
    /// ## `region2`
    /// a #GtkSourceRegion, or [`None`].
    ///
    /// # Returns
    ///
    /// the intersection as a #GtkSourceRegion
    ///   object.
    #[doc(alias = "gtk_source_region_intersect_region")]
    #[must_use]
    fn intersect_region(&self, region2: Option<&impl IsA<Region>>) -> Option<Region> {
        unsafe {
            from_glib_full(ffi::gtk_source_region_intersect_region(
                self.as_ref().to_glib_none().0,
                region2.map(|p| p.as_ref()).to_glib_none().0,
            ))
        }
    }

    /// Returns the intersection between @self and the subregion delimited by
    /// @_start and @_end.
    ///
    /// @self is not modified.
    /// ## `_start`
    /// the start of the subregion.
    /// ## `_end`
    /// the end of the subregion.
    ///
    /// # Returns
    ///
    /// the intersection as a new
    ///   #GtkSourceRegion.
    #[doc(alias = "gtk_source_region_intersect_subregion")]
    #[must_use]
    fn intersect_subregion(&self, _start: &gtk::TextIter, _end: &gtk::TextIter) -> Option<Region> {
        unsafe {
            from_glib_full(ffi::gtk_source_region_intersect_subregion(
                self.as_ref().to_glib_none().0,
                _start.to_glib_none().0,
                _end.to_glib_none().0,
            ))
        }
    }

    /// Returns whether the @self is empty.
    ///
    /// A [`None`] @self is considered empty.
    ///
    /// # Returns
    ///
    /// whether the @self is empty.
    #[doc(alias = "gtk_source_region_is_empty")]
    fn is_empty(&self) -> bool {
        unsafe {
            from_glib(ffi::gtk_source_region_is_empty(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Subtracts @region_to_subtract from @self.
    ///
    /// @region_to_subtract is not modified.
    /// ## `region_to_subtract`
    /// the #GtkSourceRegion to subtract from
    ///   @self, or [`None`].
    #[doc(alias = "gtk_source_region_subtract_region")]
    fn subtract_region(&self, region_to_subtract: Option<&impl IsA<Region>>) {
        unsafe {
            ffi::gtk_source_region_subtract_region(
                self.as_ref().to_glib_none().0,
                region_to_subtract.map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    /// Subtracts the subregion delimited by @_start and @_end from @self.
    /// ## `_start`
    /// the start of the subregion.
    /// ## `_end`
    /// the end of the subregion.
    #[doc(alias = "gtk_source_region_subtract_subregion")]
    fn subtract_subregion(&self, _start: &gtk::TextIter, _end: &gtk::TextIter) {
        unsafe {
            ffi::gtk_source_region_subtract_subregion(
                self.as_ref().to_glib_none().0,
                _start.to_glib_none().0,
                _end.to_glib_none().0,
            );
        }
    }

    /// Gets a string represention of @self, for debugging purposes.
    ///
    /// The returned string contains the character offsets of the subregions. It
    /// doesn't include a newline character at the end of the string.
    ///
    /// # Returns
    ///
    /// a string represention of @self. Free
    ///   with g_free() when no longer needed.
    #[doc(alias = "gtk_source_region_to_string")]
    #[doc(alias = "to_string")]
    fn to_str(&self) -> Option<glib::GString> {
        unsafe {
            from_glib_full(ffi::gtk_source_region_to_string(
                self.as_ref().to_glib_none().0,
            ))
        }
    }
}

impl<O: IsA<Region>> RegionExt for O {}