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 authored and vinistock committed Nov 21, 2024
1 parent eb1a2ed commit f713fdf
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 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_checksums = 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,21 @@ def workspace_did_change_watched_files(changes)
path = URI(change[:uri]).to_standardized_path
next if path.end_with?("_test.rb", "_spec.rb")

case change[:type]
when Constant::FileChangeType::CREATED, Constant::FileChangeType::CHANGED
content = File.read(path)
current_checksum = Zlib.crc32(content).to_s

if change[:type] == Constant::FileChangeType::CHANGED && @file_checksums[path] == current_checksum
$stderr.puts "File has not changed. Skipping #{path}"
next
end

@file_checksums[path] = current_checksum
when Constant::FileChangeType::DELETED
@file_checksums.delete(path)
end

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

Expand Down

0 comments on commit f713fdf

Please sign in to comment.