@@ -4,23 +4,18 @@ use anyhow::Result;
44use log:: { error, info} ;
55use lsp_server:: { Connection , Message , Response } ;
66use lsp_types:: { notification:: Notification as LspNotification , request:: Request as LspRequest } ;
7+ use squawk_thread:: ThreadIntent ;
78
8- use crate :: system:: System ;
9+ use crate :: system:: { GlobalState , MutableSystem , System } ;
910
1011pub ( crate ) struct RequestDispatcher < ' a > {
11- connection : & ' a Connection ,
1212 req : Option < lsp_server:: Request > ,
13- system : & ' a dyn System ,
13+ system : & ' a mut GlobalState ,
1414}
1515
1616impl < ' a > RequestDispatcher < ' a > {
17- pub ( crate ) fn new (
18- connection : & ' a Connection ,
19- req : lsp_server:: Request ,
20- system : & ' a dyn System ,
21- ) -> Self {
17+ pub ( crate ) fn new ( req : lsp_server:: Request , system : & ' a mut GlobalState ) -> Self {
2218 Self {
23- connection,
2419 req : Some ( req) ,
2520 system,
2621 }
@@ -41,7 +36,7 @@ impl<'a> RequestDispatcher<'a> {
4136 lsp_server:: ErrorCode :: ParseError as i32 ,
4237 err. to_string ( ) ,
4338 ) ;
44- if let Err ( err) = self . connection . sender . send ( Message :: Response ( response) ) {
39+ if let Err ( err) = self . system . sender . send ( Message :: Response ( response) ) {
4540 error ! ( "Failed to send parse error response: {err}" ) ;
4641 }
4742 None
@@ -50,25 +45,54 @@ impl<'a> RequestDispatcher<'a> {
5045 }
5146
5247 pub ( crate ) fn on < R > (
48+ self ,
49+ handler : fn ( & dyn System , R :: Params ) -> Result < R :: Result > ,
50+ ) -> Result < Self >
51+ where
52+ R : LspRequest ,
53+ R :: Params : Send + ' static ,
54+ {
55+ self . on_with_thread_intent :: < R > ( ThreadIntent :: Worker , handler)
56+ }
57+
58+ pub ( crate ) fn on_latency_sensitive < R > (
59+ self ,
60+ handler : fn ( & dyn System , R :: Params ) -> Result < R :: Result > ,
61+ ) -> Result < Self >
62+ where
63+ R : LspRequest ,
64+ R :: Params : Send + ' static ,
65+ {
66+ self . on_with_thread_intent :: < R > ( ThreadIntent :: LatencySensitive , handler)
67+ }
68+
69+ fn on_with_thread_intent < R > (
5370 mut self ,
71+ intent : ThreadIntent ,
5472 handler : fn ( & dyn System , R :: Params ) -> Result < R :: Result > ,
5573 ) -> Result < Self >
5674 where
5775 R : LspRequest ,
76+ R :: Params : Send + ' static ,
5877 {
5978 if let Some ( ( id, params) ) = self . parse :: < R > ( ) {
60- let resp = match handler ( self . system , params) {
61- Ok ( result) => Response :: new_ok ( id, result) ,
62- Err ( err) => {
63- error ! ( "Request handler failed: {err}" ) ;
64- Response :: new_err (
65- id,
66- lsp_server:: ErrorCode :: InternalError as i32 ,
67- err. to_string ( ) ,
68- )
69- }
70- } ;
71- self . connection . sender . send ( Message :: Response ( resp) ) ?;
79+ let snapshot = self . system . snapshot ( ) ;
80+
81+ self . system . task_pool . spawn ( intent, move || {
82+ let resp = match handler ( & snapshot, params) {
83+ Ok ( result) => Response :: new_ok ( id, result) ,
84+ Err ( err) => {
85+ error ! ( "Request handler failed: {err}" ) ;
86+ Response :: new_err (
87+ id,
88+ lsp_server:: ErrorCode :: InternalError as i32 ,
89+ err. to_string ( ) ,
90+ )
91+ }
92+ } ;
93+
94+ Message :: Response ( resp)
95+ } ) ;
7296 }
7397
7498 Ok ( self )
@@ -84,14 +108,14 @@ impl<'a> RequestDispatcher<'a> {
84108pub ( crate ) struct NotificationDispatcher < ' a > {
85109 connection : & ' a Connection ,
86110 notif : Option < lsp_server:: Notification > ,
87- system : & ' a mut dyn System ,
111+ system : & ' a mut dyn MutableSystem ,
88112}
89113
90114impl < ' a > NotificationDispatcher < ' a > {
91115 pub ( crate ) fn new (
92116 connection : & ' a Connection ,
93117 notif : lsp_server:: Notification ,
94- system : & ' a mut dyn System ,
118+ system : & ' a mut dyn MutableSystem ,
95119 ) -> Self {
96120 Self {
97121 connection,
@@ -119,7 +143,7 @@ impl<'a> NotificationDispatcher<'a> {
119143
120144 pub ( crate ) fn on < N > (
121145 mut self ,
122- handler : fn ( & Connection , N :: Params , & mut dyn System ) -> Result < ( ) > ,
146+ handler : fn ( & Connection , N :: Params , & mut dyn MutableSystem ) -> Result < ( ) > ,
123147 ) -> Result < Self >
124148 where
125149 N : LspNotification ,
0 commit comments