Class: RubyLsp::Rails::Server

Inherits:
ServerComponent show all
Includes:
Common
Defined in:
lib/ruby_lsp/ruby_lsp_rails/server.rb

Instance Attribute Summary

Attributes inherited from ServerComponent

#capabilities, #stderr, #stdout

Instance Method Summary collapse

Methods included from Common

#begin_progress, #end_progress, #log_message, #report_progress, #send_error_response, #send_result, #with_notification_error_handling, #with_progress, #with_request_error_handling

Constructor Details

#initialize(stdout: $stdout, stderr: $stderr, override_default_output_device: true, capabilities: {}) ⇒ Server

: (?stdout: IO | StringIO, ?stderr: IO | StringIO, ?override_default_output_device: bool, ?capabilities: Hash[Symbol | String, untyped]) -> void



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/ruby_lsp/ruby_lsp_rails/server.rb', line 263

def initialize(stdout: $stdout, stderr: $stderr, override_default_output_device: true, capabilities: {})
  # Grab references to the original pipes so that we can change the default output device further down

  @stdin = $stdin #: IO
  super(stdout, stderr, capabilities)

  @stdin.sync = true
  @stdout.sync = true
  @stderr.sync = true
  @stdin.binmode
  @stdout.binmode
  @stderr.binmode

  # # Set the default output device to be $stderr. This means that using `puts` by itself will default to printing
  # # to $stderr and only explicit `$stdout.puts` will go to $stdout. This reduces the chance that output coming
  # # from the Rails app will be accidentally sent to the client
  $> = IOWrapper.new(@stderr) if override_default_output_device

  @running = true #: bool
  @database_supports_indexing = nil #: bool?
end

Instance Method Details

#execute(request, params) ⇒ Object

: (String, Hash[Symbol | String, untyped]) -> void



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/ruby_lsp/ruby_lsp_rails/server.rb', line 308

def execute(request, params)
  case request
  when "shutdown"
    @running = false
  when "model"
    with_request_error_handling(request) do
      send_result(resolve_database_info_from_model(params.fetch(:name)))
    end
  when "association_target"
    with_request_error_handling(request) do
      send_result(resolve_association_target(params))
    end
  when "pending_migrations_message"
    with_request_error_handling(request) do
      send_result({ pending_migrations_message: pending_migrations_message })
    end
  when "run_migrations"
    with_request_error_handling(request) do
      send_result(run_migrations)
    end
  when "reload"
    with_progress("rails-reload", "Reloading Ruby LSP Rails instance") do
      with_notification_error_handling(request) do
        ::Rails.application.reloader.reload!
      end
    end
  when "route_location"
    with_request_error_handling(request) do
      send_result(route_location(params.fetch(:name)))
    end
  when "route_info"
    with_request_error_handling(request) do
      send_result(resolve_route_info(params))
    end
  when "i18n"
    with_request_error_handling(request) do
      result = resolve_i18n_key(params.fetch(:key))
      send_result(result)
    end
  when "reload_i18n"
    with_progress("rails-reload-i18n", "Reloading Ruby LSP Rails I18n") do
      with_notification_error_handling(request) do
        I18n.reload! if defined?(I18n) && I18n.respond_to?(:reload!)
      end
    end
  when "server_addon/register"
    with_notification_error_handling(request) do
      require params[:server_addon_path]
      ServerAddon.finalize_registrations!(@stdout, @stderr, @capabilities)
    end
  when "server_addon/delegate"
    server_addon_name = params[:server_addon_name]
    request_name = params[:request_name]

    # Do not wrap this in error handlers. Server add-ons need to have the flexibility to choose if they want to
    # include a response or not as part of error handling, so a blanket approach is not appropriate.
    ServerAddon.delegate(server_addon_name, request_name, params.except(:request_name, :server_addon_name))
  end
end

#startObject

: -> void



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/ruby_lsp/ruby_lsp_rails/server.rb', line 286

def start
  load_routes
  clear_file_system_resolver_hooks
  send_result({ message: "ok", root: ::Rails.root.to_s })

  while @running
    headers = @stdin.gets("\r\n\r\n")
    break unless headers

    length = headers[/Content-Length: (\d+)/i, 1]
    break unless length

    json = @stdin.read(length.to_i)
    break unless json

    request = JSON.parse(json, symbolize_names: true)
    execute(request.fetch(:method), request[:params])
    disconnect_from_database
  end
end