// except according to those terms.
use syntax::attr::HasAttrs;
-use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
use syntax::parse::ParseSess;
+use syntax::source_map::{self, BytePos, Pos, SourceMap, Span};
use syntax::{ast, visit};
use attr::*;
-use codemap::{LineRangeUtils, SpanUtils};
use comment::{CodeCharKind, CommentCodeSlices, FindUncommented};
use config::{BraceStyle, Config};
use items::{
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
- rewrite_associated_impl_type, rewrite_associated_type, rewrite_extern_crate,
- rewrite_type_alias, FnSig, StaticParts, StructParts,
+ rewrite_associated_impl_type, rewrite_associated_type, rewrite_existential_impl_type,
+ rewrite_existential_type, rewrite_extern_crate, rewrite_type_alias, FnSig, StaticParts,
+ StructParts,
};
use macros::{rewrite_macro, rewrite_macro_def, MacroPosition};
use rewrite::{Rewrite, RewriteContext};
use shape::{Indent, Shape};
+use source_map::{LineRangeUtils, SpanUtils};
use spanned::Spanned;
use utils::{
self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec,
- DEPR_SKIP_ANNOTATION,
+ rewrite_ident, DEPR_SKIP_ANNOTATION,
};
use {ErrorKind, FormatReport, FormattingError};
pub struct FmtVisitor<'a> {
pub parse_session: &'a ParseSess,
- pub codemap: &'a CodeMap,
+ pub source_map: &'a SourceMap,
pub buffer: String,
pub last_pos: BytePos,
// FIXME: use an RAII util or closure for indenting
fn visit_stmt(&mut self, stmt: &ast::Stmt) {
debug!(
"visit_stmt: {:?} {:?}",
- self.codemap.lookup_char_pos(stmt.span.lo()),
- self.codemap.lookup_char_pos(stmt.span.hi())
+ self.source_map.lookup_char_pos(stmt.span.lo()),
+ self.source_map.lookup_char_pos(stmt.span.hi())
);
match stmt.node {
ast::StmtKind::Item(ref item) => {
self.visit_item(item);
+ // Handle potential `;` after the item.
+ self.format_missing(stmt.span.hi());
}
ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
if contains_skip(get_attrs_from_stmt(stmt)) {
) {
debug!(
"visit_block: {:?} {:?}",
- self.codemap.lookup_char_pos(b.span.lo()),
- self.codemap.lookup_char_pos(b.span.hi())
+ self.source_map.lookup_char_pos(b.span.lo()),
+ self.source_map.lookup_char_pos(b.span.hi())
);
// Check if this block has braces.
let indent = self.block_indent;
let block;
let rewrite = match fk {
- visit::FnKind::ItemFn(ident, _, _, _, _, b) | visit::FnKind::Method(ident, _, _, b) => {
+ visit::FnKind::ItemFn(ident, _, _, b) | visit::FnKind::Method(ident, _, _, b) => {
block = b;
self.rewrite_fn(
indent,
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
self.visit_static(&StaticParts::from_item(item));
}
- ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
+ ast::ItemKind::Fn(ref decl, fn_header, ref generics, ref body) => {
let inner_attrs = inner_attributes(&item.attrs);
self.visit_fn(
- visit::FnKind::ItemFn(item.ident, unsafety, constness, abi, &item.vis, body),
+ visit::FnKind::ItemFn(item.ident, fn_header, &item.vis, body),
generics,
decl,
item.span,
ty,
generics,
&item.vis,
- item.span,
+ );
+ self.push_rewrite(item.span, rewrite);
+ }
+ ast::ItemKind::Existential(ref generic_bounds, ref generics) => {
+ let rewrite = rewrite_existential_type(
+ &self.get_context(),
+ self.block_indent,
+ item.ident,
+ generic_bounds,
+ generics,
+ &item.vis,
);
self.push_rewrite(item.span, rewrite);
}
ii.ident,
ii.defaultness,
Some(ty),
- None,
&self.get_context(),
self.block_indent,
);
self.push_rewrite(ii.span, rewrite);
}
+ ast::ImplItemKind::Existential(ref generic_bounds) => {
+ let rewrite = rewrite_existential_impl_type(
+ &self.get_context(),
+ ii.ident,
+ generic_bounds,
+ self.block_indent,
+ );
+ self.push_rewrite(ii.span, rewrite);
+ }
ast::ImplItemKind::Macro(ref mac) => {
self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
}
}
pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
- FmtVisitor::from_codemap(
+ FmtVisitor::from_source_map(
ctx.parse_session,
ctx.config,
ctx.snippet_provider,
)
}
- pub(crate) fn from_codemap(
+ pub(crate) fn from_source_map(
parse_session: &'a ParseSess,
config: &'a Config,
snippet_provider: &'a SnippetProvider,
) -> FmtVisitor<'a> {
FmtVisitor {
parse_session,
- codemap: parse_session.codemap(),
+ source_map: parse_session.source_map(),
buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),
last_pos: BytePos(0),
block_indent: Indent::empty(),
pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
for attr in attrs {
if attr.name() == DEPR_SKIP_ANNOTATION {
- let file_name = self.codemap.span_to_filename(attr.span).into();
+ let file_name = self.source_map.span_to_filename(attr.span).into();
self.report.append(
file_name,
vec![FormattingError::from_span(
&attr.span,
- &self.codemap,
+ &self.source_map,
ErrorKind::DeprecatedAttr,
)],
);
if attr.path.segments.len() == 1
|| attr.path.segments[1].ident.to_string() != "skip"
{
- let file_name = self.codemap.span_to_filename(attr.span).into();
+ let file_name = self.source_map.span_to_filename(attr.span).into();
self.report.append(
file_name,
vec![FormattingError::from_span(
&attr.span,
- &self.codemap,
+ &self.source_map,
ErrorKind::BadAttr,
)],
);
attrs: &[ast::Attribute],
is_internal: bool,
) {
- self.push_str(&*utils::format_visibility(vis));
+ let vis_str = utils::format_visibility(&self.get_context(), vis);
+ self.push_str(&*vis_str);
self.push_str("mod ");
- self.push_str(&ident.to_string());
+ // Calling `to_owned()` to work around borrow checker.
+ let ident_str = rewrite_ident(&self.get_context(), ident).to_owned();
+ self.push_str(&ident_str);
if is_internal {
match self.config.brace_style() {
}
}
- pub fn format_separate_mod(&mut self, m: &ast::Mod, filemap: &codemap::FileMap) {
+ pub fn format_separate_mod(&mut self, m: &ast::Mod, source_file: &source_map::SourceFile) {
self.block_indent = Indent::empty();
self.walk_mod_items(m);
- self.format_missing_with_indent(filemap.end_pos);
+ self.format_missing_with_indent(source_file.end_pos);
}
pub fn skip_empty_lines(&mut self, end_pos: BytePos) {
pub fn get_context(&self) -> RewriteContext {
RewriteContext {
parse_session: self.parse_session,
- codemap: self.codemap,
+ source_map: self.source_map,
config: self.config,
inside_macro: RefCell::new(false),
use_block: RefCell::new(false),