]> git.lizzy.rs Git - rust.git/blob - src/pairs.rs
Merge pull request #3050 from andrehjr/empty-impl-body-with-braces-newline
[rust.git] / src / pairs.rs
1 // Copyright 2018 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 use syntax::ast;
12
13 use config::lists::*;
14 use config::IndentStyle;
15 use rewrite::{Rewrite, RewriteContext};
16 use shape::Shape;
17 use utils::{first_line_width, is_single_line, last_line_width, trimmed_last_line_width, wrap_str};
18
19 /// Sigils that decorate a binop pair.
20 #[derive(new, Clone, Copy)]
21 pub(crate) struct PairParts<'a> {
22     prefix: &'a str,
23     infix: &'a str,
24     suffix: &'a str,
25 }
26
27 impl<'a> PairParts<'a> {
28     pub(crate) fn infix(infix: &'a str) -> PairParts<'a> {
29         PairParts {
30             prefix: "",
31             infix,
32             suffix: "",
33         }
34     }
35 }
36
37 // Flattens a tree of pairs into a list and tries to rewrite them all at once.
38 // FIXME would be nice to reuse the lists API for this, but because each separator
39 // can be different, we can't.
40 pub(crate) fn rewrite_all_pairs(
41     expr: &ast::Expr,
42     shape: Shape,
43     context: &RewriteContext,
44 ) -> Option<String> {
45     // First we try formatting on one line.
46     if let Some(list) = expr.flatten(context, false) {
47         if let Some(r) = rewrite_pairs_one_line(&list, shape, context) {
48             return Some(r);
49         }
50     }
51
52     // We can't format on line, so try many. When we flatten here we make sure
53     // to only flatten pairs with the same operator, that way we don't
54     // necessarily need one line per sub-expression, but we don't do anything
55     // too funny wrt precedence.
56     expr.flatten(context, true)
57         .and_then(|list| rewrite_pairs_multiline(list, shape, context))
58 }
59
60 // This may return a multi-line result since we allow the last expression to go
61 // multiline in a 'single line' formatting.
62 fn rewrite_pairs_one_line<T: Rewrite>(
63     list: &PairList<T>,
64     shape: Shape,
65     context: &RewriteContext,
66 ) -> Option<String> {
67     assert!(list.list.len() >= 2, "Not a pair?");
68
69     let mut result = String::new();
70     let base_shape = shape.block();
71
72     for (e, s) in list.list.iter().zip(list.separators.iter()) {
73         let cur_shape = base_shape.offset_left(last_line_width(&result))?;
74         let rewrite = e.rewrite(context, cur_shape)?;
75
76         if !is_single_line(&rewrite) || result.len() > shape.width {
77             return None;
78         }
79
80         result.push_str(&rewrite);
81         result.push(' ');
82         result.push_str(s);
83         result.push(' ');
84     }
85
86     let last = list.list.last().unwrap();
87     let cur_shape = base_shape.offset_left(last_line_width(&result))?;
88     let rewrite = last.rewrite(context, cur_shape)?;
89     result.push_str(&rewrite);
90
91     if first_line_width(&result) > shape.width {
92         return None;
93     }
94
95     // Check the last expression in the list. We let this expression go over
96     // multiple lines, but we check that if this is necessary, then we can't
97     // do better using multi-line formatting.
98     if !is_single_line(&result) {
99         let multiline_shape = shape.offset_left(list.separators.last().unwrap().len() + 1)?;
100         let multiline_list: PairList<T> = PairList {
101             list: vec![last],
102             separators: vec![],
103             separator_place: list.separator_place,
104         };
105         // Format as if we were multi-line.
106         if let Some(rewrite) = rewrite_pairs_multiline(multiline_list, multiline_shape, context) {
107             // Also, don't let expressions surrounded by parens go multi-line,
108             // this looks really bad.
109             if rewrite.starts_with('(') || is_single_line(&rewrite) {
110                 return None;
111             }
112         }
113     }
114
115     wrap_str(result, context.config.max_width(), shape)
116 }
117
118 fn rewrite_pairs_multiline<T: Rewrite>(
119     list: PairList<T>,
120     shape: Shape,
121     context: &RewriteContext,
122 ) -> Option<String> {
123     let rhs_offset = shape.rhs_overhead(&context.config);
124     let nested_shape = (match context.config.indent_style() {
125         IndentStyle::Visual => shape.visual_indent(0),
126         IndentStyle::Block => shape.block_indent(context.config.tab_spaces()),
127     })
128     .with_max_width(&context.config)
129     .sub_width(rhs_offset)?;
130
131     let indent_str = nested_shape.indent.to_string_with_newline(context.config);
132     let mut result = String::new();
133
134     let rewrite = list.list[0].rewrite(context, shape)?;
135     result.push_str(&rewrite);
136
137     for (e, s) in list.list[1..].iter().zip(list.separators.iter()) {
138         // The following test checks if we should keep two subexprs on the same
139         // line. We do this if not doing so would create an orphan and there is
140         // enough space to do so.
141         let offset = if result.contains('\n') {
142             0
143         } else {
144             shape.used_width()
145         };
146         if last_line_width(&result) + offset <= nested_shape.used_width() {
147             // We must snuggle the next line onto the previous line to avoid an orphan.
148             if let Some(line_shape) =
149                 shape.offset_left(s.len() + 2 + trimmed_last_line_width(&result))
150             {
151                 if let Some(rewrite) = e.rewrite(context, line_shape) {
152                     result.push(' ');
153                     result.push_str(s);
154                     result.push(' ');
155                     result.push_str(&rewrite);
156                     continue;
157                 }
158             }
159         }
160
161         let nested_overhead = s.len() + 1;
162         let line_shape = match context.config.binop_separator() {
163             SeparatorPlace::Back => {
164                 result.push(' ');
165                 result.push_str(s);
166                 result.push_str(&indent_str);
167                 nested_shape.sub_width(nested_overhead)?
168             }
169             SeparatorPlace::Front => {
170                 result.push_str(&indent_str);
171                 result.push_str(s);
172                 result.push(' ');
173                 nested_shape.offset_left(nested_overhead)?
174             }
175         };
176
177         let rewrite = e.rewrite(context, line_shape)?;
178         result.push_str(&rewrite);
179     }
180     Some(result)
181 }
182
183 // Rewrites a single pair.
184 pub(crate) fn rewrite_pair<LHS, RHS>(
185     lhs: &LHS,
186     rhs: &RHS,
187     pp: PairParts,
188     context: &RewriteContext,
189     shape: Shape,
190     separator_place: SeparatorPlace,
191 ) -> Option<String>
192 where
193     LHS: Rewrite,
194     RHS: Rewrite,
195 {
196     let tab_spaces = context.config.tab_spaces();
197     let lhs_overhead = match separator_place {
198         SeparatorPlace::Back => shape.used_width() + pp.prefix.len() + pp.infix.trim_right().len(),
199         SeparatorPlace::Front => shape.used_width(),
200     };
201     let lhs_shape = Shape {
202         width: context.budget(lhs_overhead),
203         ..shape
204     };
205     let lhs_result = lhs
206         .rewrite(context, lhs_shape)
207         .map(|lhs_str| format!("{}{}", pp.prefix, lhs_str))?;
208
209     // Try to put both lhs and rhs on the same line.
210     let rhs_orig_result = shape
211         .offset_left(last_line_width(&lhs_result) + pp.infix.len())
212         .and_then(|s| s.sub_width(pp.suffix.len()))
213         .and_then(|rhs_shape| rhs.rewrite(context, rhs_shape));
214     if let Some(ref rhs_result) = rhs_orig_result {
215         // If the length of the lhs is equal to or shorter than the tab width or
216         // the rhs looks like block expression, we put the rhs on the same
217         // line with the lhs even if the rhs is multi-lined.
218         let allow_same_line = lhs_result.len() <= tab_spaces || rhs_result
219             .lines()
220             .next()
221             .map(|first_line| first_line.ends_with('{'))
222             .unwrap_or(false);
223         if !rhs_result.contains('\n') || allow_same_line {
224             let one_line_width = last_line_width(&lhs_result)
225                 + pp.infix.len()
226                 + first_line_width(rhs_result)
227                 + pp.suffix.len();
228             if one_line_width <= shape.width {
229                 return Some(format!(
230                     "{}{}{}{}",
231                     lhs_result, pp.infix, rhs_result, pp.suffix
232                 ));
233             }
234         }
235     }
236
237     // We have to use multiple lines.
238     // Re-evaluate the rhs because we have more space now:
239     let mut rhs_shape = match context.config.indent_style() {
240         IndentStyle::Visual => shape
241             .sub_width(pp.suffix.len() + pp.prefix.len())?
242             .visual_indent(pp.prefix.len()),
243         IndentStyle::Block => {
244             // Try to calculate the initial constraint on the right hand side.
245             let rhs_overhead = shape.rhs_overhead(context.config);
246             Shape::indented(shape.indent.block_indent(context.config), context.config)
247                 .sub_width(rhs_overhead)?
248         }
249     };
250     let infix = match separator_place {
251         SeparatorPlace::Back => pp.infix.trim_right(),
252         SeparatorPlace::Front => pp.infix.trim_left(),
253     };
254     if separator_place == SeparatorPlace::Front {
255         rhs_shape = rhs_shape.offset_left(infix.len())?;
256     }
257     let rhs_result = rhs.rewrite(context, rhs_shape)?;
258     let indent_str = rhs_shape.indent.to_string_with_newline(context.config);
259     let infix_with_sep = match separator_place {
260         SeparatorPlace::Back => format!("{}{}", infix, indent_str),
261         SeparatorPlace::Front => format!("{}{}", indent_str, infix),
262     };
263     Some(format!(
264         "{}{}{}{}",
265         lhs_result, infix_with_sep, rhs_result, pp.suffix
266     ))
267 }
268
269 // A pair which forms a tree and can be flattened (e.g., binops).
270 trait FlattenPair: Rewrite + Sized {
271     // If `_same_op` is `true`, then we only combine binops with the same
272     // operator into the list. E.g,, if the source is `a * b + c`, if `_same_op`
273     // is true, we make `[(a * b), c]` if `_same_op` is false, we make
274     // `[a, b, c]`
275     fn flatten(&self, _context: &RewriteContext, _same_op: bool) -> Option<PairList<Self>> {
276         None
277     }
278 }
279
280 struct PairList<'a, 'b, T: Rewrite + 'b> {
281     list: Vec<&'b T>,
282     separators: Vec<&'a str>,
283     separator_place: SeparatorPlace,
284 }
285
286 impl FlattenPair for ast::Expr {
287     fn flatten(&self, context: &RewriteContext, same_op: bool) -> Option<PairList<ast::Expr>> {
288         let top_op = match self.node {
289             ast::ExprKind::Binary(op, _, _) => op.node,
290             _ => return None,
291         };
292
293         // Turn a tree of binop expressions into a list using a depth-first,
294         // in-order traversal.
295         let mut stack = vec![];
296         let mut list = vec![];
297         let mut separators = vec![];
298         let mut node = self;
299         loop {
300             match node.node {
301                 ast::ExprKind::Binary(op, ref lhs, _) if !same_op || op.node == top_op => {
302                     stack.push(node);
303                     node = lhs;
304                 }
305                 _ => {
306                     list.push(node);
307                     if let Some(pop) = stack.pop() {
308                         match pop.node {
309                             ast::ExprKind::Binary(op, _, ref rhs) => {
310                                 separators.push(op.node.to_string());
311                                 node = rhs;
312                             }
313                             _ => unreachable!(),
314                         }
315                     } else {
316                         break;
317                     }
318                 }
319             }
320         }
321
322         assert_eq!(list.len() - 1, separators.len());
323         Some(PairList {
324             list,
325             separators,
326             separator_place: context.config.binop_separator(),
327         })
328     }
329 }
330
331 impl FlattenPair for ast::Ty {}
332 impl FlattenPair for ast::Pat {}