GPUI Box GitHub

Components / content

DiffView builder

A virtualized, read-only diff presentation.

A builder is RenderOnce. Construct and mount it in one expression.

use gpui_kit::content::DiffView;

Reviewed in diff-view.

Construct
new(ident: impl Into<Ident>, files: impl IntoIterator<Item = DiffFile>) -> Self
shared(ident: impl Into<Ident>, files: Arc<Vec<DiffFile>>) -> Self
Options

Chain onto the value.

cursor(cursor: DiffCursor) -> Self
presentation(presentation: DiffPresentation) -> Self
visible_rows(rows: usize) -> Self
fills() -> Self
wrapping(wrapping: bool) -> Self
language(language: impl Into<SharedString>) -> Self
on_event(handler: impl Fn(DiffViewEvent, &mut Window, &mut App) + 'static) -> Self
Reports

The variants of the event it emits. It never applies the change itself.

  • FileActivated
  • HunkActivated
  • LineActivated
  • ExpandHunk
  • UnfoldFile
Example · diff-view
fn diff_view(_window: &mut Window, cx: &mut App) -> AnyElement {
    let theme = cx.theme().clone();
    stack(&theme)
        .w(px(840.0))
        .child(caption(
            &theme,
            "the same caller-owned rows arranged as unified and split",
        ))
        .child(
            div()
                .column()
                .items_start()
                .gap_token(&theme, Space::Md)
                .child(
                    div().w(px(840.0)).child(
                        DiffView::new("scene.diff.unified", scene_diff())
                            .visible_rows(7)
                            .on_event(|_, _, _| {}),
                    ),
                )
                .child(
                    div().w(px(840.0)).child(
                        DiffView::new("scene.diff.split", scene_diff())
                            .presentation(DiffPresentation::Split)
                            .visible_rows(6)
                            .on_event(|_, _, _| {}),
                    ),
                ),
        )
        .child(caption(
            &theme,
            "a review of several files: what happened to each file said above \
             its lines, two folded behind headers that say what they hold, and \
             the hunk the reader stepped to marked and put on screen — as tall \
             as the frame it was given rather than a number of rows",
        ))
        .child(
            // Given a height rather than a row count, the way a pane gives one.
            // Kept short enough that the wrapping example below still finishes
            // inside the frame, but tall enough to show the next file header:
            // a scene whose last words are cut off is the defect its own
            // caption is about.
            div().w(px(840.0)).h(px(232.0)).child(
                DiffView::shared("scene.diff.review", std::sync::Arc::new(scene_review()))
                    .language("rust")
                    .fills()
                    .cursor(DiffCursor::Hunk {
                        file_id: "report".into(),
                        hunk_id: "summary".into(),
                    })
                    .on_event(|_, _, _| {}),
            ),
        )
        .child(caption(
            &theme,
            "the same rows measured rather than slotted, so a line longer than \
             the frame wraps instead of being cut off",
        ))
        .child(
            // Every example in this scene is the same width, so a difference
            // between two of them is about the diff and not about the frame.
            div().w(px(840.0)).child(
                DiffView::new("scene.diff.wrapping", scene_long_line())
                    .wrapping(true)
                    // Room for both lines *after* they wrap. A budget counted
                    // in unwrapped rows cuts the last line in half, which is
                    // the one thing this example exists to show does not happen.
                    .visible_rows(6)
                    .on_event(|_, _, _| {}),
            ),
        )
        .into_any_element()
}

Source: crates/gpui-kit/src/content/diff_view.rs