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

Instance Method Summary collapse

Methods inherited from AstNode

#<=>, #child, #child_count, #children, #each, #end_byte, #end_point, #has_error?, #inner_node, #missing?, #named?, #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)



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

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



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

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



25
26
27
# File 'lib/ast/merge/text/word_node.rb', line 25

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



90
91
92
# File 'lib/ast/merge/text/word_node.rb', line 90

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

#end_colInteger

Get end column (0-based, exclusive)

Returns:

  • (Integer)


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

def end_col
  location.end_column
end

#hashInteger

Hash code for use in Hash keys

Returns:

  • (Integer)

    Hash code



99
100
101
# File 'lib/ast/merge/text/word_node.rb', line 99

def hash
  @content.hash
end

#inspectString

String representation for debugging

Returns:

  • (String)

    Debug representation



106
107
108
# File 'lib/ast/merge/text/word_node.rb', line 106

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

#line_numberInteger

Get the 1-based line number

Returns:

  • (Integer)


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

def line_number
  location.start_line
end

#normalized_contentString

Get normalized content (the word itself for words)

Returns:

  • (String)


64
65
66
# File 'lib/ast/merge/text/word_node.rb', line 64

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]



58
59
60
# File 'lib/ast/merge/text/word_node.rb', line 58

def signature
  [:word, @content]
end

#start_colInteger

Get start column (0-based)

Returns:

  • (Integer)


76
77
78
# File 'lib/ast/merge/text/word_node.rb', line 76

def start_col
  location.start_column
end

#to_sString

Convert to string (returns content)

Returns:

  • (String)

    Word content



113
114
115
# File 'lib/ast/merge/text/word_node.rb', line 113

def to_s
  @content
end

#typeString

TreeHaver::Node protocol: type

Returns:

  • (String)

    “word_node”



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

def type
  "word_node"
end