mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-17 01:13:11 +00:00
Update to latest Syntex
As a side-effect of the Path changes, we are now a bit more aggressive about normalising paths.
This commit is contained in:
parent
49e86a1e65
commit
6572874965
46
Cargo.lock
generated
46
Cargo.lock
generated
@ -7,14 +7,14 @@ dependencies = [
|
||||
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"itertools 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"multimap 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_errors 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_syntax 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_errors 0.56.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_syntax 0.56.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-segmentation 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -78,7 +78,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.19"
|
||||
version = "0.2.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
@ -91,7 +91,7 @@ name = "memchr"
|
||||
version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -127,7 +127,7 @@ version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
@ -141,20 +141,20 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "syntex_errors"
|
||||
version = "0.52.0"
|
||||
version = "0.56.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_pos 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_pos 0.56.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syntex_pos"
|
||||
version = "0.52.0"
|
||||
version = "0.56.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -162,17 +162,17 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "syntex_syntax"
|
||||
version = "0.52.0"
|
||||
version = "0.56.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_errors 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_pos 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_errors 0.56.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_pos 0.56.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -190,7 +190,7 @@ version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -216,7 +216,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.0.3"
|
||||
version = "0.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
@ -253,7 +253,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"
|
||||
"checksum itertools 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c6946da472dbbcbd98c049050e8e587cc4ee26985992e582b1d74a35cb8a7020"
|
||||
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
||||
"checksum libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9e030dc72013ed68994d1b2cbf36a94dd0e58418ba949c4b0db7eeb70a7a6352"
|
||||
"checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5"
|
||||
"checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054"
|
||||
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
|
||||
"checksum multimap 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9223f4774d08e06185e44e555b9a7561243d387bac49c78a6205c42d6975fbf2"
|
||||
@ -262,15 +262,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b"
|
||||
"checksum same-file 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c722bde68d432ad7982a6431b13264cc558af1707c0f321820e238c5671856ea"
|
||||
"checksum strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "54f86446ab480b4f60782188f4f78886465c5793aee248cbb48b7fdc0d022420"
|
||||
"checksum syntex_errors 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9e52bffe6202cfb67587784cf23e0ec5bf26d331eef4922a16d5c42e12aa1e9b"
|
||||
"checksum syntex_pos 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)" = "955ef4b16af4c468e4680d1497f873ff288f557d338180649e18f915af5e15ac"
|
||||
"checksum syntex_syntax 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)" = "76a302e717e348aa372ff577791c3832395650073b8d8432f8b3cb170b34afde"
|
||||
"checksum syntex_errors 0.56.0 (registry+https://github.com/rust-lang/crates.io-index)" = "13596c6a30ecd9d73d5f03167b7cc98f1e3e65063b046ac10b411dc9b2a8c600"
|
||||
"checksum syntex_pos 0.56.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d55810cf983ad75bcc52ec97a777b619b00d821219df7883d40f2aed7416966a"
|
||||
"checksum syntex_syntax 0.56.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ca00794c3556cadbcec14036c8bce354d92de33bd5ffbbc5bc090a4c595b27dc"
|
||||
"checksum term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3deff8a2b3b6607d6d7cc32ac25c0b33709453ca9cceac006caac51e963cf94a"
|
||||
"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
|
||||
"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
|
||||
"checksum toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "736b60249cb25337bc196faa43ee12c705e426f3d55c214d73a4e7be06f92cb4"
|
||||
"checksum unicode-segmentation 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7baebdc1df1363fa66161fca2fe047e4f4209011cc7e045948298996afdf85df"
|
||||
"checksum unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "36dff09cafb4ec7c8cf0023eb0b686cb6ce65499116a12201c9e11840ca01beb"
|
||||
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
|
||||
"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
|
||||
"checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"
|
||||
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
|
||||
|
@ -22,8 +22,8 @@ regex = "0.1"
|
||||
term = "0.4"
|
||||
strings = "0.0.1"
|
||||
diff = "0.1"
|
||||
syntex_syntax = "0.52"
|
||||
syntex_errors = "0.52"
|
||||
syntex_syntax = "0.56"
|
||||
syntex_errors = "0.56"
|
||||
log = "0.3"
|
||||
env_logger = "0.3"
|
||||
getopts = "0.2"
|
||||
|
@ -384,7 +384,6 @@ create_config! {
|
||||
reorder_imports: bool, false, "Reorder import statements alphabetically";
|
||||
reorder_imported_names: bool, false,
|
||||
"Reorder lists of names in import statements alphabetically";
|
||||
normalize_imports: bool, true, "Allows removing braces from imports and reducing paths";
|
||||
single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \
|
||||
expressions. A value of zero means always break \
|
||||
if-else expressions.";
|
||||
|
14
src/expr.rs
14
src/expr.rs
@ -26,7 +26,7 @@ use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_w
|
||||
use visitor::FmtVisitor;
|
||||
use config::{Config, StructLitStyle, MultilineStyle, ControlBraceStyle};
|
||||
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
|
||||
use types::rewrite_path;
|
||||
use types::{rewrite_path, PathContext};
|
||||
use items::{span_lo_for_arg, span_hi_for_arg};
|
||||
use chains::rewrite_chain;
|
||||
use macros::{rewrite_macro, MacroPosition};
|
||||
@ -54,7 +54,7 @@ fn format_expr(expr: &ast::Expr,
|
||||
offset: Indent)
|
||||
-> Option<String> {
|
||||
let result = match expr.node {
|
||||
ast::ExprKind::Vec(ref expr_vec) => {
|
||||
ast::ExprKind::Array(ref expr_vec) => {
|
||||
rewrite_array(expr_vec.iter().map(|e| &**e),
|
||||
mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi),
|
||||
context,
|
||||
@ -148,7 +148,12 @@ fn format_expr(expr: &ast::Expr,
|
||||
rewrite_match(context, cond, arms, width, offset, expr.span)
|
||||
}
|
||||
ast::ExprKind::Path(ref qself, ref path) => {
|
||||
rewrite_path(context, true, qself.as_ref(), path, width, offset)
|
||||
rewrite_path(context,
|
||||
PathContext::Expr,
|
||||
qself.as_ref(),
|
||||
path,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::Assign(ref lhs, ref rhs) => {
|
||||
rewrite_assignment(context, lhs, rhs, None, width, offset)
|
||||
@ -1727,7 +1732,8 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
|
||||
|
||||
// 2 = " {".len()
|
||||
let path_budget = try_opt!(width.checked_sub(2));
|
||||
let path_str = try_opt!(rewrite_path(context, true, None, path, path_budget, offset));
|
||||
let path_str =
|
||||
try_opt!(rewrite_path(context, PathContext::Expr, None, path, path_budget, offset));
|
||||
|
||||
// Foo { a: Foo } - indent is +3, width is -5.
|
||||
let h_budget = width.checked_sub(path_str.len() + 5).unwrap_or(0);
|
||||
|
@ -13,7 +13,7 @@ use utils;
|
||||
use syntax::codemap::{self, BytePos, Span};
|
||||
use codemap::SpanUtils;
|
||||
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic};
|
||||
use types::rewrite_path;
|
||||
use types::{rewrite_path, PathContext};
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use visitor::FmtVisitor;
|
||||
use std::cmp::{self, Ordering};
|
||||
@ -139,17 +139,20 @@ impl Rewrite for ast::ViewPath {
|
||||
// 4 = " as ".len()
|
||||
let budget = try_opt!(width.checked_sub(ident_str.len() + 4));
|
||||
|
||||
let path_str = if context.config.normalize_imports &&
|
||||
path.segments.last().unwrap().identifier.to_string() == "self" &&
|
||||
let path_str = if path.segments.last().unwrap().identifier.to_string() == "self" &&
|
||||
path.segments.len() > 1 {
|
||||
let path = &ast::Path {
|
||||
span: path.span.clone(),
|
||||
segments: path.segments[..path.segments.len() - 1].to_owned(),
|
||||
global: path.global,
|
||||
};
|
||||
try_opt!(rewrite_path(context, false, None, &path, budget, offset))
|
||||
try_opt!(rewrite_path(context,
|
||||
PathContext::Import,
|
||||
None,
|
||||
&path,
|
||||
budget,
|
||||
offset))
|
||||
} else {
|
||||
try_opt!(rewrite_path(context, false, None, path, budget, offset))
|
||||
try_opt!(rewrite_path(context, PathContext::Import, None, path, budget, offset))
|
||||
};
|
||||
|
||||
Some(if path.segments.last().unwrap().identifier == ident {
|
||||
@ -248,17 +251,22 @@ impl<'a> FmtVisitor<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn rewrite_single_use_list(path_str: Option<String>,
|
||||
vpi: &ast::PathListItem,
|
||||
context: &RewriteContext)
|
||||
-> String {
|
||||
let path_item_str = match path_str {
|
||||
Some(ref path_str) if vpi.node.name.to_string() == "self" &&
|
||||
context.config.normalize_imports => path_str.to_owned(),
|
||||
Some(path_str) => format!("{}::{}", path_str, vpi.node.name),
|
||||
None => vpi.node.name.to_string(),
|
||||
fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String {
|
||||
let mut item_str = vpi.node.name.to_string();
|
||||
if item_str == "self" {
|
||||
item_str = "".to_owned();
|
||||
}
|
||||
let path_item_str = if path_str.is_empty() {
|
||||
if item_str.is_empty() {
|
||||
"self".to_owned()
|
||||
} else {
|
||||
item_str
|
||||
}
|
||||
} else if item_str.is_empty() {
|
||||
path_str
|
||||
} else {
|
||||
format!("{}::{}", path_str, item_str)
|
||||
};
|
||||
|
||||
append_alias(path_item_str, vpi)
|
||||
}
|
||||
|
||||
@ -283,27 +291,16 @@ pub fn rewrite_use_list(width: usize,
|
||||
context: &RewriteContext)
|
||||
-> Option<String> {
|
||||
// Returns a different option to distinguish `::foo` and `foo`
|
||||
let opt_path_str = if !path.to_string().is_empty() {
|
||||
Some(path.to_string())
|
||||
} else if path.global {
|
||||
// path is absolute, we return an empty String to avoid a double `::`
|
||||
Some(String::new())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let path_str = try_opt!(rewrite_path(context, PathContext::Import, None, path, width, offset));
|
||||
|
||||
match path_list.len() {
|
||||
0 => unreachable!(),
|
||||
1 => return Some(rewrite_single_use_list(opt_path_str, &path_list[0], context)),
|
||||
1 => return Some(rewrite_single_use_list(path_str, &path_list[0])),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// 2 = ::
|
||||
let path_separation_w = if opt_path_str.is_some() { 2 } else { 0 };
|
||||
// 1 = {
|
||||
let supp_indent = path.to_string().len() + path_separation_w + 1;
|
||||
// 1 = }
|
||||
let remaining_width = width.checked_sub(supp_indent + 1).unwrap_or(0);
|
||||
// 2 = {}
|
||||
let remaining_width = width.checked_sub(path_str.len() + 2).unwrap_or(0);
|
||||
|
||||
let mut items = {
|
||||
// Dummy value, see explanation below.
|
||||
@ -330,6 +327,8 @@ pub fn rewrite_use_list(width: usize,
|
||||
items[1..].sort_by(|a, b| a.item.cmp(&b.item));
|
||||
}
|
||||
|
||||
let colons_offset = if path_str.is_empty() { 0 } else { 2 };
|
||||
|
||||
let tactic = definitive_tactic(&items[first_index..],
|
||||
::lists::ListTactic::Mixed,
|
||||
remaining_width);
|
||||
@ -337,7 +336,7 @@ pub fn rewrite_use_list(width: usize,
|
||||
tactic: tactic,
|
||||
separator: ",",
|
||||
trailing_separator: SeparatorTactic::Never,
|
||||
indent: offset + supp_indent,
|
||||
indent: offset + path_str.len() + 1 + colons_offset,
|
||||
// FIXME This is too conservative, and will not use all width
|
||||
// available
|
||||
// (loose 1 column (";"))
|
||||
@ -347,9 +346,10 @@ pub fn rewrite_use_list(width: usize,
|
||||
};
|
||||
let list_str = try_opt!(write_list(&items[first_index..], &fmt));
|
||||
|
||||
Some(match opt_path_str {
|
||||
Some(opt_path_str) => format!("{}::{{{}}}", opt_path_str, list_str),
|
||||
None => format!("{{{}}}", list_str),
|
||||
Some(if path_str.is_empty() {
|
||||
format!("{{{}}}", list_str)
|
||||
} else {
|
||||
format!("{}::{{{}}}", path_str, list_str)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ use rewrite::{Rewrite, RewriteContext};
|
||||
use utils::{wrap_str, format_mutability};
|
||||
use lists::{format_item_list, itemize_list, ListItem};
|
||||
use expr::{rewrite_unary_prefix, rewrite_pair};
|
||||
use types::rewrite_path;
|
||||
use types::{rewrite_path, PathContext};
|
||||
use super::Spanned;
|
||||
use comment::FindUncommented;
|
||||
|
||||
@ -65,10 +65,16 @@ impl Rewrite for Pat {
|
||||
rewrite_tuple_pat(items, dotdot_pos, None, self.span, context, width, offset)
|
||||
}
|
||||
PatKind::Path(ref q_self, ref path) => {
|
||||
rewrite_path(context, true, q_self.as_ref(), path, width, offset)
|
||||
rewrite_path(context,
|
||||
PathContext::Expr,
|
||||
q_self.as_ref(),
|
||||
path,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
PatKind::TupleStruct(ref path, ref pat_vec, dotdot_pos) => {
|
||||
let path_str = try_opt!(rewrite_path(context, true, None, path, width, offset));
|
||||
let path_str =
|
||||
try_opt!(rewrite_path(context, PathContext::Expr, None, path, width, offset));
|
||||
rewrite_tuple_pat(pat_vec,
|
||||
dotdot_pos,
|
||||
Some(path_str),
|
||||
@ -102,7 +108,8 @@ impl Rewrite for Pat {
|
||||
wrap_str(result, context.config.max_width, width, offset)
|
||||
}
|
||||
PatKind::Struct(ref path, ref fields, elipses) => {
|
||||
let path = try_opt!(rewrite_path(context, true, None, path, width, offset));
|
||||
let path =
|
||||
try_opt!(rewrite_path(context, PathContext::Expr, None, path, width, offset));
|
||||
|
||||
let (elipses_str, terminator) = if elipses { (", ..", "..") } else { ("", "}") };
|
||||
|
||||
|
186
src/types.rs
186
src/types.rs
@ -11,10 +11,11 @@
|
||||
use std::ops::Deref;
|
||||
use std::iter::ExactSizeIterator;
|
||||
|
||||
use syntax::ast::{self, Mutability, FunctionRetTy};
|
||||
use syntax::print::pprust;
|
||||
use syntax::codemap::{self, Span, BytePos};
|
||||
use syntax::abi;
|
||||
use syntax::ast::{self, Mutability, FunctionRetTy};
|
||||
use syntax::codemap::{self, Span, BytePos};
|
||||
use syntax::print::pprust;
|
||||
use syntax::symbol::keywords;
|
||||
|
||||
use {Indent, Spanned};
|
||||
use codemap::SpanUtils;
|
||||
@ -25,9 +26,16 @@ use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
|
||||
use config::TypeDensity;
|
||||
use itertools::Itertools;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub enum PathContext {
|
||||
Expr,
|
||||
Type,
|
||||
Import,
|
||||
}
|
||||
|
||||
// Does not wrap on simple segments.
|
||||
pub fn rewrite_path(context: &RewriteContext,
|
||||
expr_context: bool,
|
||||
path_context: PathContext,
|
||||
qself: Option<&ast::QSelf>,
|
||||
path: &ast::Path,
|
||||
width: usize,
|
||||
@ -35,7 +43,8 @@ pub fn rewrite_path(context: &RewriteContext,
|
||||
-> Option<String> {
|
||||
let skip_count = qself.map_or(0, |x| x.position);
|
||||
|
||||
let mut result = if path.global && qself.is_none() {
|
||||
let mut result = if path.is_global() && qself.is_none() &&
|
||||
path_context != PathContext::Import {
|
||||
"::".to_owned()
|
||||
} else {
|
||||
String::new()
|
||||
@ -54,7 +63,7 @@ pub fn rewrite_path(context: &RewriteContext,
|
||||
|
||||
if skip_count > 0 {
|
||||
result.push_str(" as ");
|
||||
if path.global {
|
||||
if path.is_global() && path_context != PathContext::Import {
|
||||
result.push_str("::");
|
||||
}
|
||||
|
||||
@ -62,7 +71,7 @@ pub fn rewrite_path(context: &RewriteContext,
|
||||
// 3 = ">::".len()
|
||||
let budget = try_opt!(width.checked_sub(extra_offset + 3));
|
||||
|
||||
result = try_opt!(rewrite_path_segments(false,
|
||||
result = try_opt!(rewrite_path_segments(PathContext::Type,
|
||||
result,
|
||||
path.segments.iter().take(skip_count),
|
||||
span_lo,
|
||||
@ -82,7 +91,7 @@ pub fn rewrite_path(context: &RewriteContext,
|
||||
|
||||
let extra_offset = extra_offset(&result, offset);
|
||||
let budget = try_opt!(width.checked_sub(extra_offset));
|
||||
rewrite_path_segments(expr_context,
|
||||
rewrite_path_segments(path_context,
|
||||
result,
|
||||
path.segments.iter().skip(skip_count),
|
||||
span_lo,
|
||||
@ -92,7 +101,7 @@ pub fn rewrite_path(context: &RewriteContext,
|
||||
offset + extra_offset)
|
||||
}
|
||||
|
||||
fn rewrite_path_segments<'a, I>(expr_context: bool,
|
||||
fn rewrite_path_segments<'a, I>(path_context: PathContext,
|
||||
mut buffer: String,
|
||||
iter: I,
|
||||
mut span_lo: BytePos,
|
||||
@ -106,6 +115,10 @@ fn rewrite_path_segments<'a, I>(expr_context: bool,
|
||||
let mut first = true;
|
||||
|
||||
for segment in iter {
|
||||
// Indicates a global path, shouldn't be rendered.
|
||||
if segment.identifier.name == keywords::CrateRoot.name() {
|
||||
continue;
|
||||
}
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
@ -115,7 +128,7 @@ fn rewrite_path_segments<'a, I>(expr_context: bool,
|
||||
let extra_offset = extra_offset(&buffer, offset);
|
||||
let remaining_width = try_opt!(width.checked_sub(extra_offset));
|
||||
let new_offset = offset + extra_offset;
|
||||
let segment_string = try_opt!(rewrite_segment(expr_context,
|
||||
let segment_string = try_opt!(rewrite_segment(path_context,
|
||||
segment,
|
||||
&mut span_lo,
|
||||
span_hi,
|
||||
@ -172,7 +185,7 @@ impl<'a> Rewrite for SegmentParam<'a> {
|
||||
//
|
||||
// When the segment contains a positive number of parameters, we update span_lo
|
||||
// so that invariants described above will hold for the next segment.
|
||||
fn rewrite_segment(expr_context: bool,
|
||||
fn rewrite_segment(path_context: PathContext,
|
||||
segment: &ast::PathSegment,
|
||||
span_lo: &mut BytePos,
|
||||
span_hi: BytePos,
|
||||
@ -184,62 +197,71 @@ fn rewrite_segment(expr_context: bool,
|
||||
let width = try_opt!(width.checked_sub(ident_len));
|
||||
let offset = offset + ident_len;
|
||||
|
||||
let params = match segment.parameters {
|
||||
ast::PathParameters::AngleBracketed(ref data) if !data.lifetimes.is_empty() ||
|
||||
!data.types.is_empty() ||
|
||||
!data.bindings.is_empty() => {
|
||||
let param_list = data.lifetimes
|
||||
.iter()
|
||||
.map(SegmentParam::LifeTime)
|
||||
.chain(data.types.iter().map(|x| SegmentParam::Type(&*x)))
|
||||
.chain(data.bindings.iter().map(|x| SegmentParam::Binding(&*x)))
|
||||
.collect::<Vec<_>>();
|
||||
let params = if let Some(ref params) = segment.parameters {
|
||||
match **params {
|
||||
ast::PathParameters::AngleBracketed(ref data) if !data.lifetimes.is_empty() ||
|
||||
!data.types.is_empty() ||
|
||||
!data.bindings.is_empty() => {
|
||||
let param_list = data.lifetimes
|
||||
.iter()
|
||||
.map(SegmentParam::LifeTime)
|
||||
.chain(data.types.iter().map(|x| SegmentParam::Type(&*x)))
|
||||
.chain(data.bindings.iter().map(|x| SegmentParam::Binding(&*x)))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
|
||||
let list_lo = context.codemap.span_after(codemap::mk_sp(*span_lo, span_hi), "<");
|
||||
let separator = if expr_context { "::" } else { "" };
|
||||
let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
|
||||
let list_lo = context.codemap.span_after(codemap::mk_sp(*span_lo, span_hi), "<");
|
||||
let separator = if path_context == PathContext::Expr {
|
||||
"::"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
// 1 for <
|
||||
let extra_offset = 1 + separator.len();
|
||||
// 1 for >
|
||||
let list_width = try_opt!(width.checked_sub(extra_offset + 1));
|
||||
// 1 for <
|
||||
let extra_offset = 1 + separator.len();
|
||||
// 1 for >
|
||||
let list_width = try_opt!(width.checked_sub(extra_offset + 1));
|
||||
|
||||
let items = itemize_list(context.codemap,
|
||||
param_list.into_iter(),
|
||||
">",
|
||||
|param| param.get_span().lo,
|
||||
|param| param.get_span().hi,
|
||||
|seg| seg.rewrite(context, list_width, offset + extra_offset),
|
||||
list_lo,
|
||||
span_hi);
|
||||
let list_str = try_opt!(format_item_list(items,
|
||||
list_width,
|
||||
offset + extra_offset,
|
||||
context.config));
|
||||
let items =
|
||||
itemize_list(context.codemap,
|
||||
param_list.into_iter(),
|
||||
">",
|
||||
|param| param.get_span().lo,
|
||||
|param| param.get_span().hi,
|
||||
|seg| seg.rewrite(context, list_width, offset + extra_offset),
|
||||
list_lo,
|
||||
span_hi);
|
||||
let list_str = try_opt!(format_item_list(items,
|
||||
list_width,
|
||||
offset + extra_offset,
|
||||
context.config));
|
||||
|
||||
// Update position of last bracket.
|
||||
*span_lo = next_span_lo;
|
||||
// Update position of last bracket.
|
||||
*span_lo = next_span_lo;
|
||||
|
||||
if context.config.spaces_within_angle_brackets && list_str.len() > 0 {
|
||||
format!("{}< {} >", separator, list_str)
|
||||
} else {
|
||||
format!("{}<{}>", separator, list_str)
|
||||
if context.config.spaces_within_angle_brackets && list_str.len() > 0 {
|
||||
format!("{}< {} >", separator, list_str)
|
||||
} else {
|
||||
format!("{}<{}>", separator, list_str)
|
||||
}
|
||||
}
|
||||
ast::PathParameters::Parenthesized(ref data) => {
|
||||
let output = match data.output {
|
||||
Some(ref ty) => FunctionRetTy::Ty(ty.clone()),
|
||||
None => FunctionRetTy::Default(codemap::DUMMY_SP),
|
||||
};
|
||||
try_opt!(format_function_type(data.inputs.iter().map(|x| &**x),
|
||||
&output,
|
||||
false,
|
||||
data.span,
|
||||
context,
|
||||
width,
|
||||
offset))
|
||||
}
|
||||
_ => String::new(),
|
||||
}
|
||||
ast::PathParameters::Parenthesized(ref data) => {
|
||||
let output = match data.output {
|
||||
Some(ref ty) => FunctionRetTy::Ty(ty.clone()),
|
||||
None => FunctionRetTy::Default(codemap::DUMMY_SP),
|
||||
};
|
||||
try_opt!(format_function_type(data.inputs.iter().map(|x| &**x),
|
||||
&output,
|
||||
false,
|
||||
data.span,
|
||||
context,
|
||||
width,
|
||||
offset))
|
||||
}
|
||||
_ => String::new(),
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
Some(format!("{}{}", segment.identifier, params))
|
||||
@ -393,14 +415,15 @@ impl Rewrite for ast::WherePredicate {
|
||||
.. }) => {
|
||||
try_opt!(rewrite_bounded_lifetime(lifetime, bounds.iter(), context, width, offset))
|
||||
}
|
||||
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { ref path, ref ty, .. }) => {
|
||||
let ty_str = try_opt!(ty.rewrite(context, width, offset));
|
||||
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { ref lhs_ty,
|
||||
ref rhs_ty,
|
||||
.. }) => {
|
||||
let lhs_ty_str = try_opt!(lhs_ty.rewrite(context, width, offset));
|
||||
// 3 = " = ".len()
|
||||
let used_width = 3 + ty_str.len();
|
||||
let used_width = 3 + lhs_ty_str.len();
|
||||
let budget = try_opt!(width.checked_sub(used_width));
|
||||
let path_str =
|
||||
try_opt!(rewrite_path(context, false, None, path, budget, offset + used_width));
|
||||
format!("{} = {}", path_str, ty_str)
|
||||
let rhs_ty_str = try_opt!(rhs_ty.rewrite(context, budget, offset + used_width));
|
||||
format!("{} = {}", lhs_ty_str, rhs_ty_str)
|
||||
}
|
||||
};
|
||||
|
||||
@ -462,10 +485,14 @@ impl Rewrite for ast::Lifetime {
|
||||
|
||||
impl Rewrite for ast::TyParamBounds {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
let joiner = match context.config.type_punctuation_density {
|
||||
TypeDensity::Compressed => "+",
|
||||
TypeDensity::Wide => " + ",
|
||||
};
|
||||
let strs: Vec<_> = try_opt!(self.iter()
|
||||
.map(|b| b.rewrite(context, width, offset))
|
||||
.collect());
|
||||
wrap_str(strs.join(" + "), context.config.max_width, width, offset)
|
||||
wrap_str(strs.join(joiner), context.config.max_width, width, offset)
|
||||
}
|
||||
}
|
||||
|
||||
@ -534,27 +561,14 @@ impl Rewrite for ast::PolyTraitRef {
|
||||
|
||||
impl Rewrite for ast::TraitRef {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
rewrite_path(context, false, None, &self.path, width, offset)
|
||||
rewrite_path(context, PathContext::Type, None, &self.path, width, offset)
|
||||
}
|
||||
}
|
||||
|
||||
impl Rewrite for ast::Ty {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
match self.node {
|
||||
ast::TyKind::ObjectSum(ref ty, ref bounds) => {
|
||||
let ty_str = try_opt!(ty.rewrite(context, width, offset));
|
||||
let overhead = ty_str.len() + 3;
|
||||
let plus_str = match context.config.type_punctuation_density {
|
||||
TypeDensity::Compressed => "+",
|
||||
TypeDensity::Wide => " + ",
|
||||
};
|
||||
Some(format!("{}{}{}",
|
||||
ty_str,
|
||||
plus_str,
|
||||
try_opt!(bounds.rewrite(context,
|
||||
try_opt!(width.checked_sub(overhead)),
|
||||
offset + overhead))))
|
||||
}
|
||||
ast::TyKind::TraitObject(ref bounds) => bounds.rewrite(context, width, offset),
|
||||
ast::TyKind::Ptr(ref mt) => {
|
||||
let prefix = match mt.mutbl {
|
||||
Mutability::Mutable => "*mut ",
|
||||
@ -618,9 +632,13 @@ impl Rewrite for ast::Ty {
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::TyKind::PolyTraitRef(ref trait_ref) => trait_ref.rewrite(context, width, offset),
|
||||
ast::TyKind::Path(ref q_self, ref path) => {
|
||||
rewrite_path(context, false, q_self.as_ref(), path, width, offset)
|
||||
rewrite_path(context,
|
||||
PathContext::Type,
|
||||
q_self.as_ref(),
|
||||
path,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::TyKind::Array(ref ty, ref repeats) => {
|
||||
let use_spaces = context.config.spaces_within_square_brackets;
|
||||
|
@ -39,11 +39,13 @@ pub fn format_visibility(vis: &Visibility) -> Cow<'static, str> {
|
||||
Visibility::Inherited => Cow::from(""),
|
||||
Visibility::Crate(_) => Cow::from("pub(crate) "),
|
||||
Visibility::Restricted { ref path, .. } => {
|
||||
let Path { global, ref segments, .. } = **path;
|
||||
let prefix = if global { "::" } else { "" };
|
||||
let Path { ref segments, .. } = **path;
|
||||
let mut segments_iter = segments.iter().map(|seg| seg.identifier.name.as_str());
|
||||
if path.is_global() {
|
||||
segments_iter.next().expect("Non-global path in pub(restricted)?");
|
||||
}
|
||||
|
||||
Cow::from(format!("pub({}{}) ", prefix, segments_iter.join("::")))
|
||||
Cow::from(format!("pub({}) ", segments_iter.join("::")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,8 +53,8 @@ use foo::qux as bar;
|
||||
use foo::{baz, qux as bar};
|
||||
|
||||
// With absolute paths
|
||||
use ::foo;
|
||||
use ::foo::Bar;
|
||||
use ::foo::{Bar, Baz};
|
||||
use ::Foo;
|
||||
use ::{Bar, Baz};
|
||||
use foo;
|
||||
use foo::Bar;
|
||||
use foo::{Bar, Baz};
|
||||
use Foo;
|
||||
use {Bar, Baz};
|
||||
|
@ -24,7 +24,7 @@ pub(crate) enum WriteState<D> {
|
||||
WriteData(Writer<D>),
|
||||
}
|
||||
|
||||
pub(::global::path::to::some_mod) enum WriteState<D> {
|
||||
pub(global::path::to::some_mod) enum WriteState<D> {
|
||||
WriteId {
|
||||
id: U64Writer,
|
||||
size: U64Writer,
|
||||
|
Loading…
Reference in New Issue
Block a user