From ad61c53f88ef2c15a8e3f5815bcd48deaa71b6f7 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Wed, 18 Oct 2023 10:43:20 -0400 Subject: [PATCH] Allow the LSP to work with YARP v0.13 as well --- Gemfile | 1 - Gemfile.lock | 14 ++---- lib/ruby_lsp/requests/code_lens.rb | 12 ++--- lib/ruby_lsp/requests/definition.rb | 8 ++-- lib/ruby_lsp/requests/document_symbol.rb | 4 +- lib/ruby_lsp/requests/support/sorbet.rb | 56 ++++++++++++------------ 6 files changed, 44 insertions(+), 51 deletions(-) diff --git a/Gemfile b/Gemfile index a6f9dd0efc..ed56881874 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 2b2da38ccf..8eace8bc68 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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: @@ -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) @@ -113,6 +107,7 @@ GEM yard-sorbet (0.8.1) sorbet-runtime (>= 0.5) yard (>= 0.9) + yarp (0.13.0) PLATFORMS arm64-darwin @@ -129,7 +124,6 @@ DEPENDENCIES mocha (~> 2.1) psych (~> 5.1) rake (~> 13.0) - rbi! rdoc rubocop (~> 1.57) rubocop-minitest (~> 0.32.2) diff --git a/lib/ruby_lsp/requests/code_lens.rb b/lib/ruby_lsp/requests/code_lens.rb index f044b7dafa..9f1960389b 100644 --- a/lib/ruby_lsp/requests/code_lens.rb +++ b/lib/ruby_lsp/requests/code_lens.rb @@ -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) } @@ -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) @@ -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) @@ -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( @@ -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` @@ -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) diff --git a/lib/ruby_lsp/requests/definition.rb b/lib/ruby_lsp/requests/definition.rb index 103af49ac9..20f3a69afa 100644 --- a/lib/ruby_lsp/requests/definition.rb +++ b/lib/ruby_lsp/requests/definition.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/ruby_lsp/requests/document_symbol.rb b/lib/ruby_lsp/requests/document_symbol.rb index e6fdc0ecc8..1454ea1cc2 100644 --- a/lib/ruby_lsp/requests/document_symbol.rb +++ b/lib/ruby_lsp/requests/document_symbol.rb @@ -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 @@ -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 diff --git a/lib/ruby_lsp/requests/support/sorbet.rb b/lib/ruby_lsp/requests/support/sorbet.rb index 60a116bd34..e5e463ff8a 100644 --- a/lib/ruby_lsp/requests/support/sorbet.rb +++ b/lib/ruby_lsp/requests/support/sorbet.rb @@ -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 @@ -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