mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-02 19:53:46 +00:00
If where is on the same line as the impl, { is put on the same line fixes #650
factored if clause into a separate function
This commit is contained in:
parent
b236819f72
commit
d7a3256d40
@ -270,6 +270,7 @@ create_config! {
|
|||||||
newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
|
newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
|
||||||
fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";
|
fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";
|
||||||
item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";
|
item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";
|
||||||
|
impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
|
||||||
fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
|
fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
|
||||||
fn_single_line: bool, false, "Put single-expression functions on a single line";
|
fn_single_line: bool, false, "Put single-expression functions on a single line";
|
||||||
fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,
|
fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,
|
||||||
|
35
src/items.rs
35
src/items.rs
@ -25,6 +25,8 @@ use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, Struct
|
|||||||
use syntax::{ast, abi};
|
use syntax::{ast, abi};
|
||||||
use syntax::codemap::{Span, BytePos, mk_sp};
|
use syntax::codemap::{Span, BytePos, mk_sp};
|
||||||
use syntax::parse::token;
|
use syntax::parse::token;
|
||||||
|
use syntax::ast::ImplItem;
|
||||||
|
use syntax::ptr::P;
|
||||||
|
|
||||||
// Statements of the form
|
// Statements of the form
|
||||||
// let pat: ty = init;
|
// let pat: ty = init;
|
||||||
@ -485,8 +487,18 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
|
|||||||
context.config.where_density,
|
context.config.where_density,
|
||||||
"{",
|
"{",
|
||||||
None));
|
None));
|
||||||
if !where_clause_str.contains('\n') &&
|
|
||||||
result.len() + where_clause_str.len() + offset.width() > context.config.max_width {
|
if try_opt!(is_impl_single_line(context, &items, &result, &where_clause_str, &item)) {
|
||||||
|
result.push_str(&where_clause_str);
|
||||||
|
if where_clause_str.contains('\n') {
|
||||||
|
result.push_str("\n{\n}");
|
||||||
|
} else {
|
||||||
|
result.push_str(" {}");
|
||||||
|
}
|
||||||
|
return Some(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !where_clause_str.is_empty() && !where_clause_str.contains('\n') {
|
||||||
result.push('\n');
|
result.push('\n');
|
||||||
let width = context.block_indent.width() + context.config.tab_spaces - 1;
|
let width = context.block_indent.width() + context.config.tab_spaces - 1;
|
||||||
let where_indent = Indent::new(0, width);
|
let where_indent = Indent::new(0, width);
|
||||||
@ -505,6 +517,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push('{');
|
result.push('{');
|
||||||
|
|
||||||
let snippet = context.snippet(item.span);
|
let snippet = context.snippet(item.span);
|
||||||
@ -531,13 +544,31 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
|
|||||||
result.push_str(&outer_indent_str);
|
result.push_str(&outer_indent_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if result.chars().last().unwrap() == '{' {
|
||||||
|
result.push('\n');
|
||||||
|
}
|
||||||
result.push('}');
|
result.push('}');
|
||||||
|
|
||||||
Some(result)
|
Some(result)
|
||||||
} else {
|
} else {
|
||||||
unreachable!();
|
unreachable!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_impl_single_line(context: &RewriteContext,
|
||||||
|
items: &Vec<P<ImplItem>>,
|
||||||
|
result: &str,
|
||||||
|
where_clause_str: &str,
|
||||||
|
item: &ast::Item)
|
||||||
|
-> Option<bool> {
|
||||||
|
let snippet = context.snippet(item.span);
|
||||||
|
let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
|
||||||
|
|
||||||
|
Some(context.config.impl_empty_single_line && items.is_empty() &&
|
||||||
|
result.len() + where_clause_str.len() <= context.config.max_width &&
|
||||||
|
!contains_comment(&snippet[open_pos..]))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn format_struct(context: &RewriteContext,
|
pub fn format_struct(context: &RewriteContext,
|
||||||
item_name: &str,
|
item_name: &str,
|
||||||
ident: ast::Ident,
|
ident: ast::Ident,
|
||||||
|
@ -22,6 +22,12 @@ impl<'a, 'b, X, Y: Foo<Bar>> Foo<'a, X> for Bar<'b, Y> where X: Fooooooooooooooo
|
|||||||
fn foo() { "hi" }
|
fn foo() { "hi" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Foo for Bar<T> where T: Baz
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Foo for Bar<T> where T: Baz { /* Comment */ }
|
||||||
|
|
||||||
impl Foo {
|
impl Foo {
|
||||||
fn foo() {}
|
fn foo() {}
|
||||||
}
|
}
|
||||||
@ -64,3 +70,10 @@ impl X { fn do_parse( mut self : X ) {} }
|
|||||||
impl Y5000 {
|
impl Y5000 {
|
||||||
fn bar(self: X< 'a , 'b >, y: Y) {}
|
fn bar(self: X< 'a , 'b >, y: Y) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub impl<T> Foo for Bar<T> where T: Foo
|
||||||
|
{
|
||||||
|
fn foo() { "hi" }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub impl<T, Z> Foo for Bar<T, Z> where T: Foo, Z: Baz {}
|
||||||
|
@ -16,7 +16,8 @@ pub impl Foo for Bar {
|
|||||||
// Comment 3
|
// Comment 3
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe impl<'a, 'b, X, Y: Foo<Bar>> !Foo<'a, X> for Bar<'b, Y> where X: Foo<'a, Z>
|
pub unsafe impl<'a, 'b, X, Y: Foo<Bar>> !Foo<'a, X> for Bar<'b, Y>
|
||||||
|
where X: Foo<'a, Z>
|
||||||
{
|
{
|
||||||
fn foo() {
|
fn foo() {
|
||||||
"hi"
|
"hi"
|
||||||
@ -31,13 +32,22 @@ impl<'a, 'b, X, Y: Foo<Bar>> Foo<'a, X> for Bar<'b, Y>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, X, Y: Foo<Bar>> Foo<'a, X> for Bar<'b, Y> where X: Foooooooooooooooooooooooooooo<'a, Z>
|
impl<'a, 'b, X, Y: Foo<Bar>> Foo<'a, X> for Bar<'b, Y>
|
||||||
|
where X: Foooooooooooooooooooooooooooo<'a, Z>
|
||||||
{
|
{
|
||||||
fn foo() {
|
fn foo() {
|
||||||
"hi"
|
"hi"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Foo for Bar<T> where T: Baz {}
|
||||||
|
|
||||||
|
impl<T> Foo for Bar<T>
|
||||||
|
where T: Baz
|
||||||
|
{
|
||||||
|
// Comment
|
||||||
|
}
|
||||||
|
|
||||||
impl Foo {
|
impl Foo {
|
||||||
fn foo() {}
|
fn foo() {}
|
||||||
}
|
}
|
||||||
@ -80,3 +90,17 @@ impl X {
|
|||||||
impl Y5000 {
|
impl Y5000 {
|
||||||
fn bar(self: X<'a, 'b>, y: Y) {}
|
fn bar(self: X<'a, 'b>, y: Y) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub impl<T> Foo for Bar<T>
|
||||||
|
where T: Foo
|
||||||
|
{
|
||||||
|
fn foo() {
|
||||||
|
"hi"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub impl<T, Z> Foo for Bar<T, Z>
|
||||||
|
where T: Foo,
|
||||||
|
Z: Baz
|
||||||
|
{
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user