Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype LSP Rails runner #230

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion lib/ruby_lsp/ruby_lsp_rails/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative "rails_client"
require_relative "hover"
require_relative "code_lens"
require "open3"

module RubyLsp
module Rails
Expand All @@ -20,10 +21,52 @@ def client
sig { override.params(message_queue: Thread::Queue).void }
def activate(message_queue)
client.check_if_server_is_running!
@stdin, @stdout, @stderr, @wait_thread = Open3.popen3("bin/rails runner lib/ruby_lsp/ruby_lsp_rails/server.rb")
andyw8 marked this conversation as resolved.
Show resolved Hide resolved

@stdin.binmode
@stdout.binmode
end

sig { override.void }
def deactivate; end
def deactivate
make_request("shutdown")
andyw8 marked this conversation as resolved.
Show resolved Hide resolved

@stdin.close
@stdout.close
@stderr.close
end

def make_request(request, params = nil)
send_request(request, params)
read_response(request)
end

def send_request(request, params = nil)
hash = {
id: rand(100),
andyw8 marked this conversation as resolved.
Show resolved Hide resolved
method: request,
}

hash[:params] = params if params
json = hash.to_json
@stdin.write("Content-Length: #{json.length}\r\n\r\n")
@stdin.write(json)
andyw8 marked this conversation as resolved.
Show resolved Hide resolved
end

def read_response(request)
Timeout.timeout(5) do
# Read headers until line breaks
headers = @stdout.gets("\r\n\r\n")

# Read the response content based on the length received in the headers
raw_response = @stdout.read(headers[/Content-Length: (\d+)/i, 1].to_i)

json = JSON.parse(raw_response, symbolize_names: true)
warn("*** response ***: #{json}")
end
rescue Timeout::Error
raise "Request #{request} timed out. Is the request returning a response?"
end

# Creates a new CodeLens listener. This method is invoked on every CodeLens request
sig do
Expand Down
30 changes: 30 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# typed: false
# frozen_string_literal: true

require "json"

running = true
while running
# Read headers until line breaks
headers = $stdin.gets("\r\n\r\n")

# Read the response content based on the length received in the headers
request = $stdin.read(headers[/Content-Length: (\d+)/i, 1].to_i)

json = JSON.parse(request, symbolize_names: true)

request_method = json.fetch(:method)
params = json[:params]

response_json = nil
case request_method
when "shutdown"
# returning the column names just to illustrate we have access the Rails app
response_json = { result: "ok", columns: User.column_names }.to_json

andyw8 marked this conversation as resolved.
Show resolved Hide resolved
running = false
end

$stdout.write("Content-Length: #{response_json.length}\r\n\r\n")
$stdout.write(response_json)
end
Loading