Class: RubyLsp::Requests::TypeHierarchySupertypes

Inherits:
Request
  • Object
show all
Includes:
Support::Common
Defined in:
lib/ruby_lsp/requests/type_hierarchy_supertypes.rb

Overview

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

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?

Constructor Details

#initialize(index, item) ⇒ TypeHierarchySupertypes

: (RubyIndexer::Index index, Hash[Symbol, untyped] item) -> void



13
14
15
16
17
18
# File 'lib/ruby_lsp/requests/type_hierarchy_supertypes.rb', line 13

def initialize(index, item)
  super()

  @index = index
  @item = item
end

Instance Method Details

#performObject

: -> Array?



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_lsp/requests/type_hierarchy_supertypes.rb', line 22

def perform
  name = @item[:name]
  entries = @index[name]

  parents = Set.new #: Set[RubyIndexer::Entry::Namespace]
  return unless entries&.any?

  entries.each do |entry|
    next unless entry.is_a?(RubyIndexer::Entry::Namespace)

    if entry.is_a?(RubyIndexer::Entry::Class)
      parent_class_name = entry.parent_class
      if parent_class_name
        resolved_parent_entries = @index.resolve(parent_class_name, entry.nesting)
        resolved_parent_entries&.each do |entry|
          next unless entry.is_a?(RubyIndexer::Entry::Class)

          parents << entry
        end
      end
    end

    entry.mixin_operations.each do |mixin_operation|
      mixin_name = mixin_operation.module_name
      resolved_mixin_entries = @index.resolve(mixin_name, entry.nesting)
      next unless resolved_mixin_entries

      resolved_mixin_entries.each do |mixin_entry|
        next unless mixin_entry.is_a?(RubyIndexer::Entry::Module)

        parents << mixin_entry
      end
    end
  end

  parents.map { |entry| hierarchy_item(entry) }
end