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
SELF_RESOLVING_DISABLE_COPS =

Cops where adding a rubocop:disable inline comment would itself resolve the offense, causing Lint/RedundantCopDisableDirective to flag the disable as unnecessary.

Set.new([
  "Layout/EmptyComment",
]).freeze
ENHANCED_DOC_URL =

: Set

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



33
34
35
36
37
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 33

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

Instance Method Details

#to_lsp_code_actionsObject

: -> Array



40
41
42
43
44
45
46
47
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 40

def to_lsp_code_actions
  code_actions = []

  code_actions << autocorrect_action if correctable?
  code_actions << disable_line_action unless SELF_RESOLVING_DISABLE_COPS.include?(@offense.cop_name)

  code_actions
end

#to_lsp_diagnostic(config) ⇒ Object

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 50

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