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 330c64b70..4b690f22a 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/configuration.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/configuration.rb @@ -235,6 +235,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 ed97c4ac1..b9f5a2177 100644 --- a/lib/ruby_lsp/addon.rb +++ b/lib/ruby_lsp/addon.rb @@ -65,6 +65,16 @@ def load_addons(global_state, outgoing_queue) e end + # When working on the Ruby LSP itself, we don't want to accidentally try to load this file as an add-on + unless File.basename(global_state.workspace_path) == "ruby_lsp" + project_addons = Dir.glob(File.join(global_state.workspace_path, "**", "ruby_lsp/**/addon.rb")) + project_addons.each do |addon_path| + require File.expand_path(addon_path) + rescue => e + errors << e + end + end + # Instantiate all discovered addon classes self.addons = addon_classes.map(&:new) self.file_watcher_addons = addons.select { |addon| addon.respond_to?(:workspace_did_change_watched_files) } diff --git a/test/addon_test.rb b/test/addon_test.rb index f3040c81d..66e737a93 100644 --- a/test/addon_test.rb +++ b/test/addon_test.rb @@ -131,5 +131,40 @@ def test_addons_receive_settings ensure T.must(outgoing_queue).close 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 + 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") + assert_equal("Project Addon", addon.name) + assert_predicate(T.unsafe(addon), :hello) + ensure + T.must(queue).close + end + end + end end end