Skip to content

Commit 58a257b

Browse files
committed
rustdoc: add tooltips to sidebar
1 parent 6869645 commit 58a257b

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/librustdoc/html/render.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ use html::markdown::Markdown;
6666
use html::markdown;
6767
use stability_summary;
6868

69+
/// A pair of name and its optional document.
70+
#[deriving(Clone, Eq, Ord, PartialEq, PartialOrd)]
71+
pub struct NameDoc(String, Option<String>);
72+
6973
/// Major driving force in all rustdoc rendering. This contains information
7074
/// about where in the tree-like hierarchy rendering is occurring and controls
7175
/// how the current page is being rendered.
@@ -95,7 +99,7 @@ pub struct Context {
9599
/// functions), and the value is the list of containers belonging to this
96100
/// header. This map will change depending on the surrounding context of the
97101
/// page.
98-
pub sidebar: HashMap<String, Vec<String>>,
102+
pub sidebar: HashMap<String, Vec<NameDoc>>,
99103
/// This flag indicates whether [src] links should be generated or not. If
100104
/// the source files are present in the html rendering, then this will be
101105
/// `true`.
@@ -1245,7 +1249,7 @@ impl Context {
12451249
}
12461250
}
12471251

1248-
fn build_sidebar(&self, m: &clean::Module) -> HashMap<String, Vec<String>> {
1252+
fn build_sidebar(&self, m: &clean::Module) -> HashMap<String, Vec<NameDoc>> {
12491253
let mut map = HashMap::new();
12501254
for item in m.items.iter() {
12511255
if self.ignore_private_item(item) { continue }
@@ -1262,7 +1266,7 @@ impl Context {
12621266
let short = short.to_string();
12631267
let v = map.entry(short).get().unwrap_or_else(
12641268
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
1265-
v.push(myname);
1269+
v.push(NameDoc(myname, Some(shorter_line(item.doc_value()))));
12661270
}
12671271

12681272
for (_, items) in map.iter_mut() {
@@ -1476,6 +1480,11 @@ fn shorter<'a>(s: Option<&'a str>) -> &'a str {
14761480
}
14771481
}
14781482

1483+
#[inline]
1484+
fn shorter_line(s: Option<&str>) -> String {
1485+
shorter(s).replace("\n", " ")
1486+
}
1487+
14791488
fn document(w: &mut fmt::Formatter, item: &clean::Item) -> fmt::Result {
14801489
match item.doc_value() {
14811490
Some(s) => {
@@ -2213,21 +2222,22 @@ impl<'a> fmt::String for Sidebar<'a> {
22132222
None => return Ok(())
22142223
};
22152224
try!(write!(w, "<div class='block {}'><h2>{}</h2>", short, longty));
2216-
for item in items.iter() {
2225+
for &NameDoc(ref name, ref doc) in items.iter() {
22172226
let curty = shortty(cur).to_static_str();
2218-
let class = if cur.name.as_ref().unwrap() == item &&
2227+
let class = if cur.name.as_ref().unwrap() == name &&
22192228
short == curty { "current" } else { "" };
2220-
try!(write!(w, "<a class='{ty} {class}' href='{href}{path}'>\
2221-
{name}</a>",
2229+
try!(write!(w, "<a class='{ty} {class}' href='{href}{path}' \
2230+
title='{title}'>{name}</a>",
22222231
ty = short,
22232232
class = class,
22242233
href = if curty == "mod" {"../"} else {""},
22252234
path = if short == "mod" {
2226-
format!("{}/index.html", item.as_slice())
2235+
format!("{}/index.html", name.as_slice())
22272236
} else {
2228-
format!("{}.{}.html", short, item.as_slice())
2237+
format!("{}.{}.html", short, name.as_slice())
22292238
},
2230-
name = item.as_slice()));
2239+
title = doc.as_ref().unwrap().as_slice(),
2240+
name = name.as_slice()));
22312241
}
22322242
try!(write!(w, "</div>"));
22332243
Ok(())

src/librustdoc/html/static/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,10 @@
689689
if (crates[i] == window.currentCrate) {
690690
klass += ' current';
691691
}
692+
var desc = rawSearchIndex[crates[i]].items[0][3];
692693
div.append($('<a>', {'href': '../' + crates[i] + '/index.html',
693-
'class': klass}).text(crates[i]));
694+
'title': desc.replace(/\n/g, ' '),
695+
'class': klass}).text(crates[i]));
694696
}
695697
sidebar.append(div);
696698
}

0 commit comments

Comments
 (0)