Skip to content

Commit 8cfe769

Browse files
committed
Refactor skip RBI regeneration after git op
1 parent 0f5138c commit 8cfe769

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

lib/ruby_lsp/tapioca/addon.rb

+19-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Addon < ::RubyLsp::Addon
2020
extend T::Sig
2121

2222
GEMFILE_LOCK_SNAPSHOT = "tmp/tapioca/.gemfile_lock_snapshot"
23+
GIT_OPERATION_THRESHOLD = 15.0 # seconds
2324

2425
sig { void }
2526
def initialize
@@ -117,23 +118,32 @@ def workspace_did_change_watched_files(changes)
117118
def handle_gemfile_changes
118119
return unless File.exist?(".git") && File.exist?(".ruby-lsp/shutdown-timestamp")
119120

120-
git_reflog_output = %x(git reflog --date=iso | grep -E "checkout|pull" | head -n 1).strip
121-
return if git_reflog_output.empty?
122-
123-
timestamp_string = T.must(git_reflog_output.match(/\{(.*)\}/))[1]
124-
last_git_operation_time = Time.iso8601(T.must(timestamp_string).sub(" ", "T").delete(" "))
125-
126-
return if last_git_operation_time.nil?
121+
git_timestamp = fetch_last_git_operation_time
122+
return unless git_timestamp
127123

128124
ruby_lsp_stop_time = Time.iso8601(File.read(".ruby-lsp/shutdown-timestamp"))
129125

130126
$stderr.puts("ruby_lsp_stop_time: #{ruby_lsp_stop_time}") # TODO: Remove
131-
$stderr.puts("last_git_operation_time: #{last_git_operation_time}") # TODO: Remove
127+
$stderr.puts("git_timestamp: #{git_timestamp}") # TODO: Remove
132128

133129
# If the Ruby LSP was stopped shortly after the last git checkout/pull operation, we don't need to regenerate
134130
# RBIs since any Gemfile.lock changes were likely from version control, not from running bundle install
135-
return if (ruby_lsp_stop_time - last_git_operation_time) <= 15.0
131+
return if (ruby_lsp_stop_time - git_timestamp) <= GIT_OPERATION_THRESHOLD
132+
133+
process_gemfile_changes
134+
end
135+
136+
sig { returns(T.nilable(Time)) }
137+
def fetch_last_git_operation_time
138+
git_reflog_output = %x(git reflog --date=iso | grep -E "checkout|pull" | head -n 1).strip
139+
return if git_reflog_output.empty?
140+
141+
timestamp_string = T.must(git_reflog_output.match(/\{(.*)\}/))[1]
142+
Time.iso8601(T.must(timestamp_string).sub(" ", "T").delete(" "))
143+
end
136144

145+
sig { returns(T.nilable(Integer)) }
146+
def process_gemfile_changes
137147
current_lockfile = File.read("Gemfile.lock")
138148
snapshot_lockfile = File.read(GEMFILE_LOCK_SNAPSHOT) if File.exist?(GEMFILE_LOCK_SNAPSHOT)
139149

0 commit comments

Comments
 (0)