Skip to content

Commit aedfc5d

Browse files
committed
Migrate Ruby LSP add-on to RBS comments
1 parent 1a7dc3c commit aedfc5d

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

lib/ruby_lsp/tapioca/addon.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
module RubyLsp
1919
module Tapioca
2020
class Addon < ::RubyLsp::Addon
21-
extend T::Sig
22-
2321
#: -> void
2422
def initialize
2523
super
@@ -44,7 +42,7 @@ def activate(global_state, outgoing_queue)
4442
# Get a handle to the Rails add-on's runtime client. The call to `rails_runner_client` will block this thread
4543
# until the server has finished booting, but it will not block the main LSP. This has to happen inside of a
4644
# thread
47-
addon = T.cast(::RubyLsp::Addon.get("Ruby LSP Rails", ">= 0.4.0", "< 0.5"), ::RubyLsp::Rails::Addon)
45+
addon = ::RubyLsp::Addon.get("Ruby LSP Rails", ">= 0.4.0", "< 0.5") #: as ::RubyLsp::Rails::Addon
4846
@rails_runner_client = addon.rails_runner_client
4947
@outgoing_queue << Notification.window_log_message("Activating Tapioca add-on v#{version}")
5048
@rails_runner_client.register_server_addon(File.expand_path("server_addon.rb", __dir__))
@@ -93,10 +91,11 @@ def workspace_did_change_watched_files(changes)
9391
has_route_change = false #: bool
9492
has_fixtures_change = false #: bool
9593
needs_compiler_reload = false #: bool
94+
index = @index #: as !nil
9695

9796
constants = changes.flat_map do |change|
98-
path = URI(change[:uri]).to_standardized_path
99-
next unless file_updated?(change, path)
97+
path = URI(change[:uri]).to_standardized_path #: String?
98+
next unless path && file_updated?(change, path)
10099

101100
if File.fnmatch("**/fixtures/**/*.yml{,.erb}", path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
102101
has_fixtures_change = true
@@ -115,7 +114,7 @@ def workspace_did_change_watched_files(changes)
115114
next
116115
end
117116

118-
entries = T.must(@index).entries_for(change[:uri])
117+
entries = index.entries_for(change[:uri])
119118
next unless entries
120119

121120
entries.filter_map do |entry|
@@ -178,14 +177,16 @@ def send_usage_telemetry(feature_name)
178177

179178
#: (Hash[Symbol, untyped] change, String path) -> bool
180179
def file_updated?(change, path)
180+
queue = @outgoing_queue #: as !nil
181+
181182
case change[:type]
182183
when Constant::FileChangeType::CREATED
183184
@file_checksums[path] = Zlib.crc32(File.read(path)).to_s
184185
return true
185186
when Constant::FileChangeType::CHANGED
186187
current_checksum = Zlib.crc32(File.read(path)).to_s
187188
if @file_checksums[path] == current_checksum
188-
T.must(@outgoing_queue) << Notification.window_log_message(
189+
queue << Notification.window_log_message(
189190
"File has not changed. Skipping #{path}",
190191
type: Constant::MessageType::INFO,
191192
)
@@ -196,7 +197,7 @@ def file_updated?(change, path)
196197
when Constant::FileChangeType::DELETED
197198
@file_checksums.delete(path)
198199
else
199-
T.must(@outgoing_queue) << Notification.window_log_message(
200+
queue << Notification.window_log_message(
200201
"Unexpected file change type: #{change[:type]}",
201202
type: Constant::MessageType::WARNING,
202203
)
@@ -207,16 +208,16 @@ def file_updated?(change, path)
207208

208209
#: -> void
209210
def run_gem_rbi_check
210-
gem_rbi_check = RunGemRbiCheck.new(T.must(@global_state).workspace_path)
211+
state = @global_state #: as !nil
212+
gem_rbi_check = RunGemRbiCheck.new(state.workspace_path)
211213
gem_rbi_check.run
212214

213-
T.must(@outgoing_queue) << Notification.window_log_message(
214-
gem_rbi_check.stdout,
215-
) unless gem_rbi_check.stdout.empty?
216-
T.must(@outgoing_queue) << Notification.window_log_message(
217-
gem_rbi_check.stderr,
218-
type: Constant::MessageType::WARNING,
219-
) unless gem_rbi_check.stderr.empty?
215+
queue = @outgoing_queue #: as !nil
216+
queue << Notification.window_log_message(gem_rbi_check.stdout) unless gem_rbi_check.stdout.empty?
217+
218+
unless gem_rbi_check.stderr.empty?
219+
queue << Notification.window_log_message(gem_rbi_check.stderr, type: Constant::MessageType::WARNING)
220+
end
220221
end
221222
end
222223
end

lib/ruby_lsp/tapioca/run_gem_rbi_check.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# typed: true
1+
# typed: strict
22
# frozen_string_literal: true
33

44
require "open3"
@@ -7,10 +7,10 @@
77
module RubyLsp
88
module Tapioca
99
class RunGemRbiCheck
10-
extend T::Sig
10+
#: String
11+
attr_reader :stdout, :stderr
1112

12-
attr_reader :stdout
13-
attr_reader :stderr
13+
#: Process::Status?
1414
attr_reader :status
1515

1616
#: (String project_path) -> void
@@ -34,11 +34,9 @@ def run
3434

3535
private
3636

37-
attr_reader :project_path
38-
3937
#: -> bool?
4038
def git_repo?
41-
_, status = Open3.capture2e("git", "rev-parse", "--is-inside-work-tree", chdir: project_path)
39+
_, status = Open3.capture2e("git", "rev-parse", "--is-inside-work-tree", chdir: @project_path)
4240
status.success?
4341
end
4442

@@ -49,7 +47,7 @@ def lockfile_changed?
4947

5048
#: -> Pathname
5149
def lockfile
52-
@lockfile ||= Pathname(project_path).join("Gemfile.lock") #: Pathname?
50+
@lockfile ||= Pathname(@project_path).join("Gemfile.lock") #: Pathname?
5351
end
5452

5553
#: -> String
@@ -81,15 +79,16 @@ def generate_gem_rbis
8179
#: (Array[String] gems) -> void
8280
def execute_tapioca_gem_command(gems)
8381
Bundler.with_unbundled_env do
84-
stdout, stderr, status = T.unsafe(Open3).capture3(
85-
"bundle",
86-
"exec",
87-
"tapioca",
88-
"gem",
89-
"--lsp_addon",
90-
*gems,
91-
chdir: project_path,
92-
)
82+
stdout, stderr, status = Open3 #: as untyped
83+
.capture3(
84+
"bundle",
85+
"exec",
86+
"tapioca",
87+
"gem",
88+
"--lsp_addon",
89+
*gems,
90+
chdir: @project_path,
91+
)
9392

9493
log_message(stdout) unless stdout.empty?
9594
@stderr = stderr unless stderr.empty?
@@ -101,7 +100,7 @@ def execute_tapioca_gem_command(gems)
101100
def remove_rbis(gems)
102101
files = Dir.glob(
103102
"sorbet/rbi/gems/{#{gems.join(",")}}@*.rbi",
104-
base: project_path,
103+
base: @project_path,
105104
)
106105
delete_files(files, "Removed RBIs for")
107106
end
@@ -117,16 +116,15 @@ def cleanup_orphaned_rbis
117116

118117
#: (*untyped flags) -> Array[String]
119118
def git_ls_gem_rbis(*flags)
120-
flags = T.unsafe(["git", "ls-files", *flags, "sorbet/rbi/gems/"])
121-
122-
execute_in_project_path(*flags)
119+
self #: as untyped # rubocop:disable Style/RedundantSelf
120+
.execute_in_project_path("git", "ls-files", *flags, "sorbet/rbi/gems/")
123121
.lines
124122
.map(&:strip)
125123
end
126124

127125
#: (Array[String] files, String message) -> void
128126
def delete_files(files, message)
129-
files_to_remove = files.map { |file| File.join(project_path, file) }
127+
files_to_remove = files.map { |file| File.join(@project_path, file) }
130128
FileUtils.rm(files_to_remove)
131129
log_message("#{message}: #{files.join(", ")}") unless files.empty?
132130
end
@@ -142,10 +140,12 @@ def log_message(message)
142140
@stdout += "#{message}\n"
143141
end
144142

143+
#: (*String, ?stdin: String?) -> String
145144
def execute_in_project_path(*parts, stdin: nil)
146-
options = { chdir: project_path }
145+
options = { chdir: @project_path }
147146
options[:stdin_data] = stdin if stdin
148-
stdout_and_stderr, _status = T.unsafe(Open3).capture2e(*parts, options)
147+
stdout_and_stderr, _status = Open3 #: as untyped
148+
.capture2e(*parts, options)
149149
stdout_and_stderr
150150
end
151151
end

spec/tapioca/addon_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# frozen_string_literal: true
33

44
require "spec_helper"
5-
require "language_server-protocol"
65
require "ruby_lsp/internal"
76
require "ruby_lsp/ruby_lsp_rails/addon"
87
require "ruby_lsp/tapioca/addon"

0 commit comments

Comments
 (0)