Class: Ast::Merge::Text::LineNode

Inherits:
AstNode
  • Object
show all
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.

Examples:

line = LineNode.new("Hello world!", line_number: 1)
line.content       # => "Hello world!"
line.words.size    # => 2
line.signature     # => [:line, "Hello world!"]
line.type          # => "line_node" (TreeHaver protocol)
line.text          # => "Hello world!" (TreeHaver protocol)

Instance Attribute Summary collapse

Attributes inherited from AstNode

#location, #slice

Instance Method Summary collapse

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

Parameters:

  • content (String)

    The line content (without trailing newline)

  • line_number (Integer)

    1-based line number



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

#contentString (readonly)

Returns The full line content (without trailing newline).

Returns:

  • (String)

    The full line content (without trailing newline)



21
22
23
# File 'lib/ast/merge/text/line_node.rb', line 21

def content
  @content
end

#wordsArray<WordNode> (readonly)

Returns Words contained in this line.

Returns:

  • (Array<WordNode>)

    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

Parameters:

  • other (LineNode)

    Other node to compare

Returns:

  • (Boolean)

    True if content matches exactly



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)

Returns:

  • (Boolean)

    True if line is blank



77
78
79
# File 'lib/ast/merge/text/line_node.rb', line 77

def blank?
  @content.strip.empty?
end

#childrenArray<WordNode>

TreeHaver::Node protocol: children
Returns word nodes as children

Returns:



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.

Returns:

  • (Boolean)

    True if line appears to be a comment



85
86
87
# File 'lib/ast/merge/text/line_node.rb', line 85

def comment?
  @content.strip.start_with?("#")
end

#end_lineInteger

Get the ending line (for compatibility with AST node interface)

Returns:

  • (Integer)

    1-based end line (same as start for single line)



105
106
107
# File 'lib/ast/merge/text/line_node.rb', line 105

def end_line
  location.end_line
end

#hashInteger

Hash code for use in Hash keys

Returns:

  • (Integer)

    Hash code



122
123
124
# File 'lib/ast/merge/text/line_node.rb', line 122

def hash
  @content.hash
end

#inspectString

String representation for debugging

Returns:

  • (String)

    Debug representation



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_numberInteger

Get the 1-based line number

Returns:

  • (Integer)

    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_contentString

Get normalized content (trimmed whitespace for comparison)

Returns:

  • (String)

    Whitespace-trimmed content



70
71
72
# File 'lib/ast/merge/text/line_node.rb', line 70

def normalized_content
  @content.strip
end

#signatureArray

Generate a signature for this line node.
The signature is used for matching lines across template/destination.

Returns:

  • (Array)

    Signature array [:line, normalized_content]



63
64
65
# File 'lib/ast/merge/text/line_node.rb', line 63

def signature
  [:line, normalized_content]
end

#start_lineInteger

Get the starting line (for compatibility with AST node interface)

Returns:

  • (Integer)

    1-based start line



98
99
100
# File 'lib/ast/merge/text/line_node.rb', line 98

def start_line
  location.start_line
end

#to_sString

Convert to string (returns content)

Returns:

  • (String)

    Line content



136
137
138
# File 'lib/ast/merge/text/line_node.rb', line 136

def to_s
  @content
end

#typeString

TreeHaver::Node protocol: type

Returns:

  • (String)

    “line_node”



48
49
50
# File 'lib/ast/merge/text/line_node.rb', line 48

def type
  "line_node"
end