未验证 提交 6a4396b9 编写于 作者: J Joseph Ryan

Extract `Cache` and other types from `html` module

上级 5bc97946
......@@ -32,8 +32,9 @@
use crate::clean::types::Type::{QPath, ResolvedPath};
use crate::core::DocContext;
use crate::doctree;
use crate::html::item_type::ItemType;
use crate::html::render::{cache, ExternalLocation};
use crate::formats::cache::cache;
use crate::formats::item_type::ItemType;
use crate::html::render::cache::ExternalLocation;
use self::FnRetTy::*;
use self::ItemEnum::*;
......@@ -1172,7 +1173,7 @@ impl GetDefId for Type {
fn def_id(&self) -> Option<DefId> {
match *self {
ResolvedPath { did, .. } => Some(did),
Primitive(p) => crate::html::render::cache().primitive_locations.get(&p).cloned(),
Primitive(p) => cache().primitive_locations.get(&p).cloned(),
BorrowedRef { type_: box Generic(..), .. } => {
Primitive(PrimitiveType::Reference).def_id()
}
......
......@@ -254,7 +254,7 @@ pub struct RenderOptions {
/// Temporary storage for data obtained during `RustdocVisitor::clean()`.
/// Later on moved into `CACHE_KEY`.
#[derive(Default)]
#[derive(Default, Clone)]
pub struct RenderInfo {
pub inlined: FxHashSet<DefId>,
pub external_paths: crate::core::ExternalPaths,
......
......@@ -44,9 +44,9 @@
pub struct DocContext<'tcx> {
pub tcx: TyCtxt<'tcx>,
pub resolver: Rc<RefCell<interface::BoxedResolver>>,
/// Later on moved into `html::render::CACHE_KEY`
/// Later on moved into `formats::cache::CACHE_KEY`
pub renderinfo: RefCell<RenderInfo>,
/// Later on moved through `clean::Crate` into `html::render::CACHE_KEY`
/// Later on moved through `clean::Crate` into `formats::cache::CACHE_KEY`
pub external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
/// the same time.
......
此差异已折叠。
......@@ -13,7 +13,7 @@
/// The search index uses item types encoded as smaller numbers which equal to
/// discriminants. JavaScript then is used to decode them into the original value.
/// Consequently, every change to this type should be synchronized to
/// the `itemTypes` mapping table in `static/main.js`.
/// the `itemTypes` mapping table in `html/static/main.js`.
///
/// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and
/// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
......
use std::cell::RefCell;
use std::rc::Rc;
pub mod cache;
pub mod item_type;
pub mod renderer;
use rustc_span::edition::Edition;
pub use renderer::{FormatRenderer, Renderer};
use crate::clean;
use crate::config::{RenderInfo, RenderOptions};
use crate::error::Error;
pub trait FormatRenderer: Clone {
type Output: FormatRenderer;
fn init(
krate: clean::Crate,
options: RenderOptions,
renderinfo: RenderInfo,
diag: &rustc_errors::Handler,
edition: Edition,
parent: Rc<RefCell<Renderer>>,
) -> Result<(Self::Output, clean::Crate), Error>;
/// Renders a single non-module item. This means no recursive sub-item rendering is required.
fn item(&mut self, item: clean::Item) -> Result<(), Error>;
use rustc_span::def_id::DefId;
/// Renders a module. Doesn't need to handle recursing into children, the driver does that
/// automatically.
fn mod_item_in(
&mut self,
item: &clean::Item,
item_name: &str,
module: &clean::Module,
) -> Result<(), Error>;
/// Runs after recursively rendering all sub-items of a module.
fn mod_item_out(&mut self) -> Result<(), Error>;
/// Post processing hook for cleanup and dumping output to files.
fn after_krate(&mut self, krate: &clean::Crate) -> Result<(), Error>;
use crate::clean;
use crate::clean::types::GetDefId;
/// Called after everything else to write out errors.
fn after_run(&mut self, diag: &rustc_errors::Handler) -> Result<(), Error>;
/// Metadata about implementations for a type or trait.
#[derive(Clone, Debug)]
pub struct Impl {
pub impl_item: clean::Item,
}
#[derive(Clone)]
pub struct Renderer;
impl Renderer {
pub fn new() -> Renderer {
Renderer
}
/// Main method for rendering a crate.
pub fn run<T: FormatRenderer + Clone>(
self,
krate: clean::Crate,
options: RenderOptions,
renderinfo: RenderInfo,
diag: &rustc_errors::Handler,
edition: Edition,
) -> Result<(), Error> {
let rself = Rc::new(RefCell::new(self));
let (mut renderer, mut krate) =
T::init(krate, options, renderinfo, diag, edition, rself.clone())?;
let mut item = match krate.module.take() {
Some(i) => i,
None => return Ok(()),
};
item.name = Some(krate.name.clone());
// Render the crate documentation
let mut work = vec![(renderer.clone(), item)];
while let Some((mut cx, item)) = work.pop() {
if item.is_mod() {
// modules are special because they add a namespace. We also need to
// recurse into the items of the module as well.
let name = item.name.as_ref().unwrap().to_string();
if name.is_empty() {
panic!("Unexpected module with empty name");
}
let module = match item.inner {
clean::StrippedItem(box clean::ModuleItem(ref m))
| clean::ModuleItem(ref m) => m,
_ => unreachable!(),
};
cx.mod_item_in(&item, &name, module)?;
let module = match item.inner {
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
_ => unreachable!(),
};
for it in module.items {
info!("Adding {:?} to worklist", it.name);
work.push((cx.clone(), it));
}
cx.mod_item_out()?;
} else if item.name.is_some() {
cx.item(item)?;
}
impl Impl {
pub fn inner_impl(&self) -> &clean::Impl {
match self.impl_item.inner {
clean::ImplItem(ref impl_) => impl_,
_ => panic!("non-impl item found in impl"),
}
}
renderer.after_krate(&krate)?;
renderer.after_run(diag)
pub fn trait_did(&self) -> Option<DefId> {
self.inner_impl().trait_.def_id()
}
}
use std::sync::Arc;
use rustc_span::edition::Edition;
use crate::clean;
use crate::config::{RenderInfo, RenderOptions};
use crate::error::Error;
use crate::formats::cache::{Cache, CACHE_KEY};
pub trait FormatRenderer: Clone {
type Output: FormatRenderer;
fn init(
krate: clean::Crate,
options: RenderOptions,
renderinfo: RenderInfo,
edition: Edition,
cache: &mut Cache,
) -> Result<(Self::Output, clean::Crate), Error>;
/// Renders a single non-module item. This means no recursive sub-item rendering is required.
fn item(&mut self, item: clean::Item, cache: &Cache) -> Result<(), Error>;
/// Renders a module (doesn't need to handle recursing into children).
fn mod_item_in(
&mut self,
item: &clean::Item,
item_name: &str,
cache: &Cache,
) -> Result<(), Error>;
/// Runs after recursively rendering all sub-items of a module.
fn mod_item_out(&mut self, name: &str) -> Result<(), Error>;
/// Post processing hook for cleanup and dumping output to files.
fn after_krate(&mut self, krate: &clean::Crate, cache: &Cache) -> Result<(), Error>;
/// Called after everything else to write out errors.
fn after_run(&mut self, diag: &rustc_errors::Handler) -> Result<(), Error>;
}
#[derive(Clone)]
pub struct Renderer;
impl Renderer {
pub fn new() -> Renderer {
Renderer
}
/// Main method for rendering a crate.
pub fn run<T: FormatRenderer + Clone>(
self,
krate: clean::Crate,
options: RenderOptions,
renderinfo: RenderInfo,
diag: &rustc_errors::Handler,
edition: Edition,
) -> Result<(), Error> {
let (krate, mut cache) = Cache::from_krate(
renderinfo.clone(),
options.document_private,
&options.extern_html_root_urls,
&options.output,
krate,
);
let (mut renderer, mut krate) = T::init(krate, options, renderinfo, edition, &mut cache)?;
let cache = Arc::new(cache);
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
// parallelization opportunities
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
let mut item = match krate.module.take() {
Some(i) => i,
None => return Ok(()),
};
item.name = Some(krate.name.clone());
// Render the crate documentation
let mut work = vec![(renderer.clone(), item)];
while let Some((mut cx, item)) = work.pop() {
if item.is_mod() {
// modules are special because they add a namespace. We also need to
// recurse into the items of the module as well.
let name = item.name.as_ref().unwrap().to_string();
if name.is_empty() {
panic!("Unexpected module with empty name");
}
cx.mod_item_in(&item, &name, &cache)?;
let module = match item.inner {
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
_ => unreachable!(),
};
for it in module.items {
info!("Adding {:?} to worklist", it.name);
work.push((cx.clone(), it));
}
cx.mod_item_out(&name)?;
} else if item.name.is_some() {
cx.item(item, &cache)?;
}
}
renderer.after_krate(&krate, &cache)?;
renderer.after_run(diag)
}
}
......@@ -11,13 +11,15 @@
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_span::def_id::DefId;
use rustc_target::spec::abi::Abi;
use crate::clean::{self, PrimitiveType};
use crate::formats::cache::cache;
use crate::formats::item_type::ItemType;
use crate::html::escape::Escape;
use crate::html::item_type::ItemType;
use crate::html::render::{self, cache, CURRENT_DEPTH};
use crate::html::render::cache::ExternalLocation;
use crate::html::render::CURRENT_DEPTH;
pub trait Print {
fn print(self, buffer: &mut Buffer);
......@@ -493,9 +495,9 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
fqp,
shortty,
match cache.extern_locations[&did.krate] {
(.., render::Remote(ref s)) => s.to_string(),
(.., render::Local) => "../".repeat(depth),
(.., render::Unknown) => return None,
(.., ExternalLocation::Remote(ref s)) => s.to_string(),
(.., ExternalLocation::Local) => "../".repeat(depth),
(.., ExternalLocation::Unknown) => return None,
},
)
}
......@@ -574,12 +576,12 @@ fn primitive_link(
}
Some(&def_id) => {
let loc = match m.extern_locations[&def_id.krate] {
(ref cname, _, render::Remote(ref s)) => Some((cname, s.to_string())),
(ref cname, _, render::Local) => {
(ref cname, _, ExternalLocation::Remote(ref s)) => Some((cname, s.to_string())),
(ref cname, _, ExternalLocation::Local) => {
let len = CURRENT_DEPTH.with(|s| s.get());
Some((cname, "../".repeat(len)))
}
(.., render::Unknown) => None,
(.., ExternalLocation::Unknown) => None,
};
if let Some((cname, root)) = loc {
write!(
......
crate mod escape;
crate mod format;
crate mod highlight;
crate mod layout;
pub mod markdown;
pub mod render;
crate mod sources;
crate mod static_files;
crate mod toc;
此差异已折叠。
......@@ -66,19 +66,8 @@
#[macro_use]
mod error;
mod fold;
mod formats;
pub mod html {
crate mod escape;
crate mod format;
crate mod highlight;
crate mod item_type;
crate mod layout;
pub mod markdown;
crate mod render;
crate mod sources;
crate mod static_files;
crate mod toc;
}
crate mod formats;
crate mod html;
mod markdown;
mod passes;
mod test;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册