@@ -13,6 +13,50 @@ module SyntaxTree
13
13
# stree lsp
14
14
#
15
15
class LanguageServer
16
+ # This is a small module that effectively mirrors pattern matching. We're
17
+ # using it so that we can support truffleruby without having to ignore the
18
+ # language server.
19
+ module Request
20
+ # Represents a hash pattern.
21
+ class Shape
22
+ attr_reader :values
23
+
24
+ def initialize ( values )
25
+ @values = values
26
+ end
27
+
28
+ def ===( other )
29
+ values . all? do |key , value |
30
+ value == :any ? other . key? ( key ) : value === other [ key ]
31
+ end
32
+ end
33
+ end
34
+
35
+ # Represents an array pattern.
36
+ class Tuple
37
+ attr_reader :values
38
+
39
+ def initialize ( values )
40
+ @values = values
41
+ end
42
+
43
+ def ===( other )
44
+ values . each_with_index . all? { |value , index | value === other [ index ] }
45
+ end
46
+ end
47
+
48
+ def self . []( value )
49
+ case value
50
+ when Array
51
+ Tuple . new ( value . map { |child | self [ child ] } )
52
+ when Hash
53
+ Shape . new ( value . transform_values { |child | self [ child ] } )
54
+ else
55
+ value
56
+ end
57
+ end
58
+ end
59
+
16
60
attr_reader :input , :output , :print_width
17
61
18
62
def initialize (
@@ -39,30 +83,33 @@ def run
39
83
40
84
# stree-ignore
41
85
case request
42
- in { method : "initialize" , id : }
86
+ when Request [ method : "initialize" , id : :any ]
43
87
store . clear
44
- write ( id : id , result : { capabilities : capabilities } )
45
- in { method : "initialized" }
88
+ write ( id : request [ :id ] , result : { capabilities : capabilities } )
89
+ when Request [ method : "initialized" ]
46
90
# ignored
47
- in { method : "shutdown" } # tolerate missing ID to be a good citizen
91
+ when Request [ method : "shutdown" ] # tolerate missing ID to be a good citizen
48
92
store . clear
49
93
write ( id : request [ :id ] , result : { } )
50
94
return
51
- in { method : "textDocument/didChange" , params : { textDocument : { uri : } , contentChanges : [ { text : } , *] } }
52
- store [ uri ] = text
53
- in { method : "textDocument/didOpen" , params : { textDocument : { uri :, text : } } }
54
- store [ uri ] = text
55
- in { method : "textDocument/didClose" , params : { textDocument : { uri : } } }
56
- store . delete ( uri )
57
- in { method : "textDocument/formatting" , id :, params : { textDocument : { uri : } } }
95
+ when Request [ method : "textDocument/didChange" , params : { textDocument : { uri : :any } , contentChanges : [ { text : :any } ] } ]
96
+ store [ request . dig ( :params , :textDocument , :uri ) ] = request . dig ( :params , :contentChanges , 0 , :text )
97
+ when Request [ method : "textDocument/didOpen" , params : { textDocument : { uri : :any , text : :any } } ]
98
+ store [ request . dig ( :params , :textDocument , :uri ) ] = request . dig ( :params , :textDocument , :text )
99
+ when Request [ method : "textDocument/didClose" , params : { textDocument : { uri : :any } } ]
100
+ store . delete ( request . dig ( :params , :textDocument , :uri ) )
101
+ when Request [ method : "textDocument/formatting" , id : :any , params : { textDocument : { uri : :any } } ]
102
+ uri = request . dig ( :params , :textDocument , :uri )
58
103
contents = store [ uri ]
59
- write ( id : id , result : contents ? format ( contents , uri . split ( "." ) . last ) : nil )
60
- in { method : "textDocument/inlayHint" , id :, params : { textDocument : { uri : } } }
104
+ write ( id : request [ :id ] , result : contents ? format ( contents , uri . split ( "." ) . last ) : nil )
105
+ when Request [ method : "textDocument/inlayHint" , id : :any , params : { textDocument : { uri : :any } } ]
106
+ uri = request . dig ( :params , :textDocument , :uri )
61
107
contents = store [ uri ]
62
- write ( id : id , result : contents ? inlay_hints ( contents ) : nil )
63
- in { method : "syntaxTree/visualizing" , id :, params : { textDocument : { uri : } } }
64
- write ( id : id , result : PP . pp ( SyntaxTree . parse ( store [ uri ] ) , +"" ) )
65
- in { method : %r{\$ /.+} }
108
+ write ( id : request [ :id ] , result : contents ? inlay_hints ( contents ) : nil )
109
+ when Request [ method : "syntaxTree/visualizing" , id : :any , params : { textDocument : { uri : :any } } ]
110
+ uri = request . dig ( :params , :textDocument , :uri )
111
+ write ( id : request [ :id ] , result : PP . pp ( SyntaxTree . parse ( store [ uri ] ) , +"" ) )
112
+ when Request [ method : %r{\$ /.+} ]
66
113
# ignored
67
114
else
68
115
raise ArgumentError , "Unhandled: #{ request } "
0 commit comments