@@ -17,52 +17,21 @@ pub struct Server {
1717 client : Client ,
1818 session : Session ,
1919 client_capabilities : RwLock < Option < ClientFlags > > ,
20- debouncer : SimpleTokioDebouncer < Url > ,
20+ debouncer : SimpleTokioDebouncer ,
2121}
2222
2323impl Server {
2424 pub fn new ( client : Client ) -> Self {
25- let session = Session :: new ( ) ;
26-
27- let cloned_session = session. clone ( ) ;
28- let cloned_client = client. clone ( ) ;
29-
30- let debouncer =
31- SimpleTokioDebouncer :: new ( std:: time:: Duration :: from_millis ( 500 ) , move |mut uri| {
32- normalize_uri ( & mut uri) ;
33- let url = file_path ( & uri) ;
34-
35- let diagnostics = cloned_session. get_diagnostics_sync ( url) ;
36-
37- let diagnostics: Vec < Diagnostic > = diagnostics
38- . into_iter ( )
39- . map ( |( d, r) | to_proto:: diagnostic ( d, r) )
40- . collect ( ) ;
41-
42- cloned_client. send_notification :: < ShowMessage > ( ShowMessageParams {
43- typ : MessageType :: INFO ,
44- message : format ! ( "diagnostics {}" , diagnostics. len( ) ) ,
45- } ) ;
46-
47- let params = PublishDiagnosticsParams {
48- uri,
49- diagnostics,
50- version : None ,
51- } ;
52-
53- cloned_client. send_notification :: < notification:: PublishDiagnostics > ( params) ;
54- } ) ;
55-
5625 Self {
5726 client,
5827 session : Session :: new ( ) ,
5928 client_capabilities : RwLock :: new ( None ) ,
60- debouncer,
29+ debouncer : SimpleTokioDebouncer :: new ( std :: time :: Duration :: from_millis ( 500 ) ) ,
6130 }
6231 }
6332
6433 /// When the client sends a didChangeConfiguration notification, we need to parse the received JSON.
65- fn parse_options_from_client (
34+ async fn parse_options_from_client (
6635 & self ,
6736 mut value : serde_json:: Value ,
6837 ) -> Option < ClientConfigurationOptions > {
@@ -79,7 +48,8 @@ impl Server {
7948 ) ;
8049 let typ = MessageType :: WARNING ;
8150 self . client
82- . send_notification :: < ShowMessage > ( ShowMessageParams { message, typ } ) ;
51+ . send_notification :: < ShowMessage > ( ShowMessageParams { message, typ } )
52+ . await ;
8353 None
8454 }
8555 }
@@ -106,17 +76,15 @@ impl Server {
10676 . next ( )
10777 . expect ( "workspace/configuration request did not yield expected response." ) ;
10878
109- let opts = self . parse_options_from_client ( relevant) ;
110-
111- opts
79+ self . parse_options_from_client ( relevant) . await
11280 }
11381 Err ( why) => {
11482 let message = format ! (
11583 "Unable to pull client options via workspace/configuration request: {}" ,
11684 why
11785 ) ;
11886 println ! ( "{}" , message) ;
119- self . client . log_message ( MessageType :: ERROR , message) ;
87+ self . client . log_message ( MessageType :: ERROR , message) . await ;
12088 None
12189 }
12290 }
@@ -137,7 +105,8 @@ impl Server {
137105 . send_notification :: < ShowMessage > ( ShowMessageParams {
138106 typ : MessageType :: INFO ,
139107 message : format ! ( "diagnostics {}" , diagnostics. len( ) ) ,
140- } ) ;
108+ } )
109+ . await ;
141110
142111 let params = PublishDiagnosticsParams {
143112 uri,
@@ -146,11 +115,44 @@ impl Server {
146115 } ;
147116
148117 self . client
149- . send_notification :: < notification:: PublishDiagnostics > ( params) ;
118+ . send_notification :: < notification:: PublishDiagnostics > ( params)
119+ . await ;
150120 }
151121
152- async fn publish_diagnostics_debounced ( & self , uri : Url ) {
153- self . debouncer . debounce ( uri) ;
122+ async fn publish_diagnostics_debounced ( & self , mut uri : Url ) {
123+ let session = self . session . clone ( ) ;
124+ let client = self . client . clone ( ) ;
125+
126+ self . debouncer
127+ . debounce ( Box :: pin ( async move {
128+ normalize_uri ( & mut uri) ;
129+ let url = file_path ( & uri) ;
130+
131+ let diagnostics = session. get_diagnostics_sync ( url) ;
132+
133+ let diagnostics: Vec < Diagnostic > = diagnostics
134+ . into_iter ( )
135+ . map ( |( d, r) | to_proto:: diagnostic ( d, r) )
136+ . collect ( ) ;
137+
138+ client
139+ . send_notification :: < ShowMessage > ( ShowMessageParams {
140+ typ : MessageType :: INFO ,
141+ message : format ! ( "diagnostics {}" , diagnostics. len( ) ) ,
142+ } )
143+ . await ;
144+
145+ let params = PublishDiagnosticsParams {
146+ uri,
147+ diagnostics,
148+ version : None ,
149+ } ;
150+
151+ client
152+ . send_notification :: < notification:: PublishDiagnostics > ( params)
153+ . await ;
154+ } ) )
155+ . await ;
154156 }
155157}
156158
@@ -197,6 +199,8 @@ impl LanguageServer for Server {
197199 }
198200
199201 async fn shutdown ( & self ) -> jsonrpc:: Result < ( ) > {
202+ // TODO: Shutdown stuff.
203+
200204 self . client
201205 . log_message ( MessageType :: INFO , "Postgres LSP terminated." )
202206 . await ;
@@ -213,21 +217,41 @@ impl LanguageServer for Server {
213217 . is_some_and ( |o| o. db_connection_string . is_some ( ) )
214218 {
215219 let conn_str = opts. unwrap ( ) . db_connection_string . unwrap ( ) ;
216- self . session . change_db ( conn_str) . await ;
220+ match self . session . change_db ( conn_str) . await {
221+ Ok ( _) => { }
222+ Err ( err) => {
223+ self . client
224+ . show_message (
225+ MessageType :: ERROR ,
226+ format ! ( "Pulled Client Options but failed to set them: {}" , err) ,
227+ )
228+ . await
229+ }
230+ }
217231 return ;
218232 }
219233 }
220234
221235 // if we couldn't pull settings from the client,
222236 // we'll try parsing the passed in params.
223- let opts = self . parse_options_from_client ( params. settings ) ;
237+ let opts = self . parse_options_from_client ( params. settings ) . await ;
224238
225239 if opts
226240 . as_ref ( )
227241 . is_some_and ( |o| o. db_connection_string . is_some ( ) )
228242 {
229243 let conn_str = opts. unwrap ( ) . db_connection_string . unwrap ( ) ;
230- self . session . change_db ( conn_str) . await ;
244+ match self . session . change_db ( conn_str) . await {
245+ Ok ( _) => { }
246+ Err ( err) => {
247+ self . client
248+ . show_message (
249+ MessageType :: ERROR ,
250+ format ! ( "Used Client Options from params but failed to set them: {}" , err) ,
251+ )
252+ . await
253+ }
254+ }
231255 }
232256 }
233257
@@ -268,7 +292,7 @@ impl LanguageServer for Server {
268292 let mut uri = params. text_document . uri ;
269293 normalize_uri ( & mut uri) ;
270294
271- self . debouncer . debounce ( uri) . await
295+ self . publish_diagnostics_debounced ( uri) . await ;
272296 }
273297
274298 async fn did_close ( & self , params : DidCloseTextDocumentParams ) {
0 commit comments