Repository navigation
Token-dependent indention in combination with standard indention - #353
Conversation
needs_indention.*() takes an additional argument other_trigger_tokens which is used to determine whether a trigger causes indention. The new criteria is that (1) there is no multi-line token between the trigger and the first line break and (2) there is no other token between the potential trigger and the first line break that is going to cause indention. Aims to solve r-lib#352 and r-lib#351.
krlmlr
left a comment
There was a problem hiding this comment.
Thanks. This PR adds quite a few new examples for formatting formals and arguments. I think it would be helpful to split this PR:
- Only add the new examples and the generated output files (can be merged/committed to master right away).
- Actually change the formatting, update the examples (in this PR).
This will help gauge the relevance of my comments.
| a <- function(x, # | ||
| y # | ||
| ) { # FIXME: Move to the same indention level as a | ||
| ) { |
There was a problem hiding this comment.
Do we need to indent this closing paren?
There was a problem hiding this comment.
I think it makes sense to not indent it (this is also what happens in RStudio) because a closing brace unindents itself. Alternative to leave it as it was before is moving it to the same level as ), which I think looks worse plus it's more complicated 😩.
There was a problem hiding this comment.
Also, this is only relevant for cases where the closing brace cannot go directly after the last formal, because it is blocked by a comment. Otherwise the brace moves up. Debatable whether we want that or not, but I think it's a detail.
|
|
||
| function(a, | ||
| b, | ||
| c) {} |
There was a problem hiding this comment.
Add line break if formals consume multiple lines?
There was a problem hiding this comment.
Line break for the closing brace? I think under long lines, there is no line break for that case (just an example, not covered in the floating text).
Should we open an issue to make the tidyverse style guide explicit in this regard?
There was a problem hiding this comment.
Never mind, it's not that important.
There was a problem hiding this comment.
Ok, let's leave that one for now
| function(a = | ||
| b, | ||
| f = | ||
| d, c = |
There was a problem hiding this comment.
Add newline after , if the argument consumes multiple lines?
There was a problem hiding this comment.
I don't see that covered yet in the tidyverse style guide. Should we open an issue to propose one argument per line?
There was a problem hiding this comment.
Multiline formals aren't covered either, because they're very rare. I think if a formal is spread over multiple lines, no other information should be contained on these lines.
There was a problem hiding this comment.
To do this properly, we actually need #357 because the multi-line attribute is not set and just looking for line breaks at the current nest is insufficient.
| @@ -0,0 +1,8 @@ | |||
| call(a = | |||
There was a problem hiding this comment.
Add a line break if argument is split over multiple lines?
There was a problem hiding this comment.
Yes, with scope > "indention", but this only formats up to indention (i.e indention in the file name refers to the scope argument). See the file tests/testthat/indention_operators/eq_sub_complex_tokens-out.R for the same output with scope = "tokens"
| ) | ||
|
|
||
| other_trigger_on_same_line <- | ||
| pd[remaining_row_idx_between_trigger_and_line_break, ]$token %in% |
There was a problem hiding this comment.
pd$token[...] is slightly faster.
|
Thanks @krlmlr. I see your point but I am not sure if I can easily split it up, because some changes I made affect multiple situations. Hence, I would feel more comfortable to keep it as one PR. Also, I wonder whether the tidyverse style guide needs to be extended in order to cover some of the cases we handle here too. |
f74d389 to
687037e
Compare
214c868 to
2fbde82
Compare
merge with master containing tests already by using the ours merge strategy (i.e. prefer indent-eq-formals whenever there is a conflict)
2fbde82 to
b591a12
Compare
|
Agree to merge and migrate remaining problems to a new issue. The new output looks better than what we currently have. |
Overview
Token-dependent indention plays a minor role in styler, since the tidyvese style guide does only permit it in one situation: Function declarations.
As of now, token-dependent indention was always absolute, i.e. no other indention rules were allowed. This lead to problems such as the one outlined in #351. After having tried to solve the problem by modifying styler infrastructure (which was a hack, and committed in this branch for reference), the ideas developed in the second part of this PR (that reversed some hacks from the first part) should provide a more refined and flexible solution via a transformer function and allowing other style guides in the future to use the feature of token dependent indention in other ways.
Example
Let's do it with an example. Suppose we have
Which is incorrectly indented.
Apply indention rules for braces (both curly and round). Function declarations will get indented because of the opening brace after
function(no other indention is possible because other braces won't live in the same nest.Remove all indention in function declaration headers. Note that function calls are not affected. After this step, code may look like this:
Apply other indention rules such as indention based on operators (
+,=etc.). Again, note that function calls are not affected.Finally, we add the indention of the reference token
(to all tokens in the header, so we end up with the desired indention.Closes #353, #352.