use config::lists::*;
use syntax::ast::{self, UseTreeKind};
-use syntax::codemap::{self, BytePos, Span, DUMMY_SP};
+use syntax::source_map::{self, BytePos, Span, DUMMY_SP};
-use codemap::SpanUtils;
-use config::IndentStyle;
+use comment::combine_strs_with_missing_comments;
+use config::{Edition, IndentStyle};
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
+use source_map::SpanUtils;
use spanned::Spanned;
-use utils::mk_sp;
+use utils::{is_same_visibility, mk_sp, rewrite_ident};
use visitor::FmtVisitor;
use std::borrow::Cow;
impl<'a> FmtVisitor<'a> {
pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
- let span = item.span;
+ let span = item.span();
let shape = self.shape();
let rw = UseTree::from_ast(
&self.get_context(),
}
impl Eq for UseTree {}
+impl Spanned for UseTree {
+ fn span(&self) -> Span {
+ let lo = if let Some(ref attrs) = self.attrs {
+ attrs.iter().next().map_or(self.span.lo(), |a| a.span.lo())
+ } else {
+ self.span.lo()
+ };
+ mk_sp(lo, self.span.hi())
+ }
+}
+
impl UseSegment {
// Clone a version of self with any top-level alias removed.
fn remove_alias(&self) -> UseSegment {
}
}
- fn from_path_segment(path_seg: &ast::PathSegment) -> Option<UseSegment> {
- let name = path_seg.ident.name.as_str();
- if name == "{{root}}" {
+ fn from_path_segment(
+ context: &RewriteContext,
+ path_seg: &ast::PathSegment,
+ modsep: bool,
+ ) -> Option<UseSegment> {
+ let name = rewrite_ident(context, path_seg.ident);
+ if name.is_empty() || name == "{{root}}" {
return None;
}
- Some(if name == "self" {
- UseSegment::Slf(None)
- } else if name == "super" {
- UseSegment::Super(None)
- } else {
- UseSegment::Ident((*name).to_owned(), None)
+ Some(match name {
+ "self" => UseSegment::Slf(None),
+ "super" => UseSegment::Super(None),
+ _ => {
+ let mod_sep = if modsep { "::" } else { "" };
+ UseSegment::Ident(format!("{}{}", mod_sep, name), None)
+ }
})
}
}
impl UseTree {
// Rewrite use tree with `use ` and a trailing `;`.
pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
- let mut result = String::with_capacity(256);
- if let Some(ref attrs) = self.attrs {
- result.push_str(&attrs.rewrite(context, shape)?);
- if !result.is_empty() {
- result.push_str(&shape.indent.to_string_with_newline(context.config));
- }
- }
-
- let vis = self.visibility
- .as_ref()
- .map_or(Cow::from(""), |vis| ::utils::format_visibility(&vis));
- result.push_str(&self.rewrite(context, shape.offset_left(vis.len())?)
+ let vis = self.visibility.as_ref().map_or(Cow::from(""), |vis| {
+ ::utils::format_visibility(context, &vis)
+ });
+ let use_str = self
+ .rewrite(context, shape.offset_left(vis.len())?)
.map(|s| {
if s.is_empty() {
s.to_owned()
} else {
format!("{}use {};", vis, s)
}
- })?);
- Some(result)
+ })?;
+ if let Some(ref attrs) = self.attrs {
+ let attr_str = attrs.rewrite(context, shape)?;
+ let lo = attrs.last().as_ref()?.span().hi();
+ let hi = self.span.lo();
+ let span = mk_sp(lo, hi);
+ combine_strs_with_missing_comments(context, &attr_str, &use_str, span, shape, false)
+ } else {
+ Some(use_str)
+ }
}
// FIXME: Use correct span?
use_tree,
None,
Some(item.vis.clone()),
- Some(item.span().lo()),
+ Some(item.span.lo()),
if item.attrs.is_empty() {
None
} else {
Some(item.attrs.clone())
},
- ).normalize(context.config.reorder_imported_names()),
+ ).normalize(),
),
_ => None,
}
visibility,
attrs,
};
+
+ let leading_modsep = context.config.edition() == Edition::Edition2018
+ && a.prefix.to_string().len() > 2
+ && a.prefix.to_string().starts_with("::");
+
for p in &a.prefix.segments {
- if let Some(use_segment) = UseSegment::from_path_segment(p) {
+ if let Some(use_segment) = UseSegment::from_path_segment(context, p, leading_modsep) {
result.path.push(use_segment);
}
}
.zip(items.into_iter())
.map(|(t, list_item)| {
Self::from_ast(context, &t.0, Some(list_item), None, None, None)
- })
- .collect(),
+ }).collect(),
));
}
- UseTreeKind::Simple(ref rename) => {
- let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned();
+ UseTreeKind::Simple(ref rename, ..) => {
+ let name = rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned();
let alias = rename.and_then(|ident| {
- if ident == path_to_imported_ident(&a.prefix) {
+ if ident.name == "_" {
+ // for impl-only-use
+ Some("_".to_owned())
+ } else if ident == path_to_imported_ident(&a.prefix) {
None
} else {
- Some(ident.to_string())
+ Some(rewrite_ident(context, ident).to_owned())
}
});
-
- let segment = if &name == "self" {
- UseSegment::Slf(alias)
- } else if &name == "super" {
- UseSegment::Super(alias)
- } else {
- UseSegment::Ident(name, alias)
+ let segment = match name.as_ref() {
+ "self" => UseSegment::Slf(alias),
+ "super" => UseSegment::Super(alias),
+ _ => UseSegment::Ident(name, alias),
};
// `name` is already in result.
}
// Do the adjustments that rustfmt does elsewhere to use paths.
- pub fn normalize(mut self, do_sort: bool) -> UseTree {
+ pub fn normalize(mut self) -> UseTree {
let mut last = self.path.pop().expect("Empty use tree?");
// Hack around borrow checker.
let mut normalize_sole_list = false;
// Normalise foo::self -> foo.
if let UseSegment::Slf(None) = last {
- if self.path.len() > 0 {
+ if !self.path.is_empty() {
return self;
}
}
for seg in &list[0].path {
self.path.push(seg.clone());
}
- return self.normalize(do_sort);
+ return self.normalize();
}
_ => unreachable!(),
}
// Recursively normalize elements of a list use (including sorting the list).
if let UseSegment::List(list) = last {
- let mut list = list.into_iter()
- .map(|ut| ut.normalize(do_sort))
+ let mut list = list
+ .into_iter()
+ .map(|ut| ut.normalize())
.collect::<Vec<_>>();
- if do_sort {
- list.sort();
- }
+ list.sort();
last = UseSegment::List(list);
}
fn same_visibility(&self, other: &UseTree) -> bool {
match (&self.visibility, &other.visibility) {
(
- Some(codemap::Spanned {
+ Some(source_map::Spanned {
node: ast::VisibilityKind::Inherited,
..
}),
)
| (
None,
- Some(codemap::Spanned {
+ Some(source_map::Spanned {
node: ast::VisibilityKind::Inherited,
..
}),
)
| (None, None) => true,
- (
- Some(codemap::Spanned { node: lnode, .. }),
- Some(codemap::Spanned { node: rnode, .. }),
- ) => lnode == rnode,
+ (Some(ref a), Some(ref b)) => is_same_visibility(a, b),
_ => false,
}
}
fn share_prefix(&self, other: &UseTree) -> bool {
- if self.path.is_empty() || other.path.is_empty() || self.attrs.is_some()
+ if self.path.is_empty()
+ || other.path.is_empty()
+ || self.attrs.is_some()
|| !self.same_visibility(other)
{
false
UseSegment::List(list) => {
let prefix = &self.path[..self.path.len() - 1];
let mut result = vec![];
- for nested_use_tree in list.into_iter() {
- for mut flattend in nested_use_tree.clone().flatten().iter_mut() {
+ for nested_use_tree in list {
+ for flattend in &mut nested_use_tree.clone().flatten() {
let mut new_path = prefix.to_vec();
new_path.append(&mut flattend.path);
result.push(UseTree {
fn merge(&mut self, other: UseTree) {
let mut new_path = vec![];
- for (mut a, b) in self.path
+ for (a, b) in self
+ .path
.clone()
.iter_mut()
.zip(other.path.clone().into_iter())
use self::UseSegment::*;
fn is_upper_snake_case(s: &str) -> bool {
- s.chars().all(|c| c.is_uppercase() || c == '_')
+ s.chars()
+ .all(|c| c.is_uppercase() || c == '_' || c.is_numeric())
}
match (self, other) {
list_items.push(ListItem::from_str(use_tree.rewrite(context, nested_shape)?));
}
}
- let (tactic, remaining_width) = if use_tree_list.iter().any(|use_segment| {
+ let has_nested_list = use_tree_list.iter().any(|use_segment| {
use_segment
.path
.last()
UseSegment::List(..) => true,
_ => false,
})
- }) {
- (DefinitiveListTactic::Vertical, 0)
+ });
+
+ let remaining_width = if has_nested_list {
+ 0
} else {
- let remaining_width = shape.width.checked_sub(2).unwrap_or(0);
- let tactic = definitive_tactic(
- &list_items,
- context.config.imports_layout(),
- Separator::Comma,
- remaining_width,
- );
- (tactic, remaining_width)
+ shape.width.saturating_sub(2)
};
+
+ let tactic = definitive_tactic(
+ &list_items,
+ context.config.imports_layout(),
+ Separator::Comma,
+ remaining_width,
+ );
+
let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
&& tactic != DefinitiveListTactic::Horizontal;
- let fmt = ListFormatting {
- tactic,
- separator: ",",
- trailing_separator: if ends_with_newline {
- context.config.trailing_comma()
- } else {
- SeparatorTactic::Never
- },
- separator_place: SeparatorPlace::Back,
- shape: nested_shape,
- ends_with_newline,
- preserve_newline: true,
- config: context.config,
+ let trailing_separator = if ends_with_newline {
+ context.config.trailing_comma()
+ } else {
+ SeparatorTactic::Never
};
+ let fmt = ListFormatting::new(nested_shape, context.config)
+ .tactic(tactic)
+ .trailing_separator(trailing_separator)
+ .ends_with_newline(ends_with_newline)
+ .preserve_newline(true)
+ .nested(has_nested_list);
let list_str = write_list(&list_items, &fmt)?;
impl Rewrite for UseSegment {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
- Some(match *self {
+ Some(match self {
UseSegment::Ident(ref ident, Some(ref rename)) => format!("{} as {}", ident, rename),
UseSegment::Ident(ref ident, None) => ident.clone(),
UseSegment::Slf(Some(ref rename)) => format!("self as {}", rename),
#[cfg(test)]
mod test {
use super::*;
- use syntax::codemap::DUMMY_SP;
+ use syntax::source_map::DUMMY_SP;
// Parse the path part of an import. This parser is not robust and is only
// suitable for use in a test harness.
fn bump(&mut self) {
self.input.next().unwrap();
}
+
fn eat(&mut self, c: char) {
assert!(self.input.next().unwrap() == c);
}
+
fn push_segment(
result: &mut Vec<UseSegment>,
buf: &mut String,
}
}
}
+
fn parse_in_list(&mut self) -> UseTree {
let mut result = vec![];
let mut buf = String::new();
#[test]
fn test_use_tree_normalize() {
+ assert_eq!(parse_use_tree("a::self").normalize(), parse_use_tree("a"));
assert_eq!(
- parse_use_tree("a::self").normalize(true),
- parse_use_tree("a")
- );
- assert_eq!(
- parse_use_tree("a::self as foo").normalize(true),
+ parse_use_tree("a::self as foo").normalize(),
parse_use_tree("a as foo")
);
+ assert_eq!(parse_use_tree("a::{self}").normalize(), parse_use_tree("a"));
+ assert_eq!(parse_use_tree("a::{b}").normalize(), parse_use_tree("a::b"));
assert_eq!(
- parse_use_tree("a::{self}").normalize(true),
- parse_use_tree("a")
- );
- assert_eq!(
- parse_use_tree("a::{b}").normalize(true),
- parse_use_tree("a::b")
- );
- assert_eq!(
- parse_use_tree("a::{b, c::self}").normalize(true),
+ parse_use_tree("a::{b, c::self}").normalize(),
parse_use_tree("a::{b, c}")
);
assert_eq!(
- parse_use_tree("a::{b as bar, c::self}").normalize(true),
+ parse_use_tree("a::{b as bar, c::self}").normalize(),
parse_use_tree("a::{b as bar, c}")
);
}
#[test]
fn test_use_tree_ord() {
- assert!(parse_use_tree("a").normalize(true) < parse_use_tree("aa").normalize(true));
- assert!(parse_use_tree("a").normalize(true) < parse_use_tree("a::a").normalize(true));
- assert!(parse_use_tree("a").normalize(true) < parse_use_tree("*").normalize(true));
- assert!(parse_use_tree("a").normalize(true) < parse_use_tree("{a, b}").normalize(true));
- assert!(parse_use_tree("*").normalize(true) < parse_use_tree("{a, b}").normalize(true));
+ assert!(parse_use_tree("a").normalize() < parse_use_tree("aa").normalize());
+ assert!(parse_use_tree("a").normalize() < parse_use_tree("a::a").normalize());
+ assert!(parse_use_tree("a").normalize() < parse_use_tree("*").normalize());
+ assert!(parse_use_tree("a").normalize() < parse_use_tree("{a, b}").normalize());
+ assert!(parse_use_tree("*").normalize() < parse_use_tree("{a, b}").normalize());
assert!(
- parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize(true)
- < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize(true)
- );
- assert!(
- parse_use_tree("serde::de::{Deserialize}").normalize(true)
- < parse_use_tree("serde_json").normalize(true)
+ parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize()
+ < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize()
);
assert!(
- parse_use_tree("a::b::c").normalize(true) < parse_use_tree("a::b::*").normalize(true)
+ parse_use_tree("serde::de::{Deserialize}").normalize()
+ < parse_use_tree("serde_json").normalize()
);
+ assert!(parse_use_tree("a::b::c").normalize() < parse_use_tree("a::b::*").normalize());
assert!(
- parse_use_tree("foo::{Bar, Baz}").normalize(true)
- < parse_use_tree("{Bar, Baz}").normalize(true)
+ parse_use_tree("foo::{Bar, Baz}").normalize()
+ < parse_use_tree("{Bar, Baz}").normalize()
);
assert!(
- parse_use_tree("foo::{self as bar}").normalize(true)
- < parse_use_tree("foo::{qux as bar}").normalize(true)
+ parse_use_tree("foo::{self as bar}").normalize()
+ < parse_use_tree("foo::{qux as bar}").normalize()
);
assert!(
- parse_use_tree("foo::{qux as bar}").normalize(true)
- < parse_use_tree("foo::{baz, qux as bar}").normalize(true)
+ parse_use_tree("foo::{qux as bar}").normalize()
+ < parse_use_tree("foo::{baz, qux as bar}").normalize()
);
assert!(
- parse_use_tree("foo::{self as bar, baz}").normalize(true)
- < parse_use_tree("foo::{baz, qux as bar}").normalize(true)
+ parse_use_tree("foo::{self as bar, baz}").normalize()
+ < parse_use_tree("foo::{baz, qux as bar}").normalize()
);
- assert!(parse_use_tree("foo").normalize(true) < parse_use_tree("Foo").normalize(true));
- assert!(parse_use_tree("foo").normalize(true) < parse_use_tree("foo::Bar").normalize(true));
+ assert!(parse_use_tree("foo").normalize() < parse_use_tree("Foo").normalize());
+ assert!(parse_use_tree("foo").normalize() < parse_use_tree("foo::Bar").normalize());
assert!(
- parse_use_tree("std::cmp::{d, c, b, a}").normalize(true)
- < parse_use_tree("std::cmp::{b, e, g, f}").normalize(true)
+ parse_use_tree("std::cmp::{d, c, b, a}").normalize()
+ < parse_use_tree("std::cmp::{b, e, g, f}").normalize()
);
}
}