Commit graph

1092 commits

Author SHA1 Message Date
Michael Killough
9e26575ed8 Use unreachable!() instead of panic!(). 2017-05-18 11:09:11 +07:00
Michael Killough
6098f7f924 Remove ConfigTracker - inline in Config.
Use an individual `Cell<bool>` on each config item, rather than
maintaining a set of config names that were accessed. Add a simple unit
test.
2017-05-18 11:07:49 +07:00
topecongiro
cfff3bd91e Use multiline when signature does not fit single line 2017-05-17 16:31:09 +09:00
topecongiro
d7b6f1199f Format source codes 2017-05-16 23:24:38 +09:00
topecongiro
feff6248c4 Allow last child in chain to go multiline 2017-05-16 23:24:00 +09:00
Michael Killough
7a4955f705 Add methods to serialize Config to TOML.
Two different modes:

 - Serialize the full Config object. This is useful as
   `Config::default().to_toml()` to output a rustfmt.toml with defaults
   (#317).
 - Serialize only the options that have been accessed. This could be
   useful to output a minimal rustfmt.toml for a project. (If the
   default value of any unused config item changes, you'll then get the
   new default when you come to use it).

This commit doesn't expose this anywhere - deciding a sensible CLI is a
bit trickier.

This commit also has very simple error reporting (Result<String,
String>) - once the CLI is decided, a more sensible method of reporting
errors might become obvious.
2017-05-16 18:08:24 +07:00
Michael Killough
4d879662a9 Track which config items are accessed.
Required by #865. This doesn't introduce any method to view which
parameters are accessed.

We record which config items are accessed even if we don't intend to
output them, as we assume it will be a relatively cheap operation.
2017-05-16 18:05:40 +07:00
Michael Killough
c0bdbfa531 Switch to accessing config items via method.
Preparation for #865, which proposes adding a flag which outputs which
config options are used during formatting.

This PR should not make any difference to functionality. A lot of this
was search-and-replace.

Some areas worthy of review/discussion:

 - The method for each config item returns a clone of the underlying
   value. We can't simply return an immutable reference, as lots of
   places in the code expect to be able to pass the returned value as
   `bool` (not `&bool). It would be nice if the `bool` items could
   return a copy, but the more complex types a borrowed reference... but
   unfortunately, I couldn't get the macro to do this.
 - A few places (mostly tests and `src/bin/rustfmt.rs`) were overriding
   config items by modifying the fields of the `Config` struct directly.
   They now use the existing `override_value()` method, which has been
   modified to return a `Result` for use by `src/bin/rustfmt.rs`. This
   benefits of this are that the complex `file_lines` and `write_mode`
   strings are now parsed in one place (`Config.override_value`) instead
   of multiple. The disadvantages are that it moves the compile-time
   checks for config names to become run-time checks.
2017-05-16 15:47:09 +07:00
Seiichi Uchida
762db5138e Format source codes 2017-05-15 22:55:45 +09:00
Seiichi Uchida
79ba34c607 Use offset_left for rewrite_unary_prefix 2017-05-15 22:55:01 +09:00
Nick Cameron
d1682b3473 Merge pull request #1510 from fintelia/better-filelines
Better file_lines
2017-05-15 09:19:18 +12:00
Nick Cameron
ebb26db68b Merge pull request #1551 from topecongiro/else-if-early-line-break
Forbid early line breaks inside else if
2017-05-15 08:59:03 +12:00
Nick Cameron
0ebd5775d7 Merge pull request #1550 from topecongiro/issue-1547
Prevent rewriting closure block to expr inside macro
2017-05-15 08:46:14 +12:00
Nick Cameron
65e151279d Merge pull request #1552 from guanqun/give-specific-errors
add more specific reason why it fails to parse
2017-05-14 18:08:27 +12:00
Guanqun Lu
7783b4c0b6 add more specific reason why it fails to parse 2017-05-14 13:19:06 +08:00
Jonathan Behrens
9f83f29187
Refactor file range checking 2017-05-13 13:14:24 -04:00
Seiichi Uchida
5383b6cc17 Format source code 2017-05-13 21:08:10 +09:00
Seiichi Uchida
819a13030f Use precise width when rewriting else if 2017-05-13 21:07:36 +09:00
Seiichi Uchida
7ec78711f2 Prevent rewriting closure block to expr inside macro 2017-05-13 18:32:25 +09:00
Seiichi Uchida
d0a094eefb Merge pull request #1546 from topecongiro/bug/range-full
Add a missing trailing comma before elipses
2017-05-13 17:40:47 +09:00
topecongiro
4aa64674c9 Add a missing trailing comma before elipses 2017-05-13 07:23:34 +09:00
Seiichi Uchida
56515dd4d6 Merge pull request #1543 from topecongiro/field-attr
Support struct_field_attributes
2017-05-12 20:08:36 +09:00
topecongiro
744fa427e2 Support struct_field_attributes 2017-05-12 17:58:38 +09:00
topecongiro
384ce46eef Use vertical layout when args len is larger than fn_call_width 2017-05-12 16:30:26 +09:00
topecongiro
ba1a7b9e7f Try multi-line only when fn_call_style is Block and rewriting function call 2017-05-12 10:33:05 +09:00
Nick Cameron
1eec103516 Merge pull request #1523 from topecongiro/poor-formatting/fn_call_style
Use multiline when args exceeds max_width in fn_call_style = "block"
2017-05-10 08:27:24 +12:00
topecongiro
30a8050559 Use multiline when args exceeds max_width in fn_call_style = "block" 2017-05-10 00:27:57 +09:00
topecongiro
9d96c33d59 Inherit alignment to last else 2017-05-09 17:33:45 +09:00
topecongiro
ac6e6dfd5d Refactor rewrite_pat_expr 2017-05-09 17:33:28 +09:00
Nick Cameron
35e1ca73fc Merge pull request #1519 from topecongiro/poor-formatting/if-else
Remove visual_indent when rewriting else block
2017-05-09 15:21:29 +12:00
Nick Cameron
da261ef9d3 Merge pull request #1520 from topecongiro/allow-match
Apply closure exception to match in args
2017-05-09 15:21:06 +12:00
topecongiro
0248e9869d Apply closure exception to match in args 2017-05-09 08:28:05 +09:00
topecongiro
f36411c339 Remove visual_indent when rewriting else block 2017-05-09 08:11:05 +09:00
topecongiro
2776615dc9 Replace 'try!' with '?' 2017-05-08 13:14:23 +09:00
topecongiro
d062d79fb5 Use block indent when visual indent failed 2017-05-08 08:07:18 +09:00
Nick Cameron
459b4449a6 Merge pull request #1442 from topecongiro/poor-formatting/trailing-comma
Remove comma from function definition with a single argument
2017-05-08 10:45:03 +12:00
Nick Cameron
e5f8a725db Merge pull request #1511 from topecongiro/bug/closure-fallback
Add fallback path inside rewrite_closure
2017-05-08 10:30:49 +12:00
Nick Cameron
2d88c7569f Merge pull request #1513 from topecongiro/refactoring
Add methods to Shape
2017-05-08 10:29:02 +12:00
topecongiro
4a28be486e Refactor Shape 2017-05-08 07:24:32 +09:00
topecongiro
e923df4146 Add indented and with_max_width 2017-05-08 07:24:12 +09:00
topecongiro
5e832acdc4 Allow config-path to point to a file 2017-05-08 07:02:23 +09:00
topecongiro
2ee8e730a6 Add fallback path 2017-05-07 13:13:44 +09:00
Jonathan Behrens
b1c9832c9b
Respect file_lines within write_snippet() function 2017-05-06 17:59:51 -04:00
Jonathan Behrens
7c6479358b
Respect file_lines within format_lines() function 2017-05-06 15:14:44 -04:00
Nick Cameron
75a13868da Block format control flow discriminant expressions and binops
Fixes #1450

Adds control_style option
2017-05-05 14:37:13 +12:00
topecongiro
e7c6d34854 Use SeperatorTactic::Never for a single argument 2017-05-04 14:47:50 +09:00
Nick Cameron
8579c1db0e Merge pull request #1495 from topecongiro/poor-formatting/closure
Fix weird indentation inside closures
2017-05-04 17:15:58 +12:00
Nick Cameron
c89c01efc0 Merge pull request #1500 from shioju/master
Format macros in trait item position
2017-05-04 10:00:03 +12:00
Sheng Hau
51815effbe Format macros in trait item position 2017-05-04 00:10:03 +08:00
topecongiro
58d957be3f Check format failures explicitly in visit_block 2017-05-04 00:21:51 +09:00
est31
11cc56b06b Format fix 2017-05-03 17:16:58 +02:00
est31
6e46a827b6 Simplifications 2017-05-03 17:11:34 +02:00
est31
78d4ef1992 Drop rustc-serialize, use serde, update toml dependency 2017-05-03 10:23:50 +02:00
topecongiro
f5da9d779f Format source codes 2017-05-03 11:26:25 +09:00
topecongiro
e91d498b8f Keep a chain with length chain_one_line_max in a single line 2017-05-03 11:24:08 +09:00
topecongiro
2c1d896f60 Split a long chain with a single child 2017-05-03 11:22:36 +09:00
topecongiro
3f703fd33d Use block indent when visual indent failed inside closure block 2017-05-03 11:21:31 +09:00
Nick Cameron
59d66c3ae8 Merge pull request #1453 from topecongiro/bug/empty-struct-with-comment
Handle empty tuple struct def with comment properly
2017-05-02 13:49:15 +12:00
topecongiro
93dae1a34d Add test for empty tuple struct with comment 2017-05-02 10:21:39 +09:00
topecongiro
2e00c117e5 Handle empty tuple struct def properly 2017-05-02 10:21:39 +09:00
Seiichi Uchida
19599b9a01 Use block when arm exceeds max_width 2017-05-02 10:15:31 +09:00
Vincent Esche
42dbe482e3 Made type_punctuation_density apply too all + in types 2017-05-02 01:03:46 +02:00
Nick Cameron
e809dcbf3b Merge pull request #1491 from regexident/where_density
Add todo on equal output for `where_density = Tall` or `Vertical`
2017-05-02 10:50:32 +12:00
Nick Cameron
7fb288f0b3 Merge pull request #1487 from topecongiro/issue1470
Use block indent when visual indent exceeds max_width
2017-05-02 10:45:23 +12:00
Nick Cameron
c546f1b396 Merge pull request #1454 from topecongiro/match
Format nested mathces properly.
2017-05-02 10:40:31 +12:00
Nick Cameron
2223b64f86 Merge pull request #1486 from topecongiro/refactoring
Refactor source codes
2017-05-02 10:30:11 +12:00
Vincent Esche
5ae74cab8f Add todo on equal output for where_density = Tall or Vertical 2017-05-01 22:01:19 +02:00
Nick Cameron
c6f1dd4fb5 Remove generics_style option 2017-05-02 07:56:46 +12:00
Seiichi Uchida
c7e9bcadaa Use block indent when visual indent exceeds max_width 2017-05-01 18:52:43 +09:00
topecongiro
9dc643a6ae Remove TODO from 'format_tuple_struct' 2017-05-01 15:57:08 +09:00
topecongiro
68c5915176 Remove TODO from 'rewrite_generics' 2017-05-01 15:57:08 +09:00
topecongiro
883db2f952 Refactor Shape 2017-05-01 15:57:08 +09:00
topecongiro
af3f4c7f29 Remove duplicates 2017-05-01 15:57:02 +09:00
topecongiro
3d2cd681ae Use 'first_line_width' 2017-05-01 15:36:40 +09:00
Nick Cameron
7a2577322d Merge pull request #1473 from topecongiro/bug/no-such-file-or-directory
Prevent panicking against directories or non-existent files
2017-05-01 16:38:43 +12:00
Nick Cameron
5d9a35050d Remove where_indent option 2017-05-01 16:05:38 +12:00
Nick Cameron
fba5af2a7d Merge pull request #1469 from topecongiro/bug/chains
Remove a bug when calculating the length of the chain
2017-05-01 15:48:53 +12:00
Nick Cameron
a8fbfdb4a3 Merge pull request #1459 from shioju/master
Show exit codes' meanings with the rustfmt help message
2017-05-01 15:15:30 +12:00
Nick Cameron
29e1a3329f Merge pull request #1457 from diaevd/master
format_lines: fix line_len for config.max_width
2017-05-01 15:13:19 +12:00
Nick Cameron
6a19c93f2a Merge pull request #1461 from DarkEld3r/1447-line-length-in-chars
Fix line length calculation (should be in chars, not bytes)
2017-05-01 13:15:28 +12:00
Seiichi Uchida
4cec3ab71d Prevent panicking against directories or non-existent files 2017-04-26 17:29:56 +09:00
topecongiro
02cf515be6 Format source codes 2017-04-24 16:50:11 +09:00
topecongiro
cbd33e1983 Remove '+ 1' when counting the length of chains 2017-04-24 16:48:05 +09:00
Stanislav Tkach
b53aeebbcf Fix line length calculation (should be in chars, not bytes) 2017-04-19 17:49:03 +03:00
Sheng Hau
0653b8413e Show exit codes' meanings with the rustfmt help message 2017-04-19 00:25:48 +08:00
Evgeny Duzhakov
bb7b002b23 format_lines: fix line_len for config.max_width 2017-04-18 14:31:53 +03:00
topecongiro
02c9ac93be Prohibit long return val from 'rewrite_closure_block' 2017-04-17 23:17:17 +09:00
topecongiro
0614e94d0d Format source codes 2017-04-15 18:42:45 +09:00
topecongiro
a91fd699a0 Add heuristic choosing block or visual indent for unary op based on span 2017-04-15 18:40:39 +09:00
topecongiro
8dc53d3750 Check the length of the first line of the last method in the chain 2017-04-15 16:23:38 +09:00
topecongiro
1c1ad525f6 Format source codes 2017-04-15 16:22:54 +09:00
topecongiro
8a5038c8f5 Use offset instead of indent for match arm 2017-04-15 16:22:26 +09:00
Jay Lee
c5f07eda41 fix path check 2017-04-10 05:25:12 +08:00
Nick Cameron
bdcb8d663e Merge pull request #1438 from topecongiro/poor-formatting/method-chains
Lookup the last element of 'subexpr_list' instead of the first one
2017-04-06 21:26:13 +12:00
Nick Cameron
ee9daade35 Fix a bug with overlong function calls 2017-04-06 21:17:22 +12:00
Nick Cameron
f4952d3780 Implement a max array width
Fixes #1421
2017-04-06 13:01:19 +12:00
topecongiro
8d22e85e48 Lookup the last element of 'subexpr_list' instead of the first one
Closes #1217, #1236, #1389 and #1434.
2017-04-06 09:26:25 +09:00
Nick Cameron
b3a4ba570e Merge pull request #1432 from topecongiro/parse-config
Print error and usage when decoding config file failed
2017-04-06 11:30:33 +12:00
Ryan1729
eb25ac5e11 eliminate nested match 2017-04-04 00:01:33 -06:00
Ryan1729
4c4f70957b fix bug by adding boolean argument
Signed-off-by: Ryan1729 <Ryan1729@gmail.com>
2017-04-03 17:39:14 -06:00
topecongiro
6af0968511 Change return type of 'from_toml' from 'Config' to 'Result<Config>'
Closes #1426.
2017-04-03 13:58:46 +09:00
Nick Cameron
f63815ee95 Merge pull request #1435 from iliekturtles/line-length
Do not count `\r` as part of line width.
2017-04-03 08:40:22 +12:00
Nick Cameron
20395c5c55 Merge pull request #1430 from fintelia/stdin_lines
The --file_lines argument now supports "stdin"
2017-04-03 08:27:35 +12:00
Mike Boutin
a0efdf2de9 Do not count \r as part of line width.
Resolves #1335. Does not attempt to handle a `\r` not followed by a `\n` nor
attempt to handle Unicode intricacies (#6) including zero-width or multi-byte
characters.
2017-04-02 11:09:29 -04:00
Jonathan Behrens
6241469a69 Warn about extra files listed in file_lines argument 2017-03-31 13:34:53 -04:00
topecongiro
34b90d8716 Check existence of pre comment explicitly
This commit fixes #1428.
2017-03-31 21:57:29 +09:00
Jonathan Behrens
56da223dc2 --file_lines argument now supports "stdin" 2017-03-30 19:35:58 -04:00
Nick Cameron
3edc184221 Merge pull request #1413 from Emilgardis/master
Implemented support for workspaces
2017-03-31 08:43:40 +13:00
Nick Cameron
4f226eb909 Merge pull request #1424 from topecongiro/long-struct
Split long fields in structs
2017-03-31 08:42:40 +13:00
topecongiro
3a1ffa7db7 Split long fields in structs
This commit splits long fields in structs.
Closes #1412.
2017-03-30 13:37:34 +09:00
Emil Gardström
6971b4b5c9 Implemented support for workspaces
closes rust-lang-nursery/rustfmt#1244
2017-03-30 03:29:32 +02:00
Jonathan Behrens
36f0402bac Canonicalize range path in FileLines::intersects() 2017-03-29 18:50:39 -04:00
Jonathan Behrens
b6f1b516d9 Format any statement whose span intersects file_lines 2017-03-29 01:33:14 -04:00
Nick Cameron
603f26d8f1 Merge pull request #1415 from topecongiro/remove-duplicates
Remove duplicates
2017-03-29 17:51:19 +13:00
topecongiro
78826e6783 Fix a typo 2017-03-29 09:17:58 +09:00
Fabian Zaiser
6ecc18c7de Omit space before '\n' + comment (fixes #457) 2017-03-28 16:32:12 +02:00
topecongiro
a2d57e956a Remove duplicate definitions
This commit removes duplicated definitions of `type_annotation_separator` and
`type_bound_colon`.
2017-03-28 23:20:50 +09:00
Nick Cameron
5305bc8436 test fallout 2017-03-28 11:25:59 +13:00
Nick Cameron
5fb1140688 fallout - source reformatting 2017-03-28 11:14:47 +13:00
Nick Cameron
a079b87e7c Use a char len heuristic rather than item count for chain_one_line_max 2017-03-28 11:12:15 +13:00
Nick Cameron
e4efa22983 Source formatting fallout 2017-03-28 10:58:41 +13:00
Nick Cameron
038436919d Put the second element of a chain on its own line when block formatting. 2017-03-28 10:56:46 +13:00
Nick Cameron
8bca85c4b6 Support block indent for function calls
Uses the `fn_call_style` option.

Fixes #1358
2017-03-28 10:43:46 +13:00
Nick Cameron
f53d5dd90c Merge pull request #1409 from alobb/1408-empty-tuple-enum-decl
Prevent conversion of empty tuples to unit structs
2017-03-27 14:28:41 +13:00
Aaron Lobb
548de69d2d Prevent conversion of empty tuples to unit structs
Fixes #1408
2017-03-26 15:47:48 -07:00
C4K3
abca1deded Add indent_match_arms option (#1404)
Makes it optional whether to indent arms in match expressions. Setting this
to false may be desirable for people wishing to avoid double-indents, in
that if the match arm is a block, the block will cause an extra indentation
level, and if it isn't a block but just a single line, it's still easy to
see the logic at a glance.

This style is preferred in certain other languages with switch statements,
e.g. Linux style C and the most common Java style.
2017-03-26 18:32:29 +13:00
Aaron Lobb
0dd0cc1941 Break use statements into multiple lines (#1407)
This fixes how line lengths for use statements with multiple
items don't extend beyond the maximum line length.

Fixes #1400
2017-03-26 18:21:43 +13:00
Jon Gjengset
f96e56c3a0 Avoid extra comma in vertical single-field struct patterns (#1403)
* Add (failing) test for #1397

* Fix for #1397

Specifically, we end up double-adding a trailing comma for single-member
struct patterns that are arranged vertically. One is added by write_list
(since such structs return true for needs_trailing_separator), and
another is added by the if in the old code.
2017-03-26 18:16:45 +13:00
C4K3
6be61bcdd6 Warn on unused config options (#1402)
This will make it clear if a user has misspelled a config option, or if
an option has been changed/removed.
2017-03-26 18:12:56 +13:00
Nick Cameron
488c0b9546 Test and source fallout 2017-03-22 09:25:26 +13:00
Nick Cameron
4bb31a7231 Block indenting for struct lit patterns
Now follows struct_lit_style (and most other struct_lit_ options).

Required a fair bit of refactoring and bug fixes.

Fixes #1311
2017-03-22 09:25:26 +13:00
Robert Gawdzik
bfb1c277a8 Make list_files deterministic (Closes #1141) (#1393)
- Use BTreeMap instead
2017-03-22 09:00:33 +13:00
Nick Cameron
d4c5d572c8 Unify fn_args_layout and fn_arg_indent
Closes #1390
2017-03-20 11:59:08 +13:00
Nick Cameron
21ff1d43ba Unify a bunch of option types into IndentStyle 2017-03-20 11:46:39 +13:00
Nick Cameron
c986e895bb Remove FnArgLayoutStyle::BlockAlways 2017-03-20 11:35:53 +13:00
Nick Cameron
ce7b0aacd0 Remove BlockIndentStyle::Inherit 2017-03-20 11:23:44 +13:00
Nick Cameron
0cb6f56647 Block formatting for arrays
Fixes #1331
2017-03-20 10:26:19 +13:00
Nick Cameron
0c93e7dacf Merge branch 'master' of github.com:rust-lang-nursery/rustfmt 2017-03-13 11:43:12 +13:00
Nick Cameron
ed10bf9532 Trailing comma for functions with empty bodies 2017-03-13 11:42:41 +13:00
Nick Cameron
4df405a768 Line breaking around generics in tuple structs 2017-03-13 11:25:46 +13:00
C4K3
ab832fa4bb config: Rename ideal_width -> comment_width (#1370)
Since the config option only affects comment widths, and the previous
name has led to some confusion (see #1321, #1152).
2017-03-13 09:07:33 +13:00
Nick Cameron
d5478f7d45 Reformatting source code 2017-03-10 16:06:05 +13:00
Nick Cameron
22be93780c block generics in traits 2017-03-10 16:04:08 +13:00
Nick Cameron
a1d94e545c Attempt to fixup impls with long generics 2017-03-10 15:58:35 +13:00
Nick Cameron
c8af03f475 Block formatting for tuple structs
Follows the fn_args_layout option
2017-03-10 15:31:45 +13:00
Nick Cameron
7b901130cb Insert a newline before block-formatted generics
Fixes #1359
2017-03-10 14:29:45 +13:00
Nick Cameron
4b60d94a99 Check we can put the where clause on one line when we think we can
Fixes #1362
2017-03-10 09:45:26 +13:00
Nick Cameron
70085fca84 Try harder to format function sigs
Fixes #1363
2017-03-10 09:26:14 +13:00
Nick Cameron
e3aad6ee83 Be smarter about finding the span for .. in patterns
Fixes #1319
2017-03-09 15:49:58 +13:00
Nick Cameron
d107ca12b8 Use trailing commas if block formatting fn args
Fixes #1330
2017-03-09 14:47:29 +13:00