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.



611
612
613
# File 'lib/tree_haver/backends/java.rb', line 611

def initialize(impl)
  @impl = impl
end

Instance Attribute Details

#implObject (readonly)

Returns the value of attribute impl.



608
609
610
# File 'lib/tree_haver/backends/java.rb', line 608

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



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/tree_haver/backends/java.rb', line 633

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



656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/tree_haver/backends/java.rb', line 656

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



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

def child_count
  @impl.childCount
end

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

This method returns an undefined value.

Iterate over children

Yields:

  • (Node)

    each child node



678
679
680
681
682
683
# File 'lib/tree_haver/backends/java.rb', line 678

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



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

def end_byte
  @impl.endByte
end

#end_pointHash

Get the end point (row, column)

Returns:

  • (Hash)

    with :row and :column keys



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

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



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

def has_error?
  @impl.hasError
end

#missing?Boolean

Check if this node is missing

Returns:

  • (Boolean)

    true if this is a MISSING node



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

def missing?
  @impl.isMissing
end

#named?Boolean

Check if this is a named node

Returns:

  • (Boolean)

    true if this is a named node



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

def named?
  @impl.isNamed
end

#next_siblingNode?

Get the next sibling node

Returns:

  • (Node, nil)

    the next sibling or nil if none



758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/tree_haver/backends/java.rb', line 758

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



739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# File 'lib/tree_haver/backends/java.rb', line 739

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



777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/tree_haver/backends/java.rb', line 777

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



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

def start_byte
  @impl.startByte
end

#start_pointHash

Get the start point (row, column)

Returns:

  • (Hash)

    with :row and :column keys



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

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



796
797
798
# File 'lib/tree_haver/backends/java.rb', line 796

def text
  @impl.text.to_s
end

#typeString

Get the type of this node

Returns:

  • (String)

    the node type



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

def type
  @impl.type
end