Class: RubyLsp::RuboCop::Addon

Inherits:
Addon
  • Object
show all
Defined in:
lib/ruby_lsp/rubocop/addon.rb

Overview

A Ruby LSP add-on for RuboCop.

Instance Method Summary collapse

Instance Method Details

#activate(global_state, message_queue) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

#deactivateObject



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

def deactivate
  @wraps_built_in_lsp_runtime = nil
end

#initializerObject



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

def initializer
  @wraps_built_in_lsp_runtime = nil
end

#nameObject



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

def name
  'RuboCop'
end

#register_additional_file_watchers(global_state, message_queue) ⇒ Object

rubocop:disable Layout/LineLength, Metrics/MethodLength



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object

rubocop:enable Layout/LineLength, Metrics/MethodLength



67
68
69
70
71
72
73
74
75
# 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