Skip to content

Indention with multiple tokens starting (but not ending) on same line #485

Closed

Description

@lorenzwalthert

Multiple tokens that cause indention on the same line with at least one token causing indention over more than one line are problematic if the unindention they cause is not on the same line. This is because some lines might not be indented at all which looks odd in practice.

text <- c(
  "a <- 3",
  "b <- 5",
  "c <- purrr::map(a|",
  "b,",
  "a = 1,",
  "c = 2",
  ")"
)
styler::style_text(text)
#> a <- 3
#> b <- 5
#> c <- purrr::map(a |
#>   b,
#> a = 1,
#> c = 2
#> )

Created on 2019-03-15 by the reprex package (v0.2.1)

In the example above, the opening brace does not cause indention because | will. But the scope of the indention of | ends on the next line, leaving the remainder unindented, although it is within an expression that needs indention.

Easy fix: Don't allow multiple indention tokens on the same line if their indention scope does not end on the same line but then break the line.

purrr::map(
  a | 
    b, 
  a = 1, 
  c = 2
)

The same with braces, i.e. the following is not nice:

{{
  x
}
}

But the easy fix is to move the second { one line down. Similar to #428, but just that this issue also covers indention of tokens other than braces, but when you look at the examples in #428, I think it becomes evident that the brace case is much more common. This issue is probably also blocked by #357.

Activity

  1. lorenzwalthert commented on Aug 15, 2022

    @lorenzwalthert
    CollaboratorAuthor

    Probably fixed at least for some cases with #975:

    library(styler)
    text <- c(
      "a <- 3",
      "b <- 5",
      "c <- purrr::map(a|",
      "b,",
      "a = 1,",
      "c = 2",
      ")"
    )
    style_text(text)
    #> a <- 3
    #> b <- 5
    #> c <- purrr::map(
    #>   a |
    #>     b,
    #>   a = 1,
    #>   c = 2
    #> )

    Created on 2022-08-15 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions