Skip to content

Commit

Permalink
Skip processing if the file hasn't changed
Browse files Browse the repository at this point in the history
By leveraging file checksums using `Zlib.crc32`, we can avoid triggering
the DSL generation process for files that have not changed.
  • Loading branch information
alexcrocha committed Oct 21, 2024
1 parent bdcca29 commit 68d7882
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
return
end

require "zlib"

module RubyLsp
module Tapioca
class Addon < ::RubyLsp::Addon
Expand All @@ -24,6 +26,7 @@ def initialize
@global_state = T.let(nil, T.nilable(RubyLsp::GlobalState))
@rails_runner_client = T.let(nil, T.nilable(RubyLsp::Rails::RunnerClient))
@index = T.let(nil, T.nilable(RubyIndexer::Index))
@file_hashes = T.let({}, T::Hash[String, String])
end

sig { override.params(global_state: RubyLsp::GlobalState, outgoing_queue: Thread::Queue).void }
Expand Down Expand Up @@ -71,6 +74,12 @@ def workspace_did_change_watched_files(changes)
path = URI(change[:uri]).to_standardized_path
next if path.end_with?("_test.rb", "_spec.rb")

content = File.read(path)
current_hash = Zlib.crc32(content)
next if @file_hashes[path] == current_hash # Skip processing if the file hasn't changed

@file_hashes[path] = current_hash

entries = T.must(@index).entries_for(path)
next unless entries

Expand Down

0 comments on commit 68d7882

Please sign in to comment.