gpui-component 0.11.0

GPUI Component: the styled component library of GPUI Kit, with 60+ desktop UI components for GPUI.
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
use std::{cell::RefCell, rc::Rc};

use gpui::{AppContext, Empty, ListOffset, TestAppContext, px, size};

use super::{
    DiffEvent, DiffFile, DiffFileStatus, DiffLinePosition, DiffLineRange, DiffMode, DiffSide,
    DiffState,
    state::{DisplayRow, FoldExpansion},
};

const PATCH: &str = "diff --git a/first.txt b/first.txt\n--- a/first.txt\n+++ b/first.txt\n@@ -1,3 +1,3 @@\n one\n-two\n+TWO\n three\ndiff --git a/second.txt b/second.txt\n--- a/second.txt\n+++ b/second.txt\n@@ -10 +10 @@\n-ten\n+TEN\n";

fn record_events(
    cx: &mut TestAppContext,
    state: &gpui::Entity<DiffState>,
) -> (Rc<RefCell<Vec<DiffEvent>>>, gpui::Subscription) {
    let events = Rc::new(RefCell::new(Vec::new()));
    let subscription = cx.update(|cx| {
        let events = events.clone();
        cx.subscribe(state, move |_, event: &DiffEvent, _| {
            events.borrow_mut().push(event.clone())
        })
    });
    (events, subscription)
}

fn code_rows(state: &DiffState, file: usize) -> usize {
    state
        .rows()
        .iter()
        .filter(|row| matches!(row, DisplayRow::Code { file: f, .. } if *f == file))
        .count()
}

#[gpui::test]
fn collapsed_files_keep_their_header_and_survive_replacement(cx: &mut TestAppContext) {
    let state = cx.new(|cx| DiffState::new(DiffFile::parse(PATCH).unwrap(), cx));
    let (events, _subscription) = record_events(cx, &state);
    state.update(cx, |state, cx| {
        assert!(code_rows(state, 0) > 0);
        state.set_file_collapsed("first.txt", true, cx);
        assert!(state.is_file_collapsed("first.txt"));
        assert_eq!(code_rows(state, 0), 0);
        assert!(matches!(state.rows()[0], DisplayRow::File(0)));
        assert!(matches!(state.rows()[1], DisplayRow::File(1)));
        // Programmatic changes stay silent; the header toggle reports.
        state.toggle_file_collapsed(1, cx);
        assert!(state.is_file_collapsed("second.txt"));

        // Revealing a line expands its file.
        state.scroll_to_line(
            DiffLinePosition::new("first.txt", DiffSide::Modified, 2),
            cx,
        );
        assert!(!state.is_file_collapsed("first.txt"));

        // Paths identify files, so collapse survives a newer patch revision.
        state.set_files(DiffFile::parse(PATCH).unwrap().into_iter().rev(), cx);
        assert!(state.is_file_collapsed("second.txt"));
        state.scroll_to_file("first.txt", cx);
        assert_eq!(state.list().logical_scroll_top().item_ix, 1);
        state.scroll_to_file("missing.txt", cx);
        assert_eq!(state.list().logical_scroll_top().item_ix, 1);
    });
    assert!(matches!(
        events.borrow().as_slice(),
        [DiffEvent::FileCollapsed(path)] if path.as_str() == "second.txt"
    ));
}

#[gpui::test]
fn all_files_collapse_and_expand_together(cx: &mut TestAppContext) {
    let state = cx.new(|cx| DiffState::new(DiffFile::parse(PATCH).unwrap(), cx));
    let (events, _subscription) = record_events(cx, &state);
    state.update(cx, |state, cx| {
        state.set_all_files_collapsed(true, cx);
        assert!(state.is_file_collapsed("first.txt"));
        assert!(state.is_file_collapsed("second.txt"));
        assert_eq!(code_rows(state, 0) + code_rows(state, 1), 0);
        assert!(matches!(state.rows()[0], DisplayRow::File(0)));
        assert!(matches!(state.rows()[1], DisplayRow::File(1)));

        state.set_all_files_collapsed(false, cx);
        assert!(!state.is_file_collapsed("first.txt"));
        assert!(!state.is_file_collapsed("second.txt"));
        assert!(code_rows(state, 0) > 0);

        // A mixed state still collapses in one step.
        state.set_file_collapsed("first.txt", true, cx);
        state.set_all_files_collapsed(true, cx);
        assert!(state.is_file_collapsed("second.txt"));

        // A subset collapses together; unknown paths are skipped.
        state.set_all_files_collapsed(false, cx);
        state.set_files_collapsed(["second.txt".into(), "missing.txt".into()], true, cx);
        assert!(!state.is_file_collapsed("first.txt"));
        assert!(state.is_file_collapsed("second.txt"));
    });
    // Programmatic changes stay silent, like `set_file_collapsed`.
    assert!(events.borrow().is_empty());
}

#[gpui::test]
fn folds_expand_by_direction_and_short_ranges_stay_visible(cx: &mut TestAppContext) {
    let original = (1..=60)
        .map(|line| format!("line {line}\n"))
        .collect::<String>();
    let modified = original
        .replace("line 30\n", "changed 30\n")
        .replace("line 60\n", "changed 60\n");
    let file = super::document::fixture::modified("long.txt", &original, &modified);
    let state = cx.new(|cx| {
        DiffState::new([file], cx)
            .with_context_lines(Some(2))
            .with_expansion_lines(5)
    });
    let folds = |state: &DiffState| {
        state
            .rows()
            .iter()
            .filter_map(|row| match row {
                DisplayRow::Fold { pairs, .. } => Some(pairs.clone()),
                _ => None,
            })
            .collect::<Vec<_>>()
    };
    state.update(cx, |state, cx| {
        assert_eq!(folds(state), [0..27, 32..57]);
        state.expand_fold(0, 32..57, FoldExpansion::Down, cx);
        assert_eq!(folds(state), [0..27, 37..57]);
        state.expand_fold(0, 37..57, FoldExpansion::Up, cx);
        assert_eq!(folds(state), [0..27, 37..52]);
        state.expand_fold(0, 0..27, FoldExpansion::All, cx);
        assert_eq!(folds(state), [37..52]);
        state.collapse_unchanged(cx);
        assert_eq!(folds(state), [0..27, 32..57]);
    });

    // With a high threshold, ranges shorter than it are never collapsed.
    let file = super::document::fixture::modified("long.txt", &original, &modified);
    let state = cx.new(|cx| {
        DiffState::new([file], cx)
            .with_context_lines(Some(2))
            .with_min_collapsed_lines(26)
    });
    state.update(cx, |state, _| {
        assert_eq!(folds(state), [0..27]);
        assert_eq!(state.min_collapsed_lines(), 26);
        assert_eq!(state.expansion_lines(), 20);
    });
}

#[gpui::test]
fn line_selection_reports_a_gesture_lifecycle(cx: &mut TestAppContext) {
    let window = cx.open_window(size(px(640.), px(480.)), |_, _| Empty);
    let state = cx.new(|cx| DiffState::new(DiffFile::parse(PATCH).unwrap(), cx));
    let (events, _subscription) = record_events(cx, &state);
    let position = |side, line| DiffLinePosition::new("first.txt", side, line);
    cx.update_window(window.into(), |_, window, cx| {
        state.update(cx, |state, cx| {
            state.begin_line_selection(position(DiffSide::Original, 1), false, window, cx);
            state.drag_line_selection(position(DiffSide::Original, 2), cx);
            // A drag into another file does not extend the selection.
            state.drag_line_selection(
                DiffLinePosition::new("second.txt", DiffSide::Original, 10),
                cx,
            );
            // In Unified, dragging onto the addition crosses sides.
            state.drag_line_selection(position(DiffSide::Modified, 2), cx);
            state.end_line_selection(cx);
            state.end_line_selection(cx);
        });
    })
    .unwrap();
    let cross =
        DiffLineRange::new("first.txt", DiffSide::Original, 1, 2).with_end_side(DiffSide::Modified);
    let events = events.borrow();
    assert!(matches!(&events[0], DiffEvent::SelectionStarted(range)
        if *range == DiffLineRange::new("first.txt", DiffSide::Original, 1, 1)));
    assert!(
        matches!(&events[1], DiffEvent::SelectionChanged(Some(range))
        if *range == DiffLineRange::new("first.txt", DiffSide::Original, 1, 1))
    );
    assert!(
        matches!(&events[2], DiffEvent::SelectionChanged(Some(range))
        if *range == DiffLineRange::new("first.txt", DiffSide::Original, 1, 2))
    );
    assert!(matches!(&events[3], DiffEvent::SelectionChanged(Some(range)) if *range == cross));
    assert!(matches!(&events[4], DiffEvent::SelectionEnded(range) if *range == cross));
    assert_eq!(events.len(), 5);
    cx.update(|cx| {
        let state = state.read(cx);
        assert_eq!(state.selected_lines(), Some(cross.clone()));
        assert_eq!(state.selected_text(cx), "one\ntwo\nTWO\n");
    });
}

#[gpui::test]
fn programmatic_cross_side_ranges_clip_to_supplied_lines(cx: &mut TestAppContext) {
    let state = cx.new(|cx| DiffState::new(DiffFile::parse(PATCH).unwrap(), cx));
    state.update(cx, |state, cx| {
        state.set_selected_lines(
            Some(
                DiffLineRange::new("first.txt", DiffSide::Original, 2, 99)
                    .with_end_side(DiffSide::Modified),
            ),
            cx,
        );
        assert_eq!(
            state.selected_lines(),
            Some(
                DiffLineRange::new("first.txt", DiffSide::Original, 2, 3)
                    .with_end_side(DiffSide::Modified)
            )
        );
        assert_eq!(state.selected_text(cx), "two\nTWO\nthree\n");
        // A single-side range is normalized.
        state.set_selected_lines(
            Some(DiffLineRange::new("first.txt", DiffSide::Modified, 3, 1)),
            cx,
        );
        assert_eq!(
            state.selected_lines(),
            Some(DiffLineRange::new("first.txt", DiffSide::Modified, 1, 3))
        );
        state.list().scroll_to(ListOffset::default());
    });
}

#[test]
fn files_report_git_status_and_hidden_lines() {
    let files = DiffFile::parse(concat!(
        "diff --git a/new b/new\nnew file mode 100644\n--- /dev/null\n+++ b/new\n@@ -0,0 +1 @@\n+a\n",
        "diff --git a/old b/old\ndeleted file mode 100644\n--- a/old\n+++ /dev/null\n@@ -1 +0,0 @@\n-a\n",
        "diff --git a/from b/to\nsimilarity index 100%\nrename from from\nrename to to\n",
        "diff --git a/src b/copy\nsimilarity index 100%\ncopy from src\ncopy to copy\n",
        "diff --git a/edit b/edit\n--- a/edit\n+++ b/edit\n@@ -5,2 +5,2 @@\n x\n-y\n+z\n@@ -20 +20 @@\n-p\n+q\n",
    ))
    .unwrap();
    assert_eq!(
        files.iter().map(DiffFile::status).collect::<Vec<_>>(),
        [
            DiffFileStatus::Added,
            DiffFileStatus::Deleted,
            DiffFileStatus::Renamed,
            DiffFileStatus::Copied,
            DiffFileStatus::Modified,
        ]
    );
    let hunks = files[4].hunks();
    assert_eq!(hunks[0].hidden_lines_before(None), 4);
    assert_eq!(hunks[1].hidden_lines_before(Some(&hunks[0])), 13);
    let added = files[0].hunks();
    assert_eq!(added[0].hidden_lines_before(None), 0);
    let _ = DiffMode::Unified;
}

const MERGE: &str = "start\n<<<<<<< HEAD\nours\n=======\ntheirs\n>>>>>>> feature\nmiddle\n<<<<<<< HEAD\na\n||||||| base\nb\n=======\nc\n>>>>>>> other\nend\n";

#[gpui::test]
fn conflicts_project_their_parts_and_resolve_to_working_text(cx: &mut TestAppContext) {
    let file = DiffFile::parse_conflicts("merge.txt", MERGE).unwrap();
    let state = cx.new(|cx| {
        DiffState::new([file], cx)
            .with_mode(DiffMode::Split)
            .with_context_lines(None)
    });
    let (events, _subscription) = record_events(cx, &state);
    let conflict_rows = |state: &DiffState| {
        state
            .rows()
            .iter()
            .filter(|row| matches!(row, DisplayRow::Conflict { .. }))
            .count()
    };
    state.update(cx, |state, cx| {
        // Two parts for the first conflict, three with its base for the second.
        assert_eq!(conflict_rows(state), 5);
        assert_eq!(code_rows(state, 0), 8);
        assert_eq!(state.resolved_text("merge.txt"), None);
        state.resolve_conflict(
            "merge.txt",
            0,
            Some(super::DiffConflictResolution::Incoming),
            cx,
        );
        assert_eq!(conflict_rows(state), 4);
        assert_eq!(state.resolved_text("merge.txt"), None);
        state.choose_conflict(0, 1, Some(super::DiffConflictResolution::Both), cx);
        assert_eq!(
            state.resolved_text("merge.txt").as_deref(),
            Some("start\ntheirs\nmiddle\na\nc\nend\n")
        );
        assert_eq!(
            state.conflict_resolution("merge.txt", 1),
            Some(super::DiffConflictResolution::Both)
        );
        state.choose_conflict(0, 1, None, cx);
        assert_eq!(state.conflict_resolution("merge.txt", 1), None);
        // Out-of-range conflicts and other files are ignored.
        state.resolve_conflict(
            "merge.txt",
            9,
            Some(super::DiffConflictResolution::Current),
            cx,
        );
        assert_eq!(state.resolved_text("other.txt"), None);
    });
    let events = events.borrow();
    assert_eq!(events.len(), 2);
    assert!(events.iter().all(|event| matches!(
        event,
        DiffEvent::ConflictResolved(path, 1) if path.as_str() == "merge.txt"
    )));
}

#[gpui::test]
fn unchanged_files_show_every_line_without_folding(cx: &mut TestAppContext) {
    let text = (1..=40)
        .map(|line| format!("line {line}\n"))
        .collect::<String>();
    let state = cx.new(|cx| {
        DiffState::new([DiffFile::unchanged("notes.txt", &text)], cx).with_mode(DiffMode::Split)
    });
    state.update(cx, |state, cx| {
        assert_eq!(code_rows(state, 0), 40);
        assert!(
            !state
                .rows()
                .iter()
                .any(|row| matches!(row, DisplayRow::Fold { .. }))
        );
        state.set_selected_lines(
            Some(DiffLineRange::new("notes.txt", DiffSide::Modified, 2, 3)),
            cx,
        );
        assert_eq!(state.selected_text(cx), "line 2\nline 3\n");
    });
}

#[gpui::test]
fn builders_after_new_shape_the_prepared_emphasis(cx: &mut TestAppContext) {
    let patch = "--- a/a.txt\n+++ b/a.txt\n@@ -1 +1 @@\n-let total = price;\n+let total = cost;\n";
    let inline_count = |state: &gpui::Entity<DiffState>, cx: &mut TestAppContext| {
        state.update(cx, |state, cx| state.ensure_presentation(cx));
        cx.run_until_parked();
        state.read_with(cx, |state, _| {
            let file = &state.files()[0];
            let line = &file.lines(DiffSide::Modified)[0];
            state.presentations()[0]
                .as_ref()
                .expect("prepared in the background")
                .inline_changes(line, DiffSide::Modified, 0)
                .count()
        })
    };
    let words = cx.new(|cx| DiffState::new(DiffFile::parse(patch).unwrap(), cx));
    assert_eq!(inline_count(&words, cx), 1);
    let whole_lines =
        cx.new(|cx| DiffState::new(DiffFile::parse(patch).unwrap(), cx).with_inline_unit(None));
    assert_eq!(inline_count(&whole_lines, cx), 0);
    // Runtime changes invalidate earlier work, including an in-flight preparation.
    words.update(cx, |state, cx| {
        state.set_inline_unit(None, cx);
        state.set_inline_unit(Some(super::DiffInlineUnit::Character), cx);
    });
    assert!(inline_count(&words, cx) > 0);
    words.update(cx, |state, cx| state.set_inline_max_line_length(5, cx));
    assert_eq!(inline_count(&words, cx), 0);
    words.update(cx, |state, cx| {
        state.set_inline_max_line_length(2048, cx);
        state.set_inline_unit(Some(super::DiffInlineUnit::Word), cx);
    });
    assert_eq!(inline_count(&words, cx), 1);
}

#[gpui::test]
fn changing_presentation_options_keeps_selection_and_scroll(cx: &mut TestAppContext) {
    let state = cx.new(|cx| DiffState::new(DiffFile::parse(PATCH).unwrap(), cx));
    let (events, _subscription) = record_events(cx, &state);
    state.update(cx, |state, cx| {
        let range = DiffLineRange::new("second.txt", DiffSide::Modified, 10, 10);
        state.set_selected_lines(Some(range.clone()), cx);
        state.scroll_to_line(range.start_position(), cx);
        let top = state.list().logical_scroll_top();
        state.set_inline_unit(Some(super::DiffInlineUnit::Character), cx);
        state.set_inline_max_line_length(2048, cx);
        state.set_syntax_max_line_length(4096, cx);
        state.set_expansion_lines(5, cx);
        state.set_min_collapsed_lines(4, cx);
        assert_eq!(state.selected_lines(), Some(range));
        assert_eq!(state.list().logical_scroll_top().item_ix, top.item_ix);
        assert_eq!(state.inline_unit(), Some(super::DiffInlineUnit::Character));
        assert_eq!(state.inline_max_line_length(), 2048);
        assert_eq!(state.syntax_max_line_length(), 4096);
        assert_eq!(state.expansion_lines(), 5);
        assert_eq!(state.min_collapsed_lines(), 4);
        // None disables emphasis, without replacing the entity or patch.
        state.set_inline_unit(None, cx);
        assert!(state.inline_unit().is_none());
        assert_eq!(state.files().len(), 2);
    });
    assert!(events.borrow().is_empty());
}