Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow addons to register for filewatching events #1464

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/ruby_lsp/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ class Addon

@addons = T.let([], T::Array[Addon])
@addon_classes = T.let([], T::Array[T.class_of(Addon)])
# Addon instances that have declared a handler to accept file watcher events
@file_watcher_addons = T.let([], T::Array[Addon])

class << self
extend T::Sig

sig { returns(T::Array[Addon]) }
attr_accessor :addons

sig { returns(T::Array[Addon]) }
attr_accessor :file_watcher_addons

sig { returns(T::Array[T.class_of(Addon)]) }
attr_reader :addon_classes

Expand All @@ -57,6 +62,7 @@ def load_addons(message_queue)

# 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) }

# Activate each one of the discovered addons. If any problems occur in the addons, we don't want to
# fail to boot the server
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def did_change_watched_files(changes)
end
end

Addon.file_watcher_addons.each { |addon| T.unsafe(addon).workspace_did_change_watched_files(changes) }
VOID
end

Expand Down
19 changes: 19 additions & 0 deletions test/addon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,24 @@ def name
Failed to activate
MESSAGE
end

def test_automatically_identifies_file_watcher_addons
klass = Class.new(::RubyLsp::Addon) do
def activate(message_queue); end
def deactivate; end

def workspace_did_change_watched_files(changes); end
end

begin
queue = Thread::Queue.new
Addon.load_addons(queue)
assert_equal(1, Addon.file_watcher_addons.length)
assert_instance_of(klass, Addon.file_watcher_addons.first)
ensure
T.must(queue).close
Addon.file_watcher_addons.clear
end
end
end
end
Loading