PranitInitial commit439f83c3 months agoHistory
929 lines · 34.0 KB · Zig
RawDownload
const std = @import("std");const command_output_runtime = @import("command_output_runtime.zig");const build_checkpoint = @import("../render_engine/build_checkpoint.zig");const tool_group_projection = @import("tool_group_projection.zig");const render_engine = @import("../render_engine.zig");const user_message_card = @import("../assistant/user_message_card.zig");const ui_render = @import("../render.zig"); const Allocator = std.mem.Allocator;const transcript_blocks = render_engine.transcript_blocks;const viewport_selection = render_engine.viewport_selection; const FoldedCommandBlock = command_output_runtime.FoldedCommandBlock;const TranscriptBuffer = viewport_selection.TranscriptBuffer;const TranscriptRef = viewport_selection.TranscriptRef;const buildHardLineStarts = viewport_selection.buildHardLineStarts;const buildHardLineStartsInterruptible = viewport_selection.buildHardLineStartsInterruptible;const hardLineRefAt = viewport_selection.hardLineRefAt;const leadingWelcomeBoundary = transcript_blocks.leadingWelcomeBoundary;const leadingWelcomeCutLine = transcript_blocks.leadingWelcomeCutLine;const stripTrailingNewline = transcript_blocks.stripTrailingNewline;const tailVisibleBlockKind = transcript_blocks.tailVisibleBlockKind;const visualRowsForLine = transcript_blocks.visualRowsForLine; test {    _ = tool_group_projection;} pub const TranscriptPreparationSource = struct {    bytes: []u8,    folded_summary_indices: []usize,    line_provenance: []const transcript_blocks.LineProvenance = &.{},    preview: render_engine.frame_layout.TranscriptFlowPreview,    tail_kind: ?transcript_blocks.TranscriptBlockKind,    tracked_entry_id: ?u32,    tracked_entry_start_line: ?usize,    replaceable_last_line: bool,    replaceable_start: usize,    replaceable_row: u16,    welcome_cut_line: ?usize,    welcome_boundary: ?transcript_blocks.LeadingWelcomeBoundary,    cols: u16,    recorded_entries_authoritative: bool = false,    cache_origin_untrimmed: bool = false,    hard_line_starts: []usize = &.{},    hard_lines_end_with_newline: bool = false,    transcript_visible_lines: []viewport_selection.VisibleTranscriptLine = &.{},    transcript_line_visual_rows: []u16 = &.{},    transcript_visual_row_offsets: []u32 = &.{},     pub fn deinit(self: *TranscriptPreparationSource, alloc: Allocator) void {        if (self.bytes.len > 0) alloc.free(self.bytes);        if (self.folded_summary_indices.len > 0) alloc.free(self.folded_summary_indices);        if (self.line_provenance.len > 0) alloc.free(self.line_provenance);        if (self.hard_line_starts.len > 0) alloc.free(self.hard_line_starts);        if (self.transcript_visible_lines.len > 0) alloc.free(self.transcript_visible_lines);        if (self.transcript_line_visual_rows.len > 0) alloc.free(self.transcript_line_visual_rows);        if (self.transcript_visual_row_offsets.len > 0) alloc.free(self.transcript_visual_row_offsets);        self.* = undefined;    }     pub fn ensureLineIndex(self: *TranscriptPreparationSource, alloc: Allocator) !void {        return self.ensureLineIndexInterruptible(alloc, null) catch |err| switch (err) {            error.InputPending => unreachable,            else => |other| return other,        };    }     pub fn ensureLineIndexInterruptible(        self: *TranscriptPreparationSource,        alloc: Allocator,        checkpoint: ?*build_checkpoint.BuildCheckpoint,    ) !void {        if (self.bytes.len == 0) return build_checkpoint.poll(checkpoint);        if (self.hard_line_starts.len > 0) return;        const hard_lines = try buildHardLineStartsInterruptible(alloc, self.bytes, checkpoint);        errdefer hard_lines.deinit(alloc);        const line_visual_rows = try alloc.alloc(u16, hard_lines.len());        errdefer alloc.free(line_visual_rows);        const visible_lines = try alloc.alloc(viewport_selection.VisibleTranscriptLine, hard_lines.len());        errdefer alloc.free(visible_lines);        const visual_row_offsets = try alloc.alloc(u32, hard_lines.len() + 1);        errdefer alloc.free(visual_row_offsets);        visual_row_offsets[0] = 0;        const transcript = TranscriptBuffer{ .bytes = self.bytes };        for (line_visual_rows, visible_lines, 0..) |*rows, *line, index| {            const ref = hardLineRefAt(hard_lines, self.bytes.len, index);            const bytes = (TranscriptRef{ .ref = ref }).resolve(transcript);            try build_checkpoint.consume(checkpoint, bytes.len);            rows.* = visualRowsForLine(bytes, self.cols);            line.* = viewport_selection.visibleFromTranscript(ref);            visual_row_offsets[index + 1] = visual_row_offsets[index] + rows.*;        }        self.hard_line_starts = hard_lines.starts;        self.hard_lines_end_with_newline = hard_lines.ends_with_newline;        self.transcript_visible_lines = visible_lines;        self.transcript_line_visual_rows = line_visual_rows;        self.transcript_visual_row_offsets = visual_row_offsets;    }     pub fn clone(self: *const TranscriptPreparationSource, alloc: Allocator) !TranscriptPreparationSource {        const bytes = try alloc.dupe(u8, self.bytes);        errdefer alloc.free(bytes);        const folded_summary_indices = try alloc.dupe(usize, self.folded_summary_indices);        errdefer alloc.free(folded_summary_indices);        const line_provenance = try alloc.dupe(            transcript_blocks.LineProvenance,            self.line_provenance,        );        errdefer alloc.free(line_provenance);        const hard_line_starts = try alloc.dupe(usize, self.hard_line_starts);        errdefer alloc.free(hard_line_starts);        const transcript_visible_lines = try alloc.dupe(            viewport_selection.VisibleTranscriptLine,            self.transcript_visible_lines,        );        errdefer alloc.free(transcript_visible_lines);        const transcript_line_visual_rows = try alloc.dupe(u16, self.transcript_line_visual_rows);        errdefer alloc.free(transcript_line_visual_rows);        const transcript_visual_row_offsets = try alloc.dupe(u32, self.transcript_visual_row_offsets);        errdefer alloc.free(transcript_visual_row_offsets);        return .{            .bytes = bytes,            .folded_summary_indices = folded_summary_indices,            .line_provenance = line_provenance,            .preview = self.preview,            .tail_kind = self.tail_kind,            .tracked_entry_id = self.tracked_entry_id,            .tracked_entry_start_line = self.tracked_entry_start_line,            .replaceable_last_line = self.replaceable_last_line,            .replaceable_start = self.replaceable_start,            .replaceable_row = self.replaceable_row,            .welcome_cut_line = self.welcome_cut_line,            .welcome_boundary = self.welcome_boundary,            .cols = self.cols,            .recorded_entries_authoritative = self.recorded_entries_authoritative,            .cache_origin_untrimmed = self.cache_origin_untrimmed,            .hard_line_starts = hard_line_starts,            .hard_lines_end_with_newline = self.hard_lines_end_with_newline,            .transcript_visible_lines = transcript_visible_lines,            .transcript_line_visual_rows = transcript_line_visual_rows,            .transcript_visual_row_offsets = transcript_visual_row_offsets,        };    }}; pub fn prepareTranscriptSource(    self: anytype,    alloc: Allocator,    tracked_entry_id: ?u32,) !TranscriptPreparationSource {    return prepareTranscriptSourceInternal(self, alloc, tracked_entry_id, null, null, null) catch |err| switch (err) {        error.InputPending => unreachable,        else => |other| return other,    };} pub fn prepareTranscriptSourceWithFocusedEntry(    self: anytype,    alloc: Allocator,    focused_entry_id: u32,) !TranscriptPreparationSource {    return prepareTranscriptSourceInternal(self, alloc, focused_entry_id, focused_entry_id, null, null) catch |err| switch (err) {        error.InputPending => unreachable,        else => |other| return other,    };} pub fn prepareTranscriptSourceOmittingEntry(    self: anytype,    alloc: Allocator,    omitted_entry_id: u32,) !TranscriptPreparationSource {    return prepareTranscriptSourceInternal(self, alloc, null, null, omitted_entry_id, null) catch |err| switch (err) {        error.InputPending => unreachable,        else => |other| return other,    };} pub fn prepareTranscriptSourceInterruptible(    self: anytype,    alloc: Allocator,    tracked_entry_id: ?u32,    focused_entry_id: ?u32,    omitted_entry_id: ?u32,    checkpoint: ?*build_checkpoint.BuildCheckpoint,) !TranscriptPreparationSource {    return prepareTranscriptSourceInternal(        self,        alloc,        tracked_entry_id,        focused_entry_id,        omitted_entry_id,        checkpoint,    );} pub fn renderCompactTranscriptBytes(    self: anytype,    alloc: Allocator,) ![]u8 {    var command_overrides = try buildCommandOutputOverrides(self, alloc);    defer command_overrides.deinit(alloc);    const styles = self.command_output_render.styles;     if (self.maxxing_mode == .legacy) {        const entry_actions = try buildCommandOutputActions(            alloc,            &command_overrides,            self.entries.items.len,        );        defer if (entry_actions.len > 0) alloc.free(entry_actions);        return transcript_blocks.renderEntriesWithProjectionToBytes(            alloc,            self.entries.items,            self.layout.cols,            styles,            entry_actions,            self.maxxing_mode,        );    }     var projection = try buildMinimalTranscriptProjection(        self,        alloc,        &command_overrides,        null,    );    defer projection.deinit(alloc);    return transcript_blocks.renderEntriesWithProjectionToBytes(        alloc,        self.entries.items,        self.layout.cols,        styles,        projection.entry_actions.items,        self.maxxing_mode,    );} /// Takes ownership of a bounded full-transcript visual window and prepares it/// for the ordinary transcript surface painter. Ctrl-O uses this only after/// the full document source has selected its current visual window.pub fn prepareFullTranscriptViewportSource(    self: anytype,    alloc: Allocator,    bytes: []u8,) !TranscriptPreparationSource {    return prepareFullTranscriptViewportSourceInterruptible(self, alloc, bytes, null) catch |err| switch (err) {        error.InputPending => unreachable,        else => |other| return other,    };} pub fn prepareFullTranscriptViewportSourceInterruptible(    self: anytype,    alloc: Allocator,    bytes: []u8,    checkpoint: ?*build_checkpoint.BuildCheckpoint,) !TranscriptPreparationSource {    errdefer alloc.free(bytes);    const line_provenance = if (observationEnabled(self, alloc))        try unattributedLineProvenance(alloc, bytes)    else        &.{};    errdefer if (line_provenance.len > 0) alloc.free(line_provenance);    const tail_kind = tailVisibleBlockKind(self.entries.items);    const preview = try transcriptFlowPreviewFromSourceInterruptible(        self,        alloc,        bytes,        &.{},        0,        false,        tail_kind,        checkpoint,    );    return .{        .bytes = bytes,        .folded_summary_indices = try alloc.alloc(usize, 0),        .line_provenance = line_provenance,        .preview = preview,        .tail_kind = tail_kind,        .tracked_entry_id = null,        .tracked_entry_start_line = null,        .replaceable_last_line = false,        .replaceable_start = 0,        .replaceable_row = self.replaceable_row,        .welcome_cut_line = null,        .welcome_boundary = null,        .cols = self.layout.cols,    };} fn prepareTranscriptSourceInternal(    self: anytype,    alloc: Allocator,    tracked_entry_id: ?u32,    focused_entry_id: ?u32,    omitted_entry_id: ?u32,    checkpoint: ?*build_checkpoint.BuildCheckpoint,) !TranscriptPreparationSource {    var bytes: []u8 = &.{};    errdefer if (bytes.len > 0) alloc.free(bytes);    var folded_summary_indices: []usize = &.{};    errdefer if (folded_summary_indices.len > 0) alloc.free(folded_summary_indices);    var line_provenance: []const transcript_blocks.LineProvenance = &.{};    errdefer if (line_provenance.len > 0) alloc.free(line_provenance);    var trailing_boundary_blank_rows: u16 = 0;    var tracked_entry_start_line: ?usize = null;    var replaceable_entry_start_byte: ?usize = null;    var rendered_from_entries = false;     var command_overrides = try buildCommandOutputOverridesInterruptible(self, alloc, checkpoint);    defer command_overrides.deinit(alloc);    var minimal_projection: ?tool_group_projection.Projection = null;    defer if (minimal_projection) |*projection| projection.deinit(alloc);    if (self.maxxing_mode == .minimal and self.entries.items.len > 0 and self.layout.cols > 0) {        minimal_projection = try buildMinimalTranscriptProjectionInterruptible(            self,            alloc,            &command_overrides,            focused_entry_id,            checkpoint,        );    }     const aligned_actions = if (minimal_projection == null)        try buildCommandOutputActions(            alloc,            &command_overrides,            self.entries.items.len,        )    else        &.{};    defer if (aligned_actions.len > 0) alloc.free(aligned_actions);     const entry_actions = if (minimal_projection) |*projection|        projection.entry_actions.items    else        aligned_actions;     if (self.entries.items.len > 0 and self.layout.cols > 0) {        rendered_from_entries = true;        const capture_provenance = observationEnabled(self, alloc) or            self.command_output_blocks.items.len > 0;        const replaceable_entry_id: ?u32 = if (self.replaceable_last_line and            self.entries.items[self.entries.items.len - 1] == .raw_bytes)            self.entries.items[self.entries.items.len - 1].raw_bytes.id        else            null;        const summary_entry_ids = try alloc.alloc(?u32, self.folded_command_blocks.items.len);        defer alloc.free(summary_entry_ids);        for (self.folded_command_blocks.items, 0..) |block, index| {            summary_entry_ids[index] = block.summary_entry_id;        }         const rendered = try transcript_blocks.renderEntriesForPreparationInterruptible(            alloc,            self.entries.items,            self.layout.cols,            self.command_output_render.styles,            .{                .target_entry_id = tracked_entry_id,                .target_byte_entry_id = replaceable_entry_id,                .omitted_entry_id = omitted_entry_id,                .folded_summary_entry_ids = summary_entry_ids,                .capture_provenance = capture_provenance,                .entry_actions = entry_actions,                .maxxing_mode = self.maxxing_mode,            },            checkpoint,        );        bytes = rendered.bytes;        folded_summary_indices = rendered.folded_summary_indices;        line_provenance = rendered.line_provenance;        trailing_boundary_blank_rows = rendered.trailing_boundary_blank_rows;        tracked_entry_start_line = rendered.target_entry_start_line;        replaceable_entry_start_byte = rendered.target_entry_start_byte;    } else {        bytes = try alloc.dupe(u8, self.transcript.items);        folded_summary_indices = try alloc.alloc(usize, self.folded_command_blocks.items.len);        for (self.folded_command_blocks.items, 0..) |block, index| {            folded_summary_indices[index] = block.summary_transcript_index;        }        if (observationEnabled(self, alloc)) {            line_provenance = try unattributedLineProvenance(alloc, bytes);        }    }     var replaceable_last_line = self.replaceable_last_line;    var replaceable_start = self.replaceable_start;    if (replaceable_last_line and self.entries.items.len > 0) {        const tail = &self.entries.items[self.entries.items.len - 1];        switch (tail.*) {            .raw_bytes => {                if (replaceable_entry_start_byte) |start| {                    replaceable_start = start;                } else {                    replaceable_last_line = false;                    replaceable_start = 0;                }            },            else => {                replaceable_last_line = false;                replaceable_start = 0;            },        }    }     const tail_start = if (rendered_from_entries)        0    else        cappedTailStart(bytes, self.max_transcript_bytes);    const cache_origin_untrimmed = tail_start == 0 and omitted_entry_id == null and        (rendered_from_entries or self.transcript_cache_origin_untrimmed);    const retained_len = bytes.len - tail_start;    const removed_line_count = std.mem.count(u8, bytes[0..tail_start], "\n");    const retained_origin_is_line_start =        tail_start == 0 or bytes[tail_start - 1] == '\n';    for (folded_summary_indices) |*summary_index| {        if (summary_index.* == 0) continue;        summary_index.* = if (rebaseLineIndexForCappedTail(            summary_index.* - 1,            retained_len,            removed_line_count,            retained_origin_is_line_start,        )) |line_index|            line_index + 1        else            0;    }    if (tracked_entry_start_line) |line_index| {        tracked_entry_start_line = rebaseLineIndexForCappedTail(            line_index,            retained_len,            removed_line_count,            retained_origin_is_line_start,        );    }    if (replaceable_last_line) {        if (retained_len == 0 or replaceable_start >= bytes.len) {            replaceable_last_line = false;            replaceable_start = 0;        } else if (replaceable_start > tail_start) {            replaceable_start -= tail_start;        } else {            replaceable_start = 0;        }    }    if (tail_start > 0) {        const capped = try alloc.dupe(u8, bytes[tail_start..]);        if (line_provenance.len > 0) {            const rebased = try rebaseLineProvenanceForCappedTail(                alloc,                line_provenance,                capped,                removed_line_count,                retained_origin_is_line_start,            );            alloc.free(line_provenance);            line_provenance = rebased;        }        alloc.free(bytes);        bytes = capped;    }     const tail_kind = transcript_blocks.compactTailVisibleBlockKindForProjection(        self.entries.items,        omitted_entry_id,        entry_actions,    );    const preview = try transcriptFlowPreviewFromSourceInterruptible(        self,        alloc,        bytes,        folded_summary_indices,        trailing_boundary_blank_rows,        replaceable_last_line,        tail_kind,        checkpoint,    );    const welcome_boundary = if (tail_start == 0)        try leadingWelcomeBoundary(            alloc,            self.entries.items,            self.layout.cols,            self.command_output_render.styles,        )    else        null;    const welcome_cut_line = if (tail_start > 0 or frameCommitted(self))        null    else        try leadingWelcomeCutLine(            alloc,            self.entries.items,            self.layout.cols,            self.command_output_render.styles,        ); 

Lines 1–500 of 929.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t