use syntax::codemap::{dummy_spanned, Spanned};
use syntax::feature_gate::UnstableFeatures;
use syntax::ptr::P;
-use syntax::symbol::keywords;
+use syntax::symbol::keywords::{self, Keyword};
use syntax::symbol::{Symbol, InternedString};
use syntax_pos::{self, DUMMY_SP, Pos, FileName};
use std::iter::{FromIterator, once};
use rustc_data_structures::sync::Lrc;
use std::rc::Rc;
+use std::str::FromStr;
use std::cell::RefCell;
use std::sync::Arc;
use std::u32;
_ => unreachable!(),
}
- let ExternalCrate { name, src, primitives, .. } = LOCAL_CRATE.clean(cx);
+ let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
{
let m = match module.inner {
ModuleItem(ref mut m) => m,
inner: PrimitiveItem(prim),
}
}));
+ m.items.extend(keywords.iter().map(|&(def_id, ref kw, ref attrs)| {
+ Item {
+ source: Span::empty(),
+ name: Some(kw.clone()),
+ attrs: attrs.clone(),
+ visibility: Some(Public),
+ stability: get_stability(cx, def_id),
+ deprecation: get_deprecation(cx, def_id),
+ def_id,
+ inner: KeywordItem(kw.clone()),
+ }
+ }));
}
let mut access_levels = cx.access_levels.borrow_mut();
pub src: FileName,
pub attrs: Attributes,
pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
+ pub keywords: Vec<(DefId, String, Attributes)>,
}
impl Clean<ExternalCrate> for CrateNum {
.filter_map(as_primitive).collect()
};
+ let as_keyword = |def: Def| {
+ if let Def::Mod(def_id) = def {
+ let attrs = cx.tcx.get_attrs(def_id).clean(cx);
+ let mut keyword = None;
+ for attr in attrs.lists("doc") {
+ if let Some(v) = attr.value_str() {
+ if attr.check_name("keyword") {
+ keyword = Keyword::from_str(&v.as_str()).ok()
+ .map(|x| x.name().to_string());
+ if keyword.is_some() {
+ break
+ }
+ // FIXME: should warn on unknown keywords?
+ }
+ }
+ }
+ return keyword.map(|p| (def_id, p, attrs));
+ }
+ None
+ };
+ let keywords = if root.is_local() {
+ cx.tcx.hir.krate().module.item_ids.iter().filter_map(|&id| {
+ let item = cx.tcx.hir.expect_item(id.id);
+ match item.node {
+ hir::ItemMod(_) => {
+ as_keyword(Def::Mod(cx.tcx.hir.local_def_id(id.id)))
+ }
+ hir::ItemUse(ref path, hir::UseKind::Single)
+ if item.vis == hir::Visibility::Public => {
+ as_keyword(path.def).map(|(_, prim, attrs)| {
+ // Pretend the primitive is local.
+ (cx.tcx.hir.local_def_id(id.id), prim, attrs)
+ })
+ }
+ _ => None
+ }
+ }).collect()
+ } else {
+ cx.tcx.item_children(root).iter().map(|item| item.def)
+ .filter_map(as_keyword).collect()
+ };
+
ExternalCrate {
name: cx.tcx.crate_name(*self).to_string(),
src: krate_src,
attrs: cx.tcx.get_attrs(root).clean(cx),
primitives,
+ keywords,
}
}
}
pub fn is_extern_crate(&self) -> bool {
self.type_() == ItemType::ExternCrate
}
+ pub fn is_keyword(&self) -> bool {
+ self.type_() == ItemType::Keyword
+ }
pub fn is_stripped(&self) -> bool {
match self.inner { StrippedItem(..) => true, _ => false }
AssociatedTypeItem(Vec<TyParamBound>, Option<Type>),
/// An item that has been stripped by a rustdoc pass
StrippedItem(Box<ItemEnum>),
+ KeywordItem(String),
}
impl ItemEnum {
AssociatedConst = 18,
Union = 19,
ForeignType = 20,
+ Keyword = 21,
}
Type,
Value,
Macro,
+ Keyword,
}
impl<'a> From<&'a clean::Item> for ItemType {
clean::AssociatedConstItem(..) => ItemType::AssociatedConst,
clean::AssociatedTypeItem(..) => ItemType::AssociatedType,
clean::ForeignTypeItem => ItemType::ForeignType,
+ clean::KeywordItem(..) => ItemType::Keyword,
clean::StrippedItem(..) => unreachable!(),
}
}
ItemType::Constant => "constant",
ItemType::AssociatedConst => "associatedconstant",
ItemType::ForeignType => "foreigntype",
+ ItemType::Keyword => "keyword",
}
}
ItemType::AssociatedConst => NameSpace::Value,
ItemType::Macro => NameSpace::Macro,
+
+ ItemType::Keyword => NameSpace::Keyword,
}
}
}
pub const NAMESPACE_TYPE: &'static str = "t";
pub const NAMESPACE_VALUE: &'static str = "v";
pub const NAMESPACE_MACRO: &'static str = "m";
+pub const NAMESPACE_KEYWORD: &'static str = "k";
impl NameSpace {
pub fn to_static_str(&self) -> &'static str {
NameSpace::Type => NAMESPACE_TYPE,
NameSpace::Value => NAMESPACE_VALUE,
NameSpace::Macro => NAMESPACE_MACRO,
+ NameSpace::Keyword => NAMESPACE_KEYWORD,
}
}
}
typedefs: HashSet<ItemEntry>,
statics: HashSet<ItemEntry>,
constants: HashSet<ItemEntry>,
+ keywords: HashSet<ItemEntry>,
}
impl AllTypes {
typedefs: HashSet::with_capacity(100),
statics: HashSet::with_capacity(100),
constants: HashSet::with_capacity(100),
+ keywords: HashSet::with_capacity(10),
}
}
clean::StaticItem(..) | clean::ForeignStaticItem(..) => write!(fmt, "Static ")?,
clean::ConstantItem(..) => write!(fmt, "Constant ")?,
clean::ForeignTypeItem => write!(fmt, "Foreign Type ")?,
+ clean::KeywordItem(..) => write!(fmt, "Keyword ")?,
_ => {
// We don't generate pages for any other type.
unreachable!();
}
}
- if !self.item.is_primitive() {
+ if !self.item.is_primitive() && !self.item.is_keyword() {
let cur = &self.cx.current;
let amt = if self.item.is_mod() { cur.len() - 1 } else { cur.len() };
for (i, component) in cur.iter().enumerate().take(amt) {
item_static(fmt, self.cx, self.item, i),
clean::ConstantItem(ref c) => item_constant(fmt, self.cx, self.item, c),
clean::ForeignTypeItem => item_foreign_type(fmt, self.cx, self.item),
+ clean::KeywordItem(ref k) => item_keyword(fmt, self.cx, self.item, k),
_ => {
// We don't generate pages for any other type.
unreachable!();
write!(w, "</table>")?;
}
curty = myty;
- let (short, name) = match myty.unwrap() {
- ItemType::ExternCrate |
- ItemType::Import => ("reexports", "Re-exports"),
- ItemType::Module => ("modules", "Modules"),
- ItemType::Struct => ("structs", "Structs"),
- ItemType::Union => ("unions", "Unions"),
- ItemType::Enum => ("enums", "Enums"),
- ItemType::Function => ("functions", "Functions"),
- ItemType::Typedef => ("types", "Type Definitions"),
- ItemType::Static => ("statics", "Statics"),
- ItemType::Constant => ("constants", "Constants"),
- ItemType::Trait => ("traits", "Traits"),
- ItemType::Impl => ("impls", "Implementations"),
- ItemType::TyMethod => ("tymethods", "Type Methods"),
- ItemType::Method => ("methods", "Methods"),
- ItemType::StructField => ("fields", "Struct Fields"),
- ItemType::Variant => ("variants", "Variants"),
- ItemType::Macro => ("macros", "Macros"),
- ItemType::Primitive => ("primitives", "Primitive Types"),
- ItemType::AssociatedType => ("associated-types", "Associated Types"),
- ItemType::AssociatedConst => ("associated-consts", "Associated Constants"),
- ItemType::ForeignType => ("foreign-types", "Foreign Types"),
- };
+ let (short, name) = item_ty_to_strs(&myty.unwrap());
write!(w, "<h2 id='{id}' class='section-header'>\
<a href=\"#{id}\">{name}</a></h2>\n<table>",
id = derive_id(short.to_owned()), name = name)?;
Ok(())
}
+fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) {
+ match *ty {
+ ItemType::ExternCrate |
+ ItemType::Import => ("reexports", "Re-exports"),
+ ItemType::Module => ("modules", "Modules"),
+ ItemType::Struct => ("structs", "Structs"),
+ ItemType::Union => ("unions", "Unions"),
+ ItemType::Enum => ("enums", "Enums"),
+ ItemType::Function => ("functions", "Functions"),
+ ItemType::Typedef => ("types", "Type Definitions"),
+ ItemType::Static => ("statics", "Statics"),
+ ItemType::Constant => ("constants", "Constants"),
+ ItemType::Trait => ("traits", "Traits"),
+ ItemType::Impl => ("impls", "Implementations"),
+ ItemType::TyMethod => ("tymethods", "Type Methods"),
+ ItemType::Method => ("methods", "Methods"),
+ ItemType::StructField => ("fields", "Struct Fields"),
+ ItemType::Variant => ("variants", "Variants"),
+ ItemType::Macro => ("macros", "Macros"),
+ ItemType::Primitive => ("primitives", "Primitive Types"),
+ ItemType::AssociatedType => ("associated-types", "Associated Types"),
+ ItemType::AssociatedConst => ("associated-consts", "Associated Constants"),
+ ItemType::ForeignType => ("foreign-types", "Foreign Types"),
+ ItemType::Keyword => ("keywords", "Keywords"),
+ }
+}
+
fn sidebar_module(fmt: &mut fmt::Formatter, _it: &clean::Item,
items: &[clean::Item]) -> fmt::Result {
let mut sidebar = String::new();
ItemType::TyMethod, ItemType::Method, ItemType::StructField, ItemType::Variant,
ItemType::AssociatedType, ItemType::AssociatedConst, ItemType::ForeignType] {
if items.iter().any(|it| !it.is_stripped() && it.type_() == myty) {
- let (short, name) = match myty {
- ItemType::ExternCrate |
- ItemType::Import => ("reexports", "Re-exports"),
- ItemType::Module => ("modules", "Modules"),
- ItemType::Struct => ("structs", "Structs"),
- ItemType::Union => ("unions", "Unions"),
- ItemType::Enum => ("enums", "Enums"),
- ItemType::Function => ("functions", "Functions"),
- ItemType::Typedef => ("types", "Type Definitions"),
- ItemType::Static => ("statics", "Statics"),
- ItemType::Constant => ("constants", "Constants"),
- ItemType::Trait => ("traits", "Traits"),
- ItemType::Impl => ("impls", "Implementations"),
- ItemType::TyMethod => ("tymethods", "Type Methods"),
- ItemType::Method => ("methods", "Methods"),
- ItemType::StructField => ("fields", "Struct Fields"),
- ItemType::Variant => ("variants", "Variants"),
- ItemType::Macro => ("macros", "Macros"),
- ItemType::Primitive => ("primitives", "Primitive Types"),
- ItemType::AssociatedType => ("associated-types", "Associated Types"),
- ItemType::AssociatedConst => ("associated-consts", "Associated Constants"),
- ItemType::ForeignType => ("foreign-types", "Foreign Types"),
- };
+ let (short, name) = item_ty_to_strs(&myty);
sidebar.push_str(&format!("<li><a href=\"#{id}\">{name}</a></li>",
id = short,
name = name));
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
}
+fn item_keyword(w: &mut fmt::Formatter, cx: &Context,
+ it: &clean::Item,
+ _p: &str) -> fmt::Result {
+ document(w, cx, it)?;
+ render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
+}
+
const BASIC_KEYWORDS: &'static str = "rust, rustlang, rust-lang";
fn make_item_keywords(it: &clean::Item) -> String {
"constant",
"associatedconstant",
"union",
- "foreigntype"];
+ "foreigntype",
+ "keyword"];
var search_input = document.getElementsByClassName('search-input')[0];
// used for special search precedence
var TY_PRIMITIVE = itemTypes.indexOf("primitive");
+ var TY_KEYWORD = itemTypes.indexOf("keyword");
onEach(document.getElementsByClassName('js-only'), function(e) {
removeClass(e, 'js-only');
b = bbb.index;
if (a !== b) { return a - b; }
- // special precedence for primitive pages
- if ((aaa.item.ty === TY_PRIMITIVE) && (bbb.item.ty !== TY_PRIMITIVE)) {
+ // special precedence for primitive and keyword pages
+ if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
+ (aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
return -1;
}
- if ((bbb.item.ty === TY_PRIMITIVE) && (aaa.item.ty !== TY_PRIMITIVE)) {
+ if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
+ (bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
return 1;
}
displayPath = item.path + '::';
href = rootPath + item.path.replace(/::/g, '/') + '/' +
name + '/index.html';
- } else if (type === "primitive") {
+ } else if (type === "primitive" || type === "keyword") {
displayPath = "";
href = rootPath + item.path.replace(/::/g, '/') +
'/' + type + '.' + name + '.html';
block("fn", "Functions");
block("type", "Type Definitions");
block("foreigntype", "Foreign Types");
+ block("keyword", "Keywords");
}
window.initSidebarItems = initSidebarItems;
font-style: italic;
}
+tr.result span.keyword::after {
+ content: ' (keyword)';
+ font-style: italic;
+}
+
body.blur > :not(#help) {
filter: blur(8px);
-webkit-filter: blur(8px);
.content .highlighted.constant,
.content .highlighted.static { background-color: #0063cc; }
.content .highlighted.primitive { background-color: #00708a; }
+.content .highlighted.keyword { background-color: #884719; }
.content span.enum, .content a.enum, .block a.current.enum { color: #82b089; }
.content span.struct, .content a.struct, .block a.current.struct { color: #2dbfb8; }
.content span.method, .content a.method, .block a.current.method,
.content span.tymethod, .content a.tymethod, .block a.current.tymethod,
.content .fnname{ color: #2BAB63; }
+.content span.keyword, .content a.keyword, .block a.current.keyword { color: #de5249; }
pre.rust .comment { color: #8d8d8b; }
pre.rust .doccomment { color: #8ca375; }
color: grey;
}
-tr.result span.primitive::after {
+tr.result span.primitive::after, tr.result span.keyword::after {
color: #ddd;
}
.content .highlighted.constant,
.content .highlighted.static { background-color: #c3e0ff; }
.content .highlighted.primitive { background-color: #9aecff; }
+.content .highlighted.keyword { background-color: #f99650; }
.content span.enum, .content a.enum, .block a.current.enum { color: #508157; }
.content span.struct, .content a.struct, .block a.current.struct { color: #ad448e; }
.content span.method, .content a.method, .block a.current.method,
.content span.tymethod, .content a.tymethod, .block a.current.tymethod,
.content .fnname { color: #9a6e31; }
+.content span.keyword, .content a.keyword, .block a.current.keyword { color: #de5249; }
pre.rust .comment { color: #8E908C; }
pre.rust .doccomment { color: #4D4D4C; }
color: grey;
}
-tr.result span.primitive::after {
+tr.result span.primitive::after, tr.result span.keyword::after {
color: black;
}
// Associated types are never stripped
clean::AssociatedTypeItem(..) => {}
+
+ // Keywords are never stripped
+ clean::KeywordItem(..) => {}
}
let fastreturn = match i.inner {
ident: Ident::with_empty_ctxt(super::Symbol($index))
};
)*
+
+ impl ::std::str::FromStr for Keyword {
+ type Err = ();
+
+ fn from_str(s: &str) -> Result<Self, ()> {
+ match s {
+ $($string => Ok($konst),)*
+ _ => Err(()),
+ }
+ }
+ }
}
impl Interner {
--- /dev/null
+// Copyright 2018 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.
+
+#![crate_name = "foo"]
+
+// @has foo/index.html '//h2[@id="keywords"]' 'Keywords'
+// @has foo/index.html '//a[@href="keyword.match.html"]' 'match'
+// @has foo/keyword.match.html '//a[@class="keyword"]' 'match'
+// @has foo/keyword.match.html '//section[@id="main"]//div[@class="docblock"]//p' 'this is a test!'
+#[doc(keyword = "match")]
+/// this is a test!
+mod foo{}