Class: Toml::Merge::CommentTracker

Inherits:
Ast::Merge::Comment::HashTrackerBase
  • Object
show all
Defined in:
lib/toml/merge/comment_tracker.rb

Overview

Tracks pre-extracted TOML comment entries and exposes shared comment helpers.

Inherits shared lookup, query, region-building, and attachment API from
+Ast::Merge::Comment::HashTrackerBase+. This tracker differs from most
siblings in that it receives pre-extracted comment entries from the TOML
parser rather than scanning source lines itself.

Format-specific overrides:

  • Initialization accepts pre-extracted entries + backend hint
  • Region building preserves internal blank lines between comment nodes
  • Inline comment detection is owner-aware (table headers vs key/value pairs)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, comment_entries, backend: :tree_sitter) ⇒ CommentTracker

Returns a new instance of CommentTracker.



19
20
21
22
23
# File 'lib/toml/merge/comment_tracker.rb', line 19

def initialize(lines, comment_entries, backend: :tree_sitter)
  @comment_entries = Array(comment_entries).map { |entry| normalize_entry(entry) }
  @backend = backend
  super(Array(lines))
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



17
18
19
# File 'lib/toml/merge/comment_tracker.rb', line 17

def backend
  @backend
end

#comment_entriesObject (readonly)

Returns the value of attribute comment_entries.



17
18
19
# File 'lib/toml/merge/comment_tracker.rb', line 17

def comment_entries
  @comment_entries
end

Instance Method Details

#comment_attachment_for(owner, line_num: nil, **options) ⇒ Object

Override: TOML inline comment detection is owner-aware — table headers
only match inline comments on their own line, while key/value pairs
match across their full line range.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/toml/merge/comment_tracker.rb', line 28

def comment_attachment_for(owner, line_num: nil, **options)
  owner_line = line_num || owner_start_line(owner)
  return Ast::Merge::Comment::Attachment.new(owner: owner, metadata: options) unless owner_line

  leading_region = build_toml_region(:leading, leading_comments_before(owner_line))
  inline_region = build_toml_region(:inline, owner_inline_comment_entries(owner, line_num: owner_line))

  Ast::Merge::Comment::Attachment.new(
    owner: owner,
    leading_region: leading_region,
    inline_region: inline_region,
    metadata: {
      source: native_comment_backend? ? :toml_native : :toml_source,
      line_num: owner_line,
    }.merge(options),
  )
end

#inline_comment_for(owner, line_num: nil) ⇒ Object



46
47
48
# File 'lib/toml/merge/comment_tracker.rb', line 46

def inline_comment_for(owner, line_num: nil)
  owner_inline_comment_entries(owner, line_num: line_num).first
end

#owner_inline_comment_entries(owner, line_num: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/toml/merge/comment_tracker.rb', line 50

def owner_inline_comment_entries(owner, line_num: nil)
  start_line = line_num || owner_start_line(owner)
  return [] unless start_line

  end_line = if owner_table_like?(owner)
    start_line
  else
    owner_end_line(owner) || start_line
  end

  @comment_entries.select do |entry|
    !entry[:full_line] && (start_line..end_line).cover?(entry[:line])
  end
end