Class: DiagnosticCollector

Inherits:
RuboCop::Runner
  • Object
show all
Defined in:
lib/rubocop/lsp/diagnostic_collector.rb

Constant Summary collapse

RUBOCOP_FLAGS =
[
  "--stderr", # Print any output to stderr so that our stdout does not get polluted
  "--format",
  "quiet", # Supress any progress output by setting the formatter to `quiet`
  "--auto-correct", # Apply the autocorrects on the supplied buffer
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, text:) ⇒ DiagnosticCollector

Returns a new instance of DiagnosticCollector.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/lsp/diagnostic_collector.rb', line 15

def initialize(uri:, text:)
  @uri = uri
  @file = CGI.unescape(URI.parse(uri).path)
  @text = text
  @formatted_text = nil

  super(
    ::RuboCop::Options.new.parse(RUBOCOP_FLAGS).first,
    ::RuboCop::ConfigStore.new
  )
end

Instance Attribute Details

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



13
14
15
# File 'lib/rubocop/lsp/diagnostic_collector.rb', line 13

def diagnostics
  @diagnostics
end

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/rubocop/lsp/diagnostic_collector.rb', line 13

def file
  @file
end

#formatted_textObject (readonly)

Returns the value of attribute formatted_text.



13
14
15
# File 'lib/rubocop/lsp/diagnostic_collector.rb', line 13

def formatted_text
  @formatted_text
end

#textObject (readonly)

Returns the value of attribute text.



13
14
15
# File 'lib/rubocop/lsp/diagnostic_collector.rb', line 13

def text
  @text
end

#uriObject (readonly)

Returns the value of attribute uri.



13
14
15
# File 'lib/rubocop/lsp/diagnostic_collector.rb', line 13

def uri
  @uri
end

Instance Method Details

#file_finished(_file, offenses) ⇒ Object



41
42
43
# File 'lib/rubocop/lsp/diagnostic_collector.rb', line 41

def file_finished(_file, offenses)
  @diagnostics = Model::DiagnosticCollection.new(uri, offenses)
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/lsp/diagnostic_collector.rb', line 27

def run
  # Send text via "stdin".
  # RuboCop reads all of stdin into the "stdin" option, when `--stdin`
  # flag is supplied
  @options[:stdin] = text

  # Invoke the actual run method with just this file in `paths`
  super([file])

  # RuboCop applies autocorrections to the "stdin" option,
  # so read that into the formatted text attribute
  @formatted_text = @options[:stdin]
end