Class: Model::DiagnosticCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/lsp/model/diagnostic_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, offenses) ⇒ DiagnosticCollection

Returns a new instance of DiagnosticCollection.



12
13
14
15
16
17
# File 'lib/rubocop/lsp/model/diagnostic_collection.rb', line 12

def initialize(uri, offenses)
  @uri = uri
  @diagnostics = offenses.map do |offense|
    Diagnostic.new(offense)
  end
end

Instance Attribute Details

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



10
11
12
# File 'lib/rubocop/lsp/model/diagnostic_collection.rb', line 10

def diagnostics
  @diagnostics
end

#uriObject (readonly)

Returns the value of attribute uri.



10
11
12
# File 'lib/rubocop/lsp/model/diagnostic_collection.rb', line 10

def uri
  @uri
end

Instance Method Details

#code_actions(line_range) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/lsp/model/diagnostic_collection.rb', line 27

def code_actions(line_range)
  line_diagnostics = correctable_diagnostics.select do |diagnostic|
    line_range.include?(diagnostic.start.line)
  end

  actions = line_diagnostics.map do |diagnostic|
    code_action_from(
      diagnostic,
      title: "Autocorrect `#{diagnostic.cop_name}`",
      kind: Constant::CodeActionKind::QUICK_FIX
    )
  end

  if actions.any?
    actions << code_action_from(
      line_diagnostics,
      title: "Autocorrect all offenses on line",
      kind: "rubocop.fix"
    )
  end

  actions
end

#correctable_diagnosticsObject



23
24
25
# File 'lib/rubocop/lsp/model/diagnostic_collection.rb', line 23

def correctable_diagnostics
  @correctable_diagnostics ||= diagnostics.select(&:correctable?)
end

#responseObject



19
20
21
# File 'lib/rubocop/lsp/model/diagnostic_collection.rb', line 19

def response
  diagnostics.map(&:diagnostic_response)
end