1+ use anyhow:: Result ;
12use serde:: { Deserialize , Serialize } ;
23use serde_json:: Value ;
34use std:: collections:: { HashMap , HashSet } ;
@@ -215,11 +216,11 @@ impl Lsp {
215216 }
216217 }
217218
218- pub async fn init ( & mut self , dir : & str ) {
219+ pub async fn init ( & mut self , dir : & str ) -> Result < ( ) > {
219220 let id = 0 ;
220221 let ( tx, rx) = mpsc:: channel :: < String > ( 1 ) ;
221222 self . add_pending ( id, tx) . await ;
222- let message = lsp_messages:: initialize ( dir, self . lsp_name . as_deref ( ) ) ;
223+ let message = lsp_messages:: initialize ( dir, self . lsp_name . as_deref ( ) ) ? ;
223224 self . send_async ( message) ;
224225 self . wait ( 5 , rx) . await ;
225226 self . remove_pending ( id) . await ;
@@ -231,7 +232,8 @@ impl Lsp {
231232 self . did_change_configuration ( settings) ;
232233 }
233234
234- self . ready . store ( true , Ordering :: SeqCst )
235+ self . ready . store ( true , Ordering :: SeqCst ) ;
236+ Ok ( ( ) )
235237 }
236238
237239 pub fn send_notification < N > ( & self , params : N :: Params )
@@ -308,41 +310,41 @@ impl Lsp {
308310 self . send_notification :: < DidChangeConfiguration > ( params) ;
309311 }
310312
311- pub fn did_open ( & mut self , lang : & str , path : & str , text : & str ) {
313+ pub fn did_open ( & mut self , lang : & str , path : & str , text : & str ) -> Result < ( ) > {
312314 self . opened . insert ( path. to_string ( ) ) ;
313-
315+ let uri = path_to_uri ( path ) ? ;
314316 let params = DidOpenTextDocumentParams {
315317 text_document : TextDocumentItem {
316- uri : path_to_uri ( path ) . unwrap ( ) ,
318+ uri,
317319 language_id : lang. to_string ( ) ,
318320 version : 0 ,
319321 text : text. to_string ( ) ,
320322 } ,
321323 } ;
322324 self . send_notification :: < DidOpenTextDocument > ( params) ;
325+ Ok ( ( ) )
323326 }
324327
325- pub fn did_close ( & mut self , path : & str ) {
328+ pub fn did_close ( & mut self , path : & str ) -> Result < ( ) > {
326329 if !self . opened . remove ( path) {
327- return ;
330+ return Ok ( ( ) ) ;
328331 }
332+ let uri = path_to_uri ( path) ?;
329333 let params = DidCloseTextDocumentParams {
330- text_document : TextDocumentIdentifier {
331- uri : path_to_uri ( path) . unwrap ( ) ,
332- } ,
334+ text_document : TextDocumentIdentifier { uri } ,
333335 } ;
334336 self . send_notification :: < DidCloseTextDocument > ( params) ;
337+ Ok ( ( ) )
335338 }
336339
337- pub fn did_save ( & mut self , path : & str , text : Option < & str > ) {
340+ pub fn did_save ( & mut self , path : & str ) -> Result < ( ) > {
341+ let uri = path_to_uri ( path) ?;
338342 let params = DidSaveTextDocumentParams {
339- text_document : TextDocumentIdentifier {
340- uri : path_to_uri ( path) . unwrap ( ) ,
341- } ,
343+ text_document : TextDocumentIdentifier { uri } ,
342344 text : None ,
343- // text: text.map(|s| s.to_string()),
344345 } ;
345346 self . send_notification :: < DidSaveTextDocument > ( params) ;
347+ Ok ( ( ) )
346348 }
347349
348350 fn get_next_version ( & mut self , path : & str ) -> usize {
@@ -366,7 +368,7 @@ impl Lsp {
366368 end_column : usize ,
367369 path : & str ,
368370 text : & str ,
369- ) {
371+ ) -> Result < ( ) > {
370372 self . did_change_multi (
371373 path,
372374 vec ! [ TextDocumentContentChangeEvent {
@@ -378,23 +380,25 @@ impl Lsp {
378380 text: text. to_string( ) ,
379381 } ] ,
380382 )
381- . await ;
383+ . await
382384 }
383385
384386 pub async fn did_change_multi (
385387 & mut self ,
386388 path : & str ,
387389 content_changes : Vec < TextDocumentContentChangeEvent > ,
388- ) {
390+ ) -> Result < ( ) > {
391+ let uri = path_to_uri ( path) ?;
389392 let params = DidChangeTextDocumentParams {
390393 text_document : VersionedTextDocumentIdentifier {
391- uri : path_to_uri ( path ) . unwrap ( ) ,
394+ uri,
392395 version : self . get_next_version ( path) as i32 ,
393396 } ,
394397 content_changes,
395398 } ;
396399
397400 self . send_notification :: < DidChangeTextDocument > ( params) ;
401+ Ok ( ( ) )
398402 }
399403
400404 pub async fn completion (
@@ -536,17 +540,16 @@ mod tests {
536540 None ,
537541 ) ?;
538542
539- let dir = std:: env:: current_dir ( )
540- . unwrap ( )
543+ let dir = std:: env:: current_dir ( ) ?
541544 . to_string_lossy ( )
542545 . into_owned ( ) ;
543546
544- lsp. init ( & dir) . await ;
547+ lsp. init ( & dir) . await ? ;
545548
546549 let content = r#"for i in range(10000): print(i)"# ;
547550 let file_path = "fast.py" ;
548551
549- lsp. did_open ( lang, file_path, content) ;
552+ lsp. did_open ( lang, file_path, content) ? ;
550553
551554 // Test completion on 'range'
552555 let completions = lsp. completion ( file_path, 0 , 12 ) . await ?;
@@ -665,8 +668,8 @@ pub mod lsp_messages {
665668 }
666669 }
667670
668- pub fn initialize ( dir : & str , lsp_name : Option < & str > ) -> String {
669- let uri: Uri = path_to_uri ( dir) . unwrap ( ) ;
671+ pub fn initialize ( dir : & str , lsp_name : Option < & str > ) -> Result < String > {
672+ let uri: Uri = path_to_uri ( dir) ? ;
670673
671674 let path = std:: path:: Path :: new ( dir) ;
672675 let folder_name = path
@@ -743,7 +746,7 @@ pub mod lsp_messages {
743746 "params" : params
744747 } ) ;
745748
746- to_string ( & request) . unwrap ( )
749+ Ok ( to_string ( & request) ? )
747750 }
748751}
749752
@@ -804,7 +807,10 @@ impl LspManager {
804807 . to_string_lossy ( )
805808 . into_owned ( ) ;
806809
807- lsp. init ( & dir) . await ;
810+ if let Err ( e) = lsp. init ( & dir) . await {
811+ error ! ( "error initializing lsp process {}: {}" , & lsp_cmd, e) ;
812+ return ;
813+ }
808814
809815 self . lang2lsp . insert ( lang, lsp) ;
810816 }
0 commit comments