mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
Merge pull request #367 from nrc/self-alias
Format imports with aliases.
This commit is contained in:
commit
fd057ab595
@ -48,14 +48,16 @@ impl Rewrite for ast::ViewPath {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem) -> String {
|
fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String {
|
||||||
if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
|
let path_item_str = if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
|
||||||
|
// A name.
|
||||||
if path_str.is_empty() {
|
if path_str.is_empty() {
|
||||||
name.to_string()
|
name.to_string()
|
||||||
} else {
|
} else {
|
||||||
format!("{}::{}", path_str, name)
|
format!("{}::{}", path_str, name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// `self`.
|
||||||
if !path_str.is_empty() {
|
if !path_str.is_empty() {
|
||||||
path_str
|
path_str
|
||||||
} else {
|
} else {
|
||||||
@ -63,6 +65,31 @@ fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem) -> String {
|
|||||||
// leave it alone.
|
// leave it alone.
|
||||||
"{self}".to_owned()
|
"{self}".to_owned()
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
append_alias(path_item_str, vpi)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rewrite_path_item(vpi: &&ast::PathListItem) -> String {
|
||||||
|
let path_item_str = match vpi.node {
|
||||||
|
ast::PathListItem_::PathListIdent{ name, .. } => {
|
||||||
|
name.to_string()
|
||||||
|
}
|
||||||
|
ast::PathListItem_::PathListMod{ .. } => {
|
||||||
|
"self".to_owned()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
append_alias(path_item_str, vpi)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn append_alias(path_item_str: String, vpi: &ast::PathListItem) -> String {
|
||||||
|
match vpi.node {
|
||||||
|
ast::PathListItem_::PathListIdent{ rename: Some(rename), .. } |
|
||||||
|
ast::PathListItem_::PathListMod{ rename: Some(rename), .. } => {
|
||||||
|
format!("{} as {}", path_item_str, rename)
|
||||||
|
}
|
||||||
|
_ => path_item_str,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +107,7 @@ pub fn rewrite_use_list(width: usize,
|
|||||||
|
|
||||||
match path_list.len() {
|
match path_list.len() {
|
||||||
0 => unreachable!(),
|
0 => unreachable!(),
|
||||||
1 => return Some(rewrite_single_use_list(path_str, path_list[0])),
|
1 => return Some(rewrite_single_use_list(path_str, &path_list[0])),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,16 +144,7 @@ pub fn rewrite_use_list(width: usize,
|
|||||||
"}",
|
"}",
|
||||||
|vpi| vpi.span.lo,
|
|vpi| vpi.span.lo,
|
||||||
|vpi| vpi.span.hi,
|
|vpi| vpi.span.hi,
|
||||||
|vpi| {
|
rewrite_path_item,
|
||||||
match vpi.node {
|
|
||||||
ast::PathListItem_::PathListIdent{ name, .. } => {
|
|
||||||
name.to_string()
|
|
||||||
}
|
|
||||||
ast::PathListItem_::PathListMod{ .. } => {
|
|
||||||
"self".to_owned()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
span_after(span, "{", context.codemap),
|
span_after(span, "{", context.codemap),
|
||||||
span.hi);
|
span.hi);
|
||||||
items.extend(iter);
|
items.extend(iter);
|
||||||
|
@ -44,3 +44,9 @@ use Baz::*;
|
|||||||
use foo::bar::baz as baz ;
|
use foo::bar::baz as baz ;
|
||||||
use bar::quux as kaas;
|
use bar::quux as kaas;
|
||||||
use foo;
|
use foo;
|
||||||
|
|
||||||
|
// With aliases.
|
||||||
|
use foo::{self as bar, baz};
|
||||||
|
use foo::{self as bar};
|
||||||
|
use foo::{qux as bar};
|
||||||
|
use foo::{baz, qux as bar};
|
||||||
|
@ -38,3 +38,9 @@ fn test() {
|
|||||||
use foo::bar::baz;
|
use foo::bar::baz;
|
||||||
use bar::quux as kaas;
|
use bar::quux as kaas;
|
||||||
use foo;
|
use foo;
|
||||||
|
|
||||||
|
// With aliases.
|
||||||
|
use foo::{self as bar, baz};
|
||||||
|
use foo as bar;
|
||||||
|
use foo::qux as bar;
|
||||||
|
use foo::{baz, qux as bar};
|
||||||
|
Loading…
Reference in New Issue
Block a user