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?, #inner_node, #missing?, #named?, #start_byte, #start_point, #structural?, #text, #unwrap
Constructor Details
#initialize(content, line_number:) ⇒ LineNode
Initialize a new LineNode
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ast/merge/text/line_node.rb', line 32 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).
23 24 25 |
# File 'lib/ast/merge/text/line_node.rb', line 23 def content @content end |
#words ⇒ Array<WordNode> (readonly)
Returns Words contained in this line.
26 27 28 |
# File 'lib/ast/merge/text/line_node.rb', line 26 def words @words end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check equality with another LineNode
115 116 117 |
# File 'lib/ast/merge/text/line_node.rb', line 115 def ==(other) other.is_a?(LineNode) && @content == other.content end |
#blank? ⇒ Boolean
Check if this line is blank (empty or whitespace only)
79 80 81 |
# File 'lib/ast/merge/text/line_node.rb', line 79 def blank? @content.strip.empty? end |
#children ⇒ Array<WordNode>
TreeHaver::Node protocol: children Returns word nodes as children
57 58 59 |
# File 'lib/ast/merge/text/line_node.rb', line 57 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.
87 88 89 |
# File 'lib/ast/merge/text/line_node.rb', line 87 def comment? @content.strip.start_with?("#") end |
#end_line ⇒ Integer
Get the ending line (for compatibility with AST node interface)
107 108 109 |
# File 'lib/ast/merge/text/line_node.rb', line 107 def end_line location.end_line end |
#hash ⇒ Integer
Hash code for use in Hash keys
124 125 126 |
# File 'lib/ast/merge/text/line_node.rb', line 124 def hash @content.hash end |
#inspect ⇒ String
String representation for debugging
131 132 133 |
# File 'lib/ast/merge/text/line_node.rb', line 131 def inspect "#<LineNode line=#{line_number} #{@content.inspect} words=#{@words.size}>" end |
#line_number ⇒ Integer
Get the 1-based line number
93 94 95 |
# File 'lib/ast/merge/text/line_node.rb', line 93 def line_number location.start_line end |
#normalized_content ⇒ String
Get normalized content (trimmed whitespace for comparison)
72 73 74 |
# File 'lib/ast/merge/text/line_node.rb', line 72 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.
65 66 67 |
# File 'lib/ast/merge/text/line_node.rb', line 65 def signature [:line, normalized_content] end |
#start_line ⇒ Integer
Get the starting line (for compatibility with AST node interface)
100 101 102 |
# File 'lib/ast/merge/text/line_node.rb', line 100 def start_line location.start_line end |
#to_s ⇒ String
Convert to string (returns content)
138 139 140 |
# File 'lib/ast/merge/text/line_node.rb', line 138 def to_s @content end |
#type ⇒ String
TreeHaver::Node protocol: type
50 51 52 |
# File 'lib/ast/merge/text/line_node.rb', line 50 def type "line_node" end |