Skip to content

Commit fdac2fb

Browse files
authored
Allow addons to register for filewatching events (#1464)
* Allow addons to register for filewatching events * Extend from Addon --------- Co-authored-by: Andy Waite <[email protected]>
1 parent d1da885 commit fdac2fb

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

lib/ruby_lsp/addon.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ class Addon
2727

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

3133
class << self
3234
extend T::Sig
3335

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

39+
sig { returns(T::Array[Addon]) }
40+
attr_accessor :file_watcher_addons
41+
3742
sig { returns(T::Array[T.class_of(Addon)]) }
3843
attr_reader :addon_classes
3944

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

5863
# Instantiate all discovered addon classes
5964
self.addons = addon_classes.map(&:new)
65+
self.file_watcher_addons = addons.select { |addon| addon.respond_to?(:workspace_did_change_watched_files) }
6066

6167
# Activate each one of the discovered addons. If any problems occur in the addons, we don't want to
6268
# fail to boot the server

lib/ruby_lsp/executor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def did_change_watched_files(changes)
229229
end
230230
end
231231

232+
Addon.file_watcher_addons.each { |addon| T.unsafe(addon).workspace_did_change_watched_files(changes) }
232233
VOID
233234
end
234235

test/addon_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,24 @@ def name
7676
Failed to activate
7777
MESSAGE
7878
end
79+
80+
def test_automatically_identifies_file_watcher_addons
81+
klass = Class.new(::RubyLsp::Addon) do
82+
def activate(message_queue); end
83+
def deactivate; end
84+
85+
def workspace_did_change_watched_files(changes); end
86+
end
87+
88+
begin
89+
queue = Thread::Queue.new
90+
Addon.load_addons(queue)
91+
assert_equal(1, Addon.file_watcher_addons.length)
92+
assert_instance_of(klass, Addon.file_watcher_addons.first)
93+
ensure
94+
T.must(queue).close
95+
Addon.file_watcher_addons.clear
96+
end
97+
end
7998
end
8099
end

0 commit comments

Comments
 (0)