@@ -66,40 +66,72 @@ impl LspServer {
6666 }
6767 }
6868
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+ }
7694 } ;
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+ } ] ;
77103
78104 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 {
84106 Ok ( json) => {
85107 // The client reponse fits the requested `ConfigurationParams.items`,
86108 // so the first value is what we're looking for.
87- let relevant = json
88- . into_iter ( )
109+ json. into_iter ( )
89110 . 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." )
93112 }
94113 Err ( why) => {
95114 let message = format ! (
96115 "Unable to pull client options via workspace/configuration request: {}" ,
97116 why
98117 ) ;
99118 self . client . log_message ( MessageType :: ERROR , message) . await ;
100- None
119+ return ;
101120 }
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+ } ;
103135 }
104136
105137 #[ tracing:: instrument(
@@ -185,7 +217,11 @@ impl LanguageServer for LspServer {
185217 self . client
186218 . show_message ( MessageType :: INFO , "Initialize Request received" )
187219 . await ;
220+
188221 let flags = ClientFlags :: from_initialize_request_params ( & params) ;
222+
223+ tracing:: info!( "flags: {:?}" , flags) ;
224+
189225 self . client_capabilities . write ( ) . await . replace ( flags) ;
190226
191227 Ok ( InitializeResult {
@@ -220,6 +256,12 @@ impl LanguageServer for LspServer {
220256
221257 #[ tracing:: instrument( name = "initialized" , skip( self , _params) ) ]
222258 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+
223265 self . client
224266 . log_message ( MessageType :: INFO , "Postgres LSP Connected!" )
225267 . await ;
@@ -245,51 +287,11 @@ impl LanguageServer for LspServer {
245287 let capabilities = self . client_capabilities . read ( ) . await ;
246288
247289 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+ } ;
293295 }
294296
295297 #[ tracing:: instrument(
@@ -332,13 +334,9 @@ impl LanguageServer for LspServer {
332334
333335 self . publish_diagnostics ( uri) . await ;
334336
335- // TODO: "Compute Now"
336337 let changed_urls = self . session . recompute_and_get_changed_files ( ) . await ;
337338 for url in changed_urls {
338339 let url = Url :: from_file_path ( url. as_path ( ) ) . expect ( "Expected absolute File Path" ) ;
339-
340- tracing:: info!( "publishing diagnostics: {}" , url) ;
341-
342340 self . publish_diagnostics ( url) . await ;
343341 }
344342 }
0 commit comments