]> git.lizzy.rs Git - rust.git/blobdiff - src/visitor.rs
Change `print_diff` to output the correct line number.
[rust.git] / src / visitor.rs
index fdbc9907f22a1cbb22d8a40e0d670ed88226b53b..e6ef7f23da0470bc9c544fe8b7ffcf4dce380760 100644 (file)
@@ -9,12 +9,11 @@
 // 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::{
@@ -26,6 +25,7 @@
 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,
@@ -61,7 +61,7 @@ pub fn new(start_pos: BytePos, big_snippet: &'a str) -> Self {
 
 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
@@ -83,13 +83,15 @@ pub fn shape(&self) -> Shape {
     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)) {
@@ -119,8 +121,8 @@ pub fn visit_block(
     ) {
         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.
@@ -253,7 +255,6 @@ fn close_block(&mut self, unindent_comment: bool) {
 
     // Note that this only gets called for function definitions. Required methods
     // on traits do not get handled here.
-    // FIXME(topecongiro) Format async fn (#2812).
     fn visit_fn(
         &mut self,
         fk: visit::FnKind,
@@ -574,7 +575,7 @@ pub fn push_skipped_with_span(&mut self, span: Span) {
     }
 
     pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
-        FmtVisitor::from_codemap(
+        FmtVisitor::from_source_map(
             ctx.parse_session,
             ctx.config,
             ctx.snippet_provider,
@@ -582,7 +583,7 @@ pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
         )
     }
 
-    pub(crate) fn from_codemap(
+    pub(crate) fn from_source_map(
         parse_session: &'a ParseSess,
         config: &'a Config,
         snippet_provider: &'a SnippetProvider,
@@ -590,7 +591,7 @@ pub(crate) fn from_codemap(
     ) -> 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(),
@@ -616,12 +617,12 @@ pub fn snippet(&'b self, span: Span) -> &'a str {
     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,
+                        attr.span,
+                        &self.source_map,
                         ErrorKind::DeprecatedAttr,
                     )],
                 );
@@ -629,12 +630,12 @@ pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -
                 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,
+                            attr.span,
+                            &self.source_map,
                             ErrorKind::BadAttr,
                         )],
                     );
@@ -740,10 +741,10 @@ fn format_mod(
         }
     }
 
-    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) {
@@ -778,7 +779,7 @@ pub fn with_context<F>(&mut self, f: F) -> Option<String>
     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),