Skip to content

Commit

Permalink
Prototype LSP runner
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Jan 14, 2024
1 parent 7beb6d5 commit 7bf3710
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
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")

@stdin.binmode
@stdout.binmode
end

sig { override.void }
def deactivate; end
def deactivate
make_request("shutdown")

@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),
route: request,
}

hash[:params] = params if params
json = hash.to_json
@stdin.write("Content-Length: #{json.length}\r\n\r\n")
@stdin.write(json)
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
28 changes: 28 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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_route = json.fetch(:route)
params = json[:params]

response_json = nil
case request_route
when "shutdown"
response_json = { result: "ok", columns: User.column_names }.to_json

running = false
end

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

0 comments on commit 7bf3710

Please sign in to comment.