diff --git a/jekyll/add-ons.markdown b/jekyll/add-ons.markdown index 2d86a216b..0506b5961 100644 --- a/jekyll/add-ons.markdown +++ b/jekyll/add-ons.markdown @@ -61,6 +61,10 @@ The Ruby LSP discovers add-ons based on the existence of an `addon.rb` file plac example, `my_gem/lib/ruby_lsp/my_gem/addon.rb`. This file must declare the add-on class, which can be used to perform any necessary activation when the server starts. +{: .note } +Projects can also define their own private add-ons for functionality that only applies to a particular application. As +long as a file matching `ruby_lsp/**/addon.rb` exists inside of the workspace (not necessarily at the root), it will be +loaded by the Ruby LSP. ```ruby # frozen_string_literal: true diff --git a/lib/ruby_indexer/lib/ruby_indexer/configuration.rb b/lib/ruby_indexer/lib/ruby_indexer/configuration.rb index eee3fd2e8..e3c4c9b77 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/configuration.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/configuration.rb @@ -239,6 +239,8 @@ def initial_excluded_gems excluded.uniq! excluded.map(&:name) + rescue Bundler::GemfileNotFound + [] end end end diff --git a/lib/ruby_lsp/addon.rb b/lib/ruby_lsp/addon.rb index 44f6f8496..af3171ce3 100644 --- a/lib/ruby_lsp/addon.rb +++ b/lib/ruby_lsp/addon.rb @@ -54,14 +54,19 @@ def inherited(child_class) end # Discovers and loads all add-ons. Returns a list of errors when trying to require add-ons - sig do - params(global_state: GlobalState, outgoing_queue: Thread::Queue).returns(T::Array[StandardError]) - end + sig { params(global_state: GlobalState, outgoing_queue: Thread::Queue).returns(T::Array[StandardError]) } def load_addons(global_state, outgoing_queue) # Require all add-ons entry points, which should be placed under - # `some_gem/lib/ruby_lsp/your_gem_name/addon.rb` - errors = Gem.find_files("ruby_lsp/**/addon.rb").filter_map do |addon| - require File.expand_path(addon) + # `some_gem/lib/ruby_lsp/your_gem_name/addon.rb` or in the workspace under + # `your_project/ruby_lsp/project_name/addon.rb` + errors = Gem.find_files("ruby_lsp/**/addon.rb") + .concat(Dir.glob(File.join(global_state.workspace_path, "**", "ruby_lsp/**/addon.rb"))) + .filter_map do |addon_path| + # Avoid requiring this file twice. This may happen if you're working on the Ruby LSP itself and at the same + # time have `ruby-lsp` installed as a vendored gem + next if File.basename(File.dirname(addon_path)) == "ruby_lsp" + + require File.expand_path(addon_path) nil rescue => e e @@ -90,6 +95,10 @@ def load_addons(global_state, outgoing_queue) # the responsibility of the add-ons using this API to handle these errors appropriately. sig { params(addon_name: String, version_constraints: String).returns(Addon) } def get(addon_name, *version_constraints) + if version_constraints.empty? + raise IncompatibleApiError, "Must specify version constraints when accessing other add-ons" + end + addon = addons.find { |addon| addon.name == addon_name } raise AddonNotFoundError, "Could not find add-on '#{addon_name}'" unless addon diff --git a/test/addon_test.rb b/test/addon_test.rb index ddd75236f..e3ef5923d 100644 --- a/test/addon_test.rb +++ b/test/addon_test.rb @@ -126,6 +126,12 @@ def test_raises_if_an_addon_version_does_not_match end end + def test_raises_if_no_version_constraints_are_passed + assert_raises(Addon::IncompatibleApiError) do + Addon.get("My Add-on") + end + end + def test_addons_receive_settings global_state = GlobalState.new global_state.apply_options({ @@ -153,5 +159,44 @@ def test_depend_on_constraints Addon.depend_on_ruby_lsp!(">= 0.18.0", "< 0.30.0") end + + def test_project_specific_addons + Dir.mktmpdir do |dir| + Dir.chdir(dir) do + addon_dir = File.join(dir, "lib", "ruby_lsp", "test_addon") + FileUtils.mkdir_p(addon_dir) + File.write(File.join(addon_dir, "addon.rb"), <<~RUBY) + class ProjectAddon < RubyLsp::Addon + attr_reader :hello + + def activate(global_state, outgoing_queue) + @hello = true + end + + def name + "Project Addon" + end + + def version + "0.1.0" + end + end + RUBY + + queue = Thread::Queue.new + global_state = GlobalState.new + global_state.apply_options({ + workspaceFolders: [{ uri: URI::Generic.from_path(path: dir).to_s }], + }) + Addon.load_addons(global_state, queue) + + addon = Addon.get("Project Addon", "0.1.0") + assert_equal("Project Addon", addon.name) + assert_predicate(T.unsafe(addon), :hello) + ensure + T.must(queue).close + end + end + end end end