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 Sep 23, 2024
1 parent a29808c commit e6aae43
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/ruby_lsp/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ def load_addons(global_state, outgoing_queue)
e
end

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

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

0 comments on commit e6aae43

Please sign in to comment.