@@ -10,7 +10,6 @@ defmodule Expert do
1010
1111 use GenLSP
1212
13- # FIXME(mhanberg): these were not being namespaced correctly
1413 @ server_specific_messages [
1514 GenLSP.Notifications.TextDocumentDidChange ,
1615 GenLSP.Notifications.WorkspaceDidChangeConfiguration ,
@@ -53,6 +52,7 @@ defmodule Expert do
5352 end
5453
5554 lsp = assign ( lsp , state: state )
55+ { :ok , response } = Forge.Protocol.Convert . to_lsp ( response )
5656
5757 { :reply , response , lsp }
5858
@@ -69,10 +69,11 @@ defmodule Expert do
6969 def handle_request ( % mod { } = request , lsp ) when mod in @ server_specific_messages do
7070 GenLSP . error ( lsp , "handling server specific request #{ Macro . to_string ( mod ) } " )
7171
72- case apply_to_state ( assigns ( lsp ) . state , request ) do
73- { :ok , response , state } ->
74- { :reply , response , assign ( lsp , state: state ) }
75-
72+ with { :ok , request } <- Forge.Protocol.Convert . to_native ( request ) ,
73+ { :ok , response , state } <- apply_to_state ( assigns ( lsp ) . state , request ) ,
74+ { :ok , response } <- Forge.Protocol.Convert . to_lsp ( response ) do
75+ { :reply , Forge.Protocol.Convert . to_lsp ( response ) , assign ( lsp , state: state ) }
76+ else
7677 error ->
7778 message = "Failed to handle #{ mod } , #{ inspect ( error ) } "
7879 Logger . error ( message )
@@ -85,19 +86,13 @@ defmodule Expert do
8586 end
8687 end
8788
88- # TODO: i'm not sure if we need this
89- # def handle_message(nil, %State{} = state) do
90- # # NOTE: This deals with the response after a request is requested by the server,
91- # # such as the response of `CreateWorkDoneProgress`.
92- # {:ok, state}
93- # end
94-
95- def handle_request ( % _ { } = request , lsp ) do
89+ def handle_request ( request , lsp ) do
9690 state = assigns ( lsp ) . state
9791
9892 with { :ok , handler } <- fetch_handler ( request ) ,
99- { :ok , req } <- Convert . to_native ( request ) ,
100- { :ok , response } <- handler . handle ( req , state . configuration ) do
93+ { :ok , request } <- Convert . to_native ( request ) ,
94+ { :ok , response } <- handler . handle ( request , state . configuration ) ,
95+ { :ok , response } <- Forge.Protocol.Convert . to_lsp ( response ) do
10196 { :reply , response , lsp }
10297 else
10398 { :error , { :unhandled , _ } } ->
@@ -109,30 +104,50 @@ defmodule Expert do
109104 message: "Method not found"
110105 } , lsp }
111106
112- _ ->
113- { :noreply , lsp }
107+ error ->
108+ message = "Failed to handle #{ request . method } , #{ inspect ( error ) } "
109+ Logger . error ( message )
110+
111+ { :reply ,
112+ % GenLSP.ErrorResponse {
113+ code: GenLSP.Enumerations.ErrorCodes . internal_error ( ) ,
114+ message: message
115+ } , lsp }
114116 end
115117 end
116118
117119 def handle_notification ( % mod { } = notification , lsp ) when mod in @ server_specific_messages do
118- GenLSP . error ( lsp , "handling server specific notification #{ Macro . to_string ( mod ) } " )
119-
120- case apply_to_state ( assigns ( lsp ) . state , notification ) do
121- { :ok , state } ->
122- { :noreply , assign ( lsp , state: state ) }
123-
120+ with { :ok , notification } <- Convert . to_native ( notification ) ,
121+ { :ok , state } <- apply_to_state ( assigns ( lsp ) . state , notification ) do
122+ { :noreply , assign ( lsp , state: state ) }
123+ else
124124 error ->
125- message = "Failed to handle #{ mod } , #{ inspect ( error ) } "
125+ message = "Failed to handle #{ notification . method } , #{ inspect ( error ) } "
126126 Logger . error ( message )
127127
128128 { :noreply , lsp }
129129 end
130130 end
131131
132- def handle_notification ( % mod { } , lsp ) do
133- GenLSP . error ( lsp , inspect ( @ server_specific_messages ) )
134- GenLSP . error ( lsp , "handling other notification #{ inspect ( mod ) } " )
135- { :noreply , lsp }
132+ def handle_notification ( notification , lsp ) do
133+ state = assigns ( lsp ) . state
134+
135+ with { :ok , handler } <- fetch_handler ( notification ) ,
136+ { :ok , notification } <- Convert . to_native ( notification ) ,
137+ { :ok , _response } <- handler . handle ( notification , state . configuration ) do
138+ { :noreply , lsp }
139+ else
140+ { :error , { :unhandled , _ } } ->
141+ Logger . info ( "Unhandled notification: #{ notification . method } " )
142+
143+ { :noreply , lsp }
144+
145+ error ->
146+ message = "Failed to handle #{ notification . method } , #{ inspect ( error ) } "
147+ Logger . error ( message )
148+
149+ { :noreply , lsp }
150+ end
136151 end
137152
138153 def handle_info ( :default_config , % State { configuration: nil } = state ) do
0 commit comments