serialize

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const FallbackAnnotationParseError = "[parse error, falling back to line diff]"

FallbackAnnotationParseError is the user-facing terminal badge text for parse errors.

View Source
const MaxUngroupedHighlightLines = 3

MaxUngroupedHighlightLines is how many lines a standalone move can span before dropping its contrasting color (slot 1 / Magenta) and falling back to Cyan (slot 0).

View Source
const SchemaVersion = "v1"

SchemaVersion defines the stable, versioned JSON output format version.

Variables

This section is empty.

Functions

func AssignMoveColors added in v0.10.0

func AssignMoveColors(actions []Action, srcOffsets, dstOffsets []int)

AssignMoveColors assigns a color (0: Cyan, 1: Magenta) to each move:

  • Grouped moves (siblings moving together between the same parents) share one color.
  • Children moving along inside a moving container inherit their parent's color.
  • Moves within 2 lines of each other get different colors so they don't blend together, whether they start close and diverge, or converge near the same destination lines.

func BuildLineIndex added in v0.4.0

func BuildLineIndex(data []byte) []int

BuildLineIndex returns byte offsets for the start of each line.

func ByteToLineCol added in v0.4.0

func ByteToLineCol(lineIndex []int, offset uint32) (line, col int)

ByteToLineCol maps a byte offset to a zero-indexed line and column.

func ForEachLineSpan added in v0.4.0

func ForEachLineSpan(lineIndex []int, fileBytes []byte, startByte, endByte uint32, yield func(line, startCol, endCol int))

ForEachLineSpan runs yield for every line that intersects [startByte, endByte].

func MarshalWithOptions added in v0.6.0

func MarshalWithOptions(es *actions.EditScript, ms *engine.Mapping, srcRoot, dstRoot *treesitter.ASTNode, srcBytes, dstBytes []byte, opts EnvelopeOptions) ([]byte, error)

MarshalWithOptions formats the diff envelope as indented JSON using the given options.

func ResolveMoveColor added in v0.14.0

func ResolveMoveColor(act *Action, lineCount int, groupIsMultiLine bool) int

ResolveMoveColor picks the color slot for a move. Standalone moves keep slot 1 (Magenta) up to 3 lines. Anything larger or inside a multiline group collapses to slot 0 (Cyan) so big blocks don't overwhelm the screen.

Types

type Action

type Action struct {
	Action            string              `json:"action"` // "insert", "delete", "update", "move"
	Node              *NodeRef            `json:"node"`
	Parent            *NodeRef            `json:"parent,omitempty"`
	Position          *int                `json:"position,omitempty"`
	OldParent         *NodeRef            `json:"old_parent,omitempty"`
	OldPosition       *int                `json:"old_position,omitempty"`
	OldValue          string              `json:"old_value,omitempty"`
	NewValue          string              `json:"new_value,omitempty"`
	Subtree           *bool               `json:"subtree,omitempty"`
	DestNode          *NodeRef            `json:"dest_node,omitempty"`
	DestStartByte     *uint32             `json:"dest_start_byte,omitempty"`
	DestEndByte       *uint32             `json:"dest_end_byte,omitempty"`
	GroupID           string              `json:"group_id,omitempty"`
	MoveColorIndex    int                 `json:"-"`
	OrigStartByte     uint32              `json:"-"`
	OrigEndByte       uint32              `json:"-"`
	DestOrigStartByte uint32              `json:"-"`
	DestOrigEndByte   uint32              `json:"-"`
	ASTNode           *treesitter.ASTNode `json:"-"`
	DestASTNode       *treesitter.ASTNode `json:"-"`
}

Action is a single AST edit operation. Omitting "subtree" on the wire implicitly means false.

type DelimiterSpan added in v0.8.0

type DelimiterSpan struct {
	StartByte uint32
	EndByte   uint32
	Action    string // "insert", "delete", "move"
	ActionRef *Action
}

DelimiterSpan tracks a closing delimiter (like a brace or bracket) for UI highlights.

type Envelope

type Envelope struct {
	Version            string              `json:"version"`
	IsBinary           bool                `json:"is_binary,omitempty"`
	FallbackReason     FallbackReason      `json:"fallback_reason,omitempty"`
	FallbackAnnotation string              `json:"fallback_annotation,omitempty"`
	Actions            []Action            `json:"actions,omitempty"`
	LineAlignment      []LineAlignmentPair `json:"line_alignment,omitempty"`
	LeftHighlights     []HighlightSpan     `json:"left_highlights,omitempty"`
	RightHighlights    []HighlightSpan     `json:"right_highlights,omitempty"`
}

Envelope wraps the serialized actions list with a schema version.

func BuildEnvelopeWithOptions added in v0.6.0

func BuildEnvelopeWithOptions(es *actions.EditScript, ms *engine.Mapping, srcRoot, dstRoot *treesitter.ASTNode, srcBytes, dstBytes []byte, opts EnvelopeOptions) (*Envelope, error)

BuildEnvelopeWithOptions translates raw diff actions and line alignments into the finalized JSON payload.

func BuildLineDiffEnvelopeWithOptions added in v0.6.0

func BuildLineDiffEnvelopeWithOptions(srcBytes, dstBytes []byte, opts EnvelopeOptions) *Envelope

BuildLineDiffEnvelopeWithOptions builds a fallback line-diff envelope when tree-sitter parsing fails or is skipped.

type EnvelopeOptions added in v0.6.0

type EnvelopeOptions struct {
	IncludeActions      bool
	IncludeAlignment    bool
	IncludeHighlights   bool
	CommentLineMappings map[int]int
}

EnvelopeOptions selects which sections to include in the JSON envelope.

type FallbackReason added in v0.14.0

type FallbackReason string

FallbackReason classifies why Diffmantic degraded to line-diff mode.

const (
	FallbackReasonNone                FallbackReason = ""
	FallbackReasonParseError          FallbackReason = "parse_error"
	FallbackReasonUnsupportedLanguage FallbackReason = "unsupported_language"
	FallbackReasonMismatchedLanguages FallbackReason = "mismatched_languages"
)

type HighlightSpan added in v0.6.0

type HighlightSpan struct {
	Line       int     `json:"line"`
	StartCol   int     `json:"start_col"`
	EndCol     int     `json:"end_col"`
	Action     string  `json:"action"` // "insert", "delete", "update", "move"
	ColorIndex int     `json:"-"`
	ActionRef  *Action `json:"-"`
}

HighlightSpan is a visual column range to color on a specific line.

func BuildHighlightSpans added in v0.6.0

func BuildHighlightSpans(fileBytes []byte, actions []Action, side string, extraSpans ...DelimiterSpan) []HighlightSpan

BuildHighlightSpans returns pre-merged highlight spans for one side of a diff. It splits byte ranges across newlines and merges adjacent spans of the same action when separated only by small punctuation or whitespace gaps. Pass extraSpans to include closing delimiter highlights without adding extra actions.

func (HighlightSpan) MarshalJSON added in v0.6.0

func (s HighlightSpan) MarshalJSON() ([]byte, error)

func (*HighlightSpan) UnmarshalJSON added in v0.6.0

func (s *HighlightSpan) UnmarshalJSON(b []byte) error

type LineAlignmentPair added in v0.2.0

type LineAlignmentPair struct {
	LeftLine  int `json:"left_line"`
	RightLine int `json:"right_line"`
}

LineAlignmentPair matches a source file line to a destination file line. A line index of -1 indicates a filler line.

func AlignLines added in v0.2.0

func AlignLines(srcBytes, dstBytes []byte, ms *engine.Mapping, es *actions.EditScript, commentLineMappings ...map[int]int) []LineAlignmentPair

AlignLines computes the side-by-side alignment grid using exact line diff matches and semantic AST node mappings as alignment anchors.

func (LineAlignmentPair) MarshalJSON added in v0.6.0

func (p LineAlignmentPair) MarshalJSON() ([]byte, error)

func (*LineAlignmentPair) UnmarshalJSON added in v0.6.0

func (p *LineAlignmentPair) UnmarshalJSON(b []byte) error

type NodeRef

type NodeRef struct {
	Tree      string `json:"tree"` // "before" or "after"
	Type      string `json:"type"`
	Label     string `json:"label,omitempty"`
	StartByte uint32 `json:"start_byte"`
	EndByte   uint32 `json:"end_byte"`
}

NodeRef is a stable and self-describing reference to an AST node.

Jump to

Keyboard shortcuts

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