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

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

Examples:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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



28
29
30
31
32
# File 'lib/ast/merge/text/line_node.rb', line 28

def initialize(content, line_number:)
  @content = content
  @line_number = line_number
  @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)



16
17
18
# File 'lib/ast/merge/text/line_node.rb', line 16

def content
  @content
end

#line_numberInteger (readonly)

Returns 1-based line number.

Returns:

  • (Integer)

    1-based line number



19
20
21
# File 'lib/ast/merge/text/line_node.rb', line 19

def line_number
  @line_number
end

#wordsArray<WordNode> (readonly)

Returns Words contained in this line.

Returns:

  • (Array<WordNode>)

    Words contained in this line



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

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



82
83
84
# File 'lib/ast/merge/text/line_node.rb', line 82

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



52
53
54
# File 'lib/ast/merge/text/line_node.rb', line 52

def blank?
  @content.strip.empty?
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



60
61
62
# File 'lib/ast/merge/text/line_node.rb', line 60

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)



74
75
76
# File 'lib/ast/merge/text/line_node.rb', line 74

def end_line
  @line_number
end

#hashInteger

Hash code for use in Hash keys

Returns:

  • (Integer)

    Hash code



91
92
93
# File 'lib/ast/merge/text/line_node.rb', line 91

def hash
  @content.hash
end

#inspectString

String representation for debugging

Returns:

  • (String)

    Debug representation



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

def inspect
  "#<LineNode line=#{@line_number} #{@content.inspect} words=#{@words.size}>"
end

#normalized_contentString

Get normalized content (trimmed whitespace for comparison)

Returns:

  • (String)

    Whitespace-trimmed content



45
46
47
# File 'lib/ast/merge/text/line_node.rb', line 45

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]



38
39
40
# File 'lib/ast/merge/text/line_node.rb', line 38

def signature
  [:line, normalized_content]
end

#start_lineInteger

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

Returns:

  • (Integer)

    1-based start line



67
68
69
# File 'lib/ast/merge/text/line_node.rb', line 67

def start_line
  @line_number
end

#to_sString

Convert to string (returns content)

Returns:

  • (String)

    Line content



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

def to_s
  @content
end