Skip to content

Commit

Permalink
Use the new signature stuff
Browse files Browse the repository at this point in the history
And fix a couple of bugs
  • Loading branch information
nrc committed Jun 12, 2017
1 parent 0058fdd commit a2a999f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 211 deletions.
41 changes: 8 additions & 33 deletions src/librustc_save_analysis/data.rs
Expand Up @@ -18,7 +18,7 @@ use rustc::hir::def_id::{CrateNum, DefId};
use syntax::ast::{self, Attribute, NodeId};
use syntax_pos::Span;

use rls_data::ExternalCrateData;
use rls_data::{ExternalCrateData, Signature};

pub struct CrateData {
pub name: String,
Expand Down Expand Up @@ -129,7 +129,7 @@ pub struct EnumData {
pub variants: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand Down Expand Up @@ -165,7 +165,7 @@ pub struct FunctionData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand Down Expand Up @@ -251,7 +251,7 @@ pub struct MethodData {
pub parent: Option<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand Down Expand Up @@ -292,7 +292,7 @@ pub struct StructData {
pub fields: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -307,7 +307,7 @@ pub struct StructVariantData {
pub scope: NodeId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -322,7 +322,7 @@ pub struct TraitData {
pub items: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -337,7 +337,7 @@ pub struct TupleVariantData {
pub scope: NodeId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand Down Expand Up @@ -419,28 +419,3 @@ pub struct VariableRefData {
pub scope: NodeId,
pub ref_id: DefId,
}


/// 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(Clone, Debug)]
pub struct Signature {
pub span: Span,
pub text: String,
// These identify the main identifier for the defintion as byte offsets into
// `text`. E.g., of `foo` in `pub fn foo(...)`
pub ident_start: usize,
pub ident_end: usize,
pub defs: Vec<SigElement>,
pub refs: Vec<SigElement>,
}

/// An element of a signature. `start` and `end` are byte offsets into the `text`
/// of the parent `Signature`.
#[derive(Clone, Debug)]
pub struct SigElement {
pub id: DefId,
pub start: usize,
pub end: usize,
}
37 changes: 14 additions & 23 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -48,12 +48,13 @@ use syntax::ptr::P;
use syntax::codemap::Spanned;
use syntax_pos::*;

use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs};
use super::data::*;
use super::dump::Dump;
use super::external_data::{Lower, make_def_id};
use super::span_utils::SpanUtils;
use super::recorder;
use {escape, generated_code, SaveContext, PathCollector, docs_for_attrs};
use data::*;
use dump::Dump;
use external_data::{Lower, make_def_id};
use recorder;
use span_utils::SpanUtils;
use sig;

use rls_data::ExternalCrateData;

Expand Down Expand Up @@ -646,7 +647,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: self.save_ctxt.sig_base(item),
sig: sig::item_signature(item, &self.save_ctxt),
attributes: item.attrs.clone(),
}.lower(self.tcx));
}
Expand Down Expand Up @@ -679,18 +680,6 @@ 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);
Expand All @@ -712,7 +701,8 @@ 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.hir)),
docs: docs_for_attrs(&variant.node.attrs),
sig: sig,
// TODO
sig: None,
attributes: variant.node.attrs.clone(),
}.lower(self.tcx));
}
Expand All @@ -739,7 +729,8 @@ 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.hir)),
docs: docs_for_attrs(&variant.node.attrs),
sig: sig,
// TODO
sig: None,
attributes: variant.node.attrs.clone(),
}.lower(self.tcx));
}
Expand Down Expand Up @@ -811,7 +802,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),
sig: sig::item_signature(item, &self.save_ctxt),
attributes: item.attrs.clone(),
}.lower(self.tcx));
}
Expand Down Expand Up @@ -1369,7 +1360,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)),
sig: sig::item_signature(item, &self.save_ctxt),
attributes: item.attrs.clone(),
}.lower(self.tcx));
}
Expand Down
65 changes: 19 additions & 46 deletions src/librustc_save_analysis/external_data.rs
Expand Up @@ -16,9 +16,9 @@ use syntax::codemap::CodeMap;
use syntax::print::pprust;
use syntax_pos::Span;

use data::{self, Visibility, SigElement};
use data::{self, Visibility};

use rls_data::{SpanData, CratePreludeData, Attribute};
use rls_data::{SpanData, CratePreludeData, Attribute, Signature};
use rls_span::{Column, Row};

// FIXME: this should be pub(crate), but the current snapshot doesn't allow it yet
Expand Down Expand Up @@ -103,7 +103,7 @@ pub struct EnumData {
pub variants: Vec<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -121,7 +121,7 @@ impl Lower for data::EnumData {
variants: self.variants.into_iter().map(|id| make_def_id(id, &tcx.hir)).collect(),
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand Down Expand Up @@ -186,7 +186,7 @@ pub struct FunctionData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -205,7 +205,7 @@ impl Lower for data::FunctionData {
visibility: self.visibility,
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand Down Expand Up @@ -355,7 +355,7 @@ pub struct MethodData {
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -374,7 +374,7 @@ impl Lower for data::MethodData {
visibility: self.visibility,
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand Down Expand Up @@ -410,7 +410,7 @@ impl Lower for data::ModData {
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.hir)).collect(),
visibility: self.visibility,
docs: self.docs,
sig: self.sig.map(|s| s.lower(tcx)),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand Down Expand Up @@ -450,7 +450,7 @@ pub struct StructData {
pub fields: Vec<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -469,7 +469,7 @@ impl Lower for data::StructData {
fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.hir)).collect(),
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand All @@ -486,7 +486,7 @@ pub struct StructVariantData {
pub scope: DefId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -504,7 +504,7 @@ impl Lower for data::StructVariantData {
scope: make_def_id(self.scope, &tcx.hir),
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand All @@ -521,7 +521,7 @@ pub struct TraitData {
pub items: Vec<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -539,7 +539,7 @@ impl Lower for data::TraitData {
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.hir)).collect(),
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand All @@ -556,7 +556,7 @@ pub struct TupleVariantData {
pub scope: DefId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}

Expand All @@ -574,7 +574,7 @@ impl Lower for data::TupleVariantData {
scope: make_def_id(self.scope, &tcx.hir),
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand Down Expand Up @@ -608,7 +608,7 @@ impl Lower for data::TypeDefData {
visibility: self.visibility,
parent: self.parent,
docs: self.docs,
sig: self.sig.map(|s| s.lower(tcx)),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand Down Expand Up @@ -718,7 +718,7 @@ impl Lower for data::VariableData {
parent: self.parent,
visibility: self.visibility,
docs: self.docs,
sig: self.sig.map(|s| s.lower(tcx)),
sig: self.sig,
attributes: self.attributes.lower(tcx),
}
}
Expand Down Expand Up @@ -746,30 +746,3 @@ impl Lower for data::VariableRefData {
}
}
}

#[derive(Clone, Debug)]
pub struct Signature {
pub span: SpanData,
pub text: String,
// These identify the main identifier for the defintion as byte offsets into
// `text`. E.g., of `foo` in `pub fn foo(...)`
pub ident_start: usize,
pub ident_end: usize,
pub defs: Vec<SigElement>,
pub refs: Vec<SigElement>,
}

impl Lower for data::Signature {
type Target = Signature;

fn lower(self, tcx: TyCtxt) -> Signature {
Signature {
span: span_from_span(self.span, tcx.sess.codemap()),
text: self.text,
ident_start: self.ident_start,
ident_end: self.ident_end,
defs: self.defs,
refs: self.refs,
}
}
}

0 comments on commit a2a999f

Please sign in to comment.