Class: TreeHaver::Backends::Java::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_haver/backends/java.rb

Overview

Wrapper for java-tree-sitter Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(impl) ⇒ Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Node.



618
619
620
# File 'lib/tree_haver/backends/java.rb', line 618

def initialize(impl)
  @impl = impl
end

Instance Attribute Details

#implObject (readonly)

Returns the value of attribute impl.



615
616
617
# File 'lib/tree_haver/backends/java.rb', line 615

def impl
  @impl
end

Instance Method Details

#child(index) ⇒ Node?

Get a child by index

Parameters:

  • index (Integer)

    the child index

Returns:

  • (Node, nil)

    the child node or nil if index out of bounds



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/tree_haver/backends/java.rb', line 640

def child(index)
  # jtreesitter 0.26.0: getChild returns Optional<Node> or throws IndexOutOfBoundsException
  result = @impl.getChild(index)
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    # Direct Node return (some jtreesitter versions)
    java_node = result
  end

  Node.new(java_node)
rescue ::Java::JavaLang::IndexOutOfBoundsException
  nil
end

#child_by_field_name(name) ⇒ Node?

Get a child by field name

Parameters:

  • name (String)

    the field name

Returns:

  • (Node, nil)

    the child node or nil if not found



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'lib/tree_haver/backends/java.rb', line 663

def child_by_field_name(name)
  # jtreesitter 0.26.0: getChildByFieldName returns Optional<Node>
  # However, some versions or scenarios may return null directly
  result = @impl.getChildByFieldName(name)
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    # Direct Node return (some jtreesitter versions)
    java_node = result
  end

  Node.new(java_node)
end

#child_countInteger

Get the number of children

Returns:

  • (Integer)

    child count



632
633
634
# File 'lib/tree_haver/backends/java.rb', line 632

def child_count
  @impl.childCount
end

#each {|Node| ... } ⇒ void

This method returns an undefined value.

Iterate over children

Yields:

  • (Node)

    each child node



685
686
687
688
689
690
# File 'lib/tree_haver/backends/java.rb', line 685

def each
  return enum_for(:each) unless block_given?
  child_count.times do |i|
    yield child(i)
  end
end

#end_byteInteger

Get the end byte position

Returns:

  • (Integer)

    end byte



702
703
704
# File 'lib/tree_haver/backends/java.rb', line 702

def end_byte
  @impl.endByte
end

#end_pointHash

Get the end point (row, column)

Returns:

  • (Hash)

    with :row and :column keys



717
718
719
720
# File 'lib/tree_haver/backends/java.rb', line 717

def end_point
  pt = @impl.endPoint
  {row: pt.row, column: pt.column}
end

#has_error?Boolean

Check if this node has an error

Returns:

  • (Boolean)

    true if the node or any descendant has an error



725
726
727
# File 'lib/tree_haver/backends/java.rb', line 725

def has_error?
  @impl.hasError
end

#missing?Boolean

Check if this node is missing

Returns:

  • (Boolean)

    true if this is a MISSING node



732
733
734
# File 'lib/tree_haver/backends/java.rb', line 732

def missing?
  @impl.isMissing
end

#named?Boolean

Check if this is a named node

Returns:

  • (Boolean)

    true if this is a named node



739
740
741
# File 'lib/tree_haver/backends/java.rb', line 739

def named?
  @impl.isNamed
end

#next_siblingNode?

Get the next sibling node

Returns:

  • (Node, nil)

    the next sibling or nil if none



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/tree_haver/backends/java.rb', line 765

def next_sibling
  # jtreesitter 0.26.0: getNextSibling returns Optional<Node>
  result = @impl.getNextSibling
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    java_node = result
  end

  Node.new(java_node)
end

#parentNode?

Get the parent node

Returns:

  • (Node, nil)

    the parent node or nil if this is the root



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/tree_haver/backends/java.rb', line 746

def parent
  # jtreesitter 0.26.0: getParent returns Optional<Node>
  result = @impl.getParent
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    java_node = result
  end

  Node.new(java_node)
end

#prev_siblingNode?

Get the previous sibling node

Returns:

  • (Node, nil)

    the previous sibling or nil if none



784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/tree_haver/backends/java.rb', line 784

def prev_sibling
  # jtreesitter 0.26.0: getPrevSibling returns Optional<Node>
  result = @impl.getPrevSibling
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    java_node = result
  end

  Node.new(java_node)
end

#start_byteInteger

Get the start byte position

Returns:

  • (Integer)

    start byte



695
696
697
# File 'lib/tree_haver/backends/java.rb', line 695

def start_byte
  @impl.startByte
end

#start_pointHash

Get the start point (row, column)

Returns:

  • (Hash)

    with :row and :column keys



709
710
711
712
# File 'lib/tree_haver/backends/java.rb', line 709

def start_point
  pt = @impl.startPoint
  {row: pt.row, column: pt.column}
end

#textString

Get the text of this node

Returns:

  • (String)

    the source text



803
804
805
# File 'lib/tree_haver/backends/java.rb', line 803

def text
  @impl.text.to_s
end

#typeString

Get the type of this node

Returns:

  • (String)

    the node type



625
626
627
# File 'lib/tree_haver/backends/java.rb', line 625

def type
  @impl.type
end