Class: RubyLsp::Requests::Support::RuboCopDiagnostic

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lsp/requests/support/rubocop_diagnostic.rb

Constant Summary collapse

RUBOCOP_TO_LSP_SEVERITY =
{
  info: Constant::DiagnosticSeverity::HINT,
  refactor: Constant::DiagnosticSeverity::INFORMATION,
  convention: Constant::DiagnosticSeverity::INFORMATION,
  warning: Constant::DiagnosticSeverity::WARNING,
  error: Constant::DiagnosticSeverity::ERROR,
  fatal: Constant::DiagnosticSeverity::ERROR,
}.freeze
ENHANCED_DOC_URL =

: Hash[Symbol, Integer]

begin
  gem("rubocop", ">= 1.64.0")
  true
rescue LoadError
  false
end

Instance Method Summary collapse

Constructor Details

#initialize(document, offense, uri) ⇒ RuboCopDiagnostic

TODO: avoid passing document once we have alternative ways to get at encoding and file source : (RubyDocument document, ::RuboCop::Cop::Offense offense, URI::Generic uri) -> void



27
28
29
30
31
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 27

def initialize(document, offense, uri)
  @document = document
  @offense = offense
  @uri = uri
end

Instance Method Details

#to_lsp_code_actionsObject

: -> Array



34
35
36
37
38
39
40
41
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 34

def to_lsp_code_actions
  code_actions = []

  code_actions << autocorrect_action if correctable?
  code_actions << disable_line_action

  code_actions
end

#to_lsp_diagnostic(config) ⇒ Object

: (::RuboCop::Config config) -> Interface::Diagnostic



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 44

def to_lsp_diagnostic(config)
  # highlighted_area contains the begin and end position of the first line
  # This ensures that multiline offenses don't clutter the editor
  highlighted = @offense.highlighted_area
  Interface::Diagnostic.new(
    message: message,
    source: "RuboCop",
    code: @offense.cop_name,
    code_description: code_description(config),
    severity: severity,
    range: Interface::Range.new(
      start: Interface::Position.new(
        line: @offense.line - 1,
        character: highlighted.begin_pos,
      ),
      end: Interface::Position.new(
        line: @offense.line - 1,
        character: highlighted.end_pos,
      ),
    ),
    data: {
      correctable: correctable?,
      code_actions: to_lsp_code_actions,
    },
  )
end