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

Indendation of low-precedence infix operators #1150

Open
AshesITR opened this issue Sep 15, 2023 · 5 comments
Open

Indendation of low-precedence infix operators #1150

AshesITR opened this issue Sep 15, 2023 · 5 comments

Comments

@AshesITR
Copy link

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

@MichaelChirico
Copy link
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?

@AshesITR
Copy link
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

@MichaelChirico
Copy link
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.

@HaHeho
Copy link

HaHeho commented Jan 29, 2024

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)

@lorenzwalthert
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants