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?(freeze_token) ⇒ Boolean
True if this block contains a freeze directive.
-
#freeze_action(freeze_token) ⇒ Symbol?
Check if this block contains a freeze marker.
-
#freeze_marker?(freeze_token) ⇒ Boolean
True if any child 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.
-
#type ⇒ String
TreeHaver::Node protocol: type.
-
#unfreeze?(freeze_token) ⇒ Boolean
True if this block contains an unfreeze directive.
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.
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
#children ⇒ Array<Line, Empty> (readonly)
Returns 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_content ⇒ String? (readonly)
Returns 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 |
#style ⇒ Style (readonly)
Returns 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.
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.
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.
134 135 136 |
# File 'lib/ast/merge/comment/block.rb', line 134 def freeze_marker?(freeze_token) !freeze_action(freeze_token).nil? end |
#inspect ⇒ String
Returns 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_content ⇒ String
Returns 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 |
#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.
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 |
#type ⇒ String
TreeHaver::Node protocol: type
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.
146 147 148 |
# File 'lib/ast/merge/comment/block.rb', line 146 def unfreeze?(freeze_token) freeze_action(freeze_token) == :unfreeze end |