Class: Ast::Merge::Comment::TrackedHashAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/comment/tracked_hash_adapter.rb

Overview

Converts producer-specific tracked comment hashes into standardized Ast::Merge::Comment nodes and regions.

This adapter is intentionally conservative. It currently targets the line-comment hash shape used by source trackers such as psych-merge's CommentTracker:

{
line: 12,
indent: 2,
text: "Example comment",
full_line: true,
raw: "  # Example comment"
}

The adapter does not infer ownership by itself. Callers provide region kinds explicitly and can preserve original producer facts via metadata.

Class Method Summary collapse

Class Method Details

.node(comment_hash = nil, style: nil, **options) ⇒ Comment::Line

Convert a tracked comment hash into a normalized shared comment node.

Parameters:

  • comment_hash (Hash, nil) (defaults to: nil)

    tracked hash in adapter shape

  • style (Comment::Style, Symbol, nil) (defaults to: nil)

    comment style descriptor

  • options (Hash)

    fallback hash values when comment_hash is nil

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ast/merge/comment/tracked_hash_adapter.rb', line 31

def node(comment_hash = nil, style: nil, **options)
  hash = normalize_hash(comment_hash || options)
  style = resolve_style(style)
  validate_style!(style)
  validate_hash!(hash)

  Comment::Line.new(
    text: line_text_from(hash, style),
    line_number: hash.fetch(:line),
    style: style
  )
end

.region(kind:, comments:, style: nil, metadata: {}, **options) ⇒ Region

Convert tracked comment hashes into a normalized shared comment region.

Parameters:

  • kind (Symbol)

    normalized region kind

  • comments (Array<Hash>)

    tracked comment hashes

  • style (Comment::Style, Symbol, nil) (defaults to: nil)

    comment style descriptor

  • metadata (Hash) (defaults to: {})

    base metadata for the region

  • options (Hash)

    additional metadata merged into metadata

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ast/merge/comment/tracked_hash_adapter.rb', line 52

def region(kind:, comments:, style: nil, metadata: {}, **options)
  normalized = Array(comments).map { |comment_hash| normalize_hash(comment_hash) }
  nodes = normalized.map { |comment_hash| node(comment_hash, style: style) }

  Region.new(
    kind: kind,
    nodes: nodes,
    metadata: .merge(
      source: :tracked_hash,
      tracked_hashes: normalized
    ).merge(options)
  )
end