@@ -66,40 +66,72 @@ impl LspServer {
66
66
}
67
67
}
68
68
69
- #[ tracing:: instrument( name = "Requesting Configuration from Client" , skip( self ) ) ]
70
- async fn request_config_from_client ( & self ) -> Option < ClientConfigurationOptions > {
71
- let params = ConfigurationParams {
72
- items : vec ! [ ConfigurationItem {
73
- section: Some ( "pglsp" . to_string( ) ) ,
74
- scope_uri: None ,
75
- } ] ,
69
+ #[ tracing:: instrument( name = "Processing Config" , skip( self ) ) ]
70
+ async fn process_config ( & self , opts : Option < ClientConfigurationOptions > ) -> anyhow:: Result < ( ) > {
71
+ if opts
72
+ . as_ref ( )
73
+ . is_some_and ( |o| o. db_connection_string . is_some ( ) )
74
+ {
75
+ let conn_str = opts. unwrap ( ) . db_connection_string . unwrap ( ) ;
76
+ self . session . change_db ( conn_str) . await
77
+ } else {
78
+ Ok ( ( ) )
79
+ }
80
+ }
81
+
82
+ async fn parse_and_handle_config_from_client ( & self , value : serde_json:: Value ) {
83
+ let parsed = self . parse_config_from_client ( value) . await ;
84
+ match self . process_config ( parsed) . await {
85
+ Ok ( _) => { }
86
+ Err ( e) => {
87
+ self . client
88
+ . show_message (
89
+ MessageType :: ERROR ,
90
+ format ! ( "Unable to parse received config: {e:?}" ) ,
91
+ )
92
+ . await ;
93
+ }
76
94
} ;
95
+ }
96
+
97
+ #[ tracing:: instrument( name = "Requesting & Handling Configuration from Client" , skip( self ) ) ]
98
+ async fn request_and_handle_config_from_client ( & self ) {
99
+ let config_items = vec ! [ ConfigurationItem {
100
+ section: Some ( "pglsp" . to_string( ) ) ,
101
+ scope_uri: None ,
102
+ } ] ;
77
103
78
104
tracing:: info!( "sending workspace/configuration request" ) ;
79
- match self
80
- . client
81
- . send_request :: < request:: WorkspaceConfiguration > ( params)
82
- . await
83
- {
105
+ let config = match self . client . configuration ( config_items) . await {
84
106
Ok ( json) => {
85
107
// The client reponse fits the requested `ConfigurationParams.items`,
86
108
// so the first value is what we're looking for.
87
- let relevant = json
88
- . into_iter ( )
109
+ json. into_iter ( )
89
110
. next ( )
90
- . expect ( "workspace/configuration request did not yield expected response." ) ;
91
-
92
- self . parse_config_from_client ( relevant) . await
111
+ . expect ( "workspace/configuration request did not yield expected response." )
93
112
}
94
113
Err ( why) => {
95
114
let message = format ! (
96
115
"Unable to pull client options via workspace/configuration request: {}" ,
97
116
why
98
117
) ;
99
118
self . client . log_message ( MessageType :: ERROR , message) . await ;
100
- None
119
+ return ;
101
120
}
102
- }
121
+ } ;
122
+
123
+ let parsed = self . parse_config_from_client ( config) . await ;
124
+ match self . process_config ( parsed) . await {
125
+ Ok ( ( ) ) => { }
126
+ Err ( e) => {
127
+ self . client
128
+ . log_message (
129
+ MessageType :: ERROR ,
130
+ format ! ( "Unable to process config from client: {e:?}" ) ,
131
+ )
132
+ . await
133
+ }
134
+ } ;
103
135
}
104
136
105
137
#[ tracing:: instrument(
@@ -185,7 +217,11 @@ impl LanguageServer for LspServer {
185
217
self . client
186
218
. show_message ( MessageType :: INFO , "Initialize Request received" )
187
219
. await ;
220
+
188
221
let flags = ClientFlags :: from_initialize_request_params ( & params) ;
222
+
223
+ tracing:: info!( "flags: {:?}" , flags) ;
224
+
189
225
self . client_capabilities . write ( ) . await . replace ( flags) ;
190
226
191
227
Ok ( InitializeResult {
@@ -220,6 +256,12 @@ impl LanguageServer for LspServer {
220
256
221
257
#[ tracing:: instrument( name = "initialized" , skip( self , _params) ) ]
222
258
async fn initialized ( & self , _params : InitializedParams ) {
259
+ let capabilities = self . client_capabilities . read ( ) . await ;
260
+
261
+ if capabilities. as_ref ( ) . unwrap ( ) . supports_pull_opts {
262
+ self . request_and_handle_config_from_client ( ) . await ;
263
+ }
264
+
223
265
self . client
224
266
. log_message ( MessageType :: INFO , "Postgres LSP Connected!" )
225
267
. await ;
@@ -245,51 +287,11 @@ impl LanguageServer for LspServer {
245
287
let capabilities = self . client_capabilities . read ( ) . await ;
246
288
247
289
if capabilities. as_ref ( ) . unwrap ( ) . supports_pull_opts {
248
- let opts = self . request_config_from_client ( ) . await ;
249
- if opts
250
- . as_ref ( )
251
- . is_some_and ( |o| o. db_connection_string . is_some ( ) )
252
- {
253
- let conn_str = opts. unwrap ( ) . db_connection_string . unwrap ( ) ;
254
- match self . session . change_db ( conn_str) . await {
255
- Ok ( _) => { }
256
- Err ( err) => {
257
- self . client
258
- . show_message (
259
- MessageType :: ERROR ,
260
- format ! ( "Pulled Client Options but failed to set them: {}" , err) ,
261
- )
262
- . await
263
- }
264
- }
265
- return ;
266
- }
267
- }
268
-
269
- // if we couldn't pull settings from the client,
270
- // we'll try parsing the passed in params.
271
- let opts = self . parse_config_from_client ( params. settings ) . await ;
272
-
273
- if opts
274
- . as_ref ( )
275
- . is_some_and ( |o| o. db_connection_string . is_some ( ) )
276
- {
277
- let conn_str = opts. unwrap ( ) . db_connection_string . unwrap ( ) ;
278
- match self . session . change_db ( conn_str) . await {
279
- Ok ( _) => { }
280
- Err ( err) => {
281
- self . client
282
- . show_message (
283
- MessageType :: ERROR ,
284
- format ! (
285
- "Used Client Options from params but failed to set them: {}" ,
286
- err
287
- ) ,
288
- )
289
- . await
290
- }
291
- }
292
- }
290
+ self . request_and_handle_config_from_client ( ) . await
291
+ } else {
292
+ self . parse_and_handle_config_from_client ( params. settings )
293
+ . await
294
+ } ;
293
295
}
294
296
295
297
#[ tracing:: instrument(
@@ -332,13 +334,9 @@ impl LanguageServer for LspServer {
332
334
333
335
self . publish_diagnostics ( uri) . await ;
334
336
335
- // TODO: "Compute Now"
336
337
let changed_urls = self . session . recompute_and_get_changed_files ( ) . await ;
337
338
for url in changed_urls {
338
339
let url = Url :: from_file_path ( url. as_path ( ) ) . expect ( "Expected absolute File Path" ) ;
339
-
340
- tracing:: info!( "publishing diagnostics: {}" , url) ;
341
-
342
340
self . publish_diagnostics ( url) . await ;
343
341
}
344
342
}
0 commit comments