Documentation
¶
Overview ¶
Package diff provides utilities for computing and rendering differences between files. It supports unified diff parsing, side-by-side rendering, and patch application.
Package diff provides utilities for computing and rendering differences between files. It supports unified diff parsing, side-by-side rendering, and patch application.
Index ¶
- func ApplyCommit(commit Commit, writeFn func(string, string) error, removeFn func(string) error) error
- func FormatDiff(diffText string, opts ...SideBySideOption) (string, error)
- func GenerateDiff(beforeContent, afterContent, fileName string) (string, int, int)
- func HighlightIntralineChanges(h *Hunk)
- func IdentifyFilesAdded(text string) []string
- func IdentifyFilesNeeded(text string) []string
- func LoadFiles(paths []string, openFn func(string) (string, error)) (map[string]string, error)
- func OpenFile(p string) (string, error)
- func ProcessPatch(text string, openFn func(string) (string, error), ...) (string, error)
- func RemoveFile(p string) error
- func RenderSideBySideHunk(fileName string, h Hunk, opts ...SideBySideOption) string
- func SyntaxHighlight(w io.Writer, source, fileName, formatter string, bg lipgloss.TerminalColor) error
- func TrimDiff(diffText string) string
- func ValidatePatch(patchText string, files map[string]string) (bool, string, error)
- func WriteFile(p string, content string) error
- type ActionType
- type Chunk
- type Commit
- type DiffError
- type DiffLine
- type DiffResult
- type FileChange
- type Hunk
- type LineType
- type ParseConfig
- type ParseOption
- type Parser
- type Patch
- type PatchAction
- type Segment
- type SideBySideConfig
- type SideBySideOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyCommit ¶
func FormatDiff ¶
func FormatDiff(diffText string, opts ...SideBySideOption) (string, error)
FormatDiff creates a side-by-side formatted view of a diff
func GenerateDiff ¶
GenerateDiff creates a unified diff from two file contents
func HighlightIntralineChanges ¶
func HighlightIntralineChanges(h *Hunk)
HighlightIntralineChanges updates lines in a hunk to show character-level differences
func IdentifyFilesAdded ¶
func IdentifyFilesNeeded ¶
func ProcessPatch ¶
func RemoveFile ¶
func RenderSideBySideHunk ¶
func RenderSideBySideHunk(fileName string, h Hunk, opts ...SideBySideOption) string
RenderSideBySideHunk formats a hunk for side-by-side display
func SyntaxHighlight ¶
func SyntaxHighlight(w io.Writer, source, fileName, formatter string, bg lipgloss.TerminalColor) error
SyntaxHighlight applies syntax highlighting to text based on file extension
func TrimDiff ¶ added in v1.7.0
TrimDiff strips the longest common whitespace prefix from all content lines in a unified diff, making diffs more readable when code is deeply indented.
func ValidatePatch ¶
Types ¶
type ActionType ¶
type ActionType string
ActionType represents the type of file change action.
const ( ActionAdd ActionType = "add" ActionDelete ActionType = "delete" ActionUpdate ActionType = "update" )
type Chunk ¶
type Chunk struct {
OrigIndex int // line index of the first line in the original file
DelLines []string // lines to delete
InsLines []string // lines to insert
}
Chunk represents a section of changes within a file.
type Commit ¶
type Commit struct {
Changes map[string]FileChange
}
Commit holds a collection of file changes.
func AssembleChanges ¶
type DiffError ¶
type DiffError struct {
// contains filtered or unexported fields
}
DiffError represents an error that occurs during diff or patch operations.
func NewDiffError ¶
NewDiffError creates a new DiffError with the given message.
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 ¶
DiffResult contains the parsed result of a diff
func ParseUnifiedDiff ¶
func ParseUnifiedDiff(diff string) (DiffResult, error)
ParseUnifiedDiff parses a unified diff format string into structured data
type FileChange ¶
type FileChange struct {
Type ActionType
OldContent *string
NewContent *string
MovePath *string
}
FileChange represents a single file change with old and new content.
type ParseConfig ¶
type ParseConfig struct {
ContextSize int // Number of context lines to include
}
ParseConfig configures the behavior of diff parsing
type ParseOption ¶
type ParseOption func(*ParseConfig)
ParseOption modifies a ParseConfig
func WithContextSize ¶
func WithContextSize(size int) ParseOption
WithContextSize sets the number of context lines to include
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser parses patch text into structured Patch data.
type Patch ¶
type Patch struct {
Actions map[string]PatchAction
}
Patch represents a collection of patch actions for multiple files.
type PatchAction ¶
type PatchAction struct {
Type ActionType
NewFile *string
Chunks []Chunk
MovePath *string
}
PatchAction represents a patch action for a single file.
type SideBySideConfig ¶
type SideBySideConfig struct {
TotalWidth int
}
SideBySideConfig configures the rendering of side-by-side diffs
func NewSideBySideConfig ¶
func NewSideBySideConfig(opts ...SideBySideOption) SideBySideConfig
NewSideBySideConfig creates a SideBySideConfig with default values
type SideBySideOption ¶
type SideBySideOption func(*SideBySideConfig)
SideBySideOption modifies a SideBySideConfig
func WithTotalWidth ¶
func WithTotalWidth(width int) SideBySideOption
WithTotalWidth sets the total width for side-by-side view