Class: RubyLsp::Rails::RunnerClient

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

Direct Known Subclasses

NullClient

Defined Under Namespace

Classes: EmptyMessageError, InitializationError, MessageError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(outgoing_queue, global_state) ⇒ RunnerClient

: (Thread::Queue outgoing_queue, RubyLsp::GlobalState global_state) -> void



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 51

def initialize(outgoing_queue, global_state)
  @outgoing_queue = outgoing_queue #: Thread::Queue
  @mutex = Mutex.new #: Mutex

  log_message("Ruby LSP Rails booting server")

  stdin, stdout, stderr, wait_thread = Bundler.with_original_env do
    Open3.popen3(
      { "RUBY_LSP_RAILS_RUNNER" => "true" },
      "bundle",
      "exec",
      "rails",
      "runner",
      "#{__dir__}/server.rb",
      "start",
      server_relevant_capabilities(global_state),
    )
  end

  @stdin = stdin #: IO
  @stdout = stdout #: IO
  @stderr = stderr #: IO
  @stdin.sync = true
  @stdout.sync = true
  @stderr.sync = true
  @wait_thread = wait_thread #: Process::Waiter

  # We set binmode for Windows compatibility
  @stdin.binmode
  @stdout.binmode
  @stderr.binmode

  initialize_response = read_response #: as !nil
  @rails_root = initialize_response[:root] #: String
  log_message("Finished booting Ruby LSP Rails server")

  # Responsible for transmitting notifications coming from the server to the outgoing queue, so that we can do
  # things such as showing progress notifications initiated by the server. The loop exits naturally when the
  # server closes its stderr write end (i.e., when the server process exits), at which point `read_notification`
  # returns nil.
  @notifier_thread = Thread.new do
    while (notification = read_notification)
      @outgoing_queue << notification unless @outgoing_queue.closed?
    end
  end #: Thread
rescue StandardError
  raise InitializationError, @stderr.read
end

Instance Attribute Details

#rails_rootObject (readonly)

: String



48
49
50
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 48

def rails_root
  @rails_root
end

Class Method Details

.create_client(outgoing_queue, global_state) ⇒ Object

: (Thread::Queue outgoing_queue, RubyLsp::GlobalState global_state) -> RunnerClient



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
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 12

def create_client(outgoing_queue, global_state)
  if File.exist?("bin/rails")
    new(outgoing_queue, global_state)
  else
    unless outgoing_queue.closed?
      outgoing_queue << RubyLsp::Notification.window_log_message(
        <<~MESSAGE.chomp,
          Ruby LSP Rails failed to locate bin/rails in the current directory: #{Dir.pwd}
          Server dependent features will not be available
        MESSAGE
        type: RubyLsp::Constant::MessageType::WARNING,
      )
    end

    NullClient.new
  end
rescue StandardError => e
  unless outgoing_queue.closed?
    outgoing_queue << RubyLsp::Notification.window_log_message(
      <<~MESSAGE.chomp,
        Ruby LSP Rails failed to initialize server: #{e.full_message}
        Server dependent features will not be available
      MESSAGE
      type: Constant::MessageType::ERROR,
    )
  end

  NullClient.new
end

Instance Method Details

#association_target(model_name:, association_name:) ⇒ Object

: (model_name: String, association_name: String) -> Hash[Symbol, untyped]?



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 123

def association_target(model_name:, association_name:)
  make_request(
    "association_target",
    model_name: model_name,
    association_name: association_name,
  )
rescue MessageError
  log_message(
    "Ruby LSP Rails failed to get association location",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end

#connected?Boolean

: -> bool

Returns:

  • (Boolean)


265
266
267
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 265

def connected?
  true
end

#delegate_notification(server_addon_name:, request_name:, **params) ⇒ Object

Delegates a notification to a server add-on : (server_addon_name: String, request_name: String, **untyped params) -> void



172
173
174
175
176
177
178
179
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 172

def delegate_notification(server_addon_name:, request_name:, **params)
  send_notification(
    "server_addon/delegate",
    request_name: request_name,
    server_addon_name: server_addon_name,
    **params,
  )
end

#delegate_request(server_addon_name:, request_name:, **params) ⇒ Object

Delegates a request to a server add-on : (server_addon_name: String, request_name: String, **untyped params) -> Hash[Symbol, untyped]?



206
207
208
209
210
211
212
213
214
215
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 206

def delegate_request(server_addon_name:, request_name:, **params)
  make_request(
    "server_addon/delegate",
    server_addon_name: server_addon_name,
    request_name: request_name,
    **params,
  )
rescue MessageError
  nil
end

#i18n(key) ⇒ Object

: (String key) -> Hash[Symbol, untyped]?



160
161
162
163
164
165
166
167
168
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 160

def i18n(key)
  make_request("i18n", key: key)
rescue MessageError
  log_message(
    "Ruby LSP Rails failed to get i18n information",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end

#model(name) ⇒ Object

: (String name) -> Hash[Symbol, untyped]?



112
113
114
115
116
117
118
119
120
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 112

def model(name)
  make_request("model", name: name)
rescue MessageError
  log_message(
    "Ruby LSP Rails failed to get model information",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end

#pending_migrations_messageObject

: -> String?



182
183
184
185
186
187
188
189
190
191
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 182

def pending_migrations_message
  response = make_request("pending_migrations_message")
  response[:pending_migrations_message] if response
rescue MessageError
  log_message(
    "Ruby LSP Rails failed when checking for pending migrations",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end

#register_server_addon(server_addon_path) ⇒ Object

: (String server_addon_path) -> void



101
102
103
104
105
106
107
108
109
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 101

def register_server_addon(server_addon_path)
  send_notification("server_addon/register", server_addon_path: server_addon_path)
rescue MessageError
  log_message(
    "Ruby LSP Rails failed to register server addon #{server_addon_path}",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end

#route(controller:, action:) ⇒ Object

: (controller: String, action: String) -> Hash[Symbol, untyped]?



149
150
151
152
153
154
155
156
157
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 149

def route(controller:, action:)
  make_request("route_info", controller: controller, action: action)
rescue MessageError
  log_message(
    "Ruby LSP Rails failed to get route information",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end

#route_location(name) ⇒ Object

: (String name) -> Hash[Symbol, untyped]?



138
139
140
141
142
143
144
145
146
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 138

def route_location(name)
  make_request("route_location", name: name)
rescue MessageError
  log_message(
    "Ruby LSP Rails failed to get route location",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end

#run_migrationsObject

: -> Hash[Symbol, untyped]?



194
195
196
197
198
199
200
201
202
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 194

def run_migrations
  make_request("run_migrations")
rescue MessageError
  log_message(
    "Ruby LSP Rails failed to run migrations",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end

#shutdownObject

: -> void



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 242

def shutdown
  return if stopped?

  log_message("Ruby LSP Rails shutting down server")
  send_message("shutdown")

  @stdin.close unless @stdin.closed?

  # Wait for the server to exit. Once it does, all handles it inherited (including its stderr write end) are
  # released, which lets the notifier thread drain remaining bytes and observe EOF.
  @wait_thread.join
  @notifier_thread.join

  @stdout.close unless @stdout.closed?
  @stderr.close unless @stderr.closed?
end

#stopped?Boolean

: -> bool

Returns:

  • (Boolean)


260
261
262
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 260

def stopped?
  [@stdin, @stdout, @stderr].all?(&:closed?) && !@wait_thread.alive? && !@notifier_thread.alive?
end

#trigger_i18n_reloadObject

: -> void



230
231
232
233
234
235
236
237
238
239
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 230

def trigger_i18n_reload
  log_message("Reloading I18n translations")
  send_notification("reload_i18n")
rescue MessageError
  log_message(
    "Ruby LSP Rails failed to trigger I18n reload",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end

#trigger_reloadObject

: -> void



218
219
220
221
222
223
224
225
226
227
# File 'lib/ruby_lsp/ruby_lsp_rails/runner_client.rb', line 218

def trigger_reload
  log_message("Reloading Rails application")
  send_notification("reload")
rescue MessageError
  log_message(
    "Ruby LSP Rails failed to trigger reload",
    type: RubyLsp::Constant::MessageType::ERROR,
  )
  nil
end