Class: Ast::Merge::Text::WordNode

Inherits:
AstNode
  • Object
show all
Defined in:
lib/ast/merge/text/word_node.rb

Overview

Represents a word within a line of text.
Words are the nested level of the text-based AST.
They are identified by word boundaries (regex \b).

Inherits from AstNode (SyntheticNode) to implement the TreeHaver::Node
protocol, making it compatible with all tree_haver-based merge operations.

Examples:

word = WordNode.new("hello", line_number: 1, word_index: 0, start_col: 0, end_col: 5)
word.content    # => "hello"
word.signature  # => [:word, "hello"]
word.type       # => "word_node" (TreeHaver protocol)

Instance Attribute Summary collapse

Attributes inherited from AstNode

#location, #slice

Instance Method Summary collapse

Methods inherited from AstNode

#<=>, #child, #child_count, #children, #each, #end_byte, #end_point, #has_error?, #missing?, #named?, #source, #start_byte, #start_point, #structural?, #text, #unwrap

Constructor Details

#initialize(content, line_number:, word_index:, start_col:, end_col:) ⇒ WordNode

Initialize a new WordNode

Parameters:

  • content (String)

    The word content

  • line_number (Integer)

    1-based line number

  • word_index (Integer)

    0-based word index within line

  • start_col (Integer)

    0-based start column

  • end_col (Integer)

    0-based end column (exclusive)



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ast/merge/text/word_node.rb', line 32

def initialize(content, line_number:, word_index:, start_col:, end_col:)
  @content = content
  @word_index = word_index

  location = AstNode::Location.new(
    start_line: line_number,
    end_line: line_number,
    start_column: start_col,
    end_column: end_col,
  )

  super(slice: content, location: location)
end

Instance Attribute Details

#contentString (readonly)

Returns The word content.

Returns:

  • (String)

    The word content



20
21
22
# File 'lib/ast/merge/text/word_node.rb', line 20

def content
  @content
end

#word_indexInteger (readonly)

Returns 0-based index of this word within the line.

Returns:

  • (Integer)

    0-based index of this word within the line



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

def word_index
  @word_index
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Check equality with another WordNode

Parameters:

  • other (WordNode)

    Other node to compare

Returns:

  • (Boolean)

    True if content matches



88
89
90
# File 'lib/ast/merge/text/word_node.rb', line 88

def ==(other)
  other.is_a?(WordNode) && @content == other.content
end

#end_colInteger

Get end column (0-based, exclusive)

Returns:

  • (Integer)


80
81
82
# File 'lib/ast/merge/text/word_node.rb', line 80

def end_col
  location.end_column
end

#hashInteger

Hash code for use in Hash keys

Returns:

  • (Integer)

    Hash code



97
98
99
# File 'lib/ast/merge/text/word_node.rb', line 97

def hash
  @content.hash
end

#inspectString

String representation for debugging

Returns:

  • (String)

    Debug representation



104
105
106
# File 'lib/ast/merge/text/word_node.rb', line 104

def inspect
  "#<WordNode #{@content.inspect} line=#{line_number} col=#{start_col}..#{end_col}>"
end

#line_numberInteger

Get the 1-based line number

Returns:

  • (Integer)


68
69
70
# File 'lib/ast/merge/text/word_node.rb', line 68

def line_number
  location.start_line
end

#normalized_contentString

Get normalized content (the word itself for words)

Returns:

  • (String)


62
63
64
# File 'lib/ast/merge/text/word_node.rb', line 62

def normalized_content
  @content
end

#signatureArray

Generate a signature for this word node.
The signature is used for matching words across template/destination.

Returns:

  • (Array)

    Signature array [:word, content]



56
57
58
# File 'lib/ast/merge/text/word_node.rb', line 56

def signature
  [:word, @content]
end

#start_colInteger

Get start column (0-based)

Returns:

  • (Integer)


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

def start_col
  location.start_column
end

#to_sString

Convert to string (returns content)

Returns:

  • (String)

    Word content



111
112
113
# File 'lib/ast/merge/text/word_node.rb', line 111

def to_s
  @content
end

#typeString

TreeHaver::Node protocol: type

Returns:

  • (String)

    “word_node”



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

def type
  "word_node"
end