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

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

Constant Summary collapse

RUBOCOP_TO_LSP_SEVERITY =
T.let(
  {
    convention: Constant::DiagnosticSeverity::INFORMATION,
    info: Constant::DiagnosticSeverity::INFORMATION,
    refactor: Constant::DiagnosticSeverity::INFORMATION,
    warning: Constant::DiagnosticSeverity::WARNING,
    error: Constant::DiagnosticSeverity::ERROR,
    fatal: Constant::DiagnosticSeverity::ERROR,
  }.freeze,
  T::Hash[Symbol, Integer],
)

Instance Method Summary collapse

Constructor Details

#initialize(offense, uri) ⇒ RuboCopDiagnostic

Returns a new instance of RuboCopDiagnostic.

[View source]

23
24
25
26
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 23

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

Instance Method Details

#to_lsp_code_actionObject

[View source]

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 29

def to_lsp_code_action
  Interface::CodeAction.new(
    title: "Autocorrect #{@offense.cop_name}",
    kind: Constant::CodeActionKind::QUICK_FIX,
    edit: Interface::WorkspaceEdit.new(
      document_changes: [
        Interface::TextDocumentEdit.new(
          text_document: Interface::OptionalVersionedTextDocumentIdentifier.new(
            uri: @uri,
            version: nil,
          ),
          edits: @offense.correctable? ? offense_replacements : [],
        ),
      ],
    ),
    is_preferred: true,
  )
end

#to_lsp_diagnosticObject

[View source]

49
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
76
77
78
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 49

def to_lsp_diagnostic
  if @offense.correctable?
    severity = RUBOCOP_TO_LSP_SEVERITY[@offense.severity.name]
    message = @offense.message
  else
    severity = Constant::DiagnosticSeverity::WARNING
    message = "#{@offense.message}\n\nThis offense is not auto-correctable.\n"
  end

  Interface::Diagnostic.new(
    message: message,
    source: "RuboCop",
    code: @offense.cop_name,
    severity: severity,
    range: Interface::Range.new(
      start: Interface::Position.new(
        line: @offense.line - 1,
        character: @offense.column,
      ),
      end: Interface::Position.new(
        line: @offense.last_line - 1,
        character: @offense.last_column,
      ),
    ),
    data: {
      correctable: @offense.correctable?,
      code_action: to_lsp_code_action,
    },
  )
end