@@ -15,6 +15,7 @@ use syntax::ast::{self, UseTreeKind};
15
15
use syntax:: codemap:: { self , BytePos , Span , DUMMY_SP } ;
16
16
17
17
use codemap:: SpanUtils ;
18
+ use comment:: combine_strs_with_missing_comments;
18
19
use config:: IndentStyle ;
19
20
use lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , ListItem , Separator } ;
20
21
use rewrite:: { Rewrite , RewriteContext } ;
@@ -118,6 +119,17 @@ impl PartialEq for UseTree {
118
119
}
119
120
impl Eq for UseTree { }
120
121
122
+ impl Spanned for UseTree {
123
+ fn span ( & self ) -> Span {
124
+ let lo = if let Some ( ref attrs) = self . attrs {
125
+ attrs. iter ( ) . next ( ) . map_or ( self . span . lo ( ) , |a| a. span . lo ( ) )
126
+ } else {
127
+ self . span . lo ( )
128
+ } ;
129
+ mk_sp ( lo, self . span . hi ( ) )
130
+ }
131
+ }
132
+
121
133
impl UseSegment {
122
134
// Clone a version of self with any top-level alias removed.
123
135
fn remove_alias ( & self ) -> UseSegment {
@@ -219,26 +231,26 @@ impl fmt::Display for UseTree {
219
231
impl UseTree {
220
232
// Rewrite use tree with `use ` and a trailing `;`.
221
233
pub fn rewrite_top_level ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
222
- let mut result = String :: with_capacity ( 256 ) ;
223
- if let Some ( ref attrs) = self . attrs {
224
- result. push_str ( & attrs. rewrite ( context, shape) ?) ;
225
- if !result. is_empty ( ) {
226
- result. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
227
- }
228
- }
229
-
230
234
let vis = self . visibility
231
235
. as_ref ( )
232
236
. map_or ( Cow :: from ( "" ) , |vis| :: utils:: format_visibility ( & vis) ) ;
233
- result . push_str ( & self . rewrite ( context, shape. offset_left ( vis. len ( ) ) ?)
237
+ let use_str = self . rewrite ( context, shape. offset_left ( vis. len ( ) ) ?)
234
238
. map ( |s| {
235
239
if s. is_empty ( ) {
236
240
s. to_owned ( )
237
241
} else {
238
242
format ! ( "{}use {};" , vis, s)
239
243
}
240
- } ) ?) ;
241
- Some ( result)
244
+ } ) ?;
245
+ if let Some ( ref attrs) = self . attrs {
246
+ let attr_str = attrs. rewrite ( context, shape) ?;
247
+ let lo = attrs. last ( ) . as_ref ( ) ?. span ( ) . hi ( ) ;
248
+ let hi = self . span . lo ( ) ;
249
+ let span = mk_sp ( lo, hi) ;
250
+ combine_strs_with_missing_comments ( context, & attr_str, & use_str, span, shape, false )
251
+ } else {
252
+ Some ( use_str)
253
+ }
242
254
}
243
255
244
256
// FIXME: Use correct span?
@@ -267,7 +279,7 @@ impl UseTree {
267
279
use_tree,
268
280
None ,
269
281
Some ( item. vis . clone ( ) ) ,
270
- Some ( item. span ( ) . lo ( ) ) ,
282
+ Some ( item. span . lo ( ) ) ,
271
283
if item. attrs . is_empty ( ) {
272
284
None
273
285
} else {
0 commit comments