Class: Bash::Merge::CommentTracker

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

Overview

Extracts and tracks comments with their line numbers from Bash source. Bash comments use the # syntax, making freeze block detection straightforward.

Inherits shared lookup, query, region-building, and attachment API from Ast::Merge::Comment::HashTrackerBase. Only format-specific comment extraction, shebang detection, and owner resolution are overridden here.

Examples:

Basic usage

tracker = CommentTracker.new(bash_source)
tracker.comments # => [{line: 1, indent: 0, text: "This is a comment"}]
tracker.comment_at(1) # => {line: 1, indent: 0, text: "This is a comment"}

Comment types

# Full-line comment
command # Inline comment

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ CommentTracker

Initialize comment tracker by scanning the source

Parameters:

  • source (String)

    Bash source code



24
25
26
27
28
# File 'lib/bash/merge/comment_tracker.rb', line 24

def initialize(source)
  @source = source
  @line_parser = Ast::Merge::Comment::QuotedHashLineParser.new
  super(source.lines.map(&:chomp))
end

Instance Method Details

#augment(owners: [], **options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bash/merge/comment_tracker.rb', line 40

def augment(owners: [], **options)
  Ast::Merge::Comment::Augmenter.new(
    lines: @lines,
    comments: @comments,
    owners: owners,
    style: :hash_comment,
    total_comment_count: @comments.size,
    inline_comment_count: @comments.count { |comment| !comment[:full_line] },
    **options
  )
end

#shebang?(line_num) ⇒ Boolean

Check if a line is a shebang

Parameters:

  • line_num (Integer)

    1-based line number

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/bash/merge/comment_tracker.rb', line 34

def shebang?(line_num)
  return false if line_num < 1 || line_num > @lines.length

  @lines[line_num - 1].start_with?('#!')
end