Skip to content

Commit 51afb3e

Browse files
committed
Merge pull request #475 from marcusklaas/vektorz
Format vec! macro using brackets
2 parents 36c9a3a + a9bd695 commit 51afb3e

File tree

5 files changed

+43
-19
lines changed

5 files changed

+43
-19
lines changed

src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Rewrite for ast::Expr {
3535
match self.node {
3636
ast::Expr_::ExprVec(ref expr_vec) => {
3737
rewrite_array(expr_vec.iter().map(|e| &**e),
38-
self.span,
38+
mk_sp(span_after(self.span, "[", context.codemap), self.span.hi),
3939
context,
4040
width,
4141
offset)
@@ -266,7 +266,7 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
266266
|item| item.span.hi,
267267
// 1 = [
268268
|item| item.rewrite(&inner_context, max_item_width, offset),
269-
span_after(span, "[", context.codemap),
269+
span.lo,
270270
span.hi)
271271
.collect::<Vec<_>>();
272272

src/lists.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ pub fn write_list<'b, I, T>(items: I, formatting: &ListFormatting<'b>) -> Option
232232
DefinitiveListTactic::Mixed => {
233233
let total_width = total_item_width(item) + item_sep_len;
234234

235-
if line_len > 0 && line_len + total_width > formatting.width {
235+
// 1 is space between separator and item.
236+
if line_len > 0 && line_len + 1 + total_width > formatting.width {
236237
result.push('\n');
237238
result.push_str(indent_str);
238239
line_len = 0;

src/macros.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ use std::thread;
2424
use syntax::ast;
2525
use syntax::parse::token::{Eof, Comma, Token};
2626
use syntax::parse::{ParseSess, tts_to_parser};
27+
use syntax::codemap::{mk_sp, BytePos};
2728

2829
use Indent;
2930
use rewrite::RewriteContext;
3031
use expr::{rewrite_call, rewrite_array};
3132
use comment::FindUncommented;
32-
use utils::wrap_str;
33+
use utils::{wrap_str, span_after};
34+
35+
static FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"];
3336

3437
// We need to pass `TokenTree`s to our expression parsing thread, but they are
3538
// not `Send`. We wrap them in a `Send` container to force our will.
@@ -38,19 +41,35 @@ struct ForceSend<T>(pub T);
3841
unsafe impl<T> Send for ForceSend<T> {}
3942

4043
// FIXME: use the enum from libsyntax?
44+
#[derive(Clone, Copy)]
4145
enum MacroStyle {
4246
Parens,
4347
Brackets,
4448
Braces,
4549
}
4650

51+
impl MacroStyle {
52+
fn opener(&self) -> &'static str {
53+
match *self {
54+
MacroStyle::Parens => "(",
55+
MacroStyle::Brackets => "[",
56+
MacroStyle::Braces => "{",
57+
}
58+
}
59+
}
60+
4761
pub fn rewrite_macro(mac: &ast::Mac,
4862
context: &RewriteContext,
4963
width: usize,
5064
offset: Indent)
5165
-> Option<String> {
52-
let style = macro_style(mac, context);
66+
let original_style = macro_style(mac, context);
5367
let macro_name = format!("{}!", mac.node.path);
68+
let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {
69+
MacroStyle::Brackets
70+
} else {
71+
original_style
72+
};
5473

5574
if let MacroStyle::Braces = style {
5675
return None;
@@ -100,10 +119,14 @@ pub fn rewrite_macro(mac: &ast::Mac,
100119
// Format macro invocation as array literal.
101120
let extra_offset = macro_name.len();
102121
let rewrite = try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x),
103-
mac.span,
122+
mk_sp(span_after(mac.span,
123+
original_style.opener(),
124+
context.codemap),
125+
mac.span.hi - BytePos(1)),
104126
context,
105127
try_opt!(width.checked_sub(extra_offset)),
106128
offset + extra_offset));
129+
107130
Some(format!("{}{}", macro_name, rewrite))
108131
}
109132
MacroStyle::Braces => {

tests/target/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ fn issue184(source: &str) {
168168
}
169169

170170
fn arrays() {
171-
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,
172-
5, 6, 7, 8, 9, 0];
171+
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,
172+
4, 5, 6, 7, 8, 9, 0];
173173

174174
let y = [// comment
175175
1,

tests/target/match.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ fn main() {
6666
fn main() {
6767
match r {
6868
Variableeeeeeeeeeeeeeeeee => ("variable",
69-
vec!("id", "name", "qualname", "value", "type", "scopeid"),
69+
vec!["id", "name", "qualname", "value", "type", "scopeid"],
7070
true,
7171
true),
7272
Enummmmmmmmmmmmmmmmmmmmm => ("enum",
73-
vec!("id", "qualname", "scopeid", "value"),
73+
vec!["id", "qualname", "scopeid", "value"],
7474
true,
7575
true),
7676
Variantttttttttttttttttttttttt =>
7777
("variant",
78-
vec!("id", "name", "qualname", "type", "value", "scopeid"),
78+
vec!["id", "name", "qualname", "type", "value", "scopeid"],
7979
true,
8080
true),
8181
}
@@ -142,32 +142,32 @@ fn issue339() {
142142
fn issue355() {
143143
match mac {
144144
a => println!("a", b),
145-
b => vec!(1, 2),
145+
b => vec![1, 2],
146146
c => vec!(3; 4),
147147
d => {
148148
println!("a", b)
149149
}
150150
e => {
151-
vec!(1, 2)
151+
vec![1, 2]
152152
}
153153
f => {
154154
vec!(3; 4)
155155
}
156156
h => println!("a", b), // h comment
157-
i => vec!(1, 2), // i comment
157+
i => vec![1, 2], // i comment
158158
j => vec!(3; 4), // j comment
159159
// k comment
160160
k => println!("a", b),
161161
// l comment
162-
l => vec!(1, 2),
162+
l => vec![1, 2],
163163
// m comment
164164
m => vec!(3; 4),
165165
// Rewrite splits macro
166166
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn =>
167167
println!("a", b),
168168
// Rewrite splits macro
169169
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo =>
170-
vec!(1, 2),
170+
vec![1, 2],
171171
// Macro support fails to recognise this macro as splitable
172172
// We push the whole expr to a new line, TODO split this macro as well
173173
pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp =>
@@ -176,19 +176,19 @@ fn issue355() {
176176
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq =>
177177
println!("a", b),
178178
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr =>
179-
vec!(1, 2),
179+
vec![1, 2],
180180
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss =>
181181
vec!(3; 4),
182182
// Funky bracketing styles
183183
t => println!{"a", b},
184-
u => vec!{1, 2},
184+
u => vec![1, 2],
185185
v => vec!{3; 4},
186186
w => println!["a", b],
187187
x => vec![1, 2],
188188
y => vec![3; 4],
189189
// Brackets with comments
190190
tc => println!{"a", b}, // comment
191-
uc => vec!{1, 2}, // comment
191+
uc => vec![1, 2], // comment
192192
vc => vec!{3; 4}, // comment
193193
wc => println!["a", b], // comment
194194
xc => vec![1, 2], // comment

0 commit comments

Comments
 (0)