style icon indicating copy to clipboard operation
style copied to clipboard

Clarify indentation rules if multiple apply at the same time

Open AshesITR opened this issue 3 years ago • 4 comments

Raised in https://github.com/r-lib/styler/issues/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

AshesITR avatar Dec 02 '22 05:12 AshesITR

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.

lorenzwalthert avatar Dec 02 '22 12:12 lorenzwalthert

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

https://user-images.githubusercontent.com/11330453/205431428-05783e8c-143c-4dfd-956c-d3f69fc06ce7.mov

RStudio IDE version details

Screenshot 2022-12-07 at 18 02 41

IndrajeetPatil avatar Dec 03 '22 08:12 IndrajeetPatil

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 avatar Aug 09 '24 12:08 hadley

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

hadley avatar Sep 30 '24 19:09 hadley