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 ff848d9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/ruby_lsp/ruby_lsp_rails/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def workspace_did_change_watched_files(changes)
end
@rails_runner_client.trigger_reload
end
if changes.any? do |change|
change[:uri].end_with?("routes.rb")
end
@rails_runner_client.reload_routes
end
end

sig { params(global_state: GlobalState, outgoing_queue: Thread::Queue).void }
Expand All @@ -141,7 +146,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 ff848d9

Please sign in to comment.