Class: SyntaxTree::LanguageServer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(input: $stdin, output: $stdout) ⇒ LanguageServer

Returns a new instance of LanguageServer.



18
19
20
21
# File 'lib/syntax_tree/language_server.rb', line 18

def initialize(input: $stdin, output: $stdout)
  @input = input.binmode
  @output = output.binmode
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



16
17
18
# File 'lib/syntax_tree/language_server.rb', line 16

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



16
17
18
# File 'lib/syntax_tree/language_server.rb', line 16

def output
  @output
end

Instance Method Details

#runObject



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/syntax_tree/language_server.rb', line 23

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: } }
       }
      write(id: id, result: PP.pp(SyntaxTree.parse(store[uri]), +""))
    in method: %r{\$/.+}
      # ignored
    else
      raise ArgumentError, "Unhandled: #{request}"
    end
  end
end