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

Inherits:
Base
  • Object
show all
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 inherited from Base

#post_initialize

Methods included from UriHelpers

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


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/solargraph/language_server/message/text_document/formatting.rb', line 9

def process
  filename = uri_to_file(params['textDocument']['uri'])
  filename = 'tmp.rb' if filename.nil? or filename.empty?
  original = host.read_text(params['textDocument']['uri'])
  cmd = "rubocop -a -f fi -s #{Shellwords.escape(filename)}"
  o, e, s = Open3.capture3(cmd, stdin_data: original)
  lines = o.lines
  index = lines.index{|l| l.start_with?('====================')}
  formatted = lines[index+1..-1].join
  # The response is required to send an explicit range. Text edits
  # with null ranges get ignored. See castwide/vscode-solargraph#83
  if original.end_with?("\n")
    ending = {
      line: original.lines.length,
      character: 0
    }
  else
    ending = {
      line: original.lines.length - 1,
      character: original.lines.last.length
    }
  end
  set_result(
    [
      {
        range: {
          start: {
            line: 0,
            character: 0
          },
          end: ending
        },
        newText: formatted
      }
    ]
  )
end