From 5e51a3af6f7f17c8f7d4c57762789c037f127bcf Mon Sep 17 00:00:00 2001 From: Alex Rocha Date: Mon, 21 Oct 2024 14:43:03 -0700 Subject: [PATCH] Skip processing if the file hasn't changed By leveraging file checksums using `Zlib.crc32`, we can avoid triggering the DSL generation process for files that have not changed. --- lib/ruby_lsp/tapioca/addon.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/ruby_lsp/tapioca/addon.rb b/lib/ruby_lsp/tapioca/addon.rb index 720ec5e32..364b3fbcf 100644 --- a/lib/ruby_lsp/tapioca/addon.rb +++ b/lib/ruby_lsp/tapioca/addon.rb @@ -12,6 +12,8 @@ return end +require "zlib" + module RubyLsp module Tapioca class Addon < ::RubyLsp::Addon @@ -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 } @@ -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