guise-ui 0.10.0

A Mantine-inspired component library for gpui, Zed's GPU-accelerated UI framework.
Documentation
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Pure single-line text-editing model: a string plus a char-index cursor,
//! with the operations a text field needs. No UI — fully unit-testable; the
//! `TextInput` entity drives it from key events and renders from `split`.

/// An editable line of text with a cursor and an optional selection.
#[derive(Debug, Clone, Default)]
pub struct TextEdit {
    chars: Vec<char>,
    /// Cursor position as a char index in `0..=chars.len()`.
    cursor: usize,
    /// Selection anchor; a selection spans `anchor..cursor` in either order.
    /// `None` means no selection.
    anchor: Option<usize>,
}

impl TextEdit {
    /// Start editing `text` with the cursor at the end.
    pub fn new(text: &str) -> Self {
        let chars: Vec<char> = text.chars().collect();
        let cursor = chars.len();
        Self { chars, cursor, anchor: None }
    }

    pub fn text(&self) -> String {
        self.chars.iter().collect()
    }

    pub fn is_empty(&self) -> bool {
        self.chars.is_empty()
    }

    pub fn len(&self) -> usize {
        self.chars.len()
    }

    /// The selected span `(start, end)` as char indices, or `None` if the
    /// selection is empty/collapsed.
    pub fn selection(&self) -> Option<(usize, usize)> {
        let a = self.anchor?;
        (a != self.cursor).then(|| (a.min(self.cursor), a.max(self.cursor)))
    }

    pub fn has_selection(&self) -> bool {
        self.selection().is_some()
    }

    /// The selected text, or `None` when nothing is selected.
    pub fn selected_text(&self) -> Option<String> {
        let (s, e) = self.selection()?;
        Some(self.chars[s..e].iter().collect())
    }

    /// Select the whole line.
    pub fn select_all(&mut self) {
        if self.chars.is_empty() {
            self.anchor = None;
            return;
        }
        self.anchor = Some(0);
        self.cursor = self.chars.len();
    }

    /// Drop any selection, keeping the cursor put.
    pub fn clear_selection(&mut self) {
        self.anchor = None;
    }

    /// Delete the selected text (if any), leaving the cursor at its start.
    /// Returns whether anything was removed.
    pub fn delete_selection(&mut self) -> bool {
        let Some((s, e)) = self.selection() else {
            return false;
        };
        self.chars.drain(s..e);
        self.cursor = s;
        self.anchor = None;
        true
    }

    /// Prepare for a cursor move: with `extend` (Shift held) anchor a selection
    /// at the current cursor if one isn't already open; otherwise drop it.
    pub fn pre_move(&mut self, extend: bool) {
        if extend {
            if self.anchor.is_none() {
                self.anchor = Some(self.cursor);
            }
        } else {
            self.anchor = None;
        }
    }

    /// The text split around the selection: `(before, selected, after)`, or
    /// `None` when nothing is selected.
    pub fn split_selection(&self) -> Option<(String, String, String)> {
        let (s, e) = self.selection()?;
        Some((
            self.chars[..s].iter().collect(),
            self.chars[s..e].iter().collect(),
            self.chars[e..].iter().collect(),
        ))
    }

    /// Insert `s` at the cursor, replacing any selection, advancing past it.
    pub fn insert(&mut self, s: &str) {
        self.delete_selection();
        for c in s.chars() {
            self.chars.insert(self.cursor, c);
            self.cursor += 1;
        }
    }

    /// Delete the selection, or the char before the cursor. Returns whether
    /// anything changed.
    pub fn backspace(&mut self) -> bool {
        if self.delete_selection() {
            return true;
        }
        if self.cursor == 0 {
            return false;
        }
        self.cursor -= 1;
        self.chars.remove(self.cursor);
        true
    }

    /// Delete the selection, or the char at the cursor. Returns whether anything
    /// changed.
    pub fn delete(&mut self) -> bool {
        if self.delete_selection() {
            return true;
        }
        if self.cursor >= self.chars.len() {
            return false;
        }
        self.chars.remove(self.cursor);
        true
    }

    pub fn left(&mut self) {
        self.cursor = self.cursor.saturating_sub(1);
    }

    pub fn right(&mut self) {
        if self.cursor < self.chars.len() {
            self.cursor += 1;
        }
    }

    pub fn home(&mut self) {
        self.cursor = 0;
    }

    pub fn end(&mut self) {
        self.cursor = self.chars.len();
    }

    /// Move left to the start of the previous word (Option+Left on macOS).
    pub fn word_left(&mut self) {
        while self.cursor > 0 && !is_word(self.chars[self.cursor - 1]) {
            self.cursor -= 1;
        }
        while self.cursor > 0 && is_word(self.chars[self.cursor - 1]) {
            self.cursor -= 1;
        }
    }

    /// Move right past the end of the next word (Option+Right on macOS).
    pub fn word_right(&mut self) {
        let n = self.chars.len();
        while self.cursor < n && !is_word(self.chars[self.cursor]) {
            self.cursor += 1;
        }
        while self.cursor < n && is_word(self.chars[self.cursor]) {
            self.cursor += 1;
        }
    }

    /// Delete the word before the cursor (Option+Backspace). Returns whether
    /// anything changed.
    pub fn delete_word_back(&mut self) -> bool {
        let end = self.cursor;
        self.word_left();
        if self.cursor < end {
            self.chars.drain(self.cursor..end);
            true
        } else {
            false
        }
    }

    /// Delete the word after the cursor (Option+Delete). Returns whether
    /// anything changed.
    pub fn delete_word_forward(&mut self) -> bool {
        let start = self.cursor;
        let n = self.chars.len();
        let mut end = self.cursor;
        while end < n && !is_word(self.chars[end]) {
            end += 1;
        }
        while end < n && is_word(self.chars[end]) {
            end += 1;
        }
        if end > start {
            self.chars.drain(start..end);
            true
        } else {
            false
        }
    }

    /// Delete from the cursor to the line start (Cmd+Backspace). Returns
    /// whether anything changed.
    pub fn delete_to_start(&mut self) -> bool {
        if self.cursor == 0 {
            return false;
        }
        self.chars.drain(0..self.cursor);
        self.cursor = 0;
        true
    }

    /// Delete from the cursor to the line end (Cmd+Delete / Ctrl+K). Returns
    /// whether anything changed.
    pub fn delete_to_end(&mut self) -> bool {
        if self.cursor >= self.chars.len() {
            return false;
        }
        self.chars.truncate(self.cursor);
        true
    }

    /// The text before and after the cursor, for rendering a caret between.
    pub fn split(&self) -> (String, String) {
        (
            self.chars[..self.cursor].iter().collect(),
            self.chars[self.cursor..].iter().collect(),
        )
    }

    /// Move the cursor up one line, keeping the column where possible. Multiline
    /// only (single-line text has nowhere to go).
    pub fn up(&mut self) {
        self.vmove(-1);
    }

    /// Move the cursor down one line, keeping the column where possible.
    pub fn down(&mut self) {
        self.vmove(1);
    }

    /// (line, column) of the cursor, counting `\n`-separated lines.
    fn line_col(&self) -> (usize, usize) {
        let mut line = 0;
        let mut col = 0;
        for &c in &self.chars[..self.cursor] {
            if c == '\n' {
                line += 1;
                col = 0;
            } else {
                col += 1;
            }
        }
        (line, col)
    }

    /// (start char index, length excluding newline) for each line.
    fn line_bounds(&self) -> Vec<(usize, usize)> {
        let mut out = Vec::new();
        let mut start = 0;
        let mut len = 0;
        for (i, &c) in self.chars.iter().enumerate() {
            if c == '\n' {
                out.push((start, len));
                start = i + 1;
                len = 0;
            } else {
                len += 1;
            }
        }
        out.push((start, len));
        out
    }

    fn vmove(&mut self, dir: isize) {
        let (line, col) = self.line_col();
        let bounds = self.line_bounds();
        let target = line as isize + dir;
        if target < 0 || target as usize >= bounds.len() {
            return;
        }
        let (start, len) = bounds[target as usize];
        self.cursor = start + col.min(len);
    }
}

/// Word characters for word-wise navigation/deletion.
fn is_word(c: char) -> bool {
    c.is_alphanumeric() || c == '_'
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_places_cursor_at_end() {
        let e = TextEdit::new("abc");
        assert_eq!(e.split(), ("abc".into(), "".into()));
    }

    #[test]
    fn insert_at_cursor() {
        let mut e = TextEdit::new("ac");
        e.left();
        e.insert("b");
        assert_eq!(e.text(), "abc");
        assert_eq!(e.split(), ("ab".into(), "c".into()));
    }

    #[test]
    fn backspace_and_delete() {
        let mut e = TextEdit::new("abc");
        assert!(e.backspace());
        assert_eq!(e.text(), "ab");
        e.home();
        assert!(e.delete());
        assert_eq!(e.text(), "b");
        e.home();
        assert!(!e.backspace());
        e.end();
        assert!(!e.delete());
    }

    #[test]
    fn handles_unicode() {
        let mut e = TextEdit::new("café");
        assert!(e.backspace());
        assert_eq!(e.text(), "caf");
        e.insert("é");
        assert_eq!(e.text(), "café");
    }

    #[test]
    fn vertical_movement_keeps_column() {
        // Two lines: "hello" / "hi". Cursor starts at end ("hi").
        let mut e = TextEdit::new("hello\nhi");
        // Column 2 on line 1.
        e.up();
        // Same column (2) on line 0 → between "he" and "llo".
        assert_eq!(e.split().0, "he");
        e.down();
        // Back to line 1; column clamped to its length (2) → end.
        assert_eq!(e.split(), ("hello\nhi".into(), "".into()));
    }

    #[test]
    fn vertical_movement_stops_at_edges() {
        let mut e = TextEdit::new("a\nb");
        e.home(); // line 1 has only the final char; home goes to absolute start
        e.up(); // already on first line, no-op
        assert_eq!(e.split().0, "");
    }

    #[test]
    fn word_navigation() {
        let mut e = TextEdit::new("foo bar baz");
        e.word_left();
        assert_eq!(e.split(), ("foo bar ".into(), "baz".into()));
        e.word_left();
        assert_eq!(e.split(), ("foo ".into(), "bar baz".into()));
        e.word_right();
        assert_eq!(e.split(), ("foo bar".into(), " baz".into()));
    }

    #[test]
    fn delete_word_back_and_forward() {
        let mut e = TextEdit::new("foo bar baz");
        assert!(e.delete_word_back());
        assert_eq!(e.text(), "foo bar ");
        e.home();
        assert!(e.delete_word_forward());
        assert_eq!(e.text(), " bar ");
        // Nothing before the cursor at home: no-op.
        e.home();
        assert!(!e.delete_word_back());
    }

    #[test]
    fn select_all_then_type_replaces() {
        let mut e = TextEdit::new("hello");
        e.select_all();
        assert_eq!(e.selected_text().as_deref(), Some("hello"));
        e.insert("x");
        assert_eq!(e.text(), "x");
        assert!(!e.has_selection());
    }

    #[test]
    fn shift_arrow_extends_selection() {
        let mut e = TextEdit::new("abcd");
        e.pre_move(true);
        e.left(); // select "d"
        e.pre_move(true);
        e.left(); // select "cd"
        assert_eq!(e.selected_text().as_deref(), Some("cd"));
        let (before, sel, after) = e.split_selection().unwrap();
        assert_eq!((before.as_str(), sel.as_str(), after.as_str()), ("ab", "cd", ""));
    }

    #[test]
    fn plain_move_clears_selection() {
        let mut e = TextEdit::new("abcd");
        e.select_all();
        e.pre_move(false);
        e.left();
        assert!(!e.has_selection());
    }

    #[test]
    fn backspace_deletes_selection() {
        let mut e = TextEdit::new("abcd");
        e.select_all();
        assert!(e.backspace());
        assert_eq!(e.text(), "");
    }

    #[test]
    fn delete_to_line_edges() {
        let mut e = TextEdit::new("hello world");
        e.home();
        e.right();
        e.right();
        assert!(e.delete_to_start());
        assert_eq!(e.text(), "llo world");
        assert!(e.delete_to_end());
        assert_eq!(e.text(), "");
        assert!(!e.delete_to_end());
    }
}