+ /// `span` must be the span for an item such as a function or struct. This
+ /// 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> {
+ let mut toks = self.span_to_tts(span).into_iter();
+ let mut prev = toks.next().unwrap();
+ let first_span = prev.get_span();
+ let mut angle_count = 0;
+ for tok in toks {
+ if let TokenTree::Token(_, ref tok) = prev {
+ angle_count += match *tok {
+ token::Eof => { return None; }
+ token::Lt => 1,
+ token::Gt => -1,
+ token::BinOp(token::Shl) => 2,
+ token::BinOp(token::Shr) => -2,
+ _ => 0,
+ };
+ }
+ if angle_count > 0 {
+ prev = tok;
+ continue;
+ }
+ if let TokenTree::Token(_, token::Semi) = tok {
+ return Some(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)));
+ }
+ }
+ prev = tok;
+ }
+ None
+ }
+