ra_ide: refactor readonly String -> &str

This commit is contained in:
veetaha 2020-03-16 02:35:59 +02:00
parent 4fd07a02a2
commit 98c34b725f
2 changed files with 29 additions and 26 deletions
crates/ra_ide/src

@ -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<CODE: AsRef<str>>(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<CODE, DOC>(
val: CODE,
doc: Option<DOC>,
mod_path: Option<String>,
) -> String
where
CODE: AsRef<str>,
DOC: AsRef<str>,
{
let mod_path =
mod_path.filter(|path| !path.is_empty()).map(|path| path + "\n").unwrap_or_default();
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())
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 {
write!(markup, "\n\n{}", doc).unwrap();
}
markup
}

@ -67,10 +67,10 @@ fn hover_text(
desc: Option<String>,
mod_path: Option<String>,
) -> Option<String> {
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<String> {
.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<String> {
@ -143,9 +143,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
ModuleDef::TypeAlias(it) => 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<RangeIn
}
}?;
res.extend(Some(rust_code_markup(ty.display_truncated(db, None).to_string())));
res.extend(Some(rust_code_markup(&ty.display_truncated(db, None))));
let range = sema.original_range(&node).range;
Some(RangeInfo::new(range, res))
}