Class: RubynCode::IDE::Server
- Inherits:
-
Object
- Object
- RubynCode::IDE::Server
- Defined in:
- lib/rubyn_code/ide/server.rb
Overview
JSON-RPC 2.0 server for the VS Code extension.
Reads newline-delimited JSON from $stdin, dispatches each request to a handler, and writes JSON-RPC responses/notifications to $stdout. All debug output goes to $stderr — never protocol data.
Processes one request at a time on the main thread.
Instance Attribute Summary collapse
-
#client_capabilities ⇒ Object
Attributes set by handlers during the session lifecycle.
-
#extension_version ⇒ Object
Attributes set by handlers during the session lifecycle.
-
#handler_instances ⇒ Object
Attributes set by handlers during the session lifecycle.
-
#ide_client ⇒ Object
readonly
Returns the value of attribute ide_client.
-
#permission_mode ⇒ Object
Attributes set by handlers during the session lifecycle.
-
#session_persistence ⇒ Object
Attributes set by handlers during the session lifecycle.
-
#tool_output_adapter ⇒ Object
Attributes set by handlers during the session lifecycle.
-
#workspace_path ⇒ Object
Attributes set by handlers during the session lifecycle.
Instance Method Summary collapse
-
#handler_instance(short_name) ⇒ Object
Look up a handler instance by its short name (e.g. :prompt, :cancel).
-
#initialize(permission_mode: :default, yolo: false) ⇒ Server
constructor
A new instance of Server.
-
#notify(method, params = {}) ⇒ Object
Send a JSON-RPC notification (no id) to stdout.
-
#on(method, &block) ⇒ Object
Register a handler for a given JSON-RPC method.
- #run ⇒ Object
-
#stop! ⇒ Object
Signal the server to stop its read loop.
-
#yolo ⇒ Object
Backward-compatible reader: true when permission_mode is :bypass.
Constructor Details
#initialize(permission_mode: :default, yolo: false) ⇒ Server
Returns a new instance of Server.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rubyn_code/ide/server.rb', line 24 def initialize(permission_mode: :default, yolo: false) @permission_mode = yolo ? :bypass : .to_sym @running = false @write_mutex = Mutex.new @handlers = {} @handler_instances = {} @workspace_path = nil @extension_version = nil @client_capabilities = {} @session_persistence = nil @tool_output_adapter = nil @ide_client = Client.new(self) Handlers.register_all(self) end |
Instance Attribute Details
#client_capabilities ⇒ Object
Attributes set by handlers during the session lifecycle.
19 20 21 |
# File 'lib/rubyn_code/ide/server.rb', line 19 def client_capabilities @client_capabilities end |
#extension_version ⇒ Object
Attributes set by handlers during the session lifecycle.
19 20 21 |
# File 'lib/rubyn_code/ide/server.rb', line 19 def extension_version @extension_version end |
#handler_instances ⇒ Object
Attributes set by handlers during the session lifecycle.
19 20 21 |
# File 'lib/rubyn_code/ide/server.rb', line 19 def handler_instances @handler_instances end |
#ide_client ⇒ Object (readonly)
Returns the value of attribute ide_client.
22 23 24 |
# File 'lib/rubyn_code/ide/server.rb', line 22 def ide_client @ide_client end |
#permission_mode ⇒ Object
Attributes set by handlers during the session lifecycle.
19 20 21 |
# File 'lib/rubyn_code/ide/server.rb', line 19 def @permission_mode end |
#session_persistence ⇒ Object
Attributes set by handlers during the session lifecycle.
19 20 21 |
# File 'lib/rubyn_code/ide/server.rb', line 19 def session_persistence @session_persistence end |
#tool_output_adapter ⇒ Object
Attributes set by handlers during the session lifecycle.
19 20 21 |
# File 'lib/rubyn_code/ide/server.rb', line 19 def tool_output_adapter @tool_output_adapter end |
#workspace_path ⇒ Object
Attributes set by handlers during the session lifecycle.
19 20 21 |
# File 'lib/rubyn_code/ide/server.rb', line 19 def workspace_path @workspace_path end |
Instance Method Details
#handler_instance(short_name) ⇒ Object
Look up a handler instance by its short name (e.g. :prompt, :cancel). Returns nil if the handler is not registered.
72 73 74 75 76 77 |
# File 'lib/rubyn_code/ide/server.rb', line 72 def handler_instance(short_name) method_name = Handlers::SHORT_NAMES[short_name.to_sym] return nil unless method_name @handler_instances[method_name] end |
#notify(method, params = {}) ⇒ Object
Send a JSON-RPC notification (no id) to stdout.
60 61 62 |
# File 'lib/rubyn_code/ide/server.rb', line 60 def notify(method, params = {}) write(Protocol.notification(method, params)) end |
#on(method, &block) ⇒ Object
Register a handler for a given JSON-RPC method. The block receives (params, id) and must return a result hash.
66 67 68 |
# File 'lib/rubyn_code/ide/server.rb', line 66 def on(method, &block) @handlers[method] = block end |
#run ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubyn_code/ide/server.rb', line 45 def run @running = true setup_signal_traps! warn "[IDE::Server] started (pid=#{Process.pid})" $stdout.sync = true read_loop ensure graceful_shutdown! end |
#stop! ⇒ Object
Signal the server to stop its read loop.
80 81 82 |
# File 'lib/rubyn_code/ide/server.rb', line 80 def stop! @running = false end |
#yolo ⇒ Object
Backward-compatible reader: true when permission_mode is :bypass.
41 42 43 |
# File 'lib/rubyn_code/ide/server.rb', line 41 def yolo @permission_mode == :bypass end |