Skip to content

Commit 98c34b7

Browse files
committed
ra_ide: refactor readonly String -> &str
1 parent 4fd07a0 commit 98c34b7

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

crates/ra_ide/src/display.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ mod navigation_target;
66
mod structure;
77
mod short_label;
88

9+
use std::fmt::{Display, Write};
10+
911
use ra_syntax::{
1012
ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
1113
SyntaxKind::{ATTR, COMMENT},
@@ -67,24 +69,27 @@ pub(crate) fn macro_label(node: &ast::MacroCall) -> String {
6769
format!("{}macro_rules! {}", vis, name)
6870
}
6971

70-
pub(crate) fn rust_code_markup<CODE: AsRef<str>>(val: CODE) -> String {
71-
rust_code_markup_with_doc::<_, &str>(val, None, None)
72+
pub(crate) fn rust_code_markup(code: &impl Display) -> String {
73+
rust_code_markup_with_doc(code, None, None)
7274
}
7375

74-
pub(crate) fn rust_code_markup_with_doc<CODE, DOC>(
75-
val: CODE,
76-
doc: Option<DOC>,
77-
mod_path: Option<String>,
78-
) -> String
79-
where
80-
CODE: AsRef<str>,
81-
DOC: AsRef<str>,
82-
{
83-
let mod_path =
84-
mod_path.filter(|path| !path.is_empty()).map(|path| path + "\n").unwrap_or_default();
76+
pub(crate) fn rust_code_markup_with_doc(
77+
code: &impl Display,
78+
doc: Option<&str>,
79+
mod_path: Option<&str>,
80+
) -> String {
81+
let mut markup = "```rust\n".to_owned();
82+
83+
if let Some(mod_path) = mod_path {
84+
if !mod_path.is_empty() {
85+
write!(markup, "{}\n", mod_path).unwrap();
86+
}
87+
}
88+
write!(markup, "{}\n```", code).unwrap();
89+
8590
if let Some(doc) = doc {
86-
format!("```rust\n{}{}\n```\n\n{}", mod_path, val.as_ref(), doc.as_ref())
87-
} else {
88-
format!("```rust\n{}{}\n```", mod_path, val.as_ref())
91+
write!(markup, "\n\n{}", doc).unwrap();
8992
}
93+
94+
markup
9095
}

crates/ra_ide/src/hover.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ fn hover_text(
6767
desc: Option<String>,
6868
mod_path: Option<String>,
6969
) -> Option<String> {
70-
match (desc, docs, mod_path) {
71-
(Some(desc), docs, mod_path) => Some(rust_code_markup_with_doc(desc, docs, mod_path)),
72-
(None, Some(docs), _) => Some(docs),
73-
_ => None,
70+
if let Some(desc) = desc {
71+
Some(rust_code_markup_with_doc(&desc, docs.as_deref(), mod_path.as_deref()))
72+
} else {
73+
docs
7474
}
7575
}
7676

@@ -106,7 +106,7 @@ fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
106106
.flatten()
107107
.join("::")
108108
});
109-
mod_path
109+
mod_path // FIXME: replace dashes with underscores in crate display name
110110
}
111111

112112
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
143143
ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path),
144144
ModuleDef::BuiltinType(it) => Some(it.to_string()),
145145
},
146-
Definition::Local(it) => {
147-
Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string()))
148-
}
146+
Definition::Local(it) => Some(rust_code_markup(&it.ty(db).display_truncated(db, None))),
149147
Definition::TypeParam(_) | Definition::SelfType(_) => {
150148
// FIXME: Hover for generic param
151149
None
@@ -210,7 +208,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
210208
}
211209
}?;
212210

213-
res.extend(Some(rust_code_markup(ty.display_truncated(db, None).to_string())));
211+
res.extend(Some(rust_code_markup(&ty.display_truncated(db, None))));
214212
let range = sema.original_range(&node).range;
215213
Some(RangeInfo::new(range, res))
216214
}

0 commit comments

Comments
 (0)