Class: Rbs::Merge::Backends::RbsBackend::Node Private
- Inherits:
-
TreeHaver::Base::Node
- Object
- TreeHaver::Base::Node
- Rbs::Merge::Backends::RbsBackend::Node
- Defined in:
- lib/rbs/merge/backends/rbs_backend.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
RBS node wrapper
Wraps RBS AST nodes to provide tree-sitter-compatible node API.
RBS nodes provide:
- Various declaration types (Class, Module, Interface, TypeAlias, etc.)
- Member types (MethodDefinition, Alias, AttrReader, etc.)
- Location information via .location
Instance Attribute Summary collapse
-
#children_array ⇒ Array<Object>
readonly
private
Child nodes (for synthetic root).
Class Method Summary collapse
-
.new_root(declarations, source: nil, lines: nil) ⇒ Node
private
Create a synthetic root node containing all declarations.
Instance Method Summary collapse
-
#child_by_field_name(name) ⇒ Node?
(also: #field)
private
Get a child by field name (RBS node accessor).
-
#children ⇒ Array<Node>
private
Get all child nodes.
-
#end_byte ⇒ Integer
private
Get byte offset where the node ends.
-
#end_line ⇒ Integer
private
Get the 1-based line number where this node ends.
-
#end_point ⇒ Hash{Symbol => Integer}
private
Get the end position as row/column (0-based).
-
#initialize(node, source: nil, lines: nil, children_array: nil) ⇒ Node
constructor
private
A new instance of Node.
-
#inspect ⇒ String
private
String representation for debugging.
-
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
private
Delegate unknown methods to the underlying RBS node.
-
#name ⇒ String?
private
Get the name of this declaration/member.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
private
Check if node responds to a method (includes delegation to inner_node).
-
#root_node? ⇒ Boolean
private
Check if this is a synthetic root node.
-
#source_position ⇒ Hash{Symbol => Integer}
private
Get position information as a hash.
-
#start_byte ⇒ Integer
private
Get byte offset where the node starts.
-
#start_line ⇒ Integer
private
Get the 1-based line number where this node starts.
-
#start_point ⇒ Hash{Symbol => Integer}
private
Get the start position as row/column (0-based).
-
#text ⇒ String
private
Get the text content of this node.
-
#to_s ⇒ String
private
String representation.
-
#type ⇒ String
(also: #kind)
private
Get node type from RBS class name.
Constructor Details
#initialize(node, source: nil, lines: nil, children_array: nil) ⇒ 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.
268 269 270 271 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 268 def initialize(node, source: nil, lines: nil, children_array: nil) super(node, source: source, lines: lines) @children_array = children_array end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
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.
Delegate unknown methods to the underlying RBS node
505 506 507 508 509 510 511 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 505 def method_missing(method_name, *args, **kwargs, &block) if @inner_node&.respond_to?(method_name) @inner_node.public_send(method_name, *args, **kwargs, &block) else super end end |
Instance Attribute Details
#children_array ⇒ Array<Object> (readonly)
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 child nodes (for synthetic root).
266 267 268 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 266 def children_array @children_array end |
Class Method Details
.new_root(declarations, source: nil, lines: nil) ⇒ 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.
Create a synthetic root node containing all declarations
280 281 282 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 280 def new_root(declarations, source: nil, lines: nil) new(nil, source: source, lines: lines, children_array: declarations) end |
Instance Method Details
#child_by_field_name(name) ⇒ Node? Also known as: field
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.
Get a child by field name (RBS node accessor)
RBS nodes have specific accessors for their children.
This method tries to call that accessor.
448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 448 def child_by_field_name(name) return if @inner_node.nil? return unless @inner_node.respond_to?(name) result = @inner_node.public_send(name) return if result.nil? # Wrap if it's a node, otherwise return nil if result.respond_to?(:location) Node.new(result, source: source, lines: lines) end end |
#children ⇒ Array<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.
Get all child nodes
431 432 433 434 435 436 437 438 439 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 431 def children if root_node? return @children_array.map { |n| Node.new(n, source: source, lines: lines) } end return [] unless @inner_node.respond_to?(:members) @inner_node.members.map { |n| Node.new(n, source: source, lines: lines) } end |
#end_byte ⇒ Integer
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.
Get byte offset where the node ends
342 343 344 345 346 347 348 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 342 def end_byte return source.bytesize if root_node? && source return 0 if root_node? return 0 unless @inner_node.respond_to?(:location) && @inner_node.location @inner_node.location.end_pos end |
#end_line ⇒ Integer
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.
Get the 1-based line number where this node ends
390 391 392 393 394 395 396 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 390 def end_line return lines.size if root_node? && lines.any? return 1 if root_node? return 1 unless @inner_node.respond_to?(:location) && @inner_node.location @inner_node.location.end_line end |
#end_point ⇒ Hash{Symbol => Integer}
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.
Get the end position as row/column (0-based)
364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 364 def end_point if root_node? && source last_line = lines.size - 1 last_col = lines.last&.size || 0 return {row: last_line, column: last_col} end return {row: 0, column: 0} if root_node? return {row: 0, column: 0} unless @inner_node.respond_to?(:location) && @inner_node.location loc = @inner_node.location {row: loc.end_line - 1, column: loc.end_column} end |
#inspect ⇒ String
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.
String representation for debugging
477 478 479 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 477 def inspect "#<#{self.class} type=#{type} lines=#{start_line}..#{end_line}>" end |
#name ⇒ String?
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.
Get the name of this declaration/member
466 467 468 469 470 471 472 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 466 def name return if @inner_node.nil? return unless @inner_node.respond_to?(:name) n = @inner_node.name n.respond_to?(:to_s) ? n.to_s : n end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
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.
Check if node responds to a method (includes delegation to inner_node)
493 494 495 496 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 493 def respond_to_missing?(method_name, include_private = false) return false if @inner_node.nil? @inner_node.respond_to?(method_name, include_private) || super end |
#root_node? ⇒ Boolean
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.
Check if this is a synthetic root node
413 414 415 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 413 def root_node? @inner_node.nil? && @children_array end |
#source_position ⇒ Hash{Symbol => Integer}
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.
Get position information as a hash
401 402 403 404 405 406 407 408 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 401 def source_position { start_line: start_line, end_line: end_line, start_column: start_point[:column], end_column: end_point[:column], } end |
#start_byte ⇒ Integer
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.
Get byte offset where the node starts
332 333 334 335 336 337 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 332 def start_byte return 0 if root_node? return 0 unless @inner_node.respond_to?(:location) && @inner_node.location @inner_node.location.start_pos end |
#start_line ⇒ Integer
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.
Get the 1-based line number where this node starts
380 381 382 383 384 385 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 380 def start_line return 1 if root_node? return 1 unless @inner_node.respond_to?(:location) && @inner_node.location @inner_node.location.start_line end |
#start_point ⇒ Hash{Symbol => Integer}
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.
Get the start position as row/column (0-based)
353 354 355 356 357 358 359 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 353 def start_point return {row: 0, column: 0} if root_node? return {row: 0, column: 0} unless @inner_node.respond_to?(:location) && @inner_node.location loc = @inner_node.location {row: loc.start_line - 1, column: loc.start_column} end |
#text ⇒ String
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.
Get the text content of this node
420 421 422 423 424 425 426 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 420 def text return source.to_s if root_node? return "" unless @inner_node.respond_to?(:location) && @inner_node.location loc = @inner_node.location source[loc.start_pos...loc.end_pos] || "" end |
#to_s ⇒ String
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.
String representation
484 485 486 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 484 def to_s text end |
#type ⇒ String Also known as: kind
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.
Get node type from RBS class name
Maps RBS::AST class names to tree-sitter-style type strings.
For synthetic root, returns “program”.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 291 def type return "program" if root_node? return "unknown" if @inner_node.nil? # Map RBS class to canonical type case @inner_node when ::RBS::AST::Declarations::Class then "class_decl" when ::RBS::AST::Declarations::Module then "module_decl" when ::RBS::AST::Declarations::Interface then "interface_decl" when ::RBS::AST::Declarations::TypeAlias then "type_alias_decl" when ::RBS::AST::Declarations::Constant then "const_decl" when ::RBS::AST::Declarations::Global then "global_decl" when ::RBS::AST::Declarations::ClassAlias then "class_alias_decl" when ::RBS::AST::Declarations::ModuleAlias then "module_alias_decl" when ::RBS::AST::Members::MethodDefinition then "method_member" when ::RBS::AST::Members::Alias then "alias_member" when ::RBS::AST::Members::AttrReader then "attr_reader_member" when ::RBS::AST::Members::AttrWriter then "attr_writer_member" when ::RBS::AST::Members::AttrAccessor then "attr_accessor_member" when ::RBS::AST::Members::Include then "include_member" when ::RBS::AST::Members::Extend then "extend_member" when ::RBS::AST::Members::Prepend then "prepend_member" when ::RBS::AST::Members::InstanceVariable then "ivar_member" when ::RBS::AST::Members::ClassInstanceVariable then "civar_member" when ::RBS::AST::Members::ClassVariable then "cvar_member" when ::RBS::AST::Members::Public then "public_member" when ::RBS::AST::Members::Private then "private_member" else # Fallback to class name conversion @inner_node.class.name.split("::").last .gsub(/([A-Z])/, '_\1').downcase.sub(/^_/, "") end end |