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?, #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
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
#content ⇒ String (readonly)
Returns The word content.
22 23 24 |
# File 'lib/ast/merge/text/word_node.rb', line 22 def content @content end |
#word_index ⇒ Integer (readonly)
Returns 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
90 91 92 |
# File 'lib/ast/merge/text/word_node.rb', line 90 def ==(other) other.is_a?(WordNode) && @content == other.content end |
#end_col ⇒ Integer
Get end column (0-based, exclusive)
82 83 84 |
# File 'lib/ast/merge/text/word_node.rb', line 82 def end_col location.end_column end |
#hash ⇒ Integer
Hash code for use in Hash keys
99 100 101 |
# File 'lib/ast/merge/text/word_node.rb', line 99 def hash @content.hash end |
#inspect ⇒ String
String representation for debugging
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_number ⇒ Integer
Get the 1-based line number
70 71 72 |
# File 'lib/ast/merge/text/word_node.rb', line 70 def line_number location.start_line end |
#normalized_content ⇒ String
Get normalized content (the word itself for words)
64 65 66 |
# File 'lib/ast/merge/text/word_node.rb', line 64 def normalized_content @content end |
#signature ⇒ Array
Generate a signature for this word node. The signature is used for matching words across template/destination.
58 59 60 |
# File 'lib/ast/merge/text/word_node.rb', line 58 def signature [:word, @content] end |
#start_col ⇒ Integer
Get start column (0-based)
76 77 78 |
# File 'lib/ast/merge/text/word_node.rb', line 76 def start_col location.start_column end |
#to_s ⇒ String
Convert to string (returns content)
113 114 115 |
# File 'lib/ast/merge/text/word_node.rb', line 113 def to_s @content end |
#type ⇒ String
TreeHaver::Node protocol: type
50 51 52 |
# File 'lib/ast/merge/text/word_node.rb', line 50 def type "word_node" end |