@@ -13,6 +13,50 @@ module SyntaxTree
1313 # stree lsp
1414 #
1515 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+
1660 attr_reader :input , :output , :print_width
1761
1862 def initialize (
@@ -39,30 +83,33 @@ def run
3983
4084 # stree-ignore
4185 case request
42- in { method : "initialize" , id : }
86+ when Request [ method : "initialize" , id : :any ]
4387 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" ]
4690 # 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
4892 store . clear
4993 write ( id : request [ :id ] , result : { } )
5094 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 )
58103 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 )
61107 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{\$ /.+} ]
66113 # ignored
67114 else
68115 raise ArgumentError , "Unhandled: #{ request } "
0 commit comments