-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2103 from Shopify/atvs-types-hierarchy
Add support for type hierarchy requests
- Loading branch information
Showing
12 changed files
with
406 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module Requests | ||
# ![Prepare type hierarchy demo](../../prepare_type_hierarchy.gif) | ||
# | ||
# The [prepare type hierarchy | ||
# request](https://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 PrepareTypeHierarchy < Request | ||
extend T::Sig | ||
|
||
include Support::Common | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { returns(Interface::TypeHierarchyOptions) } | ||
def provider | ||
Interface::TypeHierarchyOptions.new | ||
end | ||
end | ||
|
||
sig do | ||
params( | ||
document: Document, | ||
index: RubyIndexer::Index, | ||
position: T::Hash[Symbol, T.untyped], | ||
).void | ||
end | ||
def initialize(document, index, position) | ||
super() | ||
|
||
@document = document | ||
@index = index | ||
@position = position | ||
end | ||
|
||
sig { override.returns(T.nilable(T::Array[Interface::TypeHierarchyItem])) } | ||
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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module Requests | ||
# ![Type hierarchy supertypes demo](../../type_hierarchy_supertypes.gif) | ||
# | ||
# The [type hierarchy supertypes | ||
# request](https://microsoft.github.io/language-server-protocol/specification#typeHierarchy_supertypes) | ||
# displays the list of ancestors (supertypes) for the selected type. | ||
# | ||
# # Example | ||
# | ||
# ```ruby | ||
# class Foo; end | ||
# class Bar < Foo; end | ||
# | ||
# puts Bar # <-- right click on `Bar` and select "Show Type Hierarchy" | ||
# ``` | ||
class TypeHierarchySupertypes < Request | ||
extend T::Sig | ||
|
||
include Support::Common | ||
|
||
sig { params(index: RubyIndexer::Index, item: T::Hash[Symbol, T.untyped]).void } | ||
def initialize(index, item) | ||
super() | ||
|
||
@index = index | ||
@item = item | ||
end | ||
|
||
sig { override.returns(T.nilable(T::Array[Interface::TypeHierarchyItem])) } | ||
def perform | ||
name = @item[:name] | ||
entries = @index[name] | ||
|
||
parents = T.let(Set.new, T::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| | ||
next if mixin_operation.is_a?(RubyIndexer::Entry::Extend) | ||
|
||
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 | ||
|
||
private | ||
|
||
sig { params(entry: RubyIndexer::Entry).returns(Interface::TypeHierarchyItem) } | ||
def hierarchy_item(entry) | ||
range = range_from_location(entry.location) | ||
|
||
Interface::TypeHierarchyItem.new( | ||
name: entry.name, | ||
kind: kind_for_entry(entry), | ||
uri: URI::Generic.from_path(path: entry.file_path).to_s, | ||
range: range, | ||
selection_range: range, | ||
detail: entry.file_name, | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.