Commit Graph

227 Commits

Author SHA1 Message Date
David Lukes
ad76741bca Move license template parsing into config phase 2018-03-05 13:13:55 +01:00
David Lukes
d012d52b4d Parse template with state machine instead of regex
This allows occurrences of `{` and `}` within `{}` placeholders in the
template, and also for having literal `{` and `}` in the template by
means of escaping (`\{`).

Unbalanced, unescaped `}` at the toplevel is a syntax error which
currently triggers a panic; I'll add proper error handling as I move the
license template parsing code into the config parsing phase.
2018-03-05 13:11:21 +01:00
David Lukes
2eebe614c7 Attempt at checking for license (#209)
I'm not quite sure how best to handle loading the license template from
a path -- I mean obviously I know *how* to do it, but I'm not sure where
to fit it in the codebase :) So this first attempt puts the license
template directly into the config file.

These are my misgivings about the license template config option as a
path to a file (I'd love feedback if some of these are wrong or can be
easily circumvented!):

1. I thought the obvious choice for the type of `license_template` in
`create_config!` should be `PathBuf`, but `PathBuf` doesn't implement
`FromStr` (yet? see https://github.com/rust-lang/rust/issues/44431), so
it would have to be wrapped in a tuple struct, and I went down that road
for a little while but then it seemed like too much ceremony for too
little gain.

2. So a plain `String` then (which, mind you, also means the same
`doc_hint()`, i.e. `<string>`, not `<path>` or something like that). The
fact that it's a valid path will be checked once we try to read the
file.

3. But where in the code should the license template be read? The
obvious choice for me would be somewhere in `Config::from_toml()`, but
since `Config` is defined via the `create_config!` macro, that would
mean tight coupling between the macro invocation (which defines the
configuration option `license_template`) and its definition (which would
rely on the existence of that option to run the template loading code).

4. `license_template` could also be made a special option which is
hardwired into the macro. This gets rid of the tight coupling, but
special-casing one of the config options would make the code harder to
navigate.

5. Instead, the macro could maybe be rewritten to allow for config
options that load additional resources from files when the config is
being parsed, but that's beyond my skill level I'm afraid (and probably
overengineering the problem if it's only ever going to be used for this
one option).

6. Finally, the file can be loaded at some later point in time, e.g. in
`format_lines()`, right before `check_license()` is called. But to
face a potential *IO* error at so late a stage, when the source files
have already been parsed... I don't know, it doesn't feel right.

BTW I don't like that I'm actually parsing the license template as late
as inside `check_license()` either, but for much the same reasons, I
don't know where else to put it. If the `Config` were hand-rolled
instead of a macro, I'd just define a custom `license_template` option
and load and parse the template in the `Config`'s init. But the way
things are, I'm a bit at a loss.

However, if someone more familiar with the project would kindly provide
a few hints as to how the path approach can be done in a way that is as
clean as possible in the context of the codebase, I'll be more than
happy to implement it! :)
2018-03-05 13:11:21 +01:00
Nick Cameron
4f522794ae Tidy up and pass tests 2018-03-02 15:07:13 +13:00
Nick Cameron
39301ae5f2 Go back to a non-workspace structure
Kinda reverts https://github.com/rust-lang-nursery/rustfmt/pull/2419
2018-03-02 14:58:23 +13:00
topecongiro
4af2aa3a9e Create rustfmt_core crate 2018-02-07 22:48:05 +09:00
Nick Cameron
c9e250a1ab
Merge pull request #2417 from topecongiro/issue-2415
Avoid orphan in chain with punctuation
2018-02-06 21:23:14 +13:00
topecongiro
5e0c6f9716 Avoid orphan in chain with punctuation 2018-02-06 09:36:29 +09:00
topecongiro
7f949a5018 Explicitly disable colored output when it is not supported 2018-02-06 09:29:00 +09:00
Seiichi Uchida
f815858420 Use correct offset when unindenting code block
When using hard tabs, we should only remove '\t'.
2018-02-04 17:21:10 +09:00
Nick Cameron
7c3a422742 Update libsyntax crates 2018-02-02 14:18:30 +13:00
Nick Cameron
b7f01769f9
Merge branch 'master' into init-shorthand 2018-02-01 15:20:01 +13:00
Nick Cameron
918e79bb5a
Merge pull request #2380 from topecongiro/reorder-mods
[RFC] Reorder modules alphabetically
2018-02-01 15:18:34 +13:00
Seiichi Uchida
4c9ab8b405 Cargo fmt with modules reordering enabled 2018-01-29 22:00:07 +09:00
Seiichi Uchida
56c6d73d82 Reorder modules
Add `reorder_modules` config option.

Two things we must keep in mind when reordering modules:
1. We should not reorder modules with attributes, as doing so could
   potentially break the code (e.g. `#[macro_use]`).
2. We should not reorder inline modules e.g. `mod foo { /* .. */ }`.
   We should only reorder module declarations e.g. `mod foo;`.

Some open questions:
1. Should we bring modules with `pub` in front of those without `pub`
   so that they stand out from others?
2. Instead of keeping modules with attributes in the same place,
   can we bring them in front of others? Is this safe?
2018-01-29 21:59:15 +09:00
Seiichi Uchida
dfc67a5df7 Cargo clippy 2018-01-26 14:53:28 +09:00
Seiichi Uchida
154bf8e1af Cargo fmt 2018-01-22 13:05:18 +09:00
K.J. Valencik
1fb172f989 LineOverflow: Count tabs as tab_spaces when measuring line length for overflow 2018-01-19 12:18:25 -05:00
Seiichi Uchida
cf4ab38f67 Hide parse error in format_snippet() 2018-01-11 15:19:23 +09:00
topecongiro
75cb236711 Compare extern crate items with its name instead of span
When we use span, bugs easily sneak in.
2018-01-04 12:49:37 +09:00
Nick Cameron
f86f6dcd9c Format some macros 2.0 macro defs
cc #1539
2018-01-01 19:51:54 +13:00
Nick Cameron
e95541c7d2 Make the import of Config pub 2017-12-28 10:08:27 +13:00
Marcus Klaas
656edbf0f0 Use std time 2017-12-27 16:34:09 +13:00
Marcus Klaas
dc356ffef7 Add rudimentary timing of parsing and formatting phases 2017-12-27 16:33:21 +13:00
Seiichi Uchida
27167cbbaa Format code block in comment
Closes #554.
Closes #1695.
2017-12-24 23:56:51 +09:00
Oliver Schneider
fad903fd14 Move from String to PathBuf where applicable 2017-12-15 10:26:19 +09:00
Seiichi Uchida
3dd31e25bf Use enumerate() 2017-12-11 11:50:11 +09:00
Seiichi Uchida
e45c0c4815 Rename error_on_unformatted_comments_or_strings to error_on_unformatted 2017-12-11 11:48:17 +09:00
Seiichi Uchida
aea19d5e33 Combine error_on_line_overflow_comments/strings
1. Rename to error_on_unformatted_comments_or_strings
2. Set the option to false by default.
2017-12-11 11:48:17 +09:00
Seiichi Uchida
ef6ebaa215 Add a config option to suppress error message on string literal 2017-12-11 11:48:17 +09:00
Seiichi Uchida
414a995926 Replace StringBuffer with String 2017-12-11 09:19:00 +09:00
topecongiro
adc3b12ad4 Remove println! debug :( 2017-12-08 17:48:49 +09:00
topecongiro
821d04b2a4 Do not report errors on skipped items or statements 2017-12-08 17:46:43 +09:00
topecongiro
5e6bb3edb0 Keep track of line number in visitor 2017-12-08 16:59:04 +09:00
Seiichi Uchida
1d9a10e00e Use explicit lifetime to get rid of unsafe code
at least this is why we use Rust
2017-12-07 13:57:54 +09:00
Seiichi Uchida
69a15b2eee Update FmtVisitor::from_codemap() 2017-12-06 22:51:52 +09:00
topecongiro
16184d3e16 Cargo fmt and update a test 2017-12-03 11:34:18 +09:00
clippered
794a215b27 rename to use_colored_tty; used match as well 2017-11-13 20:18:05 +11:00
clippered
d2f2f25463 add cli option for color 2017-11-13 20:18:03 +11:00
Nick Cameron
e6253c4d31 Extract a closures module 2017-11-13 15:27:16 +13:00
topecongiro
05798572bb Remove empty lines at the beginning of the file 2017-11-10 17:09:31 +09:00
topecongiro
d8d9fedf9e Propagate an error rather than panic 2017-11-06 21:48:18 +09:00
topecongiro
3e8f4b7907 Fix PairParts related issues 2017-11-06 21:48:05 +09:00
clippered
3bae42aa5a fix output panics on unable to write to stderr 2017-11-01 23:45:35 +11:00
Nick Cameron
4022b094f8
Merge pull request #2074 from GuillaumeGomez/emitter
Update to last Emitter update
2017-10-28 18:12:46 +13:00
Chris Emerson
92e374e778 Fix a "variable does not need to be mutable" warning. 2017-10-27 22:57:32 +01:00
Nick Cameron
adbe98e522 Merge pull request #2069 from SingingTree/log_path_str_if_after_file_errors
Return a more verbose error when formatting a file fails
2017-10-27 01:39:43 +05:30
Guillaume Gomez
e35e27659d Update to last Emitter update 2017-10-20 17:02:49 +02:00
Matthew McAllister
108e2cd776 Handle #![rustfmt_skip] at file top level 2017-10-20 00:22:23 -07:00
Bryce Van Dyk
47a0bef91c Return a more verbose error when formatting a file fails
Expands the error message returned if the after_file function fails to
also include path_str. This allows users to better identify files that are
not being formatted.
2017-10-20 15:27:35 +13:00