Class: RubyLsp::Requests::Support::RuboCopFormatter

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Formatter
Defined in:
lib/ruby_lsp/requests/support/rubocop_formatter.rb

Instance Method Summary collapse

Constructor Details

#initializeRuboCopFormatter

Returns a new instance of RuboCopFormatter.



14
15
16
17
18
# File 'lib/ruby_lsp/requests/support/rubocop_formatter.rb', line 14

def initialize
  @diagnostic_runner = T.let(RuboCopRunner.new, RuboCopRunner)
  # -a is for "--auto-correct" (or "--autocorrect" on newer versions of RuboCop)
  @format_runner = T.let(RuboCopRunner.new("-a"), RuboCopRunner)
end

Instance Method Details

#run_diagnostic(uri, document) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_lsp/requests/support/rubocop_formatter.rb', line 35

def run_diagnostic(uri, document)
  filename = T.must(uri.to_standardized_path || uri.opaque)
  # Invoke RuboCop with just this file in `paths`
  @diagnostic_runner.run(filename, document.source)

  @diagnostic_runner.offenses.map do |offense|
    Support::RuboCopDiagnostic.new(
      document,
      offense,
      uri,
    ).to_lsp_diagnostic(@diagnostic_runner.config_for_working_directory)
  end
end

#run_formatting(uri, document) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ruby_lsp/requests/support/rubocop_formatter.rb', line 21

def run_formatting(uri, document)
  filename = T.must(uri.to_standardized_path || uri.opaque)

  # Invoke RuboCop with just this file in `paths`
  @format_runner.run(filename, document.source)
  @format_runner.formatted_source
end