Skip to content

Commit eb084b1

Browse files
committed
rustdoc: Make static initalizers prettier
Previously, if an initializer took multiple lines or was just large in general, it was pretty poorly rendered [1] [2]. This alters the logic to just link back to the source for any multi-line static, with a placeholder of "[definition]". This should make reading statics a little easier on the eyes. All single-line statics are still inlined in the documentation. Closes #13198 [1] - http://static.rust-lang.org/doc/master/sync/mutex/index.html#statics [2] - http://static.rust-lang.org/doc/master/std/sync/atomics/index.html#statics
1 parent 71a52a2 commit eb084b1

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

src/librustdoc/html/render.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,23 @@ impl<'a> Item<'a> {
930930
clean::ModuleItem(..) => true, _ => false
931931
}
932932
}
933+
934+
fn link(&self) -> ~str {
935+
let mut path = Vec::new();
936+
clean_srcpath(self.item.source.filename.as_bytes(), |component| {
937+
path.push(component.to_owned());
938+
});
939+
let href = if self.item.source.loline == self.item.source.hiline {
940+
format!("{}", self.item.source.loline)
941+
} else {
942+
format!("{}-{}", self.item.source.loline, self.item.source.hiline)
943+
};
944+
format!("{root}src/{krate}/{path}.html\\#{href}",
945+
root = self.cx.root_path,
946+
krate = self.cx.layout.krate,
947+
path = path.connect("/"),
948+
href = href)
949+
}
933950
}
934951

935952
impl<'a> fmt::Show for Item<'a> {
@@ -977,23 +994,8 @@ impl<'a> fmt::Show for Item<'a> {
977994

978995
// Write `src` tag
979996
if self.cx.include_sources {
980-
let mut path = Vec::new();
981-
clean_srcpath(self.item.source.filename.as_bytes(), |component| {
982-
path.push(component.to_owned());
983-
});
984-
let href = if self.item.source.loline == self.item.source.hiline {
985-
format!("{}", self.item.source.loline)
986-
} else {
987-
format!("{}-{}", self.item.source.loline, self.item.source.hiline)
988-
};
989-
try!(write!(fmt.buf,
990-
"<a class='source' \
991-
href='{root}src/{krate}/{path}.html\\#{href}'>\
992-
[src]</a>",
993-
root = self.cx.root_path,
994-
krate = self.cx.layout.krate,
995-
path = path.connect("/"),
996-
href = href));
997+
try!(write!(fmt.buf, "<a class='source' href='{}'>[src]</a>",
998+
self.link()));
997999
}
9981000
try!(write!(fmt.buf, "</h1>\n"));
9991001

@@ -1138,16 +1140,19 @@ fn item_module(w: &mut Writer, cx: &Context,
11381140

11391141
match myitem.inner {
11401142
clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => {
1141-
struct Initializer<'a>(&'a str);
1143+
struct Initializer<'a>(&'a str, Item<'a>);
11421144
impl<'a> fmt::Show for Initializer<'a> {
11431145
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1144-
let Initializer(s) = *self;
1146+
let Initializer(s, item) = *self;
11451147
if s.len() == 0 { return Ok(()); }
11461148
try!(write!(f.buf, "<code> = </code>"));
1147-
let tag = if s.contains("\n") { "pre" } else { "code" };
1148-
try!(write!(f.buf, "<{tag}>{}</{tag}>",
1149-
s.as_slice(), tag=tag));
1150-
Ok(())
1149+
if s.contains("\n") {
1150+
write!(f.buf,
1151+
"<a href='{}'>[definition]</a>",
1152+
item.link())
1153+
} else {
1154+
write!(f.buf, "<code>{}</code>", s.as_slice())
1155+
}
11511156
}
11521157
}
11531158

@@ -1160,7 +1165,7 @@ fn item_module(w: &mut Writer, cx: &Context,
11601165
VisSpace(myitem.visibility),
11611166
*myitem.name.get_ref(),
11621167
s.type_,
1163-
Initializer(s.expr),
1168+
Initializer(s.expr, Item { cx: cx, item: myitem }),
11641169
Markdown(blank(myitem.doc_value()))));
11651170
}
11661171

0 commit comments

Comments
 (0)