@@ -17,52 +17,21 @@ pub struct Server {
17
17
client : Client ,
18
18
session : Session ,
19
19
client_capabilities : RwLock < Option < ClientFlags > > ,
20
- debouncer : SimpleTokioDebouncer < Url > ,
20
+ debouncer : SimpleTokioDebouncer ,
21
21
}
22
22
23
23
impl Server {
24
24
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
-
56
25
Self {
57
26
client,
58
27
session : Session :: new ( ) ,
59
28
client_capabilities : RwLock :: new ( None ) ,
60
- debouncer,
29
+ debouncer : SimpleTokioDebouncer :: new ( std :: time :: Duration :: from_millis ( 500 ) ) ,
61
30
}
62
31
}
63
32
64
33
/// 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 (
66
35
& self ,
67
36
mut value : serde_json:: Value ,
68
37
) -> Option < ClientConfigurationOptions > {
@@ -79,7 +48,8 @@ impl Server {
79
48
) ;
80
49
let typ = MessageType :: WARNING ;
81
50
self . client
82
- . send_notification :: < ShowMessage > ( ShowMessageParams { message, typ } ) ;
51
+ . send_notification :: < ShowMessage > ( ShowMessageParams { message, typ } )
52
+ . await ;
83
53
None
84
54
}
85
55
}
@@ -106,17 +76,15 @@ impl Server {
106
76
. next ( )
107
77
. expect ( "workspace/configuration request did not yield expected response." ) ;
108
78
109
- let opts = self . parse_options_from_client ( relevant) ;
110
-
111
- opts
79
+ self . parse_options_from_client ( relevant) . await
112
80
}
113
81
Err ( why) => {
114
82
let message = format ! (
115
83
"Unable to pull client options via workspace/configuration request: {}" ,
116
84
why
117
85
) ;
118
86
println ! ( "{}" , message) ;
119
- self . client . log_message ( MessageType :: ERROR , message) ;
87
+ self . client . log_message ( MessageType :: ERROR , message) . await ;
120
88
None
121
89
}
122
90
}
@@ -137,7 +105,8 @@ impl Server {
137
105
. send_notification :: < ShowMessage > ( ShowMessageParams {
138
106
typ : MessageType :: INFO ,
139
107
message : format ! ( "diagnostics {}" , diagnostics. len( ) ) ,
140
- } ) ;
108
+ } )
109
+ . await ;
141
110
142
111
let params = PublishDiagnosticsParams {
143
112
uri,
@@ -146,11 +115,44 @@ impl Server {
146
115
} ;
147
116
148
117
self . client
149
- . send_notification :: < notification:: PublishDiagnostics > ( params) ;
118
+ . send_notification :: < notification:: PublishDiagnostics > ( params)
119
+ . await ;
150
120
}
151
121
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 ;
154
156
}
155
157
}
156
158
@@ -197,6 +199,8 @@ impl LanguageServer for Server {
197
199
}
198
200
199
201
async fn shutdown ( & self ) -> jsonrpc:: Result < ( ) > {
202
+ // TODO: Shutdown stuff.
203
+
200
204
self . client
201
205
. log_message ( MessageType :: INFO , "Postgres LSP terminated." )
202
206
. await ;
@@ -213,21 +217,41 @@ impl LanguageServer for Server {
213
217
. is_some_and ( |o| o. db_connection_string . is_some ( ) )
214
218
{
215
219
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
+ }
217
231
return ;
218
232
}
219
233
}
220
234
221
235
// if we couldn't pull settings from the client,
222
236
// 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 ;
224
238
225
239
if opts
226
240
. as_ref ( )
227
241
. is_some_and ( |o| o. db_connection_string . is_some ( ) )
228
242
{
229
243
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
+ }
231
255
}
232
256
}
233
257
@@ -268,7 +292,7 @@ impl LanguageServer for Server {
268
292
let mut uri = params. text_document . uri ;
269
293
normalize_uri ( & mut uri) ;
270
294
271
- self . debouncer . debounce ( uri) . await
295
+ self . publish_diagnostics_debounced ( uri) . await ;
272
296
}
273
297
274
298
async fn did_close ( & self , params : DidCloseTextDocumentParams ) {
0 commit comments