From 29394db26bf547b83502351e22ab23f70180bebd Mon Sep 17 00:00:00 2001 From: Ron Waldon Date: Thu, 28 Sep 2023 19:44:27 +1000 Subject: [PATCH] fix(goto-def): handle empty results --- src/main.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8d96e35..6bb4f8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ struct Backend { #[tower_lsp::async_trait] impl LanguageServer for Backend { - async fn initialize(&self, _: InitializeParams) -> Result { + async fn initialize(&self, _params: InitializeParams) -> Result { Ok(InitializeResult { capabilities: ServerCapabilities { // TODO: `nu --ide-ast` @@ -43,7 +43,7 @@ impl LanguageServer for Backend { }) } - async fn initialized(&self, _: InitializedParams) { + async fn initialized(&self, _params: InitializedParams) { self.client .log_message(MessageType::INFO, "server initialized!") .await; @@ -71,13 +71,13 @@ impl LanguageServer for Backend { let text = tokio::fs::read_to_string(&file_path).await.map_err(|e| { tower_lsp::jsonrpc::Error::invalid_params(format!("cannot read file: {}", e)) })?; - let index = convert_position(¶ms.text_document_position.position, &text); + let offset = convert_position(¶ms.text_document_position.position, &text); // TODO: call nushell Rust code directly instead of via separate process let output = tokio::process::Command::new("nu") .args([ "--ide-complete", - &format!("{}", index), + &format!("{}", offset), &format!("{}", file_path.display()), ]) .output() @@ -124,13 +124,13 @@ impl LanguageServer for Backend { let text = tokio::fs::read_to_string(&file_path).await.map_err(|e| { tower_lsp::jsonrpc::Error::invalid_params(format!("cannot read file: {}", e)) })?; - let index = convert_position(¶ms.text_document_position_params.position, &text); + let offset = convert_position(¶ms.text_document_position_params.position, &text); // TODO: call nushell Rust code directly instead of via separate process let output = tokio::process::Command::new("nu") .args([ "--ide-goto-def", - &format!("{}", index), + &format!("{}", offset), &format!("{}", file_path.display()), ]) .output() @@ -152,6 +152,23 @@ impl LanguageServer for Backend { let line_breaks = find_line_breaks(&text); + if matches!( + goto_def.file.to_str(), + None | Some("") | Some("__prelude__") + ) { + return Ok(None); + } + + if !goto_def.file.exists() { + self.client + .log_message( + MessageType::ERROR, + format!("File {} does not exist", goto_def.file.display()), + ) + .await; + return Ok(None); + } + Ok(Some(GotoDefinitionResponse::Scalar(Location { uri: Url::from_file_path(goto_def.file).map_err(|e| { let mut err = tower_lsp::jsonrpc::Error::parse_error(); @@ -183,13 +200,13 @@ impl LanguageServer for Backend { let text = tokio::fs::read_to_string(&file_path).await.map_err(|e| { tower_lsp::jsonrpc::Error::invalid_params(format!("cannot read file: {}", e)) })?; - let index = convert_position(¶ms.text_document_position_params.position, &text); + let offset = convert_position(¶ms.text_document_position_params.position, &text); // TODO: call nushell Rust code directly instead of via separate process let output = tokio::process::Command::new("nu") .args([ "--ide-hover", - &format!("{}", index), + &format!("{}", offset), &format!("{}", file_path.display()), ]) .output()