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?, #missing?, #named?, #source, #start_byte, #start_point, #structural?, #to_s, #unwrap
Constructor Details
#initialize(text:, line_number:, style: nil) ⇒ Line
Initialize a new Line.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ast/merge/comment/line.rb', line 49 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.
33 34 35 |
# File 'lib/ast/merge/comment/line.rb', line 33 def line_number @line_number end |
#style ⇒ Style (readonly)
Returns The comment style configuration.
36 37 38 |
# File 'lib/ast/merge/comment/line.rb', line 36 def style @style end |
#text ⇒ String (readonly)
Returns The raw text of the comment line.
30 31 32 |
# File 'lib/ast/merge/comment/line.rb', line 30 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.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ast/merge/comment/line.rb', line 93 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.
69 70 71 |
# File 'lib/ast/merge/comment/line.rb', line 69 def content @content ||= style.extract_line_content(text) end |
#freeze_marker?(freeze_token) ⇒ Boolean
Check if this comment contains a freeze marker.
108 109 110 111 112 113 |
# File 'lib/ast/merge/comment/line.rb', line 108 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.
116 117 118 |
# File 'lib/ast/merge/comment/line.rb', line 116 def inspect "#<Comment::Line line=#{line_number} style=#{style.name} #{text.inspect}>" end |
#normalized_content ⇒ String
Returns Normalized content for comparison.
82 83 84 |
# File 'lib/ast/merge/comment/line.rb', line 82 def normalized_content content.strip end |
#signature ⇒ Array
Generate signature for matching.
Uses normalized content (without delimiter) for better matching across files.
77 78 79 |
# File 'lib/ast/merge/comment/line.rb', line 77 def signature [:comment_line, normalized_content.downcase] end |
#type ⇒ String
TreeHaver::Node protocol: type
40 41 42 |
# File 'lib/ast/merge/comment/line.rb', line 40 def type "comment_line" end |