Class: Standard::Lsp::Standardizer

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

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ Standardizer

Returns a new instance of Standardizer.



6
7
8
9
10
# File 'lib/standard/lsp/standardizer.rb', line 6

def initialize(config, logger)
  @config = config
  @logger = logger
  @rubocop_runner = Standard::Runners::Rubocop.new
end

Instance Method Details

#format(path, text) ⇒ Object

This abuses the –stdin option of rubocop and reads the formatted text from the options that rubocop mutates. This depends on parallel: false as well as the fact that rubocop doesn’t otherwise dup or reassign that options object. Risky business!

Reassigning options is done here:

https://github.com/rubocop/rubocop/blob/master/lib/rubocop/cop/team.rb#L131

Printing options

https://github.com/rubocop/rubocop/blob/master/lib/rubocop/cli/command/execute_runner.rb#L95

Setting ‘parallel: true` would break this here:

https://github.com/rubocop/rubocop/blob/master/lib/rubocop/runner.rb#L72


23
24
25
26
27
# File 'lib/standard/lsp/standardizer.rb', line 23

def format(path, text)
  ad_hoc_config = fork_config(path, text, format: true)
  capture_rubocop_stdout(ad_hoc_config)
  ad_hoc_config.rubocop_options[:stdin]
end

#offenses(path, text) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/standard/lsp/standardizer.rb', line 29

def offenses(path, text)
  results = JSON.parse(
    capture_rubocop_stdout(fork_config(path, text, format: false)),
    symbolize_names: true
  )
  if results[:files].empty?
    @logger.puts_once "Ignoring file, per configuration: #{path}"
    []
  else
    results.dig(:files, 0, :offenses)
  end
end