]> git.lizzy.rs Git - rust.git/blob - src/vertical.rs
Do not put where clause on a single with multi-lined type
[rust.git] / src / vertical.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Format with vertical alignment.
12
13 use std::cmp;
14
15 use config::lists::*;
16 use syntax::ast;
17 use syntax::codemap::{BytePos, Span};
18
19 use codemap::SpanUtils;
20 use comment::{combine_strs_with_missing_comments, contains_comment};
21 use expr::rewrite_field;
22 use items::{rewrite_struct_field, rewrite_struct_field_prefix};
23 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
24 use rewrite::{Rewrite, RewriteContext};
25 use shape::{Indent, Shape};
26 use spanned::Spanned;
27 use utils::{contains_skip, is_attributes_extendable, mk_sp};
28
29 pub trait AlignedItem {
30     fn skip(&self) -> bool;
31     fn get_span(&self) -> Span;
32     fn rewrite_prefix(&self, context: &RewriteContext, shape: Shape) -> Option<String>;
33     fn rewrite_aligned_item(
34         &self,
35         context: &RewriteContext,
36         shape: Shape,
37         prefix_max_width: usize,
38     ) -> Option<String>;
39 }
40
41 impl AlignedItem for ast::StructField {
42     fn skip(&self) -> bool {
43         contains_skip(&self.attrs)
44     }
45
46     fn get_span(&self) -> Span {
47         self.span()
48     }
49
50     fn rewrite_prefix(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
51         let attrs_str = self.attrs.rewrite(context, shape)?;
52         let missing_span = if self.attrs.is_empty() {
53             mk_sp(self.span.lo(), self.span.lo())
54         } else {
55             mk_sp(self.attrs.last().unwrap().span.hi(), self.span.lo())
56         };
57         let attrs_extendable = self.ident.is_none() && is_attributes_extendable(&attrs_str);
58         rewrite_struct_field_prefix(context, self).and_then(|field_str| {
59             combine_strs_with_missing_comments(
60                 context,
61                 &attrs_str,
62                 &field_str,
63                 missing_span,
64                 shape,
65                 attrs_extendable,
66             )
67         })
68     }
69
70     fn rewrite_aligned_item(
71         &self,
72         context: &RewriteContext,
73         shape: Shape,
74         prefix_max_width: usize,
75     ) -> Option<String> {
76         rewrite_struct_field(context, self, shape, prefix_max_width)
77     }
78 }
79
80 impl AlignedItem for ast::Field {
81     fn skip(&self) -> bool {
82         contains_skip(&self.attrs)
83     }
84
85     fn get_span(&self) -> Span {
86         self.span()
87     }
88
89     fn rewrite_prefix(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
90         let attrs_str = self.attrs.rewrite(context, shape)?;
91         let name = &self.ident.name.to_string();
92         let missing_span = if self.attrs.is_empty() {
93             mk_sp(self.span.lo(), self.span.lo())
94         } else {
95             mk_sp(self.attrs.last().unwrap().span.hi(), self.span.lo())
96         };
97         combine_strs_with_missing_comments(
98             context,
99             &attrs_str,
100             name,
101             missing_span,
102             shape,
103             is_attributes_extendable(&attrs_str),
104         )
105     }
106
107     fn rewrite_aligned_item(
108         &self,
109         context: &RewriteContext,
110         shape: Shape,
111         prefix_max_width: usize,
112     ) -> Option<String> {
113         rewrite_field(context, self, shape, prefix_max_width)
114     }
115 }
116
117 pub fn rewrite_with_alignment<T: AlignedItem>(
118     fields: &[T],
119     context: &RewriteContext,
120     shape: Shape,
121     span: Span,
122     one_line_width: usize,
123 ) -> Option<String> {
124     let (spaces, group_index) = if context.config.struct_field_align_threshold() > 0 {
125         group_aligned_items(context, fields)
126     } else {
127         ("", fields.len() - 1)
128     };
129     let init = &fields[0..group_index + 1];
130     let rest = &fields[group_index + 1..];
131     let init_last_pos = if rest.is_empty() {
132         span.hi()
133     } else {
134         // Decide whether the missing comments should stick to init or rest.
135         let init_hi = init[init.len() - 1].get_span().hi();
136         let rest_lo = rest[0].get_span().lo();
137         let missing_span = mk_sp(init_hi, rest_lo);
138         let missing_span = mk_sp(
139             context.snippet_provider.span_after(missing_span, ","),
140             missing_span.hi(),
141         );
142
143         let snippet = context.snippet(missing_span);
144         if snippet.trim_left().starts_with("//") {
145             let offset = snippet.lines().next().map_or(0, |l| l.len());
146             // 2 = "," + "\n"
147             init_hi + BytePos(offset as u32 + 2)
148         } else if snippet.trim_left().starts_with("/*") {
149             let comment_lines = snippet
150                 .lines()
151                 .position(|line| line.trim_right().ends_with("*/"))
152                 .unwrap_or(0);
153
154             let offset = snippet
155                 .lines()
156                 .take(comment_lines + 1)
157                 .collect::<Vec<_>>()
158                 .join("\n")
159                 .len();
160
161             init_hi + BytePos(offset as u32 + 2)
162         } else {
163             missing_span.lo()
164         }
165     };
166     let init_span = mk_sp(span.lo(), init_last_pos);
167     let one_line_width = if rest.is_empty() { one_line_width } else { 0 };
168     let result =
169         rewrite_aligned_items_inner(context, init, init_span, shape.indent, one_line_width)?;
170     if rest.is_empty() {
171         Some(result + spaces)
172     } else {
173         let rest_span = mk_sp(init_last_pos, span.hi());
174         let rest_str = rewrite_with_alignment(rest, context, shape, rest_span, one_line_width)?;
175         Some(
176             result + spaces + "\n"
177                 + &shape
178                     .indent
179                     .block_indent(context.config)
180                     .to_string(context.config) + &rest_str,
181         )
182     }
183 }
184
185 fn struct_field_prefix_max_min_width<T: AlignedItem>(
186     context: &RewriteContext,
187     fields: &[T],
188     shape: Shape,
189 ) -> (usize, usize) {
190     fields
191         .iter()
192         .map(|field| {
193             field.rewrite_prefix(context, shape).and_then(|field_str| {
194                 if field_str.contains('\n') {
195                     None
196                 } else {
197                     Some(field_str.len())
198                 }
199             })
200         })
201         .fold(Some((0, ::std::usize::MAX)), |acc, len| match (acc, len) {
202             (Some((max_len, min_len)), Some(len)) => {
203                 Some((cmp::max(max_len, len), cmp::min(min_len, len)))
204             }
205             _ => None,
206         })
207         .unwrap_or((0, 0))
208 }
209
210 fn rewrite_aligned_items_inner<T: AlignedItem>(
211     context: &RewriteContext,
212     fields: &[T],
213     span: Span,
214     offset: Indent,
215     one_line_width: usize,
216 ) -> Option<String> {
217     let item_indent = offset.block_indent(context.config);
218     // 1 = ","
219     let item_shape = Shape::indented(item_indent, context.config).sub_width(1)?;
220     let (mut field_prefix_max_width, field_prefix_min_width) =
221         struct_field_prefix_max_min_width(context, fields, item_shape);
222     let max_diff = field_prefix_max_width.saturating_sub(field_prefix_min_width);
223     if max_diff > context.config.struct_field_align_threshold() {
224         field_prefix_max_width = 0;
225     }
226
227     let items = itemize_list(
228         context.snippet_provider,
229         fields.iter(),
230         "}",
231         ",",
232         |field| field.get_span().lo(),
233         |field| field.get_span().hi(),
234         |field| field.rewrite_aligned_item(context, item_shape, field_prefix_max_width),
235         span.lo(),
236         span.hi(),
237         false,
238     ).collect::<Vec<_>>();
239
240     let tactic = definitive_tactic(
241         &items,
242         ListTactic::HorizontalVertical,
243         Separator::Comma,
244         one_line_width,
245     );
246
247     let fmt = ListFormatting {
248         tactic,
249         separator: ",",
250         trailing_separator: context.config.trailing_comma(),
251         separator_place: SeparatorPlace::Back,
252         shape: item_shape,
253         ends_with_newline: true,
254         preserve_newline: true,
255         config: context.config,
256     };
257     write_list(&items, &fmt)
258 }
259
260 fn group_aligned_items<T: AlignedItem>(
261     context: &RewriteContext,
262     fields: &[T],
263 ) -> (&'static str, usize) {
264     let mut index = 0;
265     for i in 0..fields.len() - 1 {
266         if fields[i].skip() {
267             return ("", index);
268         }
269         // See if there are comments or empty lines between fields.
270         let span = mk_sp(fields[i].get_span().hi(), fields[i + 1].get_span().lo());
271         let snippet = context
272             .snippet(span)
273             .lines()
274             .skip(1)
275             .collect::<Vec<_>>()
276             .join("\n");
277         let spacings = if snippet.lines().rev().skip(1).any(|l| l.trim().is_empty()) {
278             "\n"
279         } else {
280             ""
281         };
282         if contains_comment(&snippet) || snippet.lines().count() > 1 {
283             return (spacings, index);
284         }
285         index += 1;
286     }
287     ("", index)
288 }