Skip to content

Commit 96e0712

Browse files
authored
Migrate to RBS in comments syntax (#600)
1 parent 097ad0d commit 96e0712

File tree

16 files changed

+285
-222
lines changed

16 files changed

+285
-222
lines changed

lib/ruby_lsp/ruby_lsp_rails/addon.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,25 @@ def initialize
2828

2929
# We first initialize the client as a NullClient, so that we can start the server in a background thread. Until
3030
# the real client is initialized, features that depend on it will not be blocked by using the NullClient
31-
@rails_runner_client = T.let(NullClient.new, RunnerClient)
32-
@global_state = T.let(nil, T.nilable(GlobalState))
33-
@outgoing_queue = T.let(nil, T.nilable(Thread::Queue))
34-
@settings = T.let(
35-
{
36-
enablePendingMigrationsPrompt: true,
37-
},
38-
T::Hash[Symbol, T.untyped],
39-
)
40-
@addon_mutex = T.let(Mutex.new, Mutex)
41-
@client_mutex = T.let(Mutex.new, Mutex)
31+
@rails_runner_client = NullClient.new #: RunnerClient
32+
@global_state = nil #: GlobalState?
33+
@outgoing_queue = nil #: Thread::Queue?
34+
@settings = {
35+
enablePendingMigrationsPrompt: true,
36+
} #: Hash[Symbol, untyped],
37+
38+
@addon_mutex = Mutex.new #: Mutex
39+
@client_mutex = Mutex.new #: Mutex
4240
@client_mutex.lock
4341

4442
Thread.new do
4543
@addon_mutex.synchronize do
4644
# We need to ensure the Rails client is fully loaded before we activate the server addons
4745
@client_mutex.synchronize do
48-
@rails_runner_client = RunnerClient.create_client(T.must(@outgoing_queue), T.must(@global_state))
46+
@rails_runner_client = RunnerClient.create_client(
47+
@outgoing_queue, #: as !nil
48+
@global_state, #: as !nil
49+
)
4950
end
5051
offer_to_run_pending_migrations
5152
end

lib/ruby_lsp/ruby_lsp_rails/code_lens.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ def initialize(client, global_state, response_builder, uri, dispatcher)
8080
@client = client
8181
@global_state = global_state
8282
@response_builder = response_builder
83-
@path = T.let(uri.to_standardized_path, T.nilable(String))
84-
@group_id = T.let(1, Integer)
85-
@group_id_stack = T.let([], T::Array[Integer])
86-
@constant_name_stack = T.let([], T::Array[[String, T.nilable(String)]])
83+
@path = uri.to_standardized_path #: String?
84+
@group_id = 1 #: Integer
85+
@group_id_stack = [] #: Array[Integer]
86+
@constant_name_stack = [] #: Array[[String, String?]]
8787

8888
dispatcher.register(
8989
self,
@@ -209,7 +209,7 @@ def add_jump_to_view(node)
209209

210210
#: (Prism::DefNode node) -> void
211211
def add_route_code_lens_to_action(node)
212-
class_name, _ = T.must(@constant_name_stack.last)
212+
class_name, _ = @constant_name_stack.last #: as !nil
213213
route = @client.route(controller: class_name, action: node.name.to_s)
214214
return unless route
215215

@@ -236,7 +236,9 @@ def migrate_command
236236

237237
#: -> String?
238238
def migration_version
239-
File.basename(T.must(@path)).split("_").first
239+
File.basename(
240+
@path, #: as !nil
241+
).split("_").first
240242
end
241243

242244
#: (Prism::Node node, name: String, command: String) -> void

lib/ruby_lsp/ruby_lsp_rails/completion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def handle_active_record_where_completions(node:, receiver:)
3737
return if resolved_class.nil?
3838

3939
arguments = @node_context.call_node.arguments&.arguments
40-
indexed_call_node_args = T.let({}, T::Hash[String, Prism::Node])
40+
indexed_call_node_args = {} #: Hash[String, Prism::Node]
4141

4242
if arguments
4343
indexed_call_node_args = index_call_node_args(arguments: arguments)

lib/ruby_lsp/ruby_lsp_rails/definition.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def initialize(client, response_builder, node_context, index, dispatcher)
3535
@client = client
3636
@response_builder = response_builder
3737
@node_context = node_context
38-
@nesting = T.let(node_context.nesting, T::Array[String])
38+
@nesting = node_context.nesting #: Array[String]
3939
@index = index
4040

4141
dispatcher.register(self, :on_call_node_enter, :on_symbol_node_enter, :on_string_node_enter)
@@ -121,7 +121,9 @@ def handle_association(node)
121121

122122
#: (Prism::CallNode node) -> void
123123
def handle_route(node)
124-
result = @client.route_location(T.must(node.message))
124+
result = @client.route_location(
125+
node.message, #: as !nil
126+
)
125127
return unless result
126128

127129
@response_builder << Support::LocationBuilder.line_location_from_s(result.fetch(:location))

lib/ruby_lsp/ruby_lsp_rails/document_symbol.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class DocumentSymbol
1515
#: (ResponseBuilders::DocumentSymbol response_builder, Prism::Dispatcher dispatcher) -> void
1616
def initialize(response_builder, dispatcher)
1717
@response_builder = response_builder
18-
@namespace_stack = T.let([], T::Array[String])
18+
@namespace_stack = [] #: Array[String]
1919

2020
dispatcher.register(
2121
self,
@@ -45,14 +45,16 @@ def on_call_node_enter(node)
4545
return if receiver && !receiver.is_a?(Prism::SelfNode)
4646

4747
message = node.message
48+
return unless message
49+
4850
case message
4951
when *Support::Callbacks::ALL, "validate"
50-
handle_all_arg_types(node, T.must(message))
52+
handle_all_arg_types(node, message)
5153
when "validates", "validates!", "validates_each", "belongs_to", "has_one", "has_many",
5254
"has_and_belongs_to_many", "attr_readonly", "scope"
53-
handle_symbol_and_string_arg_types(node, T.must(message))
55+
handle_symbol_and_string_arg_types(node, message)
5456
when "validates_with"
55-
handle_class_arg_types(node, T.must(message))
57+
handle_class_arg_types(node, message)
5658
end
5759
end
5860

@@ -113,7 +115,9 @@ def handle_all_arg_types(node, message)
113115
append_document_symbol(
114116
name: "#{message} :#{name}",
115117
range: range_from_location(argument.location),
116-
selection_range: range_from_location(T.must(argument.value_loc)),
118+
selection_range: range_from_location(
119+
argument.value_loc, #: as !nil
120+
),
117121
)
118122
when Prism::StringNode
119123
name = argument.content
@@ -172,7 +176,9 @@ def handle_symbol_and_string_arg_types(node, message)
172176
append_document_symbol(
173177
name: "#{message} :#{name}",
174178
range: range_from_location(argument.location),
175-
selection_range: range_from_location(T.must(argument.value_loc)),
179+
selection_range: range_from_location(
180+
argument.value_loc, #: as !nil
181+
),
176182
)
177183
when Prism::StringNode
178184
name = argument.content

lib/ruby_lsp/ruby_lsp_rails/hover.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class Hover
2121
def initialize(client, response_builder, node_context, global_state, dispatcher)
2222
@client = client
2323
@response_builder = response_builder
24-
@nesting = T.let(node_context.nesting, T::Array[String])
25-
@index = T.let(global_state.index, RubyIndexer::Index)
24+
@nesting = node_context.nesting #: Array[String]
25+
@index = global_state.index #: RubyIndexer::Index
2626
dispatcher.register(self, :on_constant_path_node_enter, :on_constant_read_node_enter)
2727
end
2828

lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def resolve_test_commands(items)
1515
full_files = []
1616

1717
until queue.empty?
18-
item = T.must(queue.shift)
18+
item = queue.shift #: as !nil
1919
tags = Set.new(item[:tags])
2020
next unless tags.include?("framework:rails")
2121

lib/ruby_lsp/ruby_lsp_rails/runner_client.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class EmptyMessageError < MessageError; end
4949

5050
#: (Thread::Queue outgoing_queue, RubyLsp::GlobalState global_state) -> void
5151
def initialize(outgoing_queue, global_state)
52-
@outgoing_queue = T.let(outgoing_queue, Thread::Queue)
53-
@mutex = T.let(Mutex.new, Mutex)
52+
@outgoing_queue = outgoing_queue #: Thread::Queue
53+
@mutex = Mutex.new #: Mutex
5454
# Spring needs a Process session ID. It uses this ID to "attach" itself to the parent process, so that when the
5555
# parent ends, the spring process ends as well. If this is not set, Spring will throw an error while trying to
5656
# set its own session ID
@@ -78,21 +78,21 @@ def initialize(outgoing_queue, global_state)
7878
)
7979
end
8080

81-
@stdin = T.let(stdin, IO)
82-
@stdout = T.let(stdout, IO)
83-
@stderr = T.let(stderr, IO)
81+
@stdin = stdin #: IO
82+
@stdout = stdout #: IO
83+
@stderr = stderr #: IO
8484
@stdin.sync = true
8585
@stdout.sync = true
8686
@stderr.sync = true
87-
@wait_thread = T.let(wait_thread, Process::Waiter)
87+
@wait_thread = wait_thread #: Process::Waiter
8888

8989
# We set binmode for Windows compatibility
9090
@stdin.binmode
9191
@stdout.binmode
9292
@stderr.binmode
9393

94-
initialize_response = T.must(read_response)
95-
@rails_root = T.let(initialize_response[:root], String)
94+
initialize_response = read_response #: as !nil
95+
@rails_root = initialize_response[:root] #: String
9696
log_message("Finished booting Ruby LSP Rails server")
9797

9898
unless ENV["RAILS_ENV"] == "test"
@@ -106,20 +106,17 @@ def initialize(outgoing_queue, global_state)
106106

107107
# Responsible for transmitting notifications coming from the server to the outgoing queue, so that we can do
108108
# things such as showing progress notifications initiated by the server
109-
@notifier_thread = T.let(
110-
Thread.new do
111-
until @stderr.closed?
112-
notification = read_notification
113-
114-
unless @outgoing_queue.closed? || !notification
115-
@outgoing_queue << notification
116-
end
109+
@notifier_thread = Thread.new do
110+
until @stderr.closed?
111+
notification = read_notification
112+
113+
unless @outgoing_queue.closed? || !notification
114+
@outgoing_queue << notification
117115
end
118-
rescue IOError
119-
# The server was shutdown and stderr is already closed
120-
end,
121-
Thread,
122-
)
116+
end
117+
rescue IOError
118+
# The server was shutdown and stderr is already closed
119+
end #: Thread
123120
rescue StandardError
124121
raise InitializationError, @stderr.read
125122
end
@@ -298,9 +295,9 @@ def read_response
298295
raise EmptyMessageError unless content_length
299296

300297
@stdout.read(content_length)
301-
end
298+
end #: as !nil
302299

303-
response = JSON.parse(T.must(raw_response), symbolize_names: true)
300+
response = JSON.parse(raw_response, symbolize_names: true)
304301

305302
if response[:error]
306303
log_message(
@@ -319,7 +316,10 @@ def read_response
319316
#: -> void
320317
def force_kill
321318
# Windows does not support the `TERM` signal, so we're forced to use `KILL` here
322-
Process.kill(T.must(Signal.list["KILL"]), @wait_thread.pid)
319+
Process.kill(
320+
Signal.list["KILL"], #: as !nil
321+
@wait_thread.pid,
322+
)
323323
end
324324

325325
#: (::String message, ?type: ::Integer) -> void

0 commit comments

Comments
 (0)