From 9e9181392498c9cd59aa53d2a19f2313eac6bc0b Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:32:03 +0200 Subject: [PATCH] Ignore non-ruby files in file watcher (#1912) Currently there is only a listener for ruby files but that might changein the future. Addons are also able to register additional file watcher events, config files as an example --- lib/ruby_lsp/server.rb | 1 + test/server_test.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/ruby_lsp/server.rb b/lib/ruby_lsp/server.rb index b09341381..66c136efa 100644 --- a/lib/ruby_lsp/server.rb +++ b/lib/ruby_lsp/server.rb @@ -610,6 +610,7 @@ def workspace_did_change_watched_files(message) uri = URI(change[:uri]) file_path = uri.to_standardized_path next if file_path.nil? || File.directory?(file_path) + next unless file_path.end_with?(".rb") load_path_entry = $LOAD_PATH.find { |load_path| file_path.start_with?(load_path) } indexable = RubyIndexer::IndexablePath.new(load_path_entry, file_path) diff --git a/test/server_test.rb b/test/server_test.rb index 152ca8db6..278f95e99 100644 --- a/test/server_test.rb +++ b/test/server_test.rb @@ -474,6 +474,27 @@ def test_backtrace_is_printed_to_stderr_on_exceptions assert_match(%r{ruby-lsp/lib/ruby_lsp/server\.rb:\d+:in `process_message'}, stderr) end + def test_changed_file_only_indexes_ruby + @server.global_state.index.expects(:index_single).once.with do |indexable| + indexable.full_path == "/foo.rb" + end + @server.process_message({ + method: "workspace/didChangeWatchedFiles", + params: { + changes: [ + { + uri: URI("file:///foo.rb"), + type: RubyLsp::Constant::FileChangeType::CREATED, + }, + { + uri: URI("file:///.rubocop.yml"), + type: RubyLsp::Constant::FileChangeType::CREATED, + }, + ], + }, + }) + end + private def with_uninstalled_rubocop(&block)