Skip to content

Commit 7e68f5c

Browse files
alexcrochaKaanOzkan
authored andcommitted
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.
1 parent 9b1714c commit 7e68f5c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/ruby_lsp/tapioca/addon.rb

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
return
1313
end
1414

15+
require "zlib"
16+
1517
module RubyLsp
1618
module Tapioca
1719
class Addon < ::RubyLsp::Addon
@@ -24,6 +26,7 @@ def initialize
2426
@global_state = T.let(nil, T.nilable(RubyLsp::GlobalState))
2527
@rails_runner_client = T.let(nil, T.nilable(RubyLsp::Rails::RunnerClient))
2628
@index = T.let(nil, T.nilable(RubyIndexer::Index))
29+
@file_checksums = T.let({}, T::Hash[String, String])
2730
end
2831

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

77+
case change[:type]
78+
when Constant::FileChangeType::CREATED, Constant::FileChangeType::CHANGED
79+
content = File.read(path)
80+
current_checksum = Zlib.crc32(content).to_s
81+
82+
if change[:type] == Constant::FileChangeType::CHANGED && @file_checksums[path] == current_checksum
83+
$stderr.puts "File has not changed. Skipping #{path}"
84+
next
85+
end
86+
87+
@file_checksums[path] = current_checksum
88+
when Constant::FileChangeType::DELETED
89+
@file_checksums.delete(path)
90+
end
91+
7492
entries = T.must(@index).entries_for(path)
7593
next unless entries
7694

0 commit comments

Comments
 (0)