Skip to content

Commit 6572874

Browse files
committed
Update to latest Syntex
As a side-effect of the Path changes, we are now a bit more aggressive about normalising paths.
1 parent 49e86a1 commit 6572874

File tree

10 files changed

+198
-166
lines changed

10 files changed

+198
-166
lines changed

Cargo.lock

+23-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ regex = "0.1"
2222
term = "0.4"
2323
strings = "0.0.1"
2424
diff = "0.1"
25-
syntex_syntax = "0.52"
26-
syntex_errors = "0.52"
25+
syntex_syntax = "0.56"
26+
syntex_errors = "0.56"
2727
log = "0.3"
2828
env_logger = "0.3"
2929
getopts = "0.2"

src/config.rs

-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ create_config! {
384384
reorder_imports: bool, false, "Reorder import statements alphabetically";
385385
reorder_imported_names: bool, false,
386386
"Reorder lists of names in import statements alphabetically";
387-
normalize_imports: bool, true, "Allows removing braces from imports and reducing paths";
388387
single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \
389388
expressions. A value of zero means always break \
390389
if-else expressions.";

src/expr.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_w
2626
use visitor::FmtVisitor;
2727
use config::{Config, StructLitStyle, MultilineStyle, ControlBraceStyle};
2828
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
29-
use types::rewrite_path;
29+
use types::{rewrite_path, PathContext};
3030
use items::{span_lo_for_arg, span_hi_for_arg};
3131
use chains::rewrite_chain;
3232
use macros::{rewrite_macro, MacroPosition};
@@ -54,7 +54,7 @@ fn format_expr(expr: &ast::Expr,
5454
offset: Indent)
5555
-> Option<String> {
5656
let result = match expr.node {
57-
ast::ExprKind::Vec(ref expr_vec) => {
57+
ast::ExprKind::Array(ref expr_vec) => {
5858
rewrite_array(expr_vec.iter().map(|e| &**e),
5959
mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi),
6060
context,
@@ -148,7 +148,12 @@ fn format_expr(expr: &ast::Expr,
148148
rewrite_match(context, cond, arms, width, offset, expr.span)
149149
}
150150
ast::ExprKind::Path(ref qself, ref path) => {
151-
rewrite_path(context, true, qself.as_ref(), path, width, offset)
151+
rewrite_path(context,
152+
PathContext::Expr,
153+
qself.as_ref(),
154+
path,
155+
width,
156+
offset)
152157
}
153158
ast::ExprKind::Assign(ref lhs, ref rhs) => {
154159
rewrite_assignment(context, lhs, rhs, None, width, offset)
@@ -1727,7 +1732,8 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
17271732

17281733
// 2 = " {".len()
17291734
let path_budget = try_opt!(width.checked_sub(2));
1730-
let path_str = try_opt!(rewrite_path(context, true, None, path, path_budget, offset));
1735+
let path_str =
1736+
try_opt!(rewrite_path(context, PathContext::Expr, None, path, path_budget, offset));
17311737

17321738
// Foo { a: Foo } - indent is +3, width is -5.
17331739
let h_budget = width.checked_sub(path_str.len() + 5).unwrap_or(0);

src/imports.rs

+35-35
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use utils;
1313
use syntax::codemap::{self, BytePos, Span};
1414
use codemap::SpanUtils;
1515
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic};
16-
use types::rewrite_path;
16+
use types::{rewrite_path, PathContext};
1717
use rewrite::{Rewrite, RewriteContext};
1818
use visitor::FmtVisitor;
1919
use std::cmp::{self, Ordering};
@@ -139,17 +139,20 @@ impl Rewrite for ast::ViewPath {
139139
// 4 = " as ".len()
140140
let budget = try_opt!(width.checked_sub(ident_str.len() + 4));
141141

142-
let path_str = if context.config.normalize_imports &&
143-
path.segments.last().unwrap().identifier.to_string() == "self" &&
142+
let path_str = if path.segments.last().unwrap().identifier.to_string() == "self" &&
144143
path.segments.len() > 1 {
145144
let path = &ast::Path {
146145
span: path.span.clone(),
147146
segments: path.segments[..path.segments.len() - 1].to_owned(),
148-
global: path.global,
149147
};
150-
try_opt!(rewrite_path(context, false, None, &path, budget, offset))
148+
try_opt!(rewrite_path(context,
149+
PathContext::Import,
150+
None,
151+
&path,
152+
budget,
153+
offset))
151154
} else {
152-
try_opt!(rewrite_path(context, false, None, path, budget, offset))
155+
try_opt!(rewrite_path(context, PathContext::Import, None, path, budget, offset))
153156
};
154157

155158
Some(if path.segments.last().unwrap().identifier == ident {
@@ -248,17 +251,22 @@ impl<'a> FmtVisitor<'a> {
248251
}
249252
}
250253

251-
fn rewrite_single_use_list(path_str: Option<String>,
252-
vpi: &ast::PathListItem,
253-
context: &RewriteContext)
254-
-> String {
255-
let path_item_str = match path_str {
256-
Some(ref path_str) if vpi.node.name.to_string() == "self" &&
257-
context.config.normalize_imports => path_str.to_owned(),
258-
Some(path_str) => format!("{}::{}", path_str, vpi.node.name),
259-
None => vpi.node.name.to_string(),
254+
fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String {
255+
let mut item_str = vpi.node.name.to_string();
256+
if item_str == "self" {
257+
item_str = "".to_owned();
258+
}
259+
let path_item_str = if path_str.is_empty() {
260+
if item_str.is_empty() {
261+
"self".to_owned()
262+
} else {
263+
item_str
264+
}
265+
} else if item_str.is_empty() {
266+
path_str
267+
} else {
268+
format!("{}::{}", path_str, item_str)
260269
};
261-
262270
append_alias(path_item_str, vpi)
263271
}
264272

@@ -283,27 +291,16 @@ pub fn rewrite_use_list(width: usize,
283291
context: &RewriteContext)
284292
-> Option<String> {
285293
// Returns a different option to distinguish `::foo` and `foo`
286-
let opt_path_str = if !path.to_string().is_empty() {
287-
Some(path.to_string())
288-
} else if path.global {
289-
// path is absolute, we return an empty String to avoid a double `::`
290-
Some(String::new())
291-
} else {
292-
None
293-
};
294+
let path_str = try_opt!(rewrite_path(context, PathContext::Import, None, path, width, offset));
294295

295296
match path_list.len() {
296297
0 => unreachable!(),
297-
1 => return Some(rewrite_single_use_list(opt_path_str, &path_list[0], context)),
298+
1 => return Some(rewrite_single_use_list(path_str, &path_list[0])),
298299
_ => (),
299300
}
300301

301-
// 2 = ::
302-
let path_separation_w = if opt_path_str.is_some() { 2 } else { 0 };
303-
// 1 = {
304-
let supp_indent = path.to_string().len() + path_separation_w + 1;
305-
// 1 = }
306-
let remaining_width = width.checked_sub(supp_indent + 1).unwrap_or(0);
302+
// 2 = {}
303+
let remaining_width = width.checked_sub(path_str.len() + 2).unwrap_or(0);
307304

308305
let mut items = {
309306
// Dummy value, see explanation below.
@@ -330,14 +327,16 @@ pub fn rewrite_use_list(width: usize,
330327
items[1..].sort_by(|a, b| a.item.cmp(&b.item));
331328
}
332329

330+
let colons_offset = if path_str.is_empty() { 0 } else { 2 };
331+
333332
let tactic = definitive_tactic(&items[first_index..],
334333
::lists::ListTactic::Mixed,
335334
remaining_width);
336335
let fmt = ListFormatting {
337336
tactic: tactic,
338337
separator: ",",
339338
trailing_separator: SeparatorTactic::Never,
340-
indent: offset + supp_indent,
339+
indent: offset + path_str.len() + 1 + colons_offset,
341340
// FIXME This is too conservative, and will not use all width
342341
// available
343342
// (loose 1 column (";"))
@@ -347,9 +346,10 @@ pub fn rewrite_use_list(width: usize,
347346
};
348347
let list_str = try_opt!(write_list(&items[first_index..], &fmt));
349348

350-
Some(match opt_path_str {
351-
Some(opt_path_str) => format!("{}::{{{}}}", opt_path_str, list_str),
352-
None => format!("{{{}}}", list_str),
349+
Some(if path_str.is_empty() {
350+
format!("{{{}}}", list_str)
351+
} else {
352+
format!("{}::{{{}}}", path_str, list_str)
353353
})
354354
}
355355

src/patterns.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rewrite::{Rewrite, RewriteContext};
1414
use utils::{wrap_str, format_mutability};
1515
use lists::{format_item_list, itemize_list, ListItem};
1616
use expr::{rewrite_unary_prefix, rewrite_pair};
17-
use types::rewrite_path;
17+
use types::{rewrite_path, PathContext};
1818
use super::Spanned;
1919
use comment::FindUncommented;
2020

@@ -65,10 +65,16 @@ impl Rewrite for Pat {
6565
rewrite_tuple_pat(items, dotdot_pos, None, self.span, context, width, offset)
6666
}
6767
PatKind::Path(ref q_self, ref path) => {
68-
rewrite_path(context, true, q_self.as_ref(), path, width, offset)
68+
rewrite_path(context,
69+
PathContext::Expr,
70+
q_self.as_ref(),
71+
path,
72+
width,
73+
offset)
6974
}
7075
PatKind::TupleStruct(ref path, ref pat_vec, dotdot_pos) => {
71-
let path_str = try_opt!(rewrite_path(context, true, None, path, width, offset));
76+
let path_str =
77+
try_opt!(rewrite_path(context, PathContext::Expr, None, path, width, offset));
7278
rewrite_tuple_pat(pat_vec,
7379
dotdot_pos,
7480
Some(path_str),
@@ -102,7 +108,8 @@ impl Rewrite for Pat {
102108
wrap_str(result, context.config.max_width, width, offset)
103109
}
104110
PatKind::Struct(ref path, ref fields, elipses) => {
105-
let path = try_opt!(rewrite_path(context, true, None, path, width, offset));
111+
let path =
112+
try_opt!(rewrite_path(context, PathContext::Expr, None, path, width, offset));
106113

107114
let (elipses_str, terminator) = if elipses { (", ..", "..") } else { ("", "}") };
108115

0 commit comments

Comments
 (0)