Skip to content

Commit 6bc226f

Browse files
bors[bot]Veetaha
andauthored
Merge #3602
3602: ra_ide: remove dead code, migrate from readonly String -> &str r=matklad a=Veetaha https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/hover/near/190671355 Co-authored-by: veetaha <veetaha2@gmail.com>
2 parents af8097f + 98c34b7 commit 6bc226f

File tree

2 files changed

+33
-66
lines changed

2 files changed

+33
-66
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: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! FIXME: write short doc here
1+
//! Logic for computing info that is displayed when the user hovers over any
2+
//! source code items (e.g. function call, struct field, variable symbol...)
23
34
use hir::{
45
Adt, AsAssocItem, AssocItemContainer, FieldSource, HasSource, HirDisplay, ModuleDef,
@@ -24,35 +25,20 @@ use itertools::Itertools;
2425
use std::iter::once;
2526

2627
/// Contains the results when hovering over an item
27-
#[derive(Debug, Clone)]
28+
#[derive(Debug, Default)]
2829
pub struct HoverResult {
2930
results: Vec<String>,
30-
exact: bool,
31-
}
32-
33-
impl Default for HoverResult {
34-
fn default() -> Self {
35-
HoverResult::new()
36-
}
3731
}
3832

3933
impl HoverResult {
4034
pub fn new() -> HoverResult {
41-
HoverResult {
42-
results: Vec::new(),
43-
// We assume exact by default
44-
exact: true,
45-
}
35+
Self::default()
4636
}
4737

4838
pub fn extend(&mut self, item: Option<String>) {
4939
self.results.extend(item);
5040
}
5141

52-
pub fn is_exact(&self) -> bool {
53-
self.exact
54-
}
55-
5642
pub fn is_empty(&self) -> bool {
5743
self.results.is_empty()
5844
}
@@ -72,20 +58,7 @@ impl HoverResult {
7258
/// Returns the results converted into markup
7359
/// for displaying in a UI
7460
pub fn to_markup(&self) -> String {
75-
let mut markup = if !self.exact {
76-
let mut msg = String::from("Failed to exactly resolve the symbol. This is probably because rust_analyzer does not yet support traits.");
77-
if !self.results.is_empty() {
78-
msg.push_str(" \nThese items were found instead:");
79-
}
80-
msg.push_str("\n\n---\n");
81-
msg
82-
} else {
83-
String::new()
84-
};
85-
86-
markup.push_str(&self.results.join("\n\n---\n"));
87-
88-
markup
61+
self.results.join("\n\n---\n")
8962
}
9063
}
9164

@@ -94,10 +67,10 @@ fn hover_text(
9467
desc: Option<String>,
9568
mod_path: Option<String>,
9669
) -> Option<String> {
97-
match (desc, docs, mod_path) {
98-
(Some(desc), docs, mod_path) => Some(rust_code_markup_with_doc(desc, docs, mod_path)),
99-
(None, Some(docs), _) => Some(docs),
100-
_ => 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
10174
}
10275
}
10376

@@ -133,7 +106,7 @@ fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
133106
.flatten()
134107
.join("::")
135108
});
136-
mod_path
109+
mod_path // FIXME: replace dashes with underscores in crate display name
137110
}
138111

139112
fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<String> {
@@ -170,9 +143,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
170143
ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path),
171144
ModuleDef::BuiltinType(it) => Some(it.to_string()),
172145
},
173-
Definition::Local(it) => {
174-
Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string()))
175-
}
146+
Definition::Local(it) => Some(rust_code_markup(&it.ty(db).display_truncated(db, None))),
176147
Definition::TypeParam(_) | Definition::SelfType(_) => {
177148
// FIXME: Hover for generic param
178149
None
@@ -237,7 +208,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
237208
}
238209
}?;
239210

240-
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))));
241212
let range = sema.original_range(&node).range;
242213
Some(RangeInfo::new(range, res))
243214
}
@@ -595,7 +566,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
595566
);
596567
let hover = analysis.hover(position).unwrap().unwrap();
597568
assert_eq!(trim_markup_opt(hover.info.first()), Some("wrapper::Thing\nfn new() -> Thing"));
598-
assert_eq!(hover.info.is_exact(), true);
599569
}
600570

601571
#[test]
@@ -618,7 +588,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
618588
);
619589
let hover = analysis.hover(position).unwrap().unwrap();
620590
assert_eq!(trim_markup_opt(hover.info.first()), Some("const C: u32"));
621-
assert_eq!(hover.info.is_exact(), true);
622591
}
623592

624593
#[test]
@@ -635,7 +604,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
635604
);
636605
let hover = analysis.hover(position).unwrap().unwrap();
637606
assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing"));
638-
assert_eq!(hover.info.is_exact(), true);
639607

640608
/* FIXME: revive these tests
641609
let (analysis, position) = single_file_with_position(
@@ -651,7 +619,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
651619
652620
let hover = analysis.hover(position).unwrap().unwrap();
653621
assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing"));
654-
assert_eq!(hover.info.is_exact(), true);
655622
656623
let (analysis, position) = single_file_with_position(
657624
"
@@ -665,7 +632,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
665632
);
666633
let hover = analysis.hover(position).unwrap().unwrap();
667634
assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing"));
668-
assert_eq!(hover.info.is_exact(), true);
669635
670636
let (analysis, position) = single_file_with_position(
671637
"
@@ -678,7 +644,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
678644
);
679645
let hover = analysis.hover(position).unwrap().unwrap();
680646
assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing"));
681-
assert_eq!(hover.info.is_exact(), true);
682647
*/
683648
}
684649

@@ -696,7 +661,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
696661
);
697662
let hover = analysis.hover(position).unwrap().unwrap();
698663
assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
699-
assert_eq!(hover.info.is_exact(), true);
700664
}
701665

702666
#[test]
@@ -714,7 +678,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
714678
);
715679
let hover = analysis.hover(position).unwrap().unwrap();
716680
assert_eq!(trim_markup_opt(hover.info.first()), Some("macro_rules! foo"));
717-
assert_eq!(hover.info.is_exact(), true);
718681
}
719682

720683
#[test]
@@ -726,7 +689,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
726689
);
727690
let hover = analysis.hover(position).unwrap().unwrap();
728691
assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
729-
assert_eq!(hover.info.is_exact(), true);
730692
}
731693

732694
#[test]

0 commit comments

Comments
 (0)