Class: RubyLsp::Rails::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lsp/ruby_lsp_rails/server.rb

Constant Summary collapse

VOID =
Object.new

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



14
15
16
17
18
19
20
# File 'lib/ruby_lsp/ruby_lsp_rails/server.rb', line 14

def initialize
  $stdin.sync = true
  $stdout.sync = true
  $stdin.binmode
  $stdout.binmode
  @running = true
end

Instance Method Details

#execute(request, params) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby_lsp/ruby_lsp_rails/server.rb', line 43

def execute(request, params)
  case request
  when "shutdown"
    @running = false
    VOID
  when "model"
    resolve_database_info_from_model(params.fetch(:name))
  when "association_target_location"
    resolve_association_target(params)
  when "reload"
    ::Rails.application.reloader.reload!
    VOID
  when "route_location"
    route_location(params.fetch(:name))
  else
    VOID
  end
rescue => e
  { error: e.full_message(highlight: false) }
end

#startObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_lsp/ruby_lsp_rails/server.rb', line 22

def start
  # Load routes if they haven't been loaded yet (see https://github.com/rails/rails/pull/51614).
  routes_reloader = ::Rails.application.routes_reloader
  routes_reloader.execute_unless_loaded if routes_reloader&.respond_to?(:execute_unless_loaded)

  initialize_result = { result: { message: "ok" } }.to_json
  $stdout.write("Content-Length: #{initialize_result.length}\r\n\r\n#{initialize_result}")

  while @running
    headers = $stdin.gets("\r\n\r\n")
    json = $stdin.read(headers[/Content-Length: (\d+)/i, 1].to_i)

    request = JSON.parse(json, symbolize_names: true)
    response = execute(request.fetch(:method), request[:params])
    next if response == VOID

    json_response = response.to_json
    $stdout.write("Content-Length: #{json_response.length}\r\n\r\n#{json_response}")
  end
end