diffview

package
v0.51.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package diffview builds what one file tool call changes on disk and renders it as a unified diff.

The construction is shared: the ACP server turns a Change into the tool_call_diff it sends its client (internal/acpserver), and chat shows the same change under the tool result, before an approval question and again in full with its :diff command (internal/chat), so both present the same preview. Chat builds this package's Lines in a form the terminal can read; nothing here knows about a terminal, an ACP client or a tool registry.

Nothing runs for a tool that only reads, and nothing is read outside the workspace: a call whose path is outside the workspace (or under a dangling symlink), a directory, a binary or an oversized file yields no change, and the call itself is not touched either way - the preview is read before the tool runs.

Index

Constants

View Source
const (
	// ToolEdit is file_edit (old_string/new_string or edits).
	ToolEdit = "file_edit"
	// ToolWrite is file_write (content).
	ToolWrite = "file_write"
	// ToolRemove is file_remove (path).
	ToolRemove = "file_remove"
)

The tools whose calls change one workspace file.

View Source
const MaxBytes = 256 << 10

MaxBytes caps each side of one change: a larger file yields no change, the caller only points at the path.

Variables

This section is empty.

Functions

func Ptr

func Ptr(text string) *string

Ptr returns a pointer to text for callers constructing a Change directly. Call and At do not need it; it is useful to describe an in-memory change.

Types

type Change

type Change struct {
	// Path is the absolute path of the file, as the file tools resolve it.
	Path string
	// Rel is that path relative to the workspace: the short form to show a
	// person (never empty, the path is always inside the workspace).
	Rel string
	// Tool is the call that makes the change: ToolEdit, ToolWrite or
	// ToolRemove.
	Tool string
	// Old is the file's text before the call; nil when the file does not exist
	// (file_write creating it).
	Old *string
	// New is the text the call writes; nil for a removal, which writes nothing.
	New *string
	// RemovedLines is how many lines the file ToolRemove takes away (0 for the
	// other tools, and for a target that is not a regular text file).
	RemovedLines int
}

Change is what one file tool call does to one file: the text before the call and the text the call writes, or the line count a removal takes away.

func At

func At(cwd, name, path string, args map[string]any) (*Change, bool)

At returns the change of one file tool call on an already resolved absolute path: the ACP server resolves the path for the call's location first and previews the same target. cwd is the workspace the path must stay inside.

func Call

func Call(cwd, name string, args map[string]any) (*Change, bool)

Call returns the change of one file tool call (ToolEdit, ToolWrite or ToolRemove) by the arguments the model sent. ok is false for every other tool and whenever there is nothing to preview: a path the file tools would refuse, a target that is not a regular text file within MaxBytes, a file_edit that cannot apply, or a file_remove whose target is gone.

func (*Change) Lines

func (c *Change) Lines() []Line

Lines returns the unified diff of the change: every change with contextLines unchanged lines around it, changes closer than that merged into one hunk. It is nil when the change shows no text (a removal only reports RemovedLines).

func (*Change) NewFile

func (c *Change) NewFile() bool

NewFile reports whether the call creates the file (file_write onto a path that did not exist).

type Kind

type Kind uint8

Kind distinguishes the lines of a unified diff.

const (
	// Hunk is a hunk header ("@@ -12,3 +12,4 @@").
	Hunk Kind = iota + 1
	// Context is a line the change left alone.
	Context
	// Add is a line the change adds.
	Add
	// Del is a line the change removes.
	Del
)

type Line

type Line struct {
	Kind Kind
	// Text is the line as it reads: the content of a context, added or removed
	// line, or the header of a hunk line.
	Text string
	// OldNo and NewNo are the numbers the line has in the old and in the new
	// file (0: that side has no such line). On a hunk line they are the first
	// numbers the hunk covers.
	OldNo, NewNo int
}

Line is one line of a unified diff.

func (Line) Number

func (l Line) Number() int

Number is the number to show next to a diff line: the new file for an added line, the old file otherwise - the numbering a unified diff's hunks follow.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL