Documentation
¶
Index ¶
- Variables
- func AncestorNameSimilarity(t1, t2 *treesitter.ASTNode) int
- func BottomUp(t1Root, t2Root *treesitter.ASTNode, m *Mapping, minDice float64)
- func ChawatheSimilarity(t1, t2 *treesitter.ASTNode, m map[*treesitter.ASTNode]*treesitter.ASTNode) float64
- func CommentSimilarity(s1, s2 string) float64
- func CompatiblePairRoles(n1, n2 *treesitter.ASTNode) bool
- func ContestContainers(t1Root, t2Root *treesitter.ASTNode, m *Mapping)
- func Dice(t1, t2 *treesitter.ASTNode, m map[*treesitter.ASTNode]*treesitter.ASTNode) float64
- func FindEnclosingScope(n *treesitter.ASTNode, r *rules.Rules) *treesitter.ASTNode
- func FprintMappings(w io.Writer, r *MatchResult) error
- func GetEnclosingDeclaration(n *treesitter.ASTNode) *treesitter.ASTNode
- func HasLongLeafToken(n *treesitter.ASTNode) bool
- func Height(n *treesitter.ASTNode) int
- func IsScopePreserved(ms *Mapping, n1, n2 *treesitter.ASTNode, r1, r2 *rules.Rules) bool
- func Isomorphic(a, b *treesitter.ASTNode) bool
- func LCS(seq1, seq2 []*treesitter.ASTNode, eq eqFunc) [][2]*treesitter.ASTNode
- func LCSLabel(seq1, seq2 []*treesitter.ASTNode) [][2]*treesitter.ASTNode
- func LCSStructure(seq1, seq2 []*treesitter.ASTNode) [][2]*treesitter.ASTNode
- func LeafSimilarity(a, b *treesitter.ASTNode) float64
- func LineDiff(linesA, linesB []string) map[int]int
- func MatchContainerKeywords(t1Root, t2Root *treesitter.ASTNode, m *Mapping)
- func MatchDataContainers(t1Root, t2Root *treesitter.ASTNode, m *Mapping, r *rules.Rules)
- func MatchUnmatchedLeaves(t1Root, t2Root *treesitter.ASTNode, m *Mapping, part *LinePartition)
- func NearestMatchedAncestor(n *treesitter.ASTNode, m *Mapping, isDst bool) *treesitter.ASTNode
- func Recover(t1, t2 *treesitter.ASTNode, m *Mapping)
- func RollupMatchedContainers(t1Root, t2Root *treesitter.ASTNode, m *Mapping)
- func RunZSRecovery(t1, t2 *treesitter.ASTNode, m *Mapping)
- func SharedMatchedAncestor(t1, t2 *treesitter.ASTNode, m *Mapping) (*treesitter.ASTNode, *treesitter.ASTNode)
- func SimpleRecovery(t1, t2 *treesitter.ASTNode, m *Mapping)
- func StructureIsomorphic(a, b *treesitter.ASTNode) bool
- func TopDown(t1Root, t2Root *treesitter.ASTNode, minHeight int, m *Mapping, ...)
- func TypesMatch(t1, t2 string, r *rules.Rules) bool
- func Unquote(s string) string
- type AffinityWeights
- type LinePartition
- type Mapping
- func (m *Mapping) Add(t1, t2 *treesitter.ASTNode)
- func (m *Mapping) DiceSrc(t1, t2 *treesitter.ASTNode) float64
- func (m *Mapping) Dst() map[*treesitter.ASTNode]*treesitter.ASTNode
- func (m *Mapping) Get(t1 *treesitter.ASTNode) *treesitter.ASTNode
- func (m *Mapping) Has(t1 *treesitter.ASTNode) bool
- func (m *Mapping) HasDst(t2 *treesitter.ASTNode) bool
- func (m *Mapping) Remove(t1 *treesitter.ASTNode)
- func (m *Mapping) Src() map[*treesitter.ASTNode]*treesitter.ASTNode
- type MappingPair
- type MatchResult
- type PairRole
Constants ¶
This section is empty.
Variables ¶
var DefaultAffinityWeights = AffinityWeights{
Sim: 0.30,
Dice: 0.25,
Scope: 0.25,
Pos: 0.10,
Label: 0.10,
KeyBonus: 0.35,
DepthCoeff: 0.20,
}
DefaultAffinityWeights is the default set of scoring weights used by candidate selection. Exported so callers can use it as a baseline for language-specific tuning.
Functions ¶
func AncestorNameSimilarity ¶
func AncestorNameSimilarity(t1, t2 *treesitter.ASTNode) int
AncestorNameSimilarity calculates the number of matching identifier labels among the ancestors of t1 and t2. This helps break ties in top-down matching by preferring pairs located in similarly named functions or classes.
func BottomUp ¶
func BottomUp( t1Root, t2Root *treesitter.ASTNode, m *Mapping, minDice float64, )
BottomUp pairs unmatched container nodes in post-order when they share matched children, running recovery on newly matched blocks.
func ChawatheSimilarity ¶
func ChawatheSimilarity(t1, t2 *treesitter.ASTNode, m map[*treesitter.ASTNode]*treesitter.ASTNode) float64
ChawatheSimilarity computes the Chawathe similarity coefficient between two subtrees given the current mapping set m (maps T1 nodes -> T2 nodes).
chawathe(t1, t2, m) = |{t ∈ s(t1) | (t, t2') ∈ m for some t2'}| / max(|s(t1)|, |s(t2)|)
func CommentSimilarity ¶ added in v0.4.0
CommentSimilarity returns the bigram Dice coefficient for two cleaned comment strings.
func CompatiblePairRoles ¶ added in v0.6.0
func CompatiblePairRoles(n1, n2 *treesitter.ASTNode) bool
CompatiblePairRoles reports whether n1 and n2 can match without crossing key/value boundaries.
func ContestContainers ¶ added in v0.5.0
func ContestContainers(t1Root, t2Root *treesitter.ASTNode, m *Mapping)
ContestContainers reassigns T2 containers that got greedily claimed by an inner T1 node during bottom-up matching back to their proper outer T1 parent.
Example: deleting an if-block can trick BottomUp into matching the if's inner block to the outer function body (if they share something like a throw). That leaves the real function body unmapped and generates a mess of spurious Move actions.
func Dice ¶
func Dice(t1, t2 *treesitter.ASTNode, m map[*treesitter.ASTNode]*treesitter.ASTNode) float64
Dice computes the Dice similarity coefficient between two subtrees given the current mapping set m (maps T1 nodes -> T2 nodes).
dice(t1, t2, m) = 2 × |{t ∈ s(t1) | (t, t2') ∈ m for some t2'}| / (|s(t1)| + |s(t2)|)
func FindEnclosingScope ¶ added in v0.8.0
func FindEnclosingScope(n *treesitter.ASTNode, r *rules.Rules) *treesitter.ASTNode
FindEnclosingScope climbs the parent hierarchy to find the nearest enclosing block, declaration, or wrapper.
func FprintMappings ¶
func FprintMappings(w io.Writer, r *MatchResult) error
func GetEnclosingDeclaration ¶ added in v0.6.0
func GetEnclosingDeclaration(n *treesitter.ASTNode) *treesitter.ASTNode
GetEnclosingDeclaration traverses up from n to find the nearest ancestor registered in rules.Declarations.
func HasLongLeafToken ¶ added in v0.5.0
func HasLongLeafToken(n *treesitter.ASTNode) bool
HasLongLeafToken reports whether a subtree contains any non-trivial leaf label with length > 3.
func Height ¶
func Height(n *treesitter.ASTNode) int
Height returns the height of a subtree rooted at n. A leaf has height 1.
func IsScopePreserved ¶ added in v0.8.0
IsScopePreserved reports whether n1 and n2 reside within corresponding (mapped) enclosing scopes.
func Isomorphic ¶
func Isomorphic(a, b *treesitter.ASTNode) bool
Isomorphic returns true if both subtrees match in type, label, and structure.
func LCS ¶ added in v0.4.0
func LCS(seq1, seq2 []*treesitter.ASTNode, eq eqFunc) [][2]*treesitter.ASTNode
LCS computes the longest common subsequence of two node sequences using a custom equality predicate.
func LCSLabel ¶
func LCSLabel(seq1, seq2 []*treesitter.ASTNode) [][2]*treesitter.ASTNode
LCSLabel matches node sequences by type, label, and structure.
func LCSStructure ¶
func LCSStructure(seq1, seq2 []*treesitter.ASTNode) [][2]*treesitter.ASTNode
LCSStructure matches node sequences by type and shape (ignoring leaf labels), using child position and label similarity to resolve ambiguous matches.
func LeafSimilarity ¶ added in v0.5.0
func LeafSimilarity(a, b *treesitter.ASTNode) float64
LeafSimilarity returns the Dice coefficient of non-trivial leaf labels between subtrees a and b.
func LineDiff ¶ added in v0.4.0
LineDiff matches corresponding line indices between linesA and linesB using trimmed LCS, sliding change boundaries to blank lines and braces via indent heuristics.
func MatchContainerKeywords ¶ added in v0.5.0
func MatchContainerKeywords(t1Root, t2Root *treesitter.ASTNode, m *Mapping)
MatchContainerKeywords maps unmapped keyword children between mapped parent containers.
func MatchDataContainers ¶ added in v0.8.0
func MatchDataContainers(t1Root, t2Root *treesitter.ASTNode, m *Mapping, r *rules.Rules)
MatchDataContainers aligns key-value pairs in dictionaries and markup trees. Matches identical keys first, then pairs up unmatched entries with unique identical values to detect key renames.
func MatchUnmatchedLeaves ¶
func MatchUnmatchedLeaves(t1Root, t2Root *treesitter.ASTNode, m *Mapping, part *LinePartition)
MatchUnmatchedLeaves pairs unmatched leaf nodes of the same type and label using parent Dice similarity and positional scores to break ties. Leaves under unmatched parents are allowed if they share a matched ancestor.
func NearestMatchedAncestor ¶
func NearestMatchedAncestor(n *treesitter.ASTNode, m *Mapping, isDst bool) *treesitter.ASTNode
NearestMatchedAncestor finds the closest ancestor of n that is present in the mapping. If isDst is true, it checks m.HasDst; otherwise it checks m.Has.
func Recover ¶ added in v0.4.0
func Recover(t1, t2 *treesitter.ASTNode, m *Mapping)
Recover aligns unmatched nodes inside a matched container pair. Uses Zhang-Shasha (1989) tree edit distance as a last-chance fallback on tiny subtrees (< 20 nodes), and uses SimpleRecovery (Sibling LCS) on larger subtrees.
func RollupMatchedContainers ¶ added in v0.5.0
func RollupMatchedContainers(t1Root, t2Root *treesitter.ASTNode, m *Mapping)
RollupMatchedContainers pairs unmatched container nodes in post-order when their mapped children predominantly belong to the same unmatched parent container in T2.
func RunZSRecovery ¶ added in v0.4.0
func RunZSRecovery(t1, t2 *treesitter.ASTNode, m *Mapping)
RunZSRecovery runs Zhang-Shasha (1989) tree edit distance on small subtrees t1 and t2 and adds optimal node mappings to m.
func SharedMatchedAncestor ¶ added in v0.5.0
func SharedMatchedAncestor(t1, t2 *treesitter.ASTNode, m *Mapping) (*treesitter.ASTNode, *treesitter.ASTNode)
SharedMatchedAncestor finds the nearest pair of ancestors (a1, a2) of t1 and t2 such that a1 in T1 is mapped to a2 in T2.
func SimpleRecovery ¶
func SimpleRecovery(t1, t2 *treesitter.ASTNode, m *Mapping)
SimpleRecovery maps unmatched children inside container pair (t1, t2) using positional anchors, label/structural LCS, and unique-type matching.
func StructureIsomorphic ¶
func StructureIsomorphic(a, b *treesitter.ASTNode) bool
StructureIsomorphic returns true if both subtrees match in shape and node types, ignoring leaf labels.
func TopDown ¶
func TopDown( t1Root, t2Root *treesitter.ASTNode, minHeight int, m *Mapping, part *LinePartition, )
TopDown matches largest isomorphic subtrees top-down by height.
func TypesMatch ¶ added in v0.6.0
TypesMatch checks if t1 and t2 are the same type, falling back to the language's equivalent_types rules when they're not.
Types ¶
type AffinityWeights ¶ added in v0.8.0
type AffinityWeights struct {
Sim float64 // Chawathe leaf similarity weight
Dice float64 // Subtree descendant Dice weight
Scope float64 // Scope proximity weight
Pos float64 // Sibling index alignment weight
Label float64 // Leaf label similarity weight
KeyBonus float64 // Exact key match bonus for pair nodes
DepthCoeff float64 // Quadratic penalty for relative depth divergence
}
AffinityWeights holds the scoring weights used to rank bottom-up candidates.
type LinePartition ¶ added in v0.4.0
type LinePartition struct {
// contains filtered or unexported fields
}
func NewLinePartition ¶ added in v0.4.0
func NewLinePartition(srcA, srcB []byte) *LinePartition
func (*LinePartition) CanMatch ¶ added in v0.4.0
func (p *LinePartition) CanMatch(n1, n2 *treesitter.ASTNode) bool
CanMatch checks if two nodes are allowed to pair up: - Untouched nodes (Group 1) only match untouched nodes on corresponding lines. - Edited nodes (Group 2) only match other edited nodes. - Cross-matching between untouched and edited nodes is forbidden.
func (*LinePartition) IsGroup1A ¶ added in v0.4.0
func (p *LinePartition) IsGroup1A(n1 *treesitter.ASTNode) (bool, int)
IsGroup1A returns true if all lines spanned by n1 in file A are non-edited lines.
func (*LinePartition) IsGroup1B ¶ added in v0.4.0
func (p *LinePartition) IsGroup1B(n2 *treesitter.ASTNode) (bool, int)
IsGroup1B returns true if all lines spanned by n2 in file B are non-edited lines.
type Mapping ¶
type Mapping struct {
Pairs []MappingPair
// contains filtered or unexported fields
}
Mapping is a 1-1 node mapping between T1 nodes and T2 nodes. Pairs preserves insertion order for deterministic iteration.
func (*Mapping) Add ¶
func (m *Mapping) Add(t1, t2 *treesitter.ASTNode)
Add registers the t1→t2 pair, enforcing strict 1:1 bijection. If t2 was already claimed by a different t1, that old mapping is evicted. If t1 was already mapped to a different t2, that old destination is replaced in-place to preserve insertion order.
func (*Mapping) DiceSrc ¶
func (m *Mapping) DiceSrc(t1, t2 *treesitter.ASTNode) float64
DiceSrc computes dice(t1, t2) using the internal src mapping.
func (*Mapping) Dst ¶
func (m *Mapping) Dst() map[*treesitter.ASTNode]*treesitter.ASTNode
Dst exposes the T2->T1 map.
func (*Mapping) Get ¶ added in v0.8.0
func (m *Mapping) Get(t1 *treesitter.ASTNode) *treesitter.ASTNode
Get returns the mapped destination node for t1, or nil if none exists.
func (*Mapping) Has ¶
func (m *Mapping) Has(t1 *treesitter.ASTNode) bool
Has reports whether t1 has been mapped to a destination node.
func (*Mapping) HasDst ¶
func (m *Mapping) HasDst(t2 *treesitter.ASTNode) bool
HasDst reports whether t2 has been claimed as a destination node.
func (*Mapping) Remove ¶
func (m *Mapping) Remove(t1 *treesitter.ASTNode)
Remove clears the mapping for t1 and any destination node it was paired with.
func (*Mapping) Src ¶
func (m *Mapping) Src() map[*treesitter.ASTNode]*treesitter.ASTNode
Src exposes the T1->T2 map for use in Dice calculations.
type MappingPair ¶
type MappingPair struct {
Src *treesitter.ASTNode
Dst *treesitter.ASTNode
}
MappingPair is a single src→dst node mapping, kept for deterministic ordering.
type MatchResult ¶
type MatchResult struct {
Mappings *Mapping
}
func Match ¶
func Match(t1, t2 *treesitter.ASTNode, srcA, srcB []byte, part *LinePartition) *MatchResult
type PairRole ¶ added in v0.6.0
type PairRole int
PairRole identifies whether a node acts as a key or a value inside a key-value pair.
func NodePairRole ¶ added in v0.6.0
func NodePairRole(n *treesitter.ASTNode) PairRole
NodePairRole checks if n sits on the key or value side of a pair.