From f0e802f4903cee864b193beb2ddcd50d6d9f60c7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 13 Jan 2021 14:09:51 +0300 Subject: 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. --- crates/ide/src/references/rename.rs | 3 --- crates/rust-analyzer/src/handlers.rs | 14 ++++---------- crates/rust-analyzer/src/to_proto.rs | 7 ++++++- 3 files changed, 10 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 3edc43e08..3db08e84c 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 29cc9051e..a21571eea 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 bdddca9da..a7ff8975a 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; -- cgit v1.2.3