Class: Ast::Merge::Text::WordNode
- 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.
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The word content.
-
#word_index ⇒ Integer
readonly
0-based index of this word within the line.
Attributes inherited from AstNode
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality with another WordNode.
-
#end_col ⇒ Integer
Get end column (0-based, exclusive).
-
#hash ⇒ Integer
Hash code for use in Hash keys.
-
#initialize(content, line_number:, word_index:, start_col:, end_col:) ⇒ WordNode
constructor
Initialize a new WordNode.
-
#inspect ⇒ String
String representation for debugging.
-
#line_number ⇒ Integer
Get the 1-based line number.
-
#normalized_content ⇒ String
Get normalized content (the word itself for words).
-
#signature ⇒ Array
Generate a signature for this word node.
-
#start_col ⇒ Integer
Get start column (0-based).
-
#to_s ⇒ String
Convert to string (returns content).
-
#type ⇒ String
TreeHaver::Node protocol: type.
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
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
#content ⇒ String (readonly)
Returns The word content.
20 21 22 |
# File 'lib/ast/merge/text/word_node.rb', line 20 def content @content end |
#word_index ⇒ Integer (readonly)
Returns 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
88 89 90 |
# File 'lib/ast/merge/text/word_node.rb', line 88 def ==(other) other.is_a?(WordNode) && @content == other.content end |
#end_col ⇒ Integer
Get end column (0-based, exclusive)
80 81 82 |
# File 'lib/ast/merge/text/word_node.rb', line 80 def end_col location.end_column end |
#hash ⇒ Integer
Hash code for use in Hash keys
97 98 99 |
# File 'lib/ast/merge/text/word_node.rb', line 97 def hash @content.hash end |
#inspect ⇒ String
String representation for debugging
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_number ⇒ Integer
Get the 1-based line number
68 69 70 |
# File 'lib/ast/merge/text/word_node.rb', line 68 def line_number location.start_line end |
#normalized_content ⇒ String
Get normalized content (the word itself for words)
62 63 64 |
# File 'lib/ast/merge/text/word_node.rb', line 62 def normalized_content @content end |
#signature ⇒ Array
Generate a signature for this word node.
The signature is used for matching words across template/destination.
56 57 58 |
# File 'lib/ast/merge/text/word_node.rb', line 56 def signature [:word, @content] end |
#start_col ⇒ Integer
Get start column (0-based)
74 75 76 |
# File 'lib/ast/merge/text/word_node.rb', line 74 def start_col location.start_column end |
#to_s ⇒ String
Convert to string (returns content)
111 112 113 |
# File 'lib/ast/merge/text/word_node.rb', line 111 def to_s @content end |
#type ⇒ String
TreeHaver::Node protocol: type
48 49 50 |
# File 'lib/ast/merge/text/word_node.rb', line 48 def type "word_node" end |