From a9bd695723d22222b994ed6f2867cf587e79923f Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Fri, 16 Oct 2015 22:54:32 +0200 Subject: [PATCH] Format vec! macro using brackets --- src/expr.rs | 4 ++-- src/lists.rs | 3 ++- src/macros.rs | 29 ++++++++++++++++++++++++++--- tests/target/expr.rs | 4 ++-- tests/target/match.rs | 22 +++++++++++----------- 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index db8e37c27e7..0b64ecf35ff 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -35,7 +35,7 @@ impl Rewrite for ast::Expr { match self.node { ast::Expr_::ExprVec(ref expr_vec) => { rewrite_array(expr_vec.iter().map(|e| &**e), - self.span, + mk_sp(span_after(self.span, "[", context.codemap), self.span.hi), context, width, offset) @@ -266,7 +266,7 @@ pub fn rewrite_array<'a, I>(expr_iter: I, |item| item.span.hi, // 1 = [ |item| item.rewrite(&inner_context, max_item_width, offset), - span_after(span, "[", context.codemap), + span.lo, span.hi) .collect::>(); diff --git a/src/lists.rs b/src/lists.rs index c9cc24f18e1..8182189178a 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -232,7 +232,8 @@ pub fn write_list<'b, I, T>(items: I, formatting: &ListFormatting<'b>) -> Option DefinitiveListTactic::Mixed => { let total_width = total_item_width(item) + item_sep_len; - if line_len > 0 && line_len + total_width > formatting.width { + // 1 is space between separator and item. + if line_len > 0 && line_len + 1 + total_width > formatting.width { result.push('\n'); result.push_str(indent_str); line_len = 0; diff --git a/src/macros.rs b/src/macros.rs index 58b2c5db25f..9035b0c7d8b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -24,12 +24,15 @@ use std::thread; use syntax::ast; use syntax::parse::token::{Eof, Comma, Token}; use syntax::parse::{ParseSess, tts_to_parser}; +use syntax::codemap::{mk_sp, BytePos}; use Indent; use rewrite::RewriteContext; use expr::{rewrite_call, rewrite_array}; use comment::FindUncommented; -use utils::wrap_str; +use utils::{wrap_str, span_after}; + +static FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"]; // We need to pass `TokenTree`s to our expression parsing thread, but they are // not `Send`. We wrap them in a `Send` container to force our will. @@ -38,19 +41,35 @@ struct ForceSend(pub T); unsafe impl Send for ForceSend {} // FIXME: use the enum from libsyntax? +#[derive(Clone, Copy)] enum MacroStyle { Parens, Brackets, Braces, } +impl MacroStyle { + fn opener(&self) -> &'static str { + match *self { + MacroStyle::Parens => "(", + MacroStyle::Brackets => "[", + MacroStyle::Braces => "{", + } + } +} + pub fn rewrite_macro(mac: &ast::Mac, context: &RewriteContext, width: usize, offset: Indent) -> Option { - let style = macro_style(mac, context); + let original_style = macro_style(mac, context); let macro_name = format!("{}!", mac.node.path); + let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) { + MacroStyle::Brackets + } else { + original_style + }; if let MacroStyle::Braces = style { return None; @@ -100,10 +119,14 @@ pub fn rewrite_macro(mac: &ast::Mac, // Format macro invocation as array literal. let extra_offset = macro_name.len(); let rewrite = try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x), - mac.span, + mk_sp(span_after(mac.span, + original_style.opener(), + context.codemap), + mac.span.hi - BytePos(1)), context, try_opt!(width.checked_sub(extra_offset)), offset + extra_offset)); + Some(format!("{}{}", macro_name, rewrite)) } MacroStyle::Braces => { diff --git a/tests/target/expr.rs b/tests/target/expr.rs index ed842289439..555d8f37e01 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -168,8 +168,8 @@ fn issue184(source: &str) { } fn arrays() { - let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 7, 8, 9, 0, 1, 2, 3, 4, - 5, 6, 7, 8, 9, 0]; + let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 7, 8, 9, 0, 1, 2, 3, + 4, 5, 6, 7, 8, 9, 0]; let y = [// comment 1, diff --git a/tests/target/match.rs b/tests/target/match.rs index e758cdd6f71..fe833dffb62 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -66,16 +66,16 @@ fn main() { fn main() { match r { Variableeeeeeeeeeeeeeeeee => ("variable", - vec!("id", "name", "qualname", "value", "type", "scopeid"), + vec!["id", "name", "qualname", "value", "type", "scopeid"], true, true), Enummmmmmmmmmmmmmmmmmmmm => ("enum", - vec!("id", "qualname", "scopeid", "value"), + vec!["id", "qualname", "scopeid", "value"], true, true), Variantttttttttttttttttttttttt => ("variant", - vec!("id", "name", "qualname", "type", "value", "scopeid"), + vec!["id", "name", "qualname", "type", "value", "scopeid"], true, true), } @@ -142,24 +142,24 @@ fn issue339() { fn issue355() { match mac { a => println!("a", b), - b => vec!(1, 2), + b => vec![1, 2], c => vec!(3; 4), d => { println!("a", b) } e => { - vec!(1, 2) + vec![1, 2] } f => { vec!(3; 4) } h => println!("a", b), // h comment - i => vec!(1, 2), // i comment + i => vec![1, 2], // i comment j => vec!(3; 4), // j comment // k comment k => println!("a", b), // l comment - l => vec!(1, 2), + l => vec![1, 2], // m comment m => vec!(3; 4), // Rewrite splits macro @@ -167,7 +167,7 @@ fn issue355() { println!("a", b), // Rewrite splits macro oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo => - vec!(1, 2), + vec![1, 2], // Macro support fails to recognise this macro as splitable // We push the whole expr to a new line, TODO split this macro as well pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp => @@ -176,19 +176,19 @@ fn issue355() { qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq => println!("a", b), rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr => - vec!(1, 2), + vec![1, 2], ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss => vec!(3; 4), // Funky bracketing styles t => println!{"a", b}, - u => vec!{1, 2}, + u => vec![1, 2], v => vec!{3; 4}, w => println!["a", b], x => vec![1, 2], y => vec![3; 4], // Brackets with comments tc => println!{"a", b}, // comment - uc => vec!{1, 2}, // comment + uc => vec![1, 2], // comment vc => vec!{3; 4}, // comment wc => println!["a", b], // comment xc => vec![1, 2], // comment