Skip to content

Commit

Permalink
Refactor skip RBI regeneration after git op
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrocha committed Nov 9, 2024
1 parent 9e8f71f commit 898a5aa
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Addon < ::RubyLsp::Addon
extend T::Sig

GEMFILE_LOCK_SNAPSHOT = "tmp/tapioca/.gemfile_lock_snapshot"
GIT_OPERATION_THRESHOLD = 15.0 # seconds

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

git_reflog_output = %x(git reflog --date=iso | grep -E "checkout|pull" | head -n 1).strip
return if git_reflog_output.empty?

timestamp_string = T.must(git_reflog_output.match(/\{(.*)\}/))[1]
last_git_operation_time = Time.iso8601(T.must(timestamp_string).sub(" ", "T").delete(" "))

return if last_git_operation_time.nil?
git_timestamp = fetch_last_git_operation_time
return unless git_timestamp

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

$stderr.puts("ruby_lsp_stop_time: #{ruby_lsp_stop_time}") # TODO: Remove
$stderr.puts("last_git_operation_time: #{last_git_operation_time}") # TODO: Remove
$stderr.puts("git_timestamp: #{git_timestamp}") # TODO: Remove

# If the Ruby LSP was stopped shortly after the last git checkout/pull operation, we don't need to regenerate
# RBIs since any Gemfile.lock changes were likely from version control, not from running bundle install
return if (ruby_lsp_stop_time - last_git_operation_time) <= 15.0
return if (ruby_lsp_stop_time - git_timestamp) <= GIT_OPERATION_THRESHOLD

process_gemfile_changes
end

sig { returns(T.nilable(Time)) }
def fetch_last_git_operation_time
git_reflog_output = %x(git reflog --date=iso | grep -E "checkout|pull" | head -n 1).strip
return if git_reflog_output.empty?

timestamp_string = T.must(git_reflog_output.match(/\{(.*)\}/))[1]
Time.iso8601(T.must(timestamp_string).sub(" ", "T").delete(" "))
end

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

Expand Down

0 comments on commit 898a5aa

Please sign in to comment.