Skip to content

Commit

Permalink
Reload routes when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Oct 31, 2024
1 parent b5f4791 commit 003e017
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/ruby_lsp/ruby_lsp_rails/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def workspace_did_change_watched_files(changes)
change[:uri].end_with?("db/schema.rb") || change[:uri].end_with?("structure.sql")
end
@rails_runner_client.trigger_reload
elsif changes.any? do |change|
change[:uri].end_with?("routes.rb") # TODO: handle other patterns
end
@rails_runner_client.reload_routes
end
end

Expand All @@ -141,7 +145,7 @@ def register_additional_file_watchers(global_state:, outgoing_queue:)
register_options: Interface::DidChangeWatchedFilesRegistrationOptions.new(
watchers: [
Interface::FileSystemWatcher.new(
glob_pattern: "**/*structure.sql",
glob_pattern: "**/*structure.sql,**/*routes.rb",
kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE,
),
],
Expand Down
12 changes: 12 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/runner_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ def trigger_reload
nil
end

sig { void }
def reload_routes
log_message("Reloading Rails routes")
send_notification("reload_routes")
rescue IncompleteMessageError
log_message(
"Ruby LSP Rails failed to reload routes",
type: RubyLsp::Constant::MessageType::ERROR,
)
nil
end

sig { void }
def shutdown
log_message("Ruby LSP Rails shutting down server")
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def execute(request, params)
send_message(resolve_association_target(params))
when "reload"
::Rails.application.reloader.reload!
when "reload_routes"
::Rails.application.routes_reloader.execute
when "route_location"
send_message(route_location(params.fetch(:name)))
when "route_info"
Expand Down

0 comments on commit 003e017

Please sign in to comment.