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.

Instance Method Summary

Instance Method Details

#activate(global_state, message_queue)

[ GitHub ]

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

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

  ::RuboCop::LSP.enable
  @wraps_built_in_lsp_runtime = WrapsBuiltinLspRuntime.new

  global_state.register_formatter('rubocop', @wraps_built_in_lsp_runtime)

  register_additional_file_watchers(global_state, message_queue)

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

#deactivate

[ GitHub ]

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

def deactivate
  @wraps_built_in_lsp_runtime = nil
end

#initializer

[ GitHub ]

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

def initializer
  @wraps_built_in_lsp_runtime = nil
end

#name

[ GitHub ]

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

def name
  'RuboCop'
end

#register_additional_file_watchers(global_state, message_queue)

Metrics/MethodLength

[ GitHub ]

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

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: '**/.rubocop{,_todo}.yml',
                kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE
              )
            ]
          )
        )
      ]
    )
  )
end

#workspace_did_change_watched_files(changes)

Metrics/MethodLength

[ GitHub ]

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

def workspace_did_change_watched_files(changes)
  return unless changes.any? { |change| change[:uri].end_with?('.rubocop.yml') }

  @wraps_built_in_lsp_runtime.init!

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