From 98c34b725f6d154092e408a067f41663148b1ee1 Mon Sep 17 00:00:00 2001 From: veetaha Date: Mon, 16 Mar 2020 02:35:59 +0200 Subject: ra_ide: refactor readonly String -> &str --- crates/ra_ide/src/display.rs | 37 +++++++++++++++++++++---------------- crates/ra_ide/src/hover.rs | 16 +++++++--------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs index eaeaaa2b4..c395057a7 100644 --- a/crates/ra_ide/src/display.rs +++ b/crates/ra_ide/src/display.rs @@ -6,6 +6,8 @@ mod navigation_target; mod structure; mod short_label; +use std::fmt::{Display, Write}; + use ra_syntax::{ ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}, @@ -67,24 +69,27 @@ pub(crate) fn macro_label(node: &ast::MacroCall) -> String { format!("{}macro_rules! {}", vis, name) } -pub(crate) fn rust_code_markup>(val: CODE) -> String { - rust_code_markup_with_doc::<_, &str>(val, None, None) +pub(crate) fn rust_code_markup(code: &impl Display) -> String { + rust_code_markup_with_doc(code, None, None) } -pub(crate) fn rust_code_markup_with_doc( - val: CODE, - doc: Option, - mod_path: Option, -) -> String -where - CODE: AsRef, - DOC: AsRef, -{ - let mod_path = - mod_path.filter(|path| !path.is_empty()).map(|path| path + "\n").unwrap_or_default(); +pub(crate) fn rust_code_markup_with_doc( + code: &impl Display, + doc: Option<&str>, + mod_path: Option<&str>, +) -> String { + let mut markup = "```rust\n".to_owned(); + + if let Some(mod_path) = mod_path { + if !mod_path.is_empty() { + write!(markup, "{}\n", mod_path).unwrap(); + } + } + write!(markup, "{}\n```", code).unwrap(); + if let Some(doc) = doc { - format!("```rust\n{}{}\n```\n\n{}", mod_path, val.as_ref(), doc.as_ref()) - } else { - format!("```rust\n{}{}\n```", mod_path, val.as_ref()) + write!(markup, "\n\n{}", doc).unwrap(); } + + markup } diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 475ceadd1..3bdd61a2e 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -67,10 +67,10 @@ fn hover_text( desc: Option, mod_path: Option, ) -> Option { - match (desc, docs, mod_path) { - (Some(desc), docs, mod_path) => Some(rust_code_markup_with_doc(desc, docs, mod_path)), - (None, Some(docs), _) => Some(docs), - _ => None, + if let Some(desc) = desc { + Some(rust_code_markup_with_doc(&desc, docs.as_deref(), mod_path.as_deref())) + } else { + docs } } @@ -106,7 +106,7 @@ fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option { .flatten() .join("::") }); - mod_path + mod_path // FIXME: replace dashes with underscores in crate display name } fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option { @@ -143,9 +143,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option from_def_source(db, it, mod_path), ModuleDef::BuiltinType(it) => Some(it.to_string()), }, - Definition::Local(it) => { - Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string())) - } + Definition::Local(it) => Some(rust_code_markup(&it.ty(db).display_truncated(db, None))), Definition::TypeParam(_) | Definition::SelfType(_) => { // FIXME: Hover for generic param None @@ -210,7 +208,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option