Class: Ast::Merge::Text::LineNode
- Defined in:
- lib/ast/merge/text/line_node.rb
Overview
Represents a line of text in the text-based AST.
Lines are top-level nodes, with words as nested children.
Inherits from AstNode (SyntheticNode) to implement the TreeHaver::Node
protocol, making it compatible with all tree_haver-based merge operations.
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The full line content (without trailing newline).
-
#words ⇒ Array<WordNode>
readonly
Words contained in this line.
Attributes inherited from AstNode
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality with another LineNode.
-
#blank? ⇒ Boolean
Check if this line is blank (empty or whitespace only).
-
#children ⇒ Array<WordNode>
TreeHaver::Node protocol: children Returns word nodes as children.
-
#comment? ⇒ Boolean
Check if this line is a comment (starts with # after whitespace) This is a simple heuristic for text files.
-
#end_line ⇒ Integer
Get the ending line (for compatibility with AST node interface).
-
#hash ⇒ Integer
Hash code for use in Hash keys.
-
#initialize(content, line_number:) ⇒ LineNode
constructor
Initialize a new LineNode.
-
#inspect ⇒ String
String representation for debugging.
-
#line_number ⇒ Integer
Get the 1-based line number.
-
#normalized_content ⇒ String
Get normalized content (trimmed whitespace for comparison).
-
#signature ⇒ Array
Generate a signature for this line node.
-
#start_line ⇒ Integer
Get the starting line (for compatibility with AST node interface).
-
#to_s ⇒ String
Convert to string (returns content).
-
#type ⇒ String
TreeHaver::Node protocol: type.
Methods inherited from AstNode
#<=>, #child, #child_count, #each, #end_byte, #end_point, #has_error?, #missing?, #named?, #source, #start_byte, #start_point, #structural?, #text, #unwrap
Constructor Details
#initialize(content, line_number:) ⇒ LineNode
Initialize a new LineNode
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ast/merge/text/line_node.rb', line 30 def initialize(content, line_number:) @content = content location = AstNode::Location.new( start_line: line_number, end_line: line_number, start_column: 0, end_column: content.length, ) super(slice: content, location: location) # Parse words AFTER super sets up location @words = parse_words end |
Instance Attribute Details
#content ⇒ String (readonly)
Returns The full line content (without trailing newline).
21 22 23 |
# File 'lib/ast/merge/text/line_node.rb', line 21 def content @content end |
#words ⇒ Array<WordNode> (readonly)
Returns Words contained in this line.
24 25 26 |
# File 'lib/ast/merge/text/line_node.rb', line 24 def words @words end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check equality with another LineNode
113 114 115 |
# File 'lib/ast/merge/text/line_node.rb', line 113 def ==(other) other.is_a?(LineNode) && @content == other.content end |
#blank? ⇒ Boolean
Check if this line is blank (empty or whitespace only)
77 78 79 |
# File 'lib/ast/merge/text/line_node.rb', line 77 def blank? @content.strip.empty? end |
#children ⇒ Array<WordNode>
TreeHaver::Node protocol: children
Returns word nodes as children
55 56 57 |
# File 'lib/ast/merge/text/line_node.rb', line 55 def children @words end |
#comment? ⇒ Boolean
Check if this line is a comment (starts with # after whitespace)
This is a simple heuristic for text files.
85 86 87 |
# File 'lib/ast/merge/text/line_node.rb', line 85 def comment? @content.strip.start_with?("#") end |
#end_line ⇒ Integer
Get the ending line (for compatibility with AST node interface)
105 106 107 |
# File 'lib/ast/merge/text/line_node.rb', line 105 def end_line location.end_line end |
#hash ⇒ Integer
Hash code for use in Hash keys
122 123 124 |
# File 'lib/ast/merge/text/line_node.rb', line 122 def hash @content.hash end |
#inspect ⇒ String
String representation for debugging
129 130 131 |
# File 'lib/ast/merge/text/line_node.rb', line 129 def inspect "#<LineNode line=#{line_number} #{@content.inspect} words=#{@words.size}>" end |
#line_number ⇒ Integer
Get the 1-based line number
91 92 93 |
# File 'lib/ast/merge/text/line_node.rb', line 91 def line_number location.start_line end |
#normalized_content ⇒ String
Get normalized content (trimmed whitespace for comparison)
70 71 72 |
# File 'lib/ast/merge/text/line_node.rb', line 70 def normalized_content @content.strip end |
#signature ⇒ Array
Generate a signature for this line node.
The signature is used for matching lines across template/destination.
63 64 65 |
# File 'lib/ast/merge/text/line_node.rb', line 63 def signature [:line, normalized_content] end |
#start_line ⇒ Integer
Get the starting line (for compatibility with AST node interface)
98 99 100 |
# File 'lib/ast/merge/text/line_node.rb', line 98 def start_line location.start_line end |
#to_s ⇒ String
Convert to string (returns content)
136 137 138 |
# File 'lib/ast/merge/text/line_node.rb', line 136 def to_s @content end |
#type ⇒ String
TreeHaver::Node protocol: type
48 49 50 |
# File 'lib/ast/merge/text/line_node.rb', line 48 def type "line_node" end |