Skip to content

Commit ee951f7

Browse files
committed
Add reloading for schema changes
1 parent efb3e77 commit ee951f7

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

lib/ruby_lsp/ruby_lsp_rails/addon.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ def create_document_symbol_listener(response_builder, dispatcher)
6868
DocumentSymbol.new(response_builder, dispatcher)
6969
end
7070

71+
sig { params(changes: T::Array[{ uri: String, type: Integer }]).void }
72+
def workspace_did_change_watched_files(changes)
73+
if changes.any? do |change|
74+
change[:uri].end_with?("db/schema.rb") || change[:uri].end_with?("structure.sql")
75+
end
76+
@client.trigger_reload
77+
end
78+
end
79+
7180
sig { override.returns(String) }
7281
def name
7382
"Ruby LSP Rails"

lib/ruby_lsp/ruby_lsp_rails/runner_client.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ def model(name)
7575
nil
7676
end
7777

78+
sig { void }
79+
def trigger_reload
80+
warn("Reloading Rails application...")
81+
send_notification("reload")
82+
warn("Reloading Rails application complete")
83+
rescue IncompleteMessageError
84+
warn("Ruby LSP Rails failed to get model information: #{@stderr.read}")
85+
nil
86+
end
87+
7888
sig { void }
7989
def shutdown
8090
warn("Ruby LSP Rails shutting down server")

lib/ruby_lsp/ruby_lsp_rails/server.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def execute(request, params = {})
6767
VOID
6868
when "model"
6969
resolve_database_info_from_model(params.fetch(:name))
70+
when "reload"
71+
::Rails.application.reloader.reload!
72+
VOID
7073
else
7174
VOID
7275
end

test/ruby_lsp_rails/addon_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,45 @@ class AddonTest < ActiveSupport::TestCase
1010
addon = Addon.new
1111
assert_equal("Ruby LSP Rails", addon.name)
1212
end
13+
14+
test "sends reload notification if db/schema.rb is changed" do
15+
changes = [
16+
{
17+
uri: "file://#{dummy_root}/db/schema.rb",
18+
type: RubyLsp::Constant::FileChangeType::CHANGED,
19+
},
20+
]
21+
22+
RunnerClient.any_instance.expects(:send_notification).with("reload").once
23+
addon = Addon.new
24+
addon.workspace_did_change_watched_files(changes)
25+
end
26+
27+
test "sends reload notification if a *structure.sql file is changed" do
28+
changes = [
29+
{
30+
uri: "file://#{dummy_root}/db/structure.sql",
31+
type: RubyLsp::Constant::FileChangeType::CHANGED,
32+
},
33+
]
34+
35+
RunnerClient.any_instance.expects(:send_notification).with("reload").once
36+
addon = Addon.new
37+
addon.workspace_did_change_watched_files(changes)
38+
end
39+
40+
test "does not send reload notification if schema is not changed" do
41+
changes = [
42+
{
43+
uri: "file://#{dummy_root}/app/models/foo.rb",
44+
type: RubyLsp::Constant::FileChangeType::CHANGED,
45+
},
46+
]
47+
48+
RunnerClient.any_instance.expects(:send_notification).never
49+
addon = Addon.new
50+
addon.workspace_did_change_watched_files(changes)
51+
end
1352
end
1453
end
1554
end

test/ruby_lsp_rails/hover_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,6 @@ def hover_on_source(source, position)
215215
assert_nil(response.error)
216216
response.response
217217
end
218-
219-
def dummy_root
220-
File.expand_path("#{__dir__}/../../test/dummy")
221-
end
222218
end
223219
end
224220
end

test/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@
1717
DEBUGGER__::CONFIG[:skip_path] =
1818
Array(DEBUGGER__::CONFIG[:skip_path]) + Gem.loaded_specs["sorbet-runtime"].full_require_paths
1919
end
20+
21+
module ActiveSupport
22+
class TestCase
23+
def dummy_root
24+
File.expand_path("#{__dir__}/dummy")
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)