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

#<=>, #child, #child_count, #each, #end_byte, #end_point, #has_error?, #missing?, #named?, #source, #start_byte, #start_point, #structural?, #text, #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)



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
82
83
84
# File 'lib/ast/merge/comment/block.rb', line 57

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)



33
34
35
# File 'lib/ast/merge/comment/block.rb', line 33

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., /* ... */)



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

def raw_content
  @raw_content
end

#styleStyle (readonly)

Returns The comment style configuration.

Returns:

  • (Style)

    The comment style configuration



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

def style
  @style
end

Instance Method Details

#freeze?(freeze_token) ⇒ Boolean

Returns true if this block contains a freeze directive.

Parameters:

  • freeze_token (String)

    The freeze token to look for

Returns:

  • (Boolean)

    true if this block contains a freeze directive



140
141
142
# File 'lib/ast/merge/comment/block.rb', line 140

def freeze?(freeze_token)
  freeze_action(freeze_token) == :freeze
end

#freeze_action(freeze_token) ⇒ Symbol?

Check if this block contains a freeze marker.

Parameters:

  • freeze_token (String)

    The freeze token to look for

Returns:

  • (Symbol, nil)

    :freeze, :unfreeze, or nil



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ast/merge/comment/block.rb', line 113

def freeze_action(freeze_token)
  return unless freeze_token

  if raw_content
    pattern = /#{Regexp.escape(freeze_token)}:(freeze|unfreeze)/i
    match = raw_content.match(pattern)
    return match[1]&.downcase&.to_sym if match
  end

  children.each do |child|
    next unless child.respond_to?(:freeze_action)

    action = child.freeze_action(freeze_token)
    return action if action
  end

  nil
end

#freeze_marker?(freeze_token) ⇒ Boolean

Returns true if any child contains a freeze marker.

Parameters:

  • freeze_token (String)

    The freeze token to look for

Returns:

  • (Boolean)

    true if any child contains a freeze marker



134
135
136
# File 'lib/ast/merge/comment/block.rb', line 134

def freeze_marker?(freeze_token)
  !freeze_action(freeze_token).nil?
end

#inspectString

Returns Human-readable representation.

Returns:

  • (String)

    Human-readable representation



151
152
153
154
155
156
157
# File 'lib/ast/merge/comment/block.rb', line 151

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



98
99
100
101
102
103
104
105
106
107
# File 'lib/ast/merge/comment/block.rb', line 98

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



92
93
94
95
# File 'lib/ast/merge/comment/block.rb', line 92

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

#typeString

TreeHaver::Node protocol: type

Returns:

  • (String)

    "comment_block"



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

def type
  'comment_block'
end

#unfreeze?(freeze_token) ⇒ Boolean

Returns true if this block contains an unfreeze directive.

Parameters:

  • freeze_token (String)

    The freeze token to look for

Returns:

  • (Boolean)

    true if this block contains an unfreeze directive



146
147
148
# File 'lib/ast/merge/comment/block.rb', line 146

def unfreeze?(freeze_token)
  freeze_action(freeze_token) == :unfreeze
end