GPUI Box GitHub

Components / content

CodeView builder

Read-only code with a gutter.

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

use gpui_kit::content::CodeView;

Reviewed in code-view.

Construct
new(ident: impl Into<Ident>, lines: impl IntoIterator<Item = CodeLine>) -> Self
from_text(ident: impl Into<Ident>, text: &str) -> Self
Options

Chain onto the value.

language(language: impl Into<SharedString>) -> Self
line_numbers(line_numbers: bool) -> Self
visible_lines(lines: usize) -> Self
copyable(copyable: bool) -> Self
Queries

Only answer; they change nothing.

text() -> String
Example · code-view
fn code_view(_window: &mut Window, cx: &mut App) -> AnyElement {
    let theme = cx.theme().clone();
    // Spans are the caller's, in byte offsets into each line, and they
    // outrank what the scanner would have found for the same line.
    let lines = [
        CodeLine::new(40, "fn report(&self) -> Outcome {").spans([CodeSpan {
            range: 0..2,
            role: SyntaxColor::Keyword,
        }]),
        CodeLine::new(41, "    let verified = self.check();")
            .spans([CodeSpan {
                range: 4..7,
                role: SyntaxColor::Keyword,
            }])
            .mark(LineMark::Added),
        CodeLine::new(42, "    let stale = self.cached();").mark(LineMark::Removed),
        CodeLine::new(43, "    Outcome::from(verified)").mark(LineMark::Changed),
        CodeLine::new(
            44,
            "    // this line runs off the edge rather than wrapping, because a column carries meaning in code",
        ),
        CodeLine::new(45, "}").mark(LineMark::Error),
    ];
    stack(&theme)
        .w(px(620.0))
        .child(caption(
            &theme,
            "line numbers are the file's, marks are the host's, colour is the caller's",
        ))
        .child(CodeView::new("scene.code.report", lines).language("rust"))
        .child(caption(
            &theme,
            "and where the caller says only what the language is, the four classes it can find",
        ))
        .child(
            CodeView::new(
                "scene.code.scanned",
                [
                    CodeLine::new(1, "def summarize(runs):  # every class in one line"),
                    CodeLine::new(2, "    total = 0"),
                    CodeLine::new(3, "    for run in runs:"),
                    CodeLine::new(4, "        if run.status == \"passed\":"),
                    CodeLine::new(5, "            total += 1"),
                    CodeLine::new(6, "    return total"),
                ],
            )
            .language("python"),
        )
        .child(caption(
            &theme,
            "a language with no table is shown and left plain, never guessed at",
        ))
        .child(
            CodeView::new(
                "scene.code.unknown",
                [
                    CodeLine::new(1, "REPORT SECTION."),
                    CodeLine::new(2, "    MOVE 0 TO TOTAL."),
                ],
            )
            .language("cobol"),
        )
        .into_any_element()
}

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