Class: SyntaxTree::LanguageServer
- Inherits:
-
Object
- Object
- SyntaxTree::LanguageServer
- Defined in:
- lib/syntax_tree/language_server.rb,
lib/syntax_tree/language_server/inlay_hints.rb
Defined Under Namespace
Classes: InlayHints
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
-
#initialize(input: STDIN, output: STDOUT) ⇒ LanguageServer
constructor
A new instance of LanguageServer.
- #run ⇒ Object
Constructor Details
#initialize(input: STDIN, output: STDOUT) ⇒ LanguageServer
Returns a new instance of LanguageServer.
13 14 15 16 |
# File 'lib/syntax_tree/language_server.rb', line 13 def initialize(input: STDIN, output: STDOUT) @input = input.binmode @output = output.binmode end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
11 12 13 |
# File 'lib/syntax_tree/language_server.rb', line 11 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
11 12 13 |
# File 'lib/syntax_tree/language_server.rb', line 11 def output @output end |
Instance Method Details
#run ⇒ Object
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 51 52 53 54 55 56 57 |
# File 'lib/syntax_tree/language_server.rb', line 18 def run store = Hash.new do |hash, uri| hash[uri] = File.binread(CGI.unescape(URI.parse(uri).path)) end while headers = input.gets("\r\n\r\n") source = input.read(headers[/Content-Length: (\d+)/i, 1].to_i) request = JSON.parse(source, symbolize_names: true) case request in { method: "initialize", id: } store.clear write(id: id, result: { capabilities: capabilities }) in { method: "initialized" } # ignored in { method: "shutdown" } store.clear return in { method: "textDocument/didChange", params: { textDocument: { uri: }, contentChanges: [{ text: }, *] } } store[uri] = text in { method: "textDocument/didOpen", params: { textDocument: { uri:, text: } } } store[uri] = text in { method: "textDocument/didClose", params: { textDocument: { uri: } } } store.delete(uri) in { method: "textDocument/formatting", id:, params: { textDocument: { uri: } } } write(id: id, result: [format(store[uri])]) in { method: "textDocument/inlayHints", id:, params: { textDocument: { uri: } } } write(id: id, result: inlay_hints(store[uri])) in { method: "syntaxTree/visualizing", id:, params: { textDocument: { uri: } } } output = [] PP.pp(SyntaxTree.parse(store[uri]), output) write(id: id, result: output.join) in { method: %r{\$/.+} } # ignored else raise "Unhandled: #{request}" end end end |