rfmt 0.1.0

Another Rust source code formatter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
rFmt ---- Rust source code formatter
===============================

https://github.com/zBaitu/rfmt

Overview
----------
rfmt is a Rust source code formatter. Yes, there is already an official tool [rustfmt](https://github.com/rust-lang-nursery/rustfmt) from [Rust Nursery](https://github.com/rust-lang-nursery). So why write another one?
* rustfmt is great for configurable, but there are still some style that i don't like in my personal taste.
* Write a code formatter for Rust can make me learn Rust more deeply, for example, the AST of Rust.
* For fun: ) 

Install, Build
----------
* Install

```
cargo install rfmt
```
* Build
```
git clone git@github.com:zBaitu/rfmt.git
cargo build --release
```

Usage
----------
```
Usage: rfmt [options] [path]
    If `path` is a dir, rfmt will do action for all files in this dir recursively.
    If `path` is not specified, use the current dir by default.
    If neither `options` nor `path` is specified, rfmt will format source code from stdin.

Options:
    -a, --ast           print the rust original syntax ast debug info
    -c, --check         check exceed lines and trailing white space lines
    -d, --debug         print the rfmt ir debug info
    -o, --overwrite     overwrite the source file
    -v, --version       show version
    -h, --help          show help
```

Running rfmt from your editor(Copy from rustfmt)
----------
* [Vim]http://johannh.me/blog/rustfmt-vim.html
* [Emacs]https://github.com/fbergroth/emacs-rustfmt
* [Sublime Text 3]https://packagecontrol.io/packages/BeautifyRust
* [Atom]atom.md
* Visual Studio Code using [RustyCode]https://github.com/saviorisdead/RustyCode or [vsc-rustfmt]https://github.com/Connorcpu/vsc-rustfmt

In fact, I only use rfmt for Vim now. I do not test for other editors. It is just to replace `rustfmt` to `rfmt`. For example, Vim:
```
let g:formatdef_rfmt = '"rfmt"'
let g:formatters_rust = ['rfmt']
```

Features
----------
Comparing to **rustfmt**, there are some main different features from **rfmt**:
* **DO NOT** parse sub module.
* Keep wrap from user input.
* Different align strategy.
* Group `crate`, `use`, `mod`, `attributes` and sort them.
* **DO NOT** format `doc`, `comment`, `string`. You can use the **check** function to show exceed lines and trailing white space lines.
* Provide check, directory recursively, ast dump.
* Nightly features, like `expr?`, `default fn`.

The following part will show such features in detail, with some existing issues from rustfmt.

### **DO NOT** parse sub mod
What happen when you format the following source by rustfmt when you edit on editor.
```
// lib.rs
pub mod a;
pub mod b;
pub mod c;
pub mod d;
...
```
It will parse all sub modules, this is the default action of the Rust parser. But in fact most of such scenario I just want to format only this file that I editing now.  
rfmt use a custom Rust parser, [rSyntax](https://github.com/zBaitu/rsyntax), it is cloned from the libsyntax of Rust. The main difference between rSyntax and Rust libsyntax is that, rSyntax skip sub module parse.  So rfmt can format quickly on editor scenario.
If you want to format all the source code in a project, just specify the project directory as rfmt command argument:
```
rfmt project_dir
```

### Keep wrap from user input
For the issue: [rustfmt reformats bit manipiulations](https://github.com/rust-lang-nursery/rustfmt/issues/626).
```
fn main() {
    let (a, b, c, d) = (0, 0, 0, 0);
    let _ = u32::from_be(((a as u32) << 24) |
                         ((b as u32) << 16) |
                         ((c as u32) <<  8) |
                          (d as u32) <<  0);
}
```
* rustfmt
```
fn main() {
    let (a, b, c, d) = (0, 0, 0, 0);
    let _ = u32::from_be(((a as u32) << 24) | ((b as u32) << 16) | ((c as u32) << 8) |
                         (d as u32) << 0);
}
```
Of cause you can use `#[rustfmt_skip]` to avoid such code, but in my personal opinon, I really don't like to add other code just for the source formatting tool.
* rfmt
```
fn main() {
    let (a, b, c, d) = (0, 0, 0, 0);
    let _ = u32::from_be(((a as u32) << 24) | 
                         ((b as u32) << 16) | 
                         ((c as u32) << 8) | 
                         (d as u32) << 0);
}
```
It looks OK, isn't it? Why rfmt can keep the user wrap? Because of the [rfmt ir]https://github.com/zBaitu/rFmt/blob/master/src/ir.rs. The custom ir of Rust AST record location information of every element as far as possible. Look another example:
```
fn main() {
    let ref_packet = [0xde, 0xf0, 0x12, 0x34, 0x45, 0x67,
                     0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc,
                     0x86, 0xdd];
}
```
* rustfmt
```
fn main() {
    let ref_packet = [0xde, 0xf0, 0x12, 0x34, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc,
                      0x86, 0xdd];
}
```
* rfmt
```
fn main() {
    let ref_packet = [0xde, 0xf0, 0x12, 0x34, 0x45, 0x67,
                      0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc,
                      0x86, 0xdd];
}
```

### Different align strategy
```
fn main() {
    f(123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz");
}
```
* rustfmt
```
fn main() {
    f(123456789,
      "abcdefg",
      "hijklmn",
      0987654321,
      "opqrst",
      "uvwxyz",
      123456789,
      "abcdefg",
      "hijklmn",
      0987654321,
      "opqrst",
      "uvwxyz");
}
```
* rfmt
```
fn main() {
    f(123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg",
      "hijklmn", 0987654321, "opqrst", "uvwxyz");
}
```
I prefer to put parameters on one line as much as possible. This is only for my personal preferences. But another case I really think it is bad looking.
```
fn main() {
    fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff(123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz");
}
```
* rustfmt
```
fn main() {
    fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff(123456789,
                                                                                      "abcdefg",
                                                                                      "hijklmn",
                                                                                      0987654321,
                                                                                      "opqrst",
                                                                                      "uvwxyz",
                                                                                      123456789,
                                                                                      "abcdefg",
                                                                                      "hijklmn",
                                                                                      0987654321,
                                                                                      "opqrst",
                                                                                      "uvwxyz");
}
```
* rfmt
```
fn main() {
    fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff(123456789,
            "abcdefg", "hijklmn", 0987654321, "opqrst", "uvwxyz", 123456789, "abcdefg",
            "hijklmn", 0987654321, "opqrst", "uvwxyz");
}
```
If the left align position is beyond limit(It is **50** for now), rfmt prefer double indent align to function call align. rfmt make source code left lean, while rustfmt is right lean, I think. An exsiting issue: [rustfmt should avoid rightwards drifting big blocks of code]https://github.com/rust-lang-nursery/rustfmt/issues/439
````
fn main() {
    let mut arms = variants.iter().enumerate().map(|(i, &(ident, v_span, ref summary))| {
        let i_expr = cx.expr_usize(v_span, i);
        let pat = cx.pat_lit(v_span, i_expr);

        let path = cx.path(v_span, vec![substr.type_ident, ident]);
        let thing = rand_thing(cx, v_span, path, summary, |cx, sp| rand_call(cx, sp));
        cx.arm(v_span, vec!( pat ), thing)
    }).collect::<Vec<ast::Arm> >();
}
````
* rustfmt
```
fn main() {
    let mut arms = variants.iter()
                           .enumerate()
                           .map(|(i, &(ident, v_span, ref summary))| {
                               let i_expr = cx.expr_usize(v_span, i);
                               let pat = cx.pat_lit(v_span, i_expr);

                               let path = cx.path(v_span, vec![substr.type_ident, ident]);
                               let thing = rand_thing(cx,
                                                      v_span,
                                                      path,
                                                      summary,
                                                      |cx, sp| rand_call(cx, sp));
                               cx.arm(v_span, vec![pat], thing)
                           })
                           .collect::<Vec<ast::Arm>>();
}
```
* rfmt
```
fn main() {
    let mut arms = variants.iter().enumerate().map(|(i, &(ident, v_span, ref summary))| {
        let i_expr = cx.expr_usize(v_span, i);
        let pat = cx.pat_lit(v_span, i_expr);

        let path = cx.path(v_span, vec![substr.type_ident, ident]);
        let thing = rand_thing(cx, v_span, path, summary, |cx, sp| rand_call(cx, sp));
        cx.arm(v_span, vec!(pat), thing)
    }).collect::<Vec<ast::Arm>>();
}
```
The result from rfmt is not changed, because this source code fits rfmt's code style.

### Group `crate`, `use`, `mod`, `attributes` and sort them
```
#![feature(custom_derive)]
#![deny(warnings)]
#![feature(question_mark)]
#![feature(iter_arith)]
#![feature(rustc_private)]

extern crate rst;
extern crate getopts;
extern crate walkdir;

use std::env;
use getopts::Options;

#[macro_use]
mod ts;

mod ir;
mod ft;
mod tr;
mod rfmt;
```
* rfmt
```
#![deny(warnings)]
#![feature(custom_derive)]
#![feature(iter_arith)]
#![feature(question_mark)]
#![feature(rustc_private)]

extern crate getopts;
extern crate rst;
extern crate walkdir;

use getopts::Options;
use std::env;

#[macro_use]
mod ts;

mod ft;
mod ir;
mod rfmt;
mod tr;
```
rfmt only group items that appear continuously. If on item is special that it must keep its order, like the `mod ts;`, make it separate from others.

### **DO NOT** format `doc`, `comment`, `string`
There are many issues about doc, comment, string, raw string from rustfmt. I think such element can leave free for user to write anything, any format they want. 

### Provide check, directory recursively, ast dump
If you want to check is there some line break the code style limit, rfmt provide check function.
```
// aaaaa  
// bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
fn main() {
    let a = r#"aaaaaaaaaaaaaaaaaaaaaaaaaaaa  
            bbbbbbbbbbbbbbbbb"#;
}
```
```
rfmt -c main.rs

a.rs
exceed_lines: {2}
trailing_ws_lines: {1, 4}
----------------------------------------
````
You can check or overwrite all files in a directory.
```
rfmt -c rust/src/libcore
rfmt -o rust/src/libstd
```
Maybe you are interested to see the Rust AST of a source code.
```
// AST
fn main() {}
```
```
rfmt -a a.rs
```
```
Crate {
    module: Mod {
        inner: Span { lo: BytePos(7), hi: BytePos(19), expn_id: ExpnId(4294967295) },
        items: [
            Item {
                ident: main#0,
                attrs: [],
                id: 4294967295,
                node: Fn(
                    FnDecl {
                    	......
                    }
                }
            }
        ]
    },
    attrs: [],
    config: [],
    span: Span { lo: BytePos(7), hi: BytePos(18), expn_id: ExpnId(4294967295) },
    exported_macros: []
}
----------------------------------------
0: Isolated [
    "// AST"
]
----------------------------------------
```

### Nightly features, like `expr?`, `default fn`
The rSyntax is cloned from Rust nightly(1.10.0-nightly), so it supports the latest language feature.
```
struct A;

impl A {
    default fn f() -> bool { true }
}

fn f() -> Result<bool, String> { Ok() }

fn ff() -> Result<bool, String> {
    f()?
}

fn main() {
    ff();
}
```
* rfmt
```
struct A;

impl A {
    default fn f() -> bool {
        true
    }
}

fn f() -> Result<bool, String> {
    Ok()
}

fn ff() -> Result<bool, String> {
    f()?
}

fn main() {
    ff();
}
```

Drawbacks
----------
As rfmt is written as a personal tool(toy) for my daily develop, it lacks some common features now.
* No config  
rustfmt provide lots of config option, but rfmt provide none. Code style is something like food, everyone has his taste. Although rustfmt has much configs now, there are still new config require open in issues. If majority part of rfmt's style suit your taste, you can clone and make some small modification, such as **LF**, **max width**, **indent**.
* Only support for some kinds of comment  
Comment can appear anywhere in source code, is it difficult to support all kinds of comment, as comment info does not exists on AST node. On the other hand, I don't think some tricky comment is really need. The following source with comment, which comment disappeared means that it is not supported by rfmt now.
```
// aaaaa

// bbbbb
struct A { // ccccc-DISAPPEARED
    // ddddd
    a: bool, // eeeee
    b: i32, // ffff
    // ggggg
} // hhhhh

// iiiii
fn f(a: bool, /* jjjjj-DISAPPEARED */ b: i32, /* kkkkk-DISAPPEARED */) -> bool { // lllll-DISAPPEARED
    // mmmmm
    const b: bool = false;                  // nnnnn
    let mut a = true;       // ooooo
    a = false; // ppppp
    a!();// qqqqq
    a // rrrrr
} // sssss
// ttttt

// uuuuu
```
* rfmt
```
// aaaaa

// bbbbb
struct A {
    // ddddd
    a: bool, // eeeee
    b: i32, // ffff
} // hhhhh

// iiiii
fn f(a: bool, b: i32) -> bool {
    // mmmmm
    const b: bool = false; // nnnnn
    let mut a = true; // ooooo
    a = false; // ppppp
    a!(); // qqqqq
    a // rrrrr
} // sssss
// ttttt

// uuuuu
```