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, #source

Instance Method Summary collapse

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

Parameters:

  • content (String)

    The line content (without trailing newline)

  • line_number (Integer)

    1-based line number



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

#contentString (readonly)

Returns The full line content (without trailing newline).

Returns:

  • (String)

    The full line content (without trailing newline)



23
24
25
# File 'lib/ast/merge/text/line_node.rb', line 23

def content
  @content
end

#wordsArray<WordNode> (readonly)

Returns Words contained in this line.

Returns:

  • (Array<WordNode>)

    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

Parameters:

  • other (LineNode)

    Other node to compare

Returns:

  • (Boolean)

    True if content matches exactly



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)

Returns:

  • (Boolean)

    True if line is blank



79
80
81
# File 'lib/ast/merge/text/line_node.rb', line 79

def blank?
  @content.strip.empty?
end

#childrenArray<WordNode>

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

Returns:



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.

Returns:

  • (Boolean)

    True if line appears to be a comment



87
88
89
# File 'lib/ast/merge/text/line_node.rb', line 87

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)



107
108
109
# File 'lib/ast/merge/text/line_node.rb', line 107

def end_line
  location.end_line
end

#hashInteger

Hash code for use in Hash keys

Returns:

  • (Integer)

    Hash code



124
125
126
# File 'lib/ast/merge/text/line_node.rb', line 124

def hash
  @content.hash
end

#inspectString

String representation for debugging

Returns:

  • (String)

    Debug representation



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_numberInteger

Get the 1-based line number

Returns:

  • (Integer)

    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_contentString

Get normalized content (trimmed whitespace for comparison)

Returns:

  • (String)

    Whitespace-trimmed content



72
73
74
# File 'lib/ast/merge/text/line_node.rb', line 72

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]



65
66
67
# File 'lib/ast/merge/text/line_node.rb', line 65

def signature
  [:line, normalized_content]
end

#start_lineInteger

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

Returns:

  • (Integer)

    1-based start line



100
101
102
# File 'lib/ast/merge/text/line_node.rb', line 100

def start_line
  location.start_line
end

#to_sString

Convert to string (returns content)

Returns:

  • (String)

    Line content



138
139
140
# File 'lib/ast/merge/text/line_node.rb', line 138

def to_s
  @content
end

#typeString

TreeHaver::Node protocol: type

Returns:

  • (String)

    “line_node”



50
51
52
# File 'lib/ast/merge/text/line_node.rb', line 50

def type
  "line_node"
end