]> git.lizzy.rs Git - rust.git/commitdiff
Preserve comments between attribute and use item
authortopecongiro <seuchida@gmail.com>
Sat, 28 Apr 2018 07:04:09 +0000 (16:04 +0900)
committertopecongiro <seuchida@gmail.com>
Sat, 28 Apr 2018 07:04:09 +0000 (16:04 +0900)
src/imports.rs

index f7286fc3b43c81a329d5392290eb02e4625f54de..9fd844a2c35b6ceec489b43dec87a990969d7c22 100644 (file)
@@ -15,6 +15,7 @@
 use syntax::codemap::{self, BytePos, Span, DUMMY_SP};
 
 use codemap::SpanUtils;
+use comment::combine_strs_with_missing_comments;
 use config::IndentStyle;
 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
 use rewrite::{Rewrite, RewriteContext};
@@ -219,26 +220,26 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl UseTree {
     // Rewrite use tree with `use ` and a trailing `;`.
     pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
-        let mut result = String::with_capacity(256);
-        if let Some(ref attrs) = self.attrs {
-            result.push_str(&attrs.rewrite(context, shape)?);
-            if !result.is_empty() {
-                result.push_str(&shape.indent.to_string_with_newline(context.config));
-            }
-        }
-
         let vis = self.visibility
             .as_ref()
             .map_or(Cow::from(""), |vis| ::utils::format_visibility(&vis));
-        result.push_str(&self.rewrite(context, shape.offset_left(vis.len())?)
+        let use_str = self.rewrite(context, shape.offset_left(vis.len())?)
             .map(|s| {
                 if s.is_empty() {
                     s.to_owned()
                 } else {
                     format!("{}use {};", vis, s)
                 }
-            })?);
-        Some(result)
+            })?;
+        if let Some(ref attrs) = self.attrs {
+            let attr_str = attrs.rewrite(context, shape)?;
+            let lo = attrs.last().as_ref()?.span().hi();
+            let hi = self.span.lo();
+            let span = mk_sp(lo, hi);
+            combine_strs_with_missing_comments(context, &attr_str, &use_str, span, shape, false)
+        } else {
+            Some(use_str)
+        }
     }
 
     // FIXME: Use correct span?