Class: Ast::Merge::Comment::Block

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

Overview

Represents a contiguous block of comment content.

A comment block can represent:

  • A sequence of line comments not separated by blank lines

  • A C-style block comment (‘/* … */`)

  • An HTML comment block (‘<!– … –>`)

The block acts as a grouping mechanism for signature matching and merge operations.

Examples:

Line comment block (Ruby/Python style)

block = Block.new(children: [
  Line.new(text: "# First line", line_number: 1),
  Line.new(text: "# Second line", line_number: 2),
])
block.signature #=> [:comment_block, "first line"]

C-style block comment

block = Block.new(
  raw_content: "/* This is a\n   multi-line comment */",
  start_line: 1,
  end_line: 2,
  style: :c_style_block
)

Instance Attribute Summary collapse

Attributes inherited from AstNode

#location, #slice

Instance Method Summary collapse

Methods inherited from AstNode

#respond_to_missing?, #to_s, #unwrap

Constructor Details

#initialize(children: nil, raw_content: nil, start_line: nil, end_line: nil, style: nil) ⇒ Block

Initialize a new Block.

For line-based comments, pass ‘children` array. For block-style comments (/* … */), pass `raw_content`.

Parameters:

  • children (Array<Line, Empty>, nil) (defaults to: nil)

    Child nodes (for line comments)

  • raw_content (String, nil) (defaults to: nil)

    Raw block content (for block comments)

  • start_line (Integer, nil) (defaults to: nil)

    Start line (required for raw_content)

  • end_line (Integer, nil) (defaults to: nil)

    End line (required for raw_content)

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

    Comment style (default: :hash_comment)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ast/merge/comment/block.rb', line 54

def initialize(children: nil, raw_content: nil, start_line: nil, end_line: nil, style: nil)
  @style = resolve_style(style)
  @children = children || []
  @raw_content = raw_content

  if raw_content
    # Block-style comment (e.g., /* ... */)
    @start_line = start_line || 1
    @end_line = end_line || @start_line
    combined_slice = raw_content
  else
    # Line-based comment block
    first_child = @children.first
    last_child = @children.last
    @start_line = first_child&.location&.start_line || 1
    @end_line = last_child&.location&.end_line || @start_line
    combined_slice = @children.map(&:slice).join("\n")
  end

  location = AstNode::Location.new(
    start_line: @start_line,
    end_line: @end_line,
    start_column: 0,
    end_column: combined_slice.split("\n").last&.length || 0,
  )

  super(slice: combined_slice, location: location)
end

Instance Attribute Details

#childrenArray<Line, Empty> (readonly)

Returns The child nodes in this block (for line-based blocks).

Returns:

  • (Array<Line, Empty>)

    The child nodes in this block (for line-based blocks)



36
37
38
# File 'lib/ast/merge/comment/block.rb', line 36

def children
  @children
end

#raw_contentString? (readonly)

Returns Raw content for block-style comments (e.g., /* … */).

Returns:

  • (String, nil)

    Raw content for block-style comments (e.g., /* … */)



39
40
41
# File 'lib/ast/merge/comment/block.rb', line 39

def raw_content
  @raw_content
end

#styleStyle (readonly)

Returns The comment style configuration.

Returns:

  • (Style)

    The comment style configuration



42
43
44
# File 'lib/ast/merge/comment/block.rb', line 42

def style
  @style
end

Instance Method Details

#freeze_marker?(freeze_token) ⇒ Boolean

Check if this block contains a freeze marker.

Parameters:

  • freeze_token (String)

    The freeze token to look for

Returns:

  • (Boolean)

    true if any child contains a freeze marker



110
111
112
113
114
115
116
117
118
119
# File 'lib/ast/merge/comment/block.rb', line 110

def freeze_marker?(freeze_token)
  return false unless freeze_token

  if raw_content
    pattern = /#{Regexp.escape(freeze_token)}:(freeze|unfreeze)/i
    raw_content.match?(pattern)
  else
    children.any? { |c| c.respond_to?(:freeze_marker?) && c.freeze_marker?(freeze_token) }
  end
end

#inspectString

Returns Human-readable representation.

Returns:

  • (String)

    Human-readable representation



122
123
124
125
126
127
128
# File 'lib/ast/merge/comment/block.rb', line 122

def inspect
  if raw_content
    "#<Comment::Block lines=#{@start_line}..#{@end_line} style=#{style.name} block_comment>"
  else
    "#<Comment::Block lines=#{@start_line}..#{@end_line} style=#{style.name} children=#{children.size}>"
  end
end

#normalized_contentString

Returns Normalized combined content.

Returns:

  • (String)

    Normalized combined content



95
96
97
98
99
100
101
102
103
104
# File 'lib/ast/merge/comment/block.rb', line 95

def normalized_content
  if raw_content
    extract_block_content
  else
    children
      .select { |c| c.is_a?(Line) }
      .map { |c| c.content.strip }
      .join("\n")
  end
end

#signatureArray

Generate signature for matching.

For line-based blocks, uses the first non-empty line’s content. For block-style comments, uses the first meaningful line of content.

Returns:

  • (Array)

    Signature for matching



89
90
91
92
# File 'lib/ast/merge/comment/block.rb', line 89

def signature
  content = first_meaningful_content
  [:comment_block, content[0..120]] # Limit signature length
end