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(
  {
    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,
  T::Hash[Symbol, Integer],
)
ENHANCED_DOC_URL =
T.let(
  begin
    gem("rubocop", ">= 1.64.0")
    true
  rescue LoadError
    false
  end,
  T::Boolean,
)

Instance Method Summary collapse

Constructor Details

#initialize(document, offense, uri) ⇒ RuboCopDiagnostic



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

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

Instance Method Details

#to_lsp_code_actionsObject



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

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



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

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