r-lib / styler

Non-invasive pretty printing of R code
https://styler.r-lib.org
Other
713 stars 70 forks source link

Question: How to customise styler so that line break before `) {` in function definitions? #1130

Closed rkrug closed 9 months ago

rkrug commented 1 year ago

Assume I have the following function definition, formatted in VS Code using the tidy verse styler:

addAttributes <- function(
    x,
    nm,
    puml) {
    cat(TRUE)
}

I would prefer to have the following:

addAttributes <- function(
    x,
    nm,
    puml
) {
    cat(TRUE)
}

How can I modify the tidyverse_style() so that it is formatted as I would like to have it, i.e. have a line break in front of the ) {, unless it is in the same line as the ( in the function definition?

Thanks,

Rainer

lorenzwalthert commented 1 year ago

I can’t tell you on top of my head but you should find the answer if you look at the docs. In particular https://styler.r-lib.org/articles/customizing_styler.html

krlmlr commented 1 year ago

I recently tried this on this particular use case. I ended up with the following script: https://rpubs.com/krlmlr/styler-resp .

I do prefer

x <- function(
    a,
    b
) {
}

over

x <- function(
    a,
    b) {
}

. This is also what the codegrip package emits -- a huge time saver. However, https://style.tidyverse.org/functions.html#long-lines-1 mandates the last form. This is unfortunate.

Because styler is about non-invasive pretty-printing, we should support keeping the first form. @lorenzwalthert: How do you feel about this, at least for strict = FALSE ?

Upstream: https://github.com/tidyverse/style/issues/202.

lorenzwalthert commented 1 year ago

a huge time saver.

Can you elaborate why? Maybe time is also an argument in the tidyverse style guide?

I think we should implement the style guide but for strict = FALSE I am open.

krlmlr commented 1 year ago

I chose to tweak codegrip instead: https://github.com/lionel-/codegrip/pull/16.

lorenzwalthert commented 9 months ago

Ok, also fine.

rkrug commented 9 months ago

But this does not help me as a lonely VSCode user...

lorenzwalthert commented 9 months ago

Sorry I can't support this in {styler}. It's not compliant with the tidyverse style guide...

rkrug commented 9 months ago

I understand - but Is there any styler option which makes this possible, as it states "styler formats your code according to the tidyverse style guide (or your custom style guide)" on https://styler.r-lib.org?

lorenzwalthert commented 9 months ago

Not with tidyverse_style(). The part of {styler} that makes it possible is the infrastructure to run you style guide. But you have to provide your own style guide. There is no option in tidyverse_style() for it.

krlmlr commented 9 months ago

@rkrug: See https://github.com/r-lib/styler/issues/1130#issuecomment-1646604751.