signature info for other items (mods, fns, methods, etc.)

This commit is contained in:
Nick Cameron 2016-11-30 11:50:08 +13:00
parent e9ecd8805d
commit d8492367bf
6 changed files with 106 additions and 50 deletions

View file

@ -135,6 +135,7 @@ pub struct EnumData {
pub variants: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}
/// Data for extern crates.
@ -169,6 +170,7 @@ pub struct FunctionData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}
/// Data about a function call.
@ -253,6 +255,7 @@ pub struct MethodData {
pub parent: Option<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}
/// Data for modules.
@ -267,6 +270,7 @@ pub struct ModData {
pub items: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}
/// Data for a reference to a module.
@ -304,6 +308,7 @@ pub struct StructVariantData {
pub scope: NodeId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}
#[derive(Debug, RustcEncodable)]
@ -317,6 +322,7 @@ pub struct TraitData {
pub items: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}
#[derive(Debug, RustcEncodable)]
@ -330,6 +336,7 @@ pub struct TupleVariantData {
pub scope: NodeId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}
/// Data for a typedef.
@ -343,6 +350,7 @@ pub struct TypeDefData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Option<Signature>,
}
/// Data for a reference to a type or trait.
@ -412,7 +420,7 @@ pub struct VariableRefData {
/// Encodes information about the signature of a definition. This should have
/// enough information to create a nice display about a definition without
/// access to the source code.
#[derive(Debug, RustcEncodable)]
#[derive(Clone, Debug, RustcEncodable)]
pub struct Signature {
pub span: Span,
pub text: String,
@ -426,7 +434,7 @@ pub struct Signature {
/// An element of a signature. `start` and `end` are byte offsets into the `text`
/// of the parent `Signature`.
#[derive(Debug, RustcEncodable)]
#[derive(Clone, Debug, RustcEncodable)]
pub struct SigElement {
pub id: DefId,
pub start: usize,

View file

@ -445,6 +445,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
parent: trait_id,
visibility: vis,
docs: docs_for_attrs(attrs),
sig: method_data.sig,
}.lower(self.tcx));
}
@ -516,6 +517,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
visibility: Visibility::Inherited,
parent: None,
docs: String::new(),
sig: None,
}.lower(self.tcx));
}
}
@ -621,9 +623,6 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
};
if !self.span.filter_generated(sub_span, item.span) {
let mut sig = self.sig_base(item);
sig.ident_start = sig.text.find(&name).expect("Name not in struct signature?");
sig.ident_end = sig.ident_start + name.len();
self.dumper.struct_data(StructData {
span: sub_span.expect("No span found for struct"),
id: item.id,
@ -635,7 +634,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
fields: fields,
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: sig,
sig: self.save_ctxt.sig_base(item),
}.lower(self.tcx));
}
@ -647,18 +646,6 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
self.process_generic_params(ty_params, item.span, &qualname, item.id);
}
fn sig_base(&self, item: &ast::Item) -> Signature {
let text = self.span.signature_string_for_span(item.span).expect("Couldn't make signature");
Signature {
span: mk_sp(item.span.lo, item.span.lo + BytePos(text.len() as u32)),
text: text,
ident_start: 0,
ident_end: 0,
defs: vec![],
refs: vec![],
}
}
fn process_enum(&mut self,
item: &'l ast::Item,
enum_definition: &'l ast::EnumDef,
@ -679,6 +666,18 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
qualname.push_str("::");
qualname.push_str(&name);
let text = self.span.signature_string_for_span(variant.span);
let ident_start = text.find(&name).unwrap();
let ident_end = ident_start + name.len();
let sig = Signature {
span: variant.span,
text: text,
ident_start: ident_start,
ident_end: ident_end,
defs: vec![],
refs: vec![],
};
match variant.node.data {
ast::VariantData::Struct(ref fields, _) => {
let sub_span = self.span.span_for_first_ident(variant.span);
@ -700,6 +699,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
scope: enum_data.scope,
parent: Some(make_def_id(item.id, &self.tcx.map)),
docs: docs_for_attrs(&variant.node.attrs),
sig: sig,
}.lower(self.tcx));
}
}
@ -725,6 +725,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
scope: enum_data.scope,
parent: Some(make_def_id(item.id, &self.tcx.map)),
docs: docs_for_attrs(&variant.node.attrs),
sig: sig,
}.lower(self.tcx));
}
}
@ -809,6 +810,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
items: methods.iter().map(|i| i.id).collect(),
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: self.save_ctxt.sig_base(item),
}.lower(self.tcx));
}
@ -1289,10 +1291,10 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
Struct(ref def, ref ty_params) => self.process_struct(item, def, ty_params),
Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
Impl(..,
ref ty_params,
ref trait_ref,
ref typ,
ref impl_items) => {
ref ty_params,
ref trait_ref,
ref typ,
ref impl_items) => {
self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
}
Trait(_, ref generics, ref trait_refs, ref methods) =>
@ -1315,6 +1317,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
visibility: From::from(&item.vis),
parent: None,
docs: docs_for_attrs(&item.attrs),
sig: Some(self.save_ctxt.sig_base(item)),
}.lower(self.tcx));
}

View file

@ -97,6 +97,7 @@ pub struct EnumData {
pub variants: Vec<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}
impl Lower for data::EnumData {
@ -113,6 +114,7 @@ impl Lower for data::EnumData {
variants: self.variants.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
}
}
}
@ -176,6 +178,7 @@ pub struct FunctionData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}
impl Lower for data::FunctionData {
@ -193,6 +196,7 @@ impl Lower for data::FunctionData {
visibility: self.visibility,
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
}
}
}
@ -341,6 +345,7 @@ pub struct MethodData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}
impl Lower for data::MethodData {
@ -358,6 +363,7 @@ impl Lower for data::MethodData {
visibility: self.visibility,
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
}
}
}
@ -374,6 +380,7 @@ pub struct ModData {
pub items: Vec<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}
impl Lower for data::ModData {
@ -390,6 +397,7 @@ impl Lower for data::ModData {
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
}
}
}
@ -462,6 +470,7 @@ pub struct StructVariantData {
pub scope: DefId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}
impl Lower for data::StructVariantData {
@ -478,6 +487,7 @@ impl Lower for data::StructVariantData {
scope: make_def_id(self.scope, &tcx.map),
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
}
}
}
@ -493,6 +503,7 @@ pub struct TraitData {
pub items: Vec<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
}
impl Lower for data::TraitData {
@ -509,6 +520,7 @@ impl Lower for data::TraitData {
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
}
}
}
@ -524,6 +536,7 @@ pub struct TupleVariantData {
pub scope: DefId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
}
impl Lower for data::TupleVariantData {
@ -540,6 +553,7 @@ impl Lower for data::TupleVariantData {
scope: make_def_id(self.scope, &tcx.map),
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
}
}
}
@ -555,6 +569,7 @@ pub struct TypeDefData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Option<Signature>,
}
impl Lower for data::TypeDefData {
@ -570,6 +585,7 @@ impl Lower for data::TypeDefData {
visibility: self.visibility,
parent: self.parent,
docs: self.docs,
sig: self.sig.map(|s| s.lower(tcx)),
}
}
}
@ -705,7 +721,7 @@ impl Lower for data::VariableRefData {
}
}
#[derive(Debug, RustcEncodable)]
#[derive(Clone, Debug, RustcEncodable)]
pub struct Signature {
pub span: SpanData,
pub text: String,

View file

@ -86,7 +86,7 @@ impl<'b, W: Write + 'b> Dump for JsonDumper<'b, W> {
children: data.items.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: None,
sig: Some(From::from(data.sig)),
};
if def.span.file_name != def.value {
// If the module is an out-of-line defintion, then we'll make the
@ -266,7 +266,7 @@ impl From<EnumData> for Def {
children: data.variants.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: None,
sig: Some(From::from(data.sig)),
}
}
}
@ -283,7 +283,7 @@ impl From<TupleVariantData> for Def {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
sig: Some(From::from(data.sig)),
}
}
}
@ -299,7 +299,7 @@ impl From<StructVariantData> for Def {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
sig: Some(From::from(data.sig)),
}
}
}
@ -331,7 +331,7 @@ impl From<TraitData> for Def {
children: data.items.into_iter().map(|id| From::from(id)).collect(),
decl_id: None,
docs: data.docs,
sig: None,
sig: Some(From::from(data.sig)),
}
}
}
@ -347,7 +347,7 @@ impl From<FunctionData> for Def {
children: vec![],
decl_id: None,
docs: data.docs,
sig: None,
sig: Some(From::from(data.sig)),
}
}
}
@ -363,7 +363,7 @@ impl From<MethodData> for Def {
children: vec![],
decl_id: data.decl_id.map(|id| From::from(id)),
docs: data.docs,
sig: None,
sig: Some(From::from(data.sig)),
}
}
}
@ -379,7 +379,7 @@ impl From<MacroData> for Def {
children: vec![],
decl_id: None,
docs: data.docs,
sig: data.sig.map(|s| From::from(s)),
sig: None,
}
}
}
@ -395,7 +395,7 @@ impl From<TypeDefData> for Def {
children: vec![],
decl_id: None,
docs: String::new(),
sig: None,
sig: data.sig.map(|s| From::from(s)),
}
}
}

View file

@ -152,6 +152,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
visibility: From::from(&item.vis),
parent: None,
docs: docs_for_attrs(&item.attrs),
sig: self.sig_base(item),
}))
}
ast::ItemKind::Static(ref typ, mt, ref expr) => {
@ -179,7 +180,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
type_value: ty_to_string(&typ),
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: None,
sig: Some(self.sig_base(item)),
}))
}
ast::ItemKind::Const(ref typ, ref expr) => {
@ -198,7 +199,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
type_value: ty_to_string(&typ),
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: None,
sig: Some(self.sig_base(item)),
}))
}
ast::ItemKind::Mod(ref m) => {
@ -209,6 +210,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Mod);
filter!(self.span_utils, sub_span, item.span, None);
Some(Data::ModData(ModData {
id: item.id,
name: item.ident.to_string(),
@ -219,6 +221,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
items: m.items.iter().map(|i| i.id).collect(),
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: self.sig_base(item),
}))
}
ast::ItemKind::Enum(ref def, _) => {
@ -241,6 +244,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
variants: def.variants.iter().map(|v| v.node.data.id()).collect(),
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: self.sig_base(item),
}))
}
ast::ItemKind::Impl(.., ref trait_ref, ref typ, _) => {
@ -305,17 +309,12 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
let text = self.span_utils.snippet(field.span);
let ident_start = text.find(&name).unwrap();
let ident_end = ident_start + name.len();
// TODO refs
let sig = Signature {
span: span,
text: text,
ident_start: ident_start,
ident_end: ident_end,
defs: vec![SigElement {
id: def_id,
start: ident_start,
end: ident_end,
}],
defs: vec![],
refs: vec![],
};
Some(VariableData {
@ -412,9 +411,24 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
let sub_span = self.span_utils.sub_span_after_keyword(span, keywords::Fn);
filter!(self.span_utils, sub_span, span, None);
let name = name.to_string();
let text = self.span_utils.signature_string_for_span(span);
println!("text: `{}`, name: `{}`", text, name);
let ident_start = text.find(&name).unwrap();
let ident_end = ident_start + name.len();
let sig = Signature {
span: span,
text: text,
ident_start: ident_start,
ident_end: ident_end,
defs: vec![],
refs: vec![],
};
Some(FunctionData {
id: id,
name: name.to_string(),
name: name,
qualname: qualname,
declaration: decl_id,
span: sub_span.unwrap(),
@ -424,6 +438,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
visibility: vis,
parent: parent_scope,
docs: docs,
sig: sig,
})
}
@ -569,11 +584,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
}
pub fn get_path_data(&self, id: NodeId, path: &ast::Path) -> Option<Data> {
<<<<<<< HEAD
let def = self.get_path_def(id);
=======
let def = option_try!(self.tcx.expect_resolution(id).maybe_full_def());
>>>>>>> save-analysis: fix ICE on partially resolved path
let sub_span = self.span_utils.span_for_last_ident(path.span);
filter!(self.span_utils, sub_span, path.span, None);
match def {
@ -723,6 +734,21 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
}
}
fn sig_base(&self, item: &ast::Item) -> Signature {
let text = self.span_utils.signature_string_for_span(item.span);
let name = item.ident.to_string();
let ident_start = text.find(&name).expect("Name not in signature?");
let ident_end = ident_start + name.len();
Signature {
span: mk_sp(item.span.lo, item.span.lo + BytePos(text.len() as u32)),
text: text,
ident_start: ident_start,
ident_end: ident_end,
defs: vec![],
refs: vec![],
}
}
#[inline]
pub fn enclosing_scope(&self, id: NodeId) -> NodeId {
self.tcx.map.get_enclosing_scope(id).unwrap_or(CRATE_NODE_ID)

View file

@ -18,6 +18,9 @@ use std::path::Path;
use syntax::ast;
use syntax::parse::lexer::{self, Reader, StringReader};
use syntax::parse::token::{self, Token};
use syntax::parse::parser::Parser;
use syntax::symbol::keywords;
use syntax::tokenstream::TokenTree;
use syntax_pos::*;
@ -317,7 +320,7 @@ impl<'a> SpanUtils<'a> {
/// function returns the program text from the start of the span until the
/// end of the 'signature' part, that is up to, but not including an opening
/// brace or semicolon.
pub fn signature_string_for_span(&self, span: Span) -> Option<String> {
pub fn signature_string_for_span(&self, span: Span) -> String {
let mut toks = self.span_to_tts(span).into_iter();
let mut prev = toks.next().unwrap();
let first_span = prev.get_span();
@ -325,7 +328,7 @@ impl<'a> SpanUtils<'a> {
for tok in toks {
if let TokenTree::Token(_, ref tok) = prev {
angle_count += match *tok {
token::Eof => { return None; }
token::Eof => { break; }
token::Lt => 1,
token::Gt => -1,
token::BinOp(token::Shl) => 2,
@ -338,15 +341,15 @@ impl<'a> SpanUtils<'a> {
continue;
}
if let TokenTree::Token(_, token::Semi) = tok {
return Some(self.snippet(mk_sp(first_span.lo, prev.get_span().hi)));
return self.snippet(mk_sp(first_span.lo, prev.get_span().hi));
} else if let TokenTree::Delimited(_, ref d) = tok {
if d.delim == token::Brace {
return Some(self.snippet(mk_sp(first_span.lo, prev.get_span().hi)));
return self.snippet(mk_sp(first_span.lo, prev.get_span().hi));
}
}
prev = tok;
}
None
self.snippet(span)
}
pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {