diff --git a/lib/ruby_lsp/requests/support/rubocop_runner.rb b/lib/ruby_lsp/requests/support/rubocop_runner.rb index 1dc0041e9..de04df331 100644 --- a/lib/ruby_lsp/requests/support/rubocop_runner.rb +++ b/lib/ruby_lsp/requests/support/rubocop_runner.rb @@ -61,6 +61,12 @@ class ConfigurationError < StandardError; end "RuboCop::Formatter::BaseFormatter", # Suppress any output by using the base formatter ] #: Array[String] + CONFIG_FILES = [ + ".rubocop.yml", + ".rubocop", + ".rubocop_todo.yml", + ] #: Array[String] + #: Array[::RuboCop::Cop::Offense] attr_reader :offenses diff --git a/lib/ruby_lsp/server.rb b/lib/ruby_lsp/server.rb index d2267f309..540f188cc 100644 --- a/lib/ruby_lsp/server.rb +++ b/lib/ruby_lsp/server.rb @@ -310,7 +310,7 @@ def run_initialize(message) @current_request_id, Interface::RelativePattern.new( base_uri: @global_state.workspace_uri.to_s, - pattern: "{.rubocop.yml,.rubocop}", + pattern: "{#{Requests::Support::RuboCopRunner::CONFIG_FILES.join(",")}}", ), registration_id: "rubocop-watcher", )) @@ -1058,7 +1058,7 @@ def workspace_did_change_watched_files(message) file_name = File.basename(file_path) - if file_name == ".rubocop.yml" || file_name == ".rubocop" + if defined?(Requests::Support::RuboCopRunner) && Requests::Support::RuboCopRunner::CONFIG_FILES.include?(file_name) handle_rubocop_config_change(uri) end end diff --git a/test/server_test.rb b/test/server_test.rb index 7565f8b13..ccb0daeb9 100644 --- a/test/server_test.rb +++ b/test/server_test.rb @@ -1123,8 +1123,6 @@ def test_edits_outside_of_declarations_do_not_trigger_indexing end def test_rubocop_config_changes_trigger_workspace_diagnostic_refresh - uri = URI::Generic.from_path(path: File.join(Dir.pwd, ".rubocop.yml")) - @server.process_message({ id: 1, method: "initialize", @@ -1140,20 +1138,10 @@ def test_rubocop_config_changes_trigger_workspace_diagnostic_refresh }) @server.global_state.index.index_all(uris: []) - @server.process_message({ - method: "workspace/didChangeWatchedFiles", - params: { - changes: [ - { - uri: uri, - type: RubyLsp::Constant::FileChangeType::CHANGED, - }, - ], - }, - }) - request = find_message(RubyLsp::Request) - assert_equal("workspace/diagnostic/refresh", request.method) + RubyLsp::Requests::Support::RuboCopRunner::CONFIG_FILES.each do |config_file| + assert_rubocop_config_triggers_diagnostic_refresh_without_setup(config_file) + end end def test_compose_bundle_creates_file_to_skip_next_compose @@ -1712,6 +1700,46 @@ def deactivate; end end end + def assert_rubocop_config_triggers_diagnostic_refresh(config_file) + uri = URI::Generic.from_path(path: File.join(Dir.pwd, config_file)) + + @server.process_message({ + id: 1, + method: "initialize", + params: { + initializationOptions: {}, + capabilities: { + general: { + positionEncodings: ["utf-8"], + }, + workspace: { diagnostics: { refreshSupport: true } }, + }, + }, + }) + + @server.global_state.index.index_all(uris: []) + assert_rubocop_config_triggers_diagnostic_refresh_without_setup(config_file) + end + + def assert_rubocop_config_triggers_diagnostic_refresh_without_setup(config_file) + uri = URI::Generic.from_path(path: File.join(Dir.pwd, config_file)) + + @server.process_message({ + method: "workspace/didChangeWatchedFiles", + params: { + changes: [ + { + uri: uri, + type: RubyLsp::Constant::FileChangeType::CHANGED, + }, + ], + }, + }) + + request = find_message(RubyLsp::Request) + assert_equal("workspace/diagnostic/refresh", request.method) + end + #: (Class desired_class, ?String? desired_method, ?id: Integer?) -> untyped def find_message(desired_class, desired_method = nil, id: nil) message = @server.pop_response