Skip to content

Commit

Permalink
Allow the LSP to work with YARP v0.13 as well
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Oct 18, 2023
1 parent 435c758 commit ad61c53
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 51 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ group :development do
gem "rdoc", require: false
gem "psych", "~> 5.1", require: false

gem "rbi", github: "Shopify/rbi", branch: "vs/move_to_prism"
gem "syntax_tree", ">= 6.1.1", "< 7"
end
14 changes: 4 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
GIT
remote: https://github.com/Shopify/rbi.git
revision: d32ad8e95ff8c31eed5cdc6ce4881cc88ecf7fd2
branch: vs/move_to_prism
specs:
rbi (0.1.1)
prism (>= 0.14.0)
sorbet-runtime (>= 0.5.9204)

PATH
remote: .
specs:
Expand Down Expand Up @@ -52,6 +43,9 @@ GEM
racc (1.7.1)
rainbow (3.1.1)
rake (13.0.6)
rbi (0.1.1)
sorbet-runtime (>= 0.5.9204)
yarp (>= 0.11.0)
rdoc (6.5.0)
psych (>= 4.0.0)
regexp_parser (2.8.2)
Expand Down Expand Up @@ -113,6 +107,7 @@ GEM
yard-sorbet (0.8.1)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
yarp (0.13.0)

PLATFORMS
arm64-darwin
Expand All @@ -129,7 +124,6 @@ DEPENDENCIES
mocha (~> 2.1)
psych (~> 5.1)
rake (~> 13.0)
rbi!
rdoc
rubocop (~> 1.57)
rubocop-minitest (~> 0.32.2)
Expand Down
12 changes: 6 additions & 6 deletions lib/ruby_lsp/requests/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CodeLens < ExtensibleListener
ResponseType = type_member { { fixed: T::Array[Interface::CodeLens] } }

BASE_COMMAND = T.let((File.exist?("Gemfile.lock") ? "bundle exec ruby" : "ruby") + " -Itest ", String)
ACCESS_MODIFIERS = T.let([:public, :private, :protected], T::Array[Symbol])
ACCESS_MODIFIERS = T.let(["public", "private", "protected"], T::Array[String])
SUPPORTED_TEST_LIBRARIES = T.let(["minitest", "test-unit"], T::Array[String])

sig { override.returns(ResponseType) }
Expand All @@ -37,7 +37,7 @@ def initialize(uri, dispatcher, message_queue)
@_response = T.let([], ResponseType)
@path = T.let(uri.to_standardized_path, T.nilable(String))
# visibility_stack is a stack of [current_visibility, previous_visibility]
@visibility_stack = T.let([[:public, :public]], T::Array[T::Array[T.nilable(Symbol)]])
@visibility_stack = T.let([["public", "public"]], T::Array[T::Array[T.nilable(String)]])
@class_stack = T.let([], T::Array[String])

super(dispatcher, message_queue)
Expand All @@ -54,7 +54,7 @@ def initialize(uri, dispatcher, message_queue)

sig { params(node: Prism::ClassNode).void }
def on_class_node_enter(node)
@visibility_stack.push([:public, :public])
@visibility_stack.push(["public", "public"])
class_name = node.constant_path.slice
@class_stack.push(class_name)

Expand All @@ -80,7 +80,7 @@ def on_def_node_enter(node)
return unless class_name&.end_with?("Test")

visibility, _ = @visibility_stack.last
if visibility == :public
if visibility == "public"
method_name = node.name.to_s
if @path && method_name.start_with?("test_")
add_test_code_lens(
Expand All @@ -95,7 +95,7 @@ def on_def_node_enter(node)

sig { params(node: Prism::CallNode).void }
def on_call_node_enter(node)
name = node.name
name = node.name.to_s
arguments = node.arguments

# If we found `private` by itself or `private def foo`
Expand All @@ -111,7 +111,7 @@ def on_call_node_enter(node)
return
end

if @path&.include?("Gemfile") && name == :gem && arguments
if @path&.include?("Gemfile") && name == "gem" && arguments
first_argument = arguments.arguments.first
return unless first_argument.is_a?(Prism::StringNode)

Expand Down
8 changes: 4 additions & 4 deletions lib/ruby_lsp/requests/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def merge_response!(other)

sig { params(node: Prism::CallNode).void }
def on_call_node_enter(node)
message = node.name
return unless message == :require || message == :require_relative
message = node.name.to_s
return unless message == "require" || message == "require_relative"

arguments = node.arguments
return unless arguments
Expand All @@ -84,7 +84,7 @@ def on_call_node_enter(node)
return unless argument.is_a?(Prism::StringNode)

case message
when :require
when "require"
entry = @index.search_require_paths(argument.content).find do |indexable_path|
indexable_path.require_path == argument.content
end
Expand All @@ -100,7 +100,7 @@ def on_call_node_enter(node)
),
)
end
when :require_relative
when "require_relative"
required_file = "#{argument.content}.rb"
path = @uri.to_standardized_path
current_folder = path ? Pathname.new(CGI.unescape(path)).dirname : Dir.pwd
Expand Down
4 changes: 2 additions & 2 deletions lib/ruby_lsp/requests/document_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DocumentSymbol < ExtensibleListener

ResponseType = type_member { { fixed: T::Array[Interface::DocumentSymbol] } }

ATTR_ACCESSORS = T.let([:attr_reader, :attr_writer, :attr_accessor].freeze, T::Array[Symbol])
ATTR_ACCESSORS = T.let(["attr_reader", "attr_writer", "attr_accessor"].freeze, T::Array[String])

class SymbolHierarchyRoot
extend T::Sig
Expand Down Expand Up @@ -105,7 +105,7 @@ def on_class_node_leave(node)

sig { params(node: Prism::CallNode).void }
def on_call_node_enter(node)
return unless ATTR_ACCESSORS.include?(node.name) && node.receiver.nil?
return unless ATTR_ACCESSORS.include?(node.name.to_s) && node.receiver.nil?

arguments = node.arguments
return unless arguments
Expand Down
56 changes: 28 additions & 28 deletions lib/ruby_lsp/requests/support/sorbet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ class << self

ANNOTATIONS = T.let(
{
abstract!: Annotation.new(arity: 0),
absurd: Annotation.new(arity: 1, receiver: true),
all: Annotation.new(arity: (2..), receiver: true),
any: Annotation.new(arity: (2..), receiver: true),
assert_type!: Annotation.new(arity: 2, receiver: true),
attached_class: Annotation.new(arity: 0, receiver: true),
bind: Annotation.new(arity: 2, receiver: true),
cast: Annotation.new(arity: 2, receiver: true),
class_of: Annotation.new(arity: 1, receiver: true),
enums: Annotation.new(arity: 0),
interface!: Annotation.new(arity: 0),
let: Annotation.new(arity: 2, receiver: true),
mixes_in_class_methods: Annotation.new(arity: 1),
must: Annotation.new(arity: 1, receiver: true),
must_because: Annotation.new(arity: 1, receiver: true),
nilable: Annotation.new(arity: 1, receiver: true),
noreturn: Annotation.new(arity: 0, receiver: true),
requires_ancestor: Annotation.new(arity: 0),
reveal_type: Annotation.new(arity: 1, receiver: true),
sealed!: Annotation.new(arity: 0),
self_type: Annotation.new(arity: 0, receiver: true),
sig: Annotation.new(arity: 0),
type_member: Annotation.new(arity: (0..1)),
type_template: Annotation.new(arity: 0),
unsafe: Annotation.new(arity: 1),
untyped: Annotation.new(arity: 0, receiver: true),
"abstract!" => Annotation.new(arity: 0),
"absurd" => Annotation.new(arity: 1, receiver: true),
"all" => Annotation.new(arity: (2..), receiver: true),
"any" => Annotation.new(arity: (2..), receiver: true),
"assert_type!" => Annotation.new(arity: 2, receiver: true),
"attached_class" => Annotation.new(arity: 0, receiver: true),
"bind" => Annotation.new(arity: 2, receiver: true),
"cast" => Annotation.new(arity: 2, receiver: true),
"class_of" => Annotation.new(arity: 1, receiver: true),
"enums" => Annotation.new(arity: 0),
"interface!" => Annotation.new(arity: 0),
"let" => Annotation.new(arity: 2, receiver: true),
"mixes_in_class_methods" => Annotation.new(arity: 1),
"must" => Annotation.new(arity: 1, receiver: true),
"must_because" => Annotation.new(arity: 1, receiver: true),
"nilable" => Annotation.new(arity: 1, receiver: true),
"noreturn" => Annotation.new(arity: 0, receiver: true),
"requires_ancestor" => Annotation.new(arity: 0),
"reveal_type" => Annotation.new(arity: 1, receiver: true),
"sealed!" => Annotation.new(arity: 0),
"self_type" => Annotation.new(arity: 0, receiver: true),
"sig" => Annotation.new(arity: 0),
"type_member" => Annotation.new(arity: (0..1)),
"type_template" => Annotation.new(arity: 0),
"unsafe" => Annotation.new(arity: 1),
"untyped" => Annotation.new(arity: 0, receiver: true),
},
T::Hash[Symbol, Annotation],
T::Hash[String, Annotation],
)

sig do
Expand All @@ -46,7 +46,7 @@ class << self
).returns(T::Boolean)
end
def annotation?(node)
!!ANNOTATIONS[node.name]&.match?(node)
!!ANNOTATIONS[node.name.to_s]&.match?(node)
end
end
end
Expand Down

0 comments on commit ad61c53

Please sign in to comment.