Class: RubyLsp::Requests::PrepareTypeHierarchy

Inherits:
Request
  • Object
show all
Extended by:
T::Sig
Includes:
Support::Common
Defined in:
lib/ruby_lsp/requests/prepare_type_hierarchy.rb

Overview

![Prepare type hierarchy demo](../../prepare_type_hierarchy.gif)

The [prepare type hierarchy request](microsoft.github.io/language-server-protocol/specification#textDocument_prepareTypeHierarchy) displays the list of ancestors (supertypes) and descendants (subtypes) for the selected type.

Currently only supports supertypes due to a limitation of the index.

# Example

“‘ruby class Foo; end class Bar < Foo; end

puts Bar # <– right click on ‘Bar` and select “Show Type Hierarchy” “`

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Common

#categorized_markdown_from_index_entries, #constant_name, #create_code_lens, #each_constant_path_part, #kind_for_entry, #markdown_from_index_entries, #namespace_constant_name, #not_in_dependencies?, #range_from_location, #range_from_node, #self_receiver?, #sorbet_level_true_or_higher?, #visible?

Constructor Details

#initialize(document, index, position) ⇒ PrepareTypeHierarchy



43
44
45
46
47
48
49
# File 'lib/ruby_lsp/requests/prepare_type_hierarchy.rb', line 43

def initialize(document, index, position)
  super()

  @document = document
  @index = index
  @position = position
end

Class Method Details

.providerObject



31
32
33
# File 'lib/ruby_lsp/requests/prepare_type_hierarchy.rb', line 31

def provider
  Interface::TypeHierarchyOptions.new
end

Instance Method Details

#performObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby_lsp/requests/prepare_type_hierarchy.rb', line 52

def perform
  context = @document.locate_node(
    @position,
    node_types: [
      Prism::ConstantReadNode,
      Prism::ConstantWriteNode,
      Prism::ConstantPathNode,
    ],
  )

  node = context.node
  parent = context.parent
  return unless node && parent

  target = determine_target(node, parent, @position)
  entries = @index.resolve(target.slice, context.nesting)
  return unless entries

  # While the spec allows for multiple entries, VSCode seems to only support one
  # We'll just return the first one for now
  first_entry = T.must(entries.first)

  range = range_from_location(first_entry.location)

  [
    Interface::TypeHierarchyItem.new(
      name: first_entry.name,
      kind: kind_for_entry(first_entry),
      uri: URI::Generic.from_path(path: first_entry.file_path).to_s,
      range: range,
      selection_range: range,
    ),
  ]
end