Skip to content

Token-dependent indention in combination with standard indention - #353

Merged
lorenzwalthert merged 14 commits into
r-lib:masterfrom
lorenzwalthert:indent-eq-formal
Mar 1, 2018
Merged

lorenzwalthert merged 14 commits into
r-lib:masterfrom
lorenzwalthert:indent-eq-formal

Conversation

@lorenzwalthert

@lorenzwalthert lorenzwalthert commented Feb 23, 2018 •

Copy link
Copy Markdown
Collaborator

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

function(a = 
b, d +
e, 
c) {
  call(1, 
    2
)
}

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.

function(a = 
  b, d,
  e, 
  c) {
  call(1, 
    2
)
}

Remove all indention in function declaration headers. Note that function calls are not affected. After this step, code may look like this:

function(a = 
b, d,
e, 
c) {
  call(1, 
    2
)
}

Apply other indention rules such as indention based on operators (+, = etc.). Again, note that function calls are not affected.

function(a = 
  b, d,
  e,
c) {
  call(1, 
    2
)
}

Finally, we add the indention of the reference token ( to all tokens in the header, so we end up with the desired indention.

function(a = 
           b, d,
         e,
        c) {
  call(1, 
    2
)
}

Closes #353, #352.

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 krlmlr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This PR adds quite a few new examples for formatting formals and arguments. I think it would be helpful to split this PR:

  1. Only add the new examples and the generated output files (can be merged/committed to master right away).
  2. 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
) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to indent this closing paren?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😩.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add line break if formals consume multiple lines?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, it's not that important.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's leave that one for now

function(a =
b,
f =
d, c =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add newline after , if the argument consumes multiple lines?

@lorenzwalthert lorenzwalthert Feb 23, 2018 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see that covered yet in the tidyverse style guide. Should we open an issue to propose one argument per line?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deal 😊

@lorenzwalthert lorenzwalthert Feb 25, 2018 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@lorenzwalthert lorenzwalthert Feb 25, 2018 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this (in particular the part solving #351) is part of milestone styler 1.0.1 (which was due a few days ago), I don't think I can implement #357 reasonably quickly. Should we do it improperly and add a FIXME or leave it as is and add a FIXME?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we migrate this to a new issue? Because a merge of #353 would close #352 and #351 and all the other issues raised in #353 seem to be gone or declared as negligible.

@@ -0,0 +1,8 @@
call(a =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a line break if argument is split over multiple lines?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Comment thread R/indent.R Outdated
)

other_trigger_on_same_line <-
pd[remaining_row_idx_between_trigger_and_line_break, ]$token %in%

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pd$token[...] is slightly faster.

@lorenzwalthert

Copy link
Copy Markdown
Collaborator Author

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.

@krlmlr krlmlr mentioned this pull request Feb 23, 2018
lorenzwalthert added a commit that referenced this pull request Feb 24, 2018
@lorenzwalthert
lorenzwalthert force-pushed the indent-eq-formal branch 2 times, most recently from 214c868 to 2fbde82 Compare February 24, 2018 22:23
merge with master containing tests already by using the ours merge strategy (i.e. prefer indent-eq-formals whenever there is a conflict)
@krlmlr

krlmlr commented Mar 1, 2018

Copy link
Copy Markdown
Member

Agree to merge and migrate remaining problems to a new issue. The new output looks better than what we currently have.

@lorenzwalthert
lorenzwalthert merged commit b591a12 into r-lib:master Mar 1, 2018
@lorenzwalthert
lorenzwalthert deleted the indent-eq-formal branch March 2, 2018 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants