Class: Solargraph::LanguageServer::Message::TextDocument::Formatting

Inherits:
Base
  • Object
show all
Includes:
Diagnostics::RubocopHelpers
Defined in:
lib/solargraph/language_server/message/text_document/formatting.rb

Instance Attribute Summary

Attributes inherited from Base

#filename

Attributes inherited from Base

#error, #host, #id, #method, #params, #request, #result

Instance Method Summary collapse

Methods included from Diagnostics::RubocopHelpers

find_rubocop_file, fix_drive_letter, generate_options, redirect_stdout

Methods inherited from Base

#post_initialize

Methods included from UriHelpers

decode, encode, file_to_uri, uri_to_file

Methods inherited from Base

#initialize, #post_initialize, #send_response, #set_error, #set_result

Constructor Details

This class inherits a constructor from Solargraph::LanguageServer::Message::Base

Instance Method Details

#processObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/solargraph/language_server/message/text_document/formatting.rb', line 13

def process
  filename = uri_to_file(params['textDocument']['uri'])
  # Make the temp file in the original file's directory so RuboCop
  # detects the correct configuration
  # the .rb extension is needed for ruby file without extension, else rubocop won't format
  tempfile = File.join(File.dirname(filename), "_tmp_#{SecureRandom.hex(8)}_#{File.basename(filename)}.rb")
  rubocop_file = Diagnostics::RubocopHelpers.find_rubocop_file(filename)
  original = host.read_text(params['textDocument']['uri'])
  File.write tempfile, original
  begin
    args = ['-a', '-f', 'fi', tempfile]
    args.unshift('-c', fix_drive_letter(rubocop_file)) unless rubocop_file.nil?
    options, paths = RuboCop::Options.new.parse(args)
    store = RuboCop::ConfigStore.new
    redirect_stdout { RuboCop::Runner.new(options, store).run(paths) }
    result = File.read(tempfile)
    File.unlink tempfile
    format original, result
  rescue RuboCop::ValidationError, RuboCop::ConfigNotFoundError => e
    set_error(Solargraph::LanguageServer::ErrorCodes::INTERNAL_ERROR, "[#{e.class}] #{e.message}")
    File.unlink tempfile
  end
end