Skip to content

Indendation of low-precedence infix operators #1150

Open

Description

@AshesITR

Related to: r-lib/lintr#1718

y ~
  x1 +
    x2

Has parse tree ~(y, +(x1, x2)) so the ~ ends in line 3 and should thus cause an indentation for lines 2 and 3.
+ ends in line 3 and should cause another level of indentation on line 3.

styler::style_text("
y ~
  x1 +
    x2
")
#> y ~
#>   x1 +
#>   x2

Created on 2023-09-15 with reprex v2.0.2

Activity

  1. MichaelChirico commented on Sep 15, 2023

    @MichaelChirico
    Contributor

    The style guide doesn't really say much about indentation. Perhaps we could raise that; meanwhile, editing code that doesn't violate anything in the style guide generally goes against styler's intention, right?

  2. AshesITR commented on Sep 15, 2023

    @AshesITR
    Author

    Note that a + b + c is (a + b) + c whereas a ~ b + c is a ~ (b + c).
    Therefor, indentation shouldn't be the same for the two.

    styler::style_text("
    y +
      x1 +
      x2
    ")
    #> y +
    #>   x1 +
    #>   x2

    Created on 2023-09-15 with reprex v2.0.2

  3. MichaelChirico commented on Sep 15, 2023

    @MichaelChirico
    Contributor

    I think "should"/"shouldn't" can only be ruled by the style guide (for our defaults, at least).

    If the style guide is silent and we're trying to be consistent, we should come to an agreement after discussion.

    Reasoning in terms of "mixed precedence" makes sense to me. There's a related styler issue I've raised: tidyverse/style#160. ?Syntax lists precedence groups, we could use that as an objective measure.

  4. HaHeho commented on Jan 29, 2024

    @HaHeho

    I can add the following similar example that yields malformed code (the input is the correctly formatted code that I would expect).

    > styler::style_text("
    + stats::model.matrix(
    +   ~ a
    +   + b
    +   + c,
    +   dd
    + )
    + ")
    stats::model.matrix(
      ~ a
      + b
        + c,
      dd
    )

    Note that considerations around the operator precedence do not apply here since they don't actually function as mathematical operators in this context. I believe something similar may arise around the ggplot2::facet_grid function.

    EDIT:
    For reference, the built-in formatter of R Studio yields

    stats::model.matrix(~ a
                        + b
                        + c,
                        dd)
  5. lorenzwalthert commented on Feb 14, 2024

    @lorenzwalthert
    Collaborator

    Note that considerations around the operator precedence do not apply here since they don't actually function as mathematical operators in this context

    I don’t think we can differentiate these cases here in styler. Also, the issue caused some headache already in the past and I can’t recall the reasons for tie current implementation and if your representative was intentional or not (but it could probably be for d searching the issues) and given that the style guide is silent on it and the syntax is generally not used very often, this has low priority for me.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions