Move Spanned to spanned.rs from lib.rs

This commit is contained in:
Seiichi Uchida 2017-09-17 15:32:00 +09:00 committed by topecongiro
parent df7d2be562
commit 32fa51a6a9
9 changed files with 188 additions and 173 deletions

View file

@ -17,7 +17,7 @@ use syntax::{ast, ptr};
use syntax::codemap::{BytePos, CodeMap, Span};
use syntax::parse::classify;
use Spanned;
use spanned::Spanned;
use chains::rewrite_chain;
use codemap::{LineRangeUtils, SpanUtils};
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,

View file

@ -13,7 +13,7 @@ use std::cmp::Ordering;
use syntax::ast;
use syntax::codemap::{BytePos, Span};
use Spanned;
use spanned::Spanned;
use codemap::SpanUtils;
use comment::combine_strs_with_missing_comments;
use config::IndentStyle;

View file

@ -16,7 +16,7 @@ use syntax::{abi, ast, ptr, symbol};
use syntax::ast::ImplItem;
use syntax::codemap::{BytePos, Span};
use Spanned;
use spanned::Spanned;
use codemap::{LineRangeUtils, SpanUtils};
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented};

View file

@ -34,17 +34,16 @@ use std::rc::Rc;
use errors::{DiagnosticBuilder, Handler};
use errors::emitter::{ColorConfig, EmitterWriter};
use macros::MacroArg;
use strings::string_buffer::StringBuffer;
use syntax::ast;
use syntax::codemap::{CodeMap, FilePathMapping, Span};
use syntax::codemap::{CodeMap, FilePathMapping};
use syntax::parse::{self, ParseSess};
use checkstyle::{output_footer, output_header};
use config::Config;
use filemap::FileMap;
use issues::{BadIssueSeeker, Issue};
use utils::{isatty, mk_sp, outer_attributes};
use utils::isatty;
use visitor::FmtVisitor;
pub use self::summary::Summary;
@ -52,6 +51,7 @@ pub use self::summary::Summary;
#[macro_use]
mod utils;
mod shape;
mod spanned;
pub mod config;
pub mod codemap;
pub mod filemap;
@ -76,169 +76,6 @@ mod patterns;
mod summary;
mod vertical;
/// Spanned returns a span including attributes, if available.
pub trait Spanned {
fn span(&self) -> Span;
}
macro_rules! span_with_attrs_lo_hi {
($this:ident, $lo:expr, $hi:expr) => {
{
let attrs = outer_attributes(&$this.attrs);
if attrs.is_empty() {
mk_sp($lo, $hi)
} else {
mk_sp(attrs[0].span.lo(), $hi)
}
}
}
}
macro_rules! span_with_attrs {
($this:ident) => {
span_with_attrs_lo_hi!($this, $this.span.lo(), $this.span.hi())
}
}
macro_rules! implement_spanned {
($this:ty) => {
impl Spanned for $this {
fn span(&self) -> Span {
span_with_attrs!(self)
}
}
}
}
// Implement `Spanned` for structs with `attrs` field.
implement_spanned!(ast::Expr);
implement_spanned!(ast::Field);
implement_spanned!(ast::ForeignItem);
implement_spanned!(ast::Item);
implement_spanned!(ast::Local);
impl Spanned for ast::Stmt {
fn span(&self) -> Span {
match self.node {
ast::StmtKind::Local(ref local) => mk_sp(local.span().lo(), self.span.hi()),
ast::StmtKind::Item(ref item) => mk_sp(item.span().lo(), self.span.hi()),
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
mk_sp(expr.span().lo(), self.span.hi())
}
ast::StmtKind::Mac(ref mac) => {
let (_, _, ref attrs) = **mac;
if attrs.is_empty() {
self.span
} else {
mk_sp(attrs[0].span.lo(), self.span.hi())
}
}
}
}
}
impl Spanned for ast::Pat {
fn span(&self) -> Span {
self.span
}
}
impl Spanned for ast::Ty {
fn span(&self) -> Span {
self.span
}
}
impl Spanned for ast::Arm {
fn span(&self) -> Span {
span_with_attrs_lo_hi!(self, self.pats[0].span.lo(), self.body.span.hi())
}
}
impl Spanned for ast::Arg {
fn span(&self) -> Span {
if items::is_named_arg(self) {
utils::mk_sp(self.pat.span.lo(), self.ty.span.hi())
} else {
self.ty.span
}
}
}
impl Spanned for ast::StructField {
fn span(&self) -> Span {
span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
}
}
impl Spanned for ast::WherePredicate {
fn span(&self) -> Span {
match *self {
ast::WherePredicate::BoundPredicate(ref p) => p.span,
ast::WherePredicate::RegionPredicate(ref p) => p.span,
ast::WherePredicate::EqPredicate(ref p) => p.span,
}
}
}
impl Spanned for ast::FunctionRetTy {
fn span(&self) -> Span {
match *self {
ast::FunctionRetTy::Default(span) => span,
ast::FunctionRetTy::Ty(ref ty) => ty.span,
}
}
}
impl Spanned for ast::TyParam {
fn span(&self) -> Span {
// Note that ty.span is the span for ty.ident, not the whole item.
let lo = if self.attrs.is_empty() {
self.span.lo()
} else {
self.attrs[0].span.lo()
};
if let Some(ref def) = self.default {
return mk_sp(lo, def.span.hi());
}
if self.bounds.is_empty() {
return mk_sp(lo, self.span.hi());
}
let hi = self.bounds[self.bounds.len() - 1].span().hi();
mk_sp(lo, hi)
}
}
impl Spanned for ast::TyParamBound {
fn span(&self) -> Span {
match *self {
ast::TyParamBound::TraitTyParamBound(ref ptr, _) => ptr.span,
ast::TyParamBound::RegionTyParamBound(ref l) => l.span,
}
}
}
impl Spanned for ast::LifetimeDef {
fn span(&self) -> Span {
let hi = if self.bounds.is_empty() {
self.lifetime.span.hi()
} else {
self.bounds[self.bounds.len() - 1].span.hi()
};
mk_sp(self.lifetime.span.lo(), hi)
}
}
impl Spanned for MacroArg {
fn span(&self) -> Span {
match *self {
MacroArg::Expr(ref expr) => expr.span(),
MacroArg::Ty(ref ty) => ty.span(),
MacroArg::Pat(ref pat) => pat.span(),
}
}
}
pub enum ErrorKind {
// Line has exceeded character limit (found, maximum)
LineOverflow(usize, usize),

View file

@ -12,7 +12,7 @@ use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd};
use syntax::codemap::{self, BytePos, Span};
use syntax::ptr;
use Spanned;
use spanned::Spanned;
use codemap::SpanUtils;
use comment::FindUncommented;
use expr::{can_be_overflowed_expr, rewrite_call_inner, rewrite_pair, rewrite_unary_prefix,

178
src/spanned.rs Normal file
View file

@ -0,0 +1,178 @@
// Copyright 2017 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.
use syntax::ast;
use syntax::codemap::Span;
use macros::MacroArg;
use utils::{mk_sp, outer_attributes};
/// Spanned returns a span including attributes, if available.
pub trait Spanned {
fn span(&self) -> Span;
}
macro_rules! span_with_attrs_lo_hi {
($this:ident, $lo:expr, $hi:expr) => {
{
let attrs = outer_attributes(&$this.attrs);
if attrs.is_empty() {
mk_sp($lo, $hi)
} else {
mk_sp(attrs[0].span.lo(), $hi)
}
}
}
}
macro_rules! span_with_attrs {
($this:ident) => {
span_with_attrs_lo_hi!($this, $this.span.lo(), $this.span.hi())
}
}
macro_rules! implement_spanned {
($this:ty) => {
impl Spanned for $this {
fn span(&self) -> Span {
span_with_attrs!(self)
}
}
}
}
// Implement `Spanned` for structs with `attrs` field.
implement_spanned!(ast::Expr);
implement_spanned!(ast::Field);
implement_spanned!(ast::ForeignItem);
implement_spanned!(ast::Item);
implement_spanned!(ast::Local);
impl Spanned for ast::Stmt {
fn span(&self) -> Span {
match self.node {
ast::StmtKind::Local(ref local) => mk_sp(local.span().lo(), self.span.hi()),
ast::StmtKind::Item(ref item) => mk_sp(item.span().lo(), self.span.hi()),
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
mk_sp(expr.span().lo(), self.span.hi())
}
ast::StmtKind::Mac(ref mac) => {
let (_, _, ref attrs) = **mac;
if attrs.is_empty() {
self.span
} else {
mk_sp(attrs[0].span.lo(), self.span.hi())
}
}
}
}
}
impl Spanned for ast::Pat {
fn span(&self) -> Span {
self.span
}
}
impl Spanned for ast::Ty {
fn span(&self) -> Span {
self.span
}
}
impl Spanned for ast::Arm {
fn span(&self) -> Span {
span_with_attrs_lo_hi!(self, self.pats[0].span.lo(), self.body.span.hi())
}
}
impl Spanned for ast::Arg {
fn span(&self) -> Span {
if ::items::is_named_arg(self) {
mk_sp(self.pat.span.lo(), self.ty.span.hi())
} else {
self.ty.span
}
}
}
impl Spanned for ast::StructField {
fn span(&self) -> Span {
span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
}
}
impl Spanned for ast::WherePredicate {
fn span(&self) -> Span {
match *self {
ast::WherePredicate::BoundPredicate(ref p) => p.span,
ast::WherePredicate::RegionPredicate(ref p) => p.span,
ast::WherePredicate::EqPredicate(ref p) => p.span,
}
}
}
impl Spanned for ast::FunctionRetTy {
fn span(&self) -> Span {
match *self {
ast::FunctionRetTy::Default(span) => span,
ast::FunctionRetTy::Ty(ref ty) => ty.span,
}
}
}
impl Spanned for ast::TyParam {
fn span(&self) -> Span {
// Note that ty.span is the span for ty.ident, not the whole item.
let lo = if self.attrs.is_empty() {
self.span.lo()
} else {
self.attrs[0].span.lo()
};
if let Some(ref def) = self.default {
return mk_sp(lo, def.span.hi());
}
if self.bounds.is_empty() {
return mk_sp(lo, self.span.hi());
}
let hi = self.bounds[self.bounds.len() - 1].span().hi();
mk_sp(lo, hi)
}
}
impl Spanned for ast::TyParamBound {
fn span(&self) -> Span {
match *self {
ast::TyParamBound::TraitTyParamBound(ref ptr, _) => ptr.span,
ast::TyParamBound::RegionTyParamBound(ref l) => l.span,
}
}
}
impl Spanned for ast::LifetimeDef {
fn span(&self) -> Span {
let hi = if self.bounds.is_empty() {
self.lifetime.span.hi()
} else {
self.bounds[self.bounds.len() - 1].span.hi()
};
mk_sp(self.lifetime.span.lo(), hi)
}
}
impl Spanned for MacroArg {
fn span(&self) -> Span {
match *self {
MacroArg::Expr(ref expr) => expr.span(),
MacroArg::Ty(ref ty) => ty.span(),
MacroArg::Pat(ref pat) => pat.span(),
}
}
}

View file

@ -17,7 +17,7 @@ use syntax::codemap::{self, BytePos, Span};
use syntax::print::pprust;
use syntax::symbol::keywords;
use Spanned;
use spanned::Spanned;
use codemap::SpanUtils;
use config::{IndentStyle, Style, TypeDensity};
use expr::{rewrite_pair, rewrite_tuple, rewrite_unary_prefix, wrap_args_with_parens};

View file

@ -15,7 +15,7 @@ use std::cmp;
use syntax::ast;
use syntax::codemap::{BytePos, Span};
use Spanned;
use spanned::Spanned;
use codemap::SpanUtils;
use comment::{combine_strs_with_missing_comments, contains_comment};
use expr::rewrite_field;

View file

@ -16,7 +16,7 @@ use syntax::attr::HasAttrs;
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
use syntax::parse::ParseSess;
use Spanned;
use spanned::Spanned;
use codemap::{LineRangeUtils, SpanUtils};
use comment::{contains_comment, recover_missing_comment_in_span, CodeCharKind, CommentCodeSlices,
FindUncommented};