Class: SorbetView::Lsp::SorbetProcess
- Inherits:
-
Object
- Object
- SorbetView::Lsp::SorbetProcess
- Extended by:
- T::Sig
- Defined in:
- lib/sorbet_view/lsp/sorbet_process.rb
Overview
Manages Sorbet LSP as a child process
Instance Attribute Summary collapse
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Instance Method Summary collapse
- #forward(message) ⇒ Object
-
#initialize(config:, logger:) ⇒ SorbetProcess
constructor
A new instance of SorbetProcess.
- #on_notification(method_name, &block) ⇒ Object
- #send_notification(method_name, params) ⇒ Object
- #send_request(method_name, params) ⇒ Object
- #start(extra_args: []) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(config:, logger:) ⇒ SorbetProcess
Returns a new instance of SorbetProcess.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sorbet_view/lsp/sorbet_process.rb', line 14 def initialize(config:, logger:) @config = config @logger = logger @process = T.let(nil, T.nilable(IO)) @pid = T.let(nil, T.nilable(Integer)) @transport = T.let(nil, T.nilable(Transport)) @read_thread = T.let(nil, T.nilable(Thread)) @notification_handlers = T.let({}, T::Hash[String, T.proc.params(msg: T::Hash[String, T.untyped]).void]) @pending_requests = T.let({}, T::Hash[T.untyped, Thread::Queue]) @pending_mutex = T.let(Mutex.new, Mutex) @next_id = T.let(1, Integer) end |
Instance Attribute Details
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
11 12 13 |
# File 'lib/sorbet_view/lsp/sorbet_process.rb', line 11 def transport @transport end |
Instance Method Details
#forward(message) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/sorbet_view/lsp/sorbet_process.rb', line 116 def forward() transport = @transport raise 'Sorbet process not started' unless transport transport.() end |
#on_notification(method_name, &block) ⇒ Object
124 125 126 |
# File 'lib/sorbet_view/lsp/sorbet_process.rb', line 124 def on_notification(method_name, &block) @notification_handlers[method_name] = block end |
#send_notification(method_name, params) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/sorbet_view/lsp/sorbet_process.rb', line 107 def send_notification(method_name, params) transport = @transport raise 'Sorbet process not started' unless transport transport.send_notification(method_name, params) end |
#send_request(method_name, params) ⇒ Object
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 99 100 101 102 103 104 |
# File 'lib/sorbet_view/lsp/sorbet_process.rb', line 68 def send_request(method_name, params) transport = @transport raise 'Sorbet process not started' unless transport id = @pending_mutex.synchronize do current = @next_id @next_id += 1 current end queue = Thread::Queue.new @pending_mutex.synchronize { @pending_requests[id] = queue } transport.send_request(id, method_name, params) # Wait for response with timeout result = nil deadline = Time.now + 30 loop do break unless result.nil? break if Time.now > deadline begin result = queue.pop(true) # non-blocking rescue ThreadError sleep 0.05 end end @pending_mutex.synchronize { @pending_requests.delete(id) } if result.nil? @logger.error("Sorbet request '#{method_name}' timed out (30s)") end result end |
#start(extra_args: []) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/sorbet_view/lsp/sorbet_process.rb', line 28 def start(extra_args: []) cmd = [ @config.sorbet_path, 'tc', '--lsp', '--enable-all-experimental-lsp-features', *@config., *extra_args ] @logger.info("Starting Sorbet: #{cmd.join(' ')}") stdin_r, stdin_w = IO.pipe stdout_r, stdout_w = IO.pipe err_r, err_w = IO.pipe @pid = Process.spawn( *cmd, in: stdin_r, out: stdout_w, err: err_w ) stdin_r.close stdout_w.close err_w.close @process = stdin_w @transport = Transport.new(input: stdout_r, output: stdin_w) @read_thread = Thread.new { read_loop(stdout_r) } # Log stderr from Sorbet in background Thread.new do err_r.each_line { |line| @logger.info("Sorbet stderr: #{line.chomp}") } err_r.close end end |
#stop ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/sorbet_view/lsp/sorbet_process.rb', line 129 def stop if @pid Process.kill('TERM', @pid) Process.wait(@pid) @pid = nil end @read_thread&.kill rescue Errno::ESRCH, Errno::ECHILD # Process already gone end |