123456789_123456789_123456789_123456789_123456789_

Class: RubyLsp::RuboCop::Addon

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, RubyLsp::Addon
Instance Chain:
self, RubyLsp::Addon
Inherits: RubyLsp::Addon
  • Object
Defined in: lib/ruby_lsp/rubocop/addon.rb

Overview

A Ruby LSP add-on for RuboCop.

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.newAddon

[ GitHub ]

  
# File 'lib/ruby_lsp/rubocop/addon.rb', line 13

def initialize
  super
  @runtime_adapter = nil
end

Instance Method Details

#activate(global_state, message_queue)

[ GitHub ]

  
# File 'lib/ruby_lsp/rubocop/addon.rb', line 26

def activate(global_state, message_queue)
  ::RuboCop::LSP::Logger.log(
    "Activating RuboCop LSP addon #{::RuboCop::Version::STRING}.", prefix: '[RuboCop]'
  )

  @runtime_adapter = RuntimeAdapter.new(message_queue)
  global_state.register_formatter('rubocop', @runtime_adapter)
  register_additional_file_watchers(global_state, message_queue)

  ::RuboCop::LSP::Logger.log(
    "Initialized RuboCop LSP addon #{::RuboCop::Version::STRING}.", prefix: '[RuboCop]'
  )
end

#changed_config_file(changes) (private)

[ GitHub ]

  
# File 'lib/ruby_lsp/rubocop/addon.rb', line 83

def changed_config_file(changes)
  RESTART_WATCHERS.find do |file_name|
    changes.any? { |change| change[:uri].end_with?(file_name) }
  end
end

#deactivate

[ GitHub ]

  
# File 'lib/ruby_lsp/rubocop/addon.rb', line 40

def deactivate
  @runtime_adapter = nil
end

#name

[ GitHub ]

  
# File 'lib/ruby_lsp/rubocop/addon.rb', line 18

def name
  'RuboCop'
end

#register_additional_file_watchers(global_state, message_queue)

[ GitHub ]

  
# File 'lib/ruby_lsp/rubocop/addon.rb', line 45

def register_additional_file_watchers(global_state, message_queue)
  return unless global_state.supports_watching_files

  message_queue << Request.new(
    id: 'rubocop-file-watcher',
    method: 'client/registerCapability',
    params: Interface::RegistrationParams.new(
      registrations: [
        Interface::Registration.new(
          id: 'workspace/didChangeWatchedFilesRuboCop',
          method: 'workspace/didChangeWatchedFiles',
          register_options: Interface::DidChangeWatchedFilesRegistrationOptions.new(
            watchers: [
              Interface::FileSystemWatcher.new(
                glob_pattern: "**/{#{RESTART_WATCHERS.join(',')}}",
                kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE
              )
            ]
          )
        )
      ]
    )
  )
end

#version

[ GitHub ]

  
# File 'lib/ruby_lsp/rubocop/addon.rb', line 22

def version
  ::RuboCop::Version::STRING
end

#workspace_did_change_watched_files(changes)

[ GitHub ]

  
# File 'lib/ruby_lsp/rubocop/addon.rb', line 71

def workspace_did_change_watched_files(changes)
  if (changed_config_file = changed_config_file(changes))
    @runtime_adapter.reload_config

    ::RuboCop::LSP::Logger.log(<<~MESSAGE, prefix: '[RuboCop]')
      Re-initialized RuboCop LSP addon #{::RuboCop::Version::STRING} due to #{changed_config_file} change.
    MESSAGE
  end
end