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.
Methods inherited from AstNode
#children, #respond_to_missing?, #to_s, #unwrap
Constructor Details
#initialize(text:, line_number:, style: nil) ⇒ Line
Initialize a new Line.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ast/merge/comment/line.rb', line 46 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.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ast/merge/comment/line.rb', line 90 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.
66 67 68 |
# File 'lib/ast/merge/comment/line.rb', line 66 def content @content ||= style.extract_line_content(text) end |
#freeze_marker?(freeze_token) ⇒ Boolean
Check if this comment contains a freeze marker.
105 106 107 108 109 110 |
# File 'lib/ast/merge/comment/line.rb', line 105 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.
113 114 115 |
# File 'lib/ast/merge/comment/line.rb', line 113 def inspect "#<Comment::Line line=#{line_number} style=#{style.name} #{text.inspect}>" end |
#normalized_content ⇒ String
Returns Normalized content for comparison.
79 80 81 |
# File 'lib/ast/merge/comment/line.rb', line 79 def normalized_content content.strip end |
#signature ⇒ Array
Generate signature for matching. Uses normalized content (without delimiter) for better matching across files.
74 75 76 |
# File 'lib/ast/merge/comment/line.rb', line 74 def signature [:comment_line, normalized_content.downcase] end |