Documentation
¶
Overview ¶
Package diff provides diff parsing, rendering, and formatting with syntax highlighting.
This package handles the core diff functionality including:
- Parsing unified diff format (ParseUnifiedDiff in parser.go)
- Character-level diff highlighting (HighlightIntralineChanges in highlighter.go)
- Syntax highlighting integration (SyntaxHighlight in syntax.go)
- Unified view rendering (RenderUnifiedHunk, FormatUnifiedDiff in unified.go)
- Side-by-side view rendering (RenderSideBySideHunk, FormatDiff in sidebyside.go)
High-level API:
- FormatUnifiedDiff(filename, diffText, config, opts...) - Full unified diff
- FormatDiff(filename, diffText, config, opts...) - Full side-by-side diff
Architecture:
- types.go: Core data structures (LineType, DiffLine, Hunk, etc.)
- parser.go: Unified diff parsing
- highlighter.go: Intra-line diff highlighting
- syntax.go: Syntax highlighting integration with Chroma
- render.go: Shared rendering primitives
- unified.go: Unified view rendering
- sidebyside.go: Side-by-side view rendering
This package was refactored from a single 976-line file into focused modules for better maintainability and testability.
Index ¶
- func CountHunks(results []DiffResult) int
- func CreateFileHyperlink(path string, text string, enabled bool) string
- func FormatDiff(filename string, diffText string, cfg *config.Config, opts ...UnifiedOption) (string, error)
- func FormatLineNumbers(oldLine, newLine int, style LineNumberStyle, lineType LineType) string
- func FormatUnifiedDiff(filename string, diffText string, cfg *config.Config, opts ...UnifiedOption) (string, error)
- func GetLineNumberWidth(style LineNumberStyle) int
- func HighlightIntralineChanges(h *Hunk)
- func ParseStats(diff string) (map[string]DiffStats, error)
- func RenderDiffStats(results []DiffResult) string
- func RenderFileHeader(result DiffResult, showStats bool, hyperlinksEnabled bool, graphicalStats bool) string
- func RenderFileSeparator(style SeparatorStyle, width int, t theme.Theme) string
- func RenderHunkHeader(h Hunk, cfg *config.Config, width int) string
- func RenderHunkSeparator(style SeparatorStyle, width int, t theme.Theme) string
- func RenderSeparator(style SeparatorStyle, width int, t theme.Theme) string
- func RenderSideBySideHunk(fileName string, h Hunk, cfg *config.Config, hunkIndex int, totalHunks int, ...) string
- func RenderUnifiedHunk(fileName string, h Hunk, cfg *config.Config, hunkIndex int, totalHunks int, ...) string
- func SupportsHyperlinks() bool
- func SyntaxHighlight(w io.Writer, source, fileName, formatter string, bg color.Color) error
- type ChangeStats
- type DiffLine
- type DiffResult
- type DiffStats
- type FileMetadata
- type FileStatus
- type Hunk
- type HunkInfo
- type HunkMetadata
- type LineNumberStyle
- type LineType
- type Segment
- type SeparatorStyle
- type UnifiedConfig
- type UnifiedOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountHunks ¶
func CountHunks(results []DiffResult) int
CountHunks returns the total number of hunks across all diff results
func CreateFileHyperlink ¶
CreateFileHyperlink creates an OSC 8 hyperlink for a file path Format: ESC]8;;file://absolute/path ESC\text ESC]8;; ESC\
func FormatDiff ¶
func FormatDiff(filename string, diffText string, cfg *config.Config, opts ...UnifiedOption) (string, error)
FormatDiff creates a side-by-side formatted view of a diff
func FormatLineNumbers ¶
func FormatLineNumbers(oldLine, newLine int, style LineNumberStyle, lineType LineType) string
FormatLineNumbers formats line numbers according to the specified style
func FormatUnifiedDiff ¶
func FormatUnifiedDiff(filename string, diffText string, cfg *config.Config, opts ...UnifiedOption) (string, error)
FormatUnifiedDiff creates a unified formatted view of a diff
func GetLineNumberWidth ¶
func GetLineNumberWidth(style LineNumberStyle) int
GetLineNumberWidth returns the display width for the given style
func HighlightIntralineChanges ¶
func HighlightIntralineChanges(h *Hunk)
HighlightIntralineChanges updates lines in a hunk to show character-level differences
func RenderDiffStats ¶
func RenderDiffStats(results []DiffResult) string
RenderDiffStats renders a summary of all file changes
func RenderFileHeader ¶
func RenderFileHeader(result DiffResult, showStats bool, hyperlinksEnabled bool, graphicalStats bool) string
RenderFileHeader renders a file header with status and statistics
func RenderFileSeparator ¶
func RenderFileSeparator(style SeparatorStyle, width int, t theme.Theme) string
RenderFileSeparator renders a separator between files
func RenderHunkHeader ¶
RenderHunkHeader renders a styled hunk header line
func RenderHunkSeparator ¶
func RenderHunkSeparator(style SeparatorStyle, width int, t theme.Theme) string
RenderHunkSeparator renders a separator between hunks
func RenderSeparator ¶
func RenderSeparator(style SeparatorStyle, width int, t theme.Theme) string
RenderSeparator renders a separator line with the specified style and width
func RenderSideBySideHunk ¶
func RenderSideBySideHunk(fileName string, h Hunk, cfg *config.Config, hunkIndex int, totalHunks int, opts ...UnifiedOption) string
RenderSideBySideHunk formats a hunk for side-by-side display
func RenderUnifiedHunk ¶
func RenderUnifiedHunk(fileName string, h Hunk, cfg *config.Config, hunkIndex int, totalHunks int, opts ...UnifiedOption) string
RenderUnifiedHunk formats a hunk for unified display
func SupportsHyperlinks ¶
func SupportsHyperlinks() bool
SupportsHyperlinks attempts to detect if the terminal supports OSC 8 hyperlinks
Types ¶
type ChangeStats ¶
type ChangeStats struct {
TotalFiles int
ModifiedFiles int
AddedFiles int
DeletedFiles int
RenamedFiles int
TotalHunks int
LinesAdded int
LinesRemoved int
}
CountFileChanges returns statistics about file changes
func CalculateStats ¶
func CalculateStats(results []DiffResult) ChangeStats
CalculateStats computes statistics for multiple diff results
type DiffLine ¶
type DiffLine struct {
OldLineNo int // Line number in old file (0 for added lines)
NewLineNo int // Line number in new file (0 for removed lines)
Kind LineType // Type of line (added, removed, context)
Content string // Content of the line
Segments []Segment // Segments for intraline highlighting
}
DiffLine represents a single line in a diff
type DiffResult ¶
type DiffResult struct {
OldFile string
NewFile string
Metadata FileMetadata // File metadata
Hunks []Hunk
}
DiffResult contains the parsed result of a diff
func ParseMultiFileDiff ¶
func ParseMultiFileDiff(diff string) ([]DiffResult, error)
ParseMultiFileDiff parses a diff containing multiple files
func ParseUnifiedDiff ¶
func ParseUnifiedDiff(diff string) (DiffResult, error)
ParseUnifiedDiff parses a unified diff format string into structured data
type FileMetadata ¶
type FileMetadata struct {
OldPath string // Path in old version
NewPath string // Path in new version
Status FileStatus // File modification status
LinesAdded int // Number of lines added
LinesRemoved int // Number of lines removed
}
FileMetadata contains metadata about a file in a diff
type FileStatus ¶
type FileStatus int
FileStatus represents the modification status of a file
const ( FileModified FileStatus = iota // File was modified FileAdded // File was added FileDeleted // File was deleted FileRenamed // File was renamed FileCopied // File was copied )
func (FileStatus) String ¶
func (fs FileStatus) String() string
String returns the short string representation of file status
type HunkInfo ¶
type HunkInfo struct {
OldStart int // Starting line in old file
OldCount int // Number of lines in old file
NewStart int // Starting line in new file
NewCount int // Number of lines in new file
Context string // Optional function/context info
}
HunkInfo contains parsed information from a hunk header
func ParseHunkHeader ¶
ParseHunkHeader extracts line range information from a hunk header
type HunkMetadata ¶
type HunkMetadata struct {
FileIndex int // Index of the file this hunk belongs to
HunkIndex int // Index of this hunk within the file
GlobalIndex int // Global index across all files
OldStart int // Starting line in old file
OldCount int // Number of lines in old file
NewStart int // Starting line in new file
NewCount int // Number of lines in new file
Context string // Optional function/context info
LineCount int // Total number of lines in this hunk
}
HunkMetadata contains metadata about a hunk for navigation purposes
func BuildHunkIndex ¶
func BuildHunkIndex(results []DiffResult) []HunkMetadata
BuildHunkIndex creates a complete index of all hunks for navigation
func GetHunkMetadata ¶
func GetHunkMetadata(h Hunk, fileIndex, hunkIndex, globalIndex int) HunkMetadata
GetHunkMetadata extracts metadata from a hunk for navigation
type LineNumberStyle ¶
type LineNumberStyle int
LineNumberStyle defines how line numbers are formatted
const ( // LineNumberStandard shows full line numbers with padding (default) // Format: " 4 5" (12 chars width) LineNumberStandard LineNumberStyle = iota // LineNumberCompact shows condensed line numbers // Format: "4 5" (minimal width) LineNumberCompact // LineNumberMinimal shows no line numbers, only markers // Format: "" (0 width, just the marker) LineNumberMinimal // LineNumberLeft shows only left (old) line numbers // Format: " 4" (6 chars width) LineNumberLeft // LineNumberRight shows only right (new) line numbers // Format: " 5" (6 chars width) LineNumberRight )
func ParseLineNumberStyle ¶
func ParseLineNumberStyle(s string) LineNumberStyle
ParseLineNumberStyle parses a string into a LineNumberStyle
func (LineNumberStyle) String ¶
func (s LineNumberStyle) String() string
String returns the string representation of the line number style
type SeparatorStyle ¶
type SeparatorStyle int
SeparatorStyle defines the visual style of separators
const ( // SeparatorNone shows no separator SeparatorNone SeparatorStyle = iota // SeparatorBlank shows a blank line SeparatorBlank // SeparatorLine shows a single line (─) SeparatorLine // SeparatorDoubleLine shows a double line (═) SeparatorDoubleLine // SeparatorDashed shows a dashed line (┄) SeparatorDashed // SeparatorDotted shows a dotted line (⋯) SeparatorDotted )
func ParseSeparatorStyle ¶
func ParseSeparatorStyle(s string) SeparatorStyle
ParseSeparatorStyle parses a string into a SeparatorStyle
func (SeparatorStyle) String ¶
func (s SeparatorStyle) String() string
String returns the string representation of the separator style
type UnifiedConfig ¶
type UnifiedConfig struct {
Width int
}
UnifiedConfig configures the rendering of unified diffs
func NewSideBySideConfig ¶
func NewSideBySideConfig(opts ...UnifiedOption) UnifiedConfig
NewSideBySideConfig creates a SideBySideConfig with default values
func NewUnifiedConfig ¶
func NewUnifiedConfig(opts ...UnifiedOption) UnifiedConfig
NewUnifiedConfig creates a UnifiedConfig with default values