From 21c085261b3fac745768b03b10f5acb6e25e2972 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 6 Jan 2017 17:02:56 +1300 Subject: [PATCH] Remove `self` from `use foo::bar::self;` Also adds the `normalize_imports` config option. Fixes #1252 --- src/config.rs | 1 + src/imports.rs | 29 +++++++++++++++++++++-------- tests/source/imports.rs | 4 ++++ tests/target/imports.rs | 4 ++++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7ee8e4a9097..641b8993fc5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -393,6 +393,7 @@ fn default() -> 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."; diff --git a/src/imports.rs b/src/imports.rs index d1c36299454..b2909cfecd2 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -133,15 +133,24 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt ast::ViewPath_::ViewPathList(ref path, ref path_list) => { rewrite_use_list(width, offset, path, path_list, self.span, context) } - ast::ViewPath_::ViewPathGlob(_) => { - // FIXME convert to list? - None - } + ast::ViewPath_::ViewPathGlob(_) => None, ast::ViewPath_::ViewPathSimple(ident, ref path) => { let ident_str = ident.to_string(); // 4 = " as ".len() let budget = try_opt!(width.checked_sub(ident_str.len() + 4)); - let path_str = try_opt!(rewrite_path(context, false, None, path, budget, offset)); + + let path_str = if context.config.normalize_imports && + 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)) + } else { + try_opt!(rewrite_path(context, false, None, path, budget, offset)) + }; Some(if path.segments.last().unwrap().identifier == ident { path_str @@ -239,9 +248,13 @@ pub fn format_import(&mut self, vis: &ast::Visibility, vp: &ast::ViewPath, span: } } -fn rewrite_single_use_list(path_str: Option, vpi: &ast::PathListItem) -> String { +fn rewrite_single_use_list(path_str: Option, + vpi: &ast::PathListItem, + context: &RewriteContext) + -> String { let path_item_str = match path_str { - Some(ref path_str) if vpi.node.name.to_string() == "self" => path_str.to_owned(), + 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(), }; @@ -281,7 +294,7 @@ pub fn rewrite_use_list(width: usize, match path_list.len() { 0 => unreachable!(), - 1 => return Some(rewrite_single_use_list(opt_path_str, &path_list[0])), + 1 => return Some(rewrite_single_use_list(opt_path_str, &path_list[0], context)), _ => (), } diff --git a/tests/source/imports.rs b/tests/source/imports.rs index 45ed1e03fe0..4296a152f4e 100644 --- a/tests/source/imports.rs +++ b/tests/source/imports.rs @@ -20,6 +20,10 @@ pub use syntax::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath}; use syntax::some::{}; +use self; +use std::io::{self}; +use std::io::self; + mod Foo { pub use syntax::ast::{ ItemForeignMod, diff --git a/tests/target/imports.rs b/tests/target/imports.rs index 4e2f6903644..0be8f2c35e0 100644 --- a/tests/target/imports.rs +++ b/tests/target/imports.rs @@ -21,6 +21,10 @@ use Foo::{Bar, Baz}; pub use syntax::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath}; +use self; +use std::io; +use std::io; + mod Foo { pub use syntax::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, ItemDefaultImpl}; -- 2.44.0