about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-01-13 14:09:51 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-01-13 15:07:30 +0300
commitf0e802f4903cee864b193beb2ddcd50d6d9f60c7 (patch)
tree9830f7b3a9607d7b385fb3311ef078ade5a42768
parent52fa926f005890f07dffc789c84c2be57a6bdccc (diff)
downloadrust-f0e802f4903cee864b193beb2ddcd50d6d9f60c7.tar.gz
rust-f0e802f4903cee864b193beb2ddcd50d6d9f60c7.zip
Don't show internal server error on rename
Doesn't quite work due to https://github.com/microsoft/vscode-languageserver-node/issues/730

Note that this intentionally removes `impl std::Error for RenameError`
-- we nether want to blindly bubble the rename error.
-rw-r--r--crates/ide/src/references/rename.rs3
-rw-r--r--crates/rust-analyzer/src/handlers.rs14
-rw-r--r--crates/rust-analyzer/src/to_proto.rs7
3 files changed, 10 insertions, 14 deletions
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs
index 3edc43e0828..3db08e84cfc 100644
--- a/crates/ide/src/references/rename.rs
+++ b/crates/ide/src/references/rename.rs
@@ -1,7 +1,6 @@
 //! FIXME: write short doc here
 use std::{
     convert::TryInto,
-    error::Error,
     fmt::{self, Display},
 };
 
@@ -34,8 +33,6 @@ impl fmt::Display for RenameError {
     }
 }
 
-impl Error for RenameError {}
-
 macro_rules! format_err {
     ($fmt:expr) => {RenameError(format!($fmt))};
     ($fmt:expr, $($arg:tt)+) => {RenameError(format!($fmt, $($arg)+))}
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index 29cc9051ee7..a21571eea02 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -773,7 +773,8 @@ pub(crate) fn handle_prepare_rename(
     let _p = profile::span("handle_prepare_rename");
     let position = from_proto::file_position(&snap, params)?;
 
-    let change = snap.analysis.prepare_rename(position)??;
+    let change = snap.analysis.prepare_rename(position)?.map_err(to_proto::rename_error)?;
+
     let line_index = snap.analysis.file_line_index(position.file_id)?;
     let range = to_proto::range(&line_index, change.range);
     Ok(Some(PrepareRenameResponse::Range(range)))
@@ -786,15 +787,8 @@ pub(crate) fn handle_rename(
     let _p = profile::span("handle_rename");
     let position = from_proto::file_position(&snap, params.text_document_position)?;
 
-    if params.new_name.is_empty() {
-        return Err(LspError::new(
-            ErrorCode::InvalidParams as i32,
-            "New Name cannot be empty".into(),
-        )
-        .into());
-    }
-
-    let change = snap.analysis.rename(position, &*params.new_name)??;
+    let change =
+        snap.analysis.rename(position, &*params.new_name)?.map_err(to_proto::rename_error)?;
     let workspace_edit = to_proto::workspace_edit(&snap, change.info)?;
     Ok(Some(workspace_edit))
 }
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index bdddca9daa2..a7ff8975adf 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -8,7 +8,8 @@ use ide::{
     Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind, Documentation, FileId,
     FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HlMod, HlPunct, HlRange, HlTag, Indel,
     InlayHint, InlayKind, InsertTextFormat, LineIndex, Markup, NavigationTarget, ReferenceAccess,
-    Runnable, Severity, SourceChange, SourceFileEdit, SymbolKind, TextEdit, TextRange, TextSize,
+    RenameError, Runnable, Severity, SourceChange, SourceFileEdit, SymbolKind, TextEdit, TextRange,
+    TextSize,
 };
 use itertools::Itertools;
 
@@ -855,6 +856,10 @@ pub(crate) fn markup_content(markup: Markup) -> lsp_types::MarkupContent {
     lsp_types::MarkupContent { kind: lsp_types::MarkupKind::Markdown, value }
 }
 
+pub(crate) fn rename_error(err: RenameError) -> crate::LspError {
+    crate::LspError { code: lsp_server::ErrorCode::InvalidParams as i32, message: err.to_string() }
+}
+
 #[cfg(test)]
 mod tests {
     use ide::Analysis;