Skip to content

Commit

Permalink
[Tapioca Add-on] Trigger DSL generation upon file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
KaanOzkan committed Oct 3, 2024
1 parent f567a16 commit eef6cb2
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class Addon < ::RubyLsp::Addon
def initialize
super

@global_state = T.let(nil, T.nilable(RubyLsp::GlobalState))
@rails_runner_client = T.let(nil, T.nilable(RubyLsp::Rails::RunnerClient))
@index = T.let(nil, T.nilable(RubyIndexer::Index))
end

sig { override.params(global_state: RubyLsp::GlobalState, outgoing_queue: Thread::Queue).void }
def activate(global_state, outgoing_queue)
@global_state = global_state
return unless @global_state.experimental_features
return unless global_state.experimental_features

@index = global_state.index
Thread.new do
# Get a handle to the Rails add-on's runtime client. The call to `rails_runner_client` will block this thread
# until the server has finished booting, but it will not block the main LSP. This has to happen inside of a
Expand Down Expand Up @@ -60,6 +60,22 @@ def version

sig { params(changes: T::Array[{ uri: String, type: Integer }]).void }
def workspace_did_change_watched_files(changes)
constants = changes.filter_map do |change|
path = change[:uri].gsub("file://", "")

entries = T.must(@index).entries_for(path, RubyIndexer::Entry::Class, RubyIndexer::Entry::Module)
next unless entries
end.flatten

return if constants.empty?

T.must(@rails_runner_client).send_notification("reload", {})
T.must(@rails_runner_client).send_notification(
"server_addon/delegate",
request_name: "dsl",
server_addon_name: "Tapioca",
constants: constants,
)
end
end
end
Expand Down

0 comments on commit eef6cb2

Please sign in to comment.