Merge pull request #475 from marcusklaas/vektorz

Format vec! macro using brackets
This commit is contained in:
Nick Cameron 2015-10-18 15:17:49 +13:00
commit 51afb3ed10
5 changed files with 43 additions and 19 deletions

View File

@ -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::<Vec<_>>();

View File

@ -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;

View File

@ -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<T>(pub T);
unsafe impl<T> Send for ForceSend<T> {}
// 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<String> {
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(&&macro_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 => {

View File

@ -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,

View File

@ -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