Class: RuboCop::Lsp::Runtime Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/lsp/runtime.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Runtime for Language Server Protocol of RuboCop.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_store) ⇒ Runtime

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Runtime.



19
20
21
22
23
24
25
# File 'lib/rubocop/lsp/runtime.rb', line 19

def initialize(config_store)
  @config_store = config_store
  @logged_paths = []
  @safe_autocorrect = true
  @lint_mode = false
  @layout_mode = false
end

Instance Attribute Details

#layout_mode=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
# File 'lib/rubocop/lsp/runtime.rb', line 17

def layout_mode=(value)
  @layout_mode = value
end

#lint_mode=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
# File 'lib/rubocop/lsp/runtime.rb', line 17

def lint_mode=(value)
  @lint_mode = value
end

#safe_autocorrect=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
# File 'lib/rubocop/lsp/runtime.rb', line 17

def safe_autocorrect=(value)
  @safe_autocorrect = value
end

Instance Method Details

#format(path, text, command:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This abuses the --stdin option of rubocop and reads the formatted text from the options[:stdin] that rubocop mutates. This depends on parallel: false as well as the fact that RuboCop doesn’t otherwise dup or reassign that options object. Risky business!



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/lsp/runtime.rb', line 38

def format(path, text, command:)
  safe_autocorrect = if command
                       command == 'rubocop.formatAutocorrects'
                     else
                       @safe_autocorrect
                     end

  formatting_options = {
    stdin: text, force_exclusion: true, autocorrect: true, safe_autocorrect: safe_autocorrect
  }
  formatting_options[:only] = config_only_options if @lint_mode || @layout_mode

  redirect_stdout { run_rubocop(formatting_options, path) }

  formatting_options[:stdin]
end

#offenses(path, text) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubocop/lsp/runtime.rb', line 55

def offenses(path, text)
  diagnostic_options = {
    stdin: text, force_exclusion: true, formatters: ['json'], format: 'json'
  }
  diagnostic_options[:only] = config_only_options if @lint_mode || @layout_mode

  json = redirect_stdout { run_rubocop(diagnostic_options, path) }
  results = JSON.parse(json, symbolize_names: true)

  if results[:files].empty?
    unless @logged_paths.include?(path)
      Logger.log "Ignoring file, per configuration: #{path}"
      @logged_paths << path
    end
    return []
  end

  results.dig(:files, 0, :offenses)
end