Module: TreeHaver::Backends::MRI

Defined in:
lib/tree_haver/backends/mri.rb

Overview

MRI backend using the ruby_tree_sitter gem

This backend wraps the ruby_tree_sitter gem, which is a native C extension for MRI Ruby. It provides the most feature-complete tree-sitter integration on MRI, including support for the Query API.

Platform Compatibility

  • MRI Ruby: ✓ Full support (fastest tree-sitter backend on MRI)

  • JRuby: ✗ Cannot load native C extensions (runs on JVM)

  • TruffleRuby: ✗ C extension not compatible with TruffleRuby

See Also:

Defined Under Namespace

Classes: Language, Parser

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tree_haver/backends/mri.rb', line 42

def available?
  return @loaded if @load_attempted # rubocop:disable ThreadSafety/ClassInstanceVariable
  @load_attempted = true # rubocop:disable ThreadSafety/ClassInstanceVariable

  # ruby_tree_sitter is a C extension that only works on MRI
  # It doesn't work on JRuby or TruffleRuby
  unless RUBY_ENGINE == "ruby"
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
    return @loaded
  end

  begin
    require "tree_sitter" # Note: gem is ruby_tree_sitter but requires tree_sitter

    @loaded = true # rubocop:disable ThreadSafety/ClassInstanceVariable
  rescue LoadError
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  end
  @loaded # rubocop:disable ThreadSafety/ClassInstanceVariable
end

.capabilitiesHash{Symbol => Object}

Get capabilities supported by this backend

Examples:

TreeHaver::Backends::MRI.capabilities
# => { backend: :mri, query: true, bytes_field: true, incremental: true }

Returns:

  • (Hash{Symbol => Object})

    capability map



78
79
80
81
82
83
84
85
86
# File 'lib/tree_haver/backends/mri.rb', line 78

def capabilities
  return {} unless available?
  {
    backend: :mri,
    query: true,
    bytes_field: true,
    incremental: true,
  }
end

.reset!void

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.

This method returns an undefined value.

Reset the load state (primarily for testing)



67
68
69
70
# File 'lib/tree_haver/backends/mri.rb', line 67

def reset!
  @load_attempted = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
end