Skip to content

Commit

Permalink
Allow declaration of project addons
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Oct 2, 2024
1 parent 3558809 commit bd83a91
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
4 changes: 4 additions & 0 deletions jekyll/add-ons.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ def initial_excluded_gems

excluded.uniq!
excluded.map(&:name)
rescue Bundler::GemfileNotFound
[]
end
end
end
21 changes: 15 additions & 6 deletions lib/ruby_lsp/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 addon_path.end_with?("lib/ruby_lsp/addon.rb")

require File.expand_path(addon_path)
nil
rescue => e
e
Expand Down Expand Up @@ -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

Expand Down
45 changes: 45 additions & 0 deletions test/addon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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

0 comments on commit bd83a91

Please sign in to comment.