Skip to content

Commit

Permalink
Allow addons to register for filewatching events (#1464)
Browse files Browse the repository at this point in the history
* Allow addons to register for filewatching events

* Extend from Addon

---------

Co-authored-by: Andy Waite <andyw8@users.noreply.github.com>
  • Loading branch information
andyw8 and andyw8 authored Mar 6, 2024
1 parent d1da885 commit fdac2fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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

0 comments on commit fdac2fb

Please sign in to comment.