From dad7653a3f12b2d41f1ed6013a1407f854566137 Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Thu, 1 Feb 2024 23:09:55 +0200 Subject: [PATCH 1/3] Add RubyLSP dependencies custom request Co-authored-by: Vinicius Stock --- lib/ruby_lsp/executor.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/ruby_lsp/executor.rb b/lib/ruby_lsp/executor.rb index 3466eea90..642c5bff9 100644 --- a/lib/ruby_lsp/executor.rb +++ b/lib/ruby_lsp/executor.rb @@ -222,6 +222,16 @@ def run(request) workspace_symbol(request.dig(:params, :query)) when "rubyLsp/textDocument/showSyntaxTree" show_syntax_tree(uri, request.dig(:params, :range)) + when "rubyLsp/workspace/dependencies" + dep_keys = Bundler.definition.locked_deps.keys.to_set + Bundler.definition.specs.map do |spec| + { + name: spec.name, + version: spec.version, + path: spec.full_gem_path, + dependency: dep_keys.include?(spec.name), + } + end else VOID end From e7dd90b487d5970986983cf8d4c6ceb2a0460941 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Fri, 2 Feb 2024 14:45:06 -0500 Subject: [PATCH 2/3] Add test for workspace dependencies --- test/executor_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/executor_test.rb b/test/executor_test.rb index b98822983..e147d5972 100644 --- a/test/executor_test.rb +++ b/test/executor_test.rb @@ -364,6 +364,20 @@ def test_initialize_sets_client_name assert_equal("Foo", @store.client_name) end + def test_workspace_dependencies + result = @executor.execute({ + method: "rubyLsp/workspace/dependencies", + params: {}, + }) + + result.response.each do |gem_info| + assert_instance_of(String, gem_info[:name]) + assert_instance_of(Gem::Version, gem_info[:version]) + assert_instance_of(String, gem_info[:path]) + assert(gem_info[:dependency].is_a?(TrueClass) || gem_info[:dependency].is_a?(FalseClass)) + end + end + private def with_uninstalled_rubocop(&block) From a1da8a63bafb4986480c44610089097e93b8e519 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Fri, 2 Feb 2024 14:45:16 -0500 Subject: [PATCH 3/3] Prevent workspace dependencies from failing on missing bundle --- lib/ruby_lsp/executor.rb | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/ruby_lsp/executor.rb b/lib/ruby_lsp/executor.rb index 642c5bff9..3f200c69b 100644 --- a/lib/ruby_lsp/executor.rb +++ b/lib/ruby_lsp/executor.rb @@ -223,15 +223,7 @@ def run(request) when "rubyLsp/textDocument/showSyntaxTree" show_syntax_tree(uri, request.dig(:params, :range)) when "rubyLsp/workspace/dependencies" - dep_keys = Bundler.definition.locked_deps.keys.to_set - Bundler.definition.specs.map do |spec| - { - name: spec.name, - version: spec.version, - path: spec.full_gem_path, - dependency: dep_keys.include?(spec.name), - } - end + workspace_dependencies else VOID end @@ -262,6 +254,22 @@ def did_change_watched_files(changes) VOID end + sig { returns(T::Array[T::Hash[Symbol, T.untyped]]) } + def workspace_dependencies + definition = Bundler.definition + dep_keys = definition.locked_deps.keys.to_set + definition.specs.map do |spec| + { + name: spec.name, + version: spec.version, + path: spec.full_gem_path, + dependency: dep_keys.include?(spec.name), + } + end + rescue Bundler::GemfileNotFound + [] + end + sig { void } def perform_initial_indexing # The begin progress invocation happens during `initialize`, so that the notification is sent before we are