Class: Ast::Merge::Comment::Block
- 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.
Instance Attribute Summary collapse
-
#children ⇒ Array<Line, Empty>
readonly
The child nodes in this block (for line-based blocks).
-
#raw_content ⇒ String?
readonly
Raw content for block-style comments (e.g., /* … */).
-
#style ⇒ Style
readonly
The comment style configuration.
Attributes inherited from AstNode
Instance Method Summary collapse
-
#freeze_marker?(freeze_token) ⇒ Boolean
Check if this block contains a freeze marker.
-
#initialize(children: nil, raw_content: nil, start_line: nil, end_line: nil, style: nil) ⇒ Block
constructor
Initialize a new Block.
-
#inspect ⇒ String
Human-readable representation.
-
#normalized_content ⇒ String
Normalized combined content.
-
#signature ⇒ Array
Generate signature for matching.
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`.
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
#children ⇒ Array<Line, Empty> (readonly)
Returns 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_content ⇒ String? (readonly)
Returns 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 |
#style ⇒ Style (readonly)
Returns 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.
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 |
#inspect ⇒ String
Returns 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_content ⇒ String
Returns 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 |
#signature ⇒ Array
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.
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 |