Class: Ast::Merge::Comment::Line
- Defined in:
- lib/ast/merge/comment/line.rb
Overview
Represents a single comment line in source code.
A comment line is a line that starts with a comment delimiter (e.g., ‘#` in Ruby, `//` in JavaScript, `<!–` in HTML). The style determines how the comment is parsed and normalized.
Instance Attribute Summary collapse
-
#line_number ⇒ Integer
readonly
The line number in source.
-
#style ⇒ Style
readonly
The comment style configuration.
-
#text ⇒ String
readonly
The raw text of the comment line.
Attributes inherited from AstNode
Instance Method Summary collapse
-
#contains_token?(token, action: nil) ⇒ Boolean
Check if this comment contains a specific token pattern.
-
#content ⇒ String
Extract the comment content without the delimiter.
-
#freeze_marker?(freeze_token) ⇒ Boolean
Check if this comment contains a freeze marker.
-
#initialize(text:, line_number:, style: nil) ⇒ Line
constructor
Initialize a new Line.
-
#inspect ⇒ String
Human-readable representation.
-
#normalized_content ⇒ String
Normalized content for comparison.
-
#signature ⇒ Array
Generate signature for matching.
-
#type ⇒ String
TreeHaver::Node protocol: type.
Methods inherited from AstNode
#<=>, #child, #child_count, #children, #each, #end_byte, #end_point, #has_error?, #inner_node, #missing?, #named?, #start_byte, #start_point, #structural?, #to_s, #unwrap
Constructor Details
#initialize(text:, line_number:, style: nil) ⇒ Line
Initialize a new Line.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ast/merge/comment/line.rb', line 52 def initialize(text:, line_number:, style: nil) @text = text.to_s @line_number = line_number @style = resolve_style(style) location = AstNode::Location.new( start_line: line_number, end_line: line_number, start_column: 0, end_column: @text.length, ) super(slice: @text, location: location) end |
Instance Attribute Details
#line_number ⇒ Integer (readonly)
Returns The line number in source.
36 37 38 |
# File 'lib/ast/merge/comment/line.rb', line 36 def line_number @line_number end |
#style ⇒ Style (readonly)
Returns The comment style configuration.
39 40 41 |
# File 'lib/ast/merge/comment/line.rb', line 39 def style @style end |
#text ⇒ String (readonly)
Returns The raw text of the comment line.
33 34 35 |
# File 'lib/ast/merge/comment/line.rb', line 33 def text @text end |
Instance Method Details
#contains_token?(token, action: nil) ⇒ Boolean
Check if this comment contains a specific token pattern.
Useful for detecting freeze markers or other special directives.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ast/merge/comment/line.rb', line 96 def contains_token?(token, action: nil) return false unless token pattern = if action /#{Regexp.escape(token)}:#{action}/i else /#{Regexp.escape(token)}/i end text.match?(pattern) end |
#content ⇒ String
Extract the comment content without the delimiter.
Uses the style configuration to properly strip delimiters.
72 73 74 |
# File 'lib/ast/merge/comment/line.rb', line 72 def content @content ||= style.extract_line_content(text) end |
#freeze_marker?(freeze_token) ⇒ Boolean
Check if this comment contains a freeze marker.
111 112 113 114 115 116 |
# File 'lib/ast/merge/comment/line.rb', line 111 def freeze_marker?(freeze_token) return false unless freeze_token pattern = /#{Regexp.escape(freeze_token)}:(freeze|unfreeze)/i text.match?(pattern) end |
#inspect ⇒ String
Returns Human-readable representation.
119 120 121 |
# File 'lib/ast/merge/comment/line.rb', line 119 def inspect "#<Comment::Line line=#{line_number} style=#{style.name} #{text.inspect}>" end |
#normalized_content ⇒ String
Returns Normalized content for comparison.
85 86 87 |
# File 'lib/ast/merge/comment/line.rb', line 85 def normalized_content content.strip end |
#signature ⇒ Array
Generate signature for matching. Uses normalized content (without delimiter) for better matching across files.
80 81 82 |
# File 'lib/ast/merge/comment/line.rb', line 80 def signature [:comment_line, normalized_content.downcase] end |
#type ⇒ String
TreeHaver::Node protocol: type
43 44 45 |
# File 'lib/ast/merge/comment/line.rb', line 43 def type "comment_line" end |