diff

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package diff computes structured diffs between file contents.

It supports three display styles: Unified (standard git diff format), SideBySide (old on left, new on right), and FullFile (entire file with changed lines highlighted). Optional word-level diff highlighting shows exactly which characters changed within modified lines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectLanguage

func DetectLanguage(filename string) string

DetectLanguage returns the Chroma language name for a given filename, or empty string if the language cannot be determined.

Types

type DiffLine

type DiffLine struct {
	Type          LineType
	Content       string
	OldLineNum    int               // 0 if not applicable (e.g., added lines)
	NewLineNum    int               // 0 if not applicable (e.g., removed lines)
	InlineChanges []InlineHighlight // non-nil when word diff is enabled
}

DiffLine represents a single line in a diff result.

type DiffResult

type DiffResult struct {
	Lines           []DiffLine       // populated for Unified and FullFile styles
	SideBySidePairs []SideBySidePair // populated for SideBySide style
	Style           DiffStyle
	HasChanges      bool
}

DiffResult contains the result of a diff computation.

func Compute

func Compute(oldContent, newContent string, opts Options) DiffResult

Compute computes a diff between oldContent and newContent.

type DiffStyle

type DiffStyle int

DiffStyle represents the display style for diffs.

const (
	Unified    DiffStyle = iota // Standard git diff format
	SideBySide                  // Old on left, new on right
	FullFile                    // Entire file with changed lines highlighted
)

type Highlighter

type Highlighter struct {
	// contains filtered or unexported fields
}

Highlighter provides syntax highlighting for diff content.

func NewHighlighter

func NewHighlighter(theme string) *Highlighter

NewHighlighter creates a Highlighter with the given Chroma theme name. Falls back to "dracula" if the theme is not found.

func (*Highlighter) Highlight

func (h *Highlighter) Highlight(filename, content string) (string, error)

Highlight returns content with ANSI terminal256 syntax highlighting applied. The filename is used to detect the programming language.

func (*Highlighter) SetTheme

func (h *Highlighter) SetTheme(theme string)

SetTheme changes the highlighting theme. No-op if theme is not found.

func (*Highlighter) Style

func (h *Highlighter) Style() *chroma.Style

Style returns the underlying Chroma style for direct UI integration.

func (*Highlighter) Theme

func (h *Highlighter) Theme() string

Theme returns the current theme name.

func (*Highlighter) Tokenize

func (h *Highlighter) Tokenize(filename, content string) ([][]Token, error)

Tokenize returns syntax tokens grouped by line for the given content. This is useful for UI rendering where tokens need to be combined with diff-level styling (e.g., green background for added lines with syntax colors on the text).

type InlineHighlight

type InlineHighlight struct {
	Start int // byte offset (inclusive)
	End   int // byte offset (exclusive)
}

InlineHighlight marks a byte range within a line that changed (for word diff).

type LineType

type LineType int

LineType represents the type of a diff line.

const (
	Context LineType = iota // Unchanged line
	Added                   // Line added in new content
	Removed                 // Line removed from old content
	Header                  // Hunk header (@@ ... @@)
)

type Options

type Options struct {
	ContextLines int       // lines of context around changes (default 3)
	Style        DiffStyle // display style
	FileName     string    // used for syntax highlighting language detection
	WordDiff     bool      // enable word-level diff highlighting
}

Options configures the diff computation.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns Options with sensible defaults.

type SideBySidePair

type SideBySidePair struct {
	Left  *DiffLine // nil if line was added (only on right)
	Right *DiffLine // nil if line was removed (only on left)
}

SideBySidePair represents a pair of lines for side-by-side display.

type Token

type Token struct {
	Type  chroma.TokenType
	Value string
}

Token represents a syntax-highlighted segment of text.

Jump to

Keyboard shortcuts

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