Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tapioca Add-on] Optimize File Save Handling #2052

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a little noisy to show every time, but we can keep it for now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Let's monitor it and I'll remove if it's too much.

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
Loading