Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify indentation rules if multiple apply at the same time #197

Open
AshesITR opened this issue Dec 2, 2022 · 4 comments
Open

Clarify indentation rules if multiple apply at the same time #197

AshesITR opened this issue Dec 2, 2022 · 4 comments

Comments

@AshesITR
Copy link

AshesITR commented Dec 2, 2022

Raised in r-lib/styler#1065

There are circumstances where both, a hanging / block indent for a function call and a block indent for line continuation of binary operators (e.g. pipes), are triggered on the same line of code.
Currently, lintr's indentation_linter() requires two levels of indent in such cases whereas styler only wants one.

Here are some code examples that need clarification on what the tidyverse style should be:

# multi-line if condition
if (long_conditional &&
      other_long_conditional) {
  conditionally_evaluated_code()
}
# vs.
if (long_conditional &&
  other_long_conditional) {
  conditionally_evaluated_code()
}

# multi-line if condition with pipe
if (long_conditional |>
    long_computation() |>
    other_long_computation()) {
  conditionally_evaluated_code()
}
# vs.
if (long_conditional |>
  long_computation() |>
  other_long_computation()) {
  conditionally_evaluated_code()
}

# (assuming hanging indent is allowed for function calls too)
# multi-line function call
fun(arg1 &&
      still_arg1,
    arg2)
# vs.
fun(arg1 &&
    still_arg1,
    arg2)

# multi-line function call with pipe
fun(arg1 |>
      arg1_processor1() |>
      arg1_processor2(),
    arg2)
# vs.
fun(arg1 |>
    arg1_processor1() |>
    arg1_processor2(),
    arg2)

cc @IndrajeetPatil, @lorenzwalthert

@lorenzwalthert
Copy link
Contributor

lorenzwalthert commented Dec 2, 2022

Thanks @AshesITR. Clarification for {styler} decision: The same indention is applied when working with RStudio interactively and {styler} currently usees the principle that indention per new line is at most one level.

@IndrajeetPatil
Copy link

IndrajeetPatil commented Dec 3, 2022

The same indention is applied when working with RStudio interactively

@lorenzwalthert I can't reproduce this. Maybe RStudio behaviour has changed in the meanwhile?
Now, the indentation is similar to what is expected by {lintr}.

rstudio-if.mov

RStudio IDE version details

Screenshot 2022-12-07 at 18 02 41

@hadley
Copy link
Member

hadley commented Aug 9, 2024

I think my informal advice would be "don't do that" 😄 Generally, where I encounter such code in a function, I'll try to refactor it by introducing a variable or a helper function. I can think about where to add such advice to the style guide.

@hadley
Copy link
Member

hadley commented Sep 30, 2024

I think a hanging indent looks fine inside of function calls:

fun(
  arg1 |>
    arg1_processor1() |>
    arg1_processor2(),
  arg2
)

And for if statements, @mine-cetinkaya-rundel suggested another form:

if (
    long_conditional &&
    other_long_conditional
) {
  conditionally_evaluated_code()
}

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

No branches or pull requests

4 participants