Class: Standard::Lsp::Diagnostic

Inherits:
Object
  • Object
show all
Defined in:
lib/standard/lsp/diagnostic.rb

Constant Summary collapse

Constant =
LanguageServer::Protocol::Constant
Interface =
LanguageServer::Protocol::Interface
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

Instance Method Summary collapse

Constructor Details

#initialize(document_encoding, offense, uri, cop_class) ⇒ Diagnostic

Returns a new instance of Diagnostic.



16
17
18
19
20
21
# File 'lib/standard/lsp/diagnostic.rb', line 16

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

Instance Method Details

#to_lsp_code_actionsObject



23
24
25
26
27
28
29
30
# File 'lib/standard/lsp/diagnostic.rb', line 23

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/standard/lsp/diagnostic.rb', line 32

def to_lsp_diagnostic(config)
  highlighted = @offense.highlighted_area
  Interface::Diagnostic.new(
    message: message,
    source: "Standard Ruby",
    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