Class: SyntaxTree::LanguageServer
- Inherits:
-
Object
- Object
- SyntaxTree::LanguageServer
- Defined in:
- lib/syntax_tree/language_server.rb,
lib/syntax_tree/language_server/inlay_hints.rb
Overview
Syntax Tree additionally ships with a language server conforming to the language server protocol. It can be invoked through the CLI by running:
stree lsp
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.
-
#print_width ⇒ Object
readonly
Returns the value of attribute print_width.
Instance Method Summary collapse
-
#initialize(input: $stdin, output: $stdout, print_width: DEFAULT_PRINT_WIDTH) ⇒ LanguageServer
constructor
A new instance of LanguageServer.
-
#run ⇒ Object
rubocop:disable Layout/LineLength.
Constructor Details
#initialize(input: $stdin, output: $stdout, print_width: DEFAULT_PRINT_WIDTH) ⇒ LanguageServer
Returns a new instance of LanguageServer.
18 19 20 21 22 23 24 25 26 |
# File 'lib/syntax_tree/language_server.rb', line 18 def initialize( input: $stdin, output: $stdout, print_width: DEFAULT_PRINT_WIDTH ) @input = input.binmode @output = output.binmode @print_width = print_width end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
16 17 18 |
# File 'lib/syntax_tree/language_server.rb', line 16 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
16 17 18 |
# File 'lib/syntax_tree/language_server.rb', line 16 def output @output end |
#print_width ⇒ Object (readonly)
Returns the value of attribute print_width.
16 17 18 |
# File 'lib/syntax_tree/language_server.rb', line 16 def print_width @print_width end |
Instance Method Details
#run ⇒ Object
rubocop:disable Layout/LineLength
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 66 67 68 69 70 71 |
# File 'lib/syntax_tree/language_server.rb', line 29 def run store = Hash.new do |hash, uri| filepath = CGI.unescape(URI.parse(uri).path) File.exist?(filepath) ? (hash[uri] = File.read(filepath)) : nil 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) # stree-ignore case request in { method: "initialize", id: } store.clear write(id: id, result: { capabilities: capabilities }) in { method: "initialized" } # ignored in { method: "shutdown" } # tolerate missing ID to be a good citizen store.clear write(id: request[:id], result: {}) 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: } } } contents = store[uri] write(id: id, result: contents ? [format(store[uri], uri.split(".").last)] : nil) in { method: "textDocument/inlayHint", id:, params: { textDocument: { uri: } } } contents = store[uri] write(id: id, result: contents ? inlay_hints(store[uri]) : nil) in { method: "syntaxTree/visualizing", id:, params: { textDocument: { uri: } } } write(id: id, result: PP.pp(SyntaxTree.parse(store[uri]), +"")) in { method: %r{\$/.+} } # ignored else raise ArgumentError, "Unhandled: #{request}" end end end |