Skip to content

rustdoc: Linkify extern crates #33196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use rustc_trans::back::link;
use rustc::middle::cstore::{self, CrateStore};
use rustc::middle::privacy::AccessLevels;
use rustc::hir::def::Def;
use rustc::hir::def_id::{DefId, DefIndex};
use rustc::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
use rustc::ty::subst::{self, ParamSpace, VecPerParamSpace};
use rustc::ty;
use rustc::middle::stability;
Expand Down Expand Up @@ -2388,7 +2388,7 @@ impl Clean<Item> for doctree::ExternCrate {
name: None,
attrs: self.attrs.clean(cx),
source: self.whence.clean(cx),
def_id: cx.map.local_def_id(0),
def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
visibility: self.vis.clean(cx),
stability: None,
deprecation: None,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ pub struct Macro {

pub struct ExternCrate {
pub name: Name,
pub cnum: ast::CrateNum,
pub path: Option<String>,
pub vis: hir::Visibility,
pub attrs: hir::HirVec<ast::Attribute>,
Expand Down
33 changes: 24 additions & 9 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
pub struct CommaSep<'a, T: 'a>(pub &'a [T]);
pub struct AbiSpace(pub Abi);

pub struct HRef<'a> {
pub did: DefId,
pub text: &'a str,
}

impl<'a> VisSpace<'a> {
pub fn get(self) -> &'a Option<clean::Visibility> {
let VisSpace(v) = self; v
Expand Down Expand Up @@ -361,15 +366,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
}
}
}

match href(did) {
Some((url, shortty, fqp)) => {
write!(w, "<a class='{}' href='{}' title='{}'>{}</a>",
shortty, url, fqp.join("::"), last.name)?;
}
_ => write!(w, "{}", last.name)?,
}
write!(w, "{}", last.params)?;
write!(w, "{}{}", HRef::new(did, &last.name), last.params)?;
Ok(())
}

Expand Down Expand Up @@ -435,6 +432,24 @@ fn tybounds(w: &mut fmt::Formatter,
}
}

impl<'a> HRef<'a> {
pub fn new(did: DefId, text: &'a str) -> HRef<'a> {
HRef { did: did, text: text }
}
}

impl<'a> fmt::Display for HRef<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match href(self.did) {
Some((url, shortty, fqp)) => {
write!(f, "<a class='{}' href='{}' title='{}'>{}</a>",
shortty, url, fqp.join("::"), self.text)
}
_ => write!(f, "{}", self.text),
}
}
}

impl fmt::Display for clean::Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down
7 changes: 5 additions & 2 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,16 +1739,19 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,

match myitem.inner {
clean::ExternCrateItem(ref name, ref src) => {
use html::format::HRef;

match *src {
Some(ref src) => {
write!(w, "<tr><td><code>{}extern crate {} as {};",
VisSpace(&myitem.visibility),
src,
HRef::new(myitem.def_id, src),
name)?
}
None => {
write!(w, "<tr><td><code>{}extern crate {};",
VisSpace(&myitem.visibility), name)?
VisSpace(&myitem.visibility),
HRef::new(myitem.def_id, name))?
}
}
write!(w, "</code></td></tr>")?;
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
let name = renamed.unwrap_or(item.name);
match item.node {
hir::ItemExternCrate(ref p) => {
let cstore = &self.cx.sess().cstore;
om.extern_crates.push(ExternCrate {
cnum: cstore.extern_mod_stmt_cnum(item.id)
.unwrap_or(ast::CrateNum::max_value()),
name: name,
path: p.map(|x|x.to_string()),
vis: item.vis.clone(),
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/visit_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<'a, 'b, 'tcx> LibEmbargoVisitor<'a, 'b, 'tcx> {

pub fn visit_lib(&mut self, cnum: ast::CrateNum) {
let did = DefId { krate: cnum, index: CRATE_DEF_INDEX };
self.update(did, Some(AccessLevel::Public));
self.visit_mod(did);
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/rustdoc/issue-33178-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:empty.rs
// aux-build:variant-struct.rs
// ignore-cross-compile

// @has issue_33178_1/index.html
// @!has - //a/@title empty
pub extern crate empty;

// @!has - //a/@title variant_struct
pub extern crate variant_struct as foo;
23 changes: 23 additions & 0 deletions src/test/rustdoc/issue-33178.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:empty.rs
// aux-build:variant-struct.rs
// build-aux-docs
// ignore-cross-compile

// @has issue_33178/index.html
// @has - //a/@title empty
// @has - //a/@href ../empty/index.html
pub extern crate empty;

// @has - //a/@title variant_struct
// @has - //a/@href ../variant_struct/index.html
pub extern crate variant_struct as foo;