-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathprocess_server.rb
50 lines (39 loc) · 1.27 KB
/
process_server.rb
1
2
3
4
5
6
7
8
9
10
11
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
41
42
43
44
45
46
47
48
49
50
# typed: strict
# frozen_string_literal: true
require "sorbet-runtime"
module RubyLsp
class Addon
class ProcessServer
extend T::Sig
extend T::Generic
abstract!
VOID = Object.new
sig { void }
def initialize
$stdin.sync = true
$stdout.sync = true
$stdin.binmode
$stdout.binmode
@running = T.let(true, T.nilable(T::Boolean))
end
sig { void }
def start
initialize_result = generate_initialize_response
$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
sig { abstract.returns(String) }
def generate_initialize_response; end
sig { abstract.params(request: String, params: T.untyped).returns(T.untyped) }
def execute(request, params); end
end
end
end