styler icon indicating copy to clipboard operation
styler copied to clipboard

Add newline after multiline function declaration

Open krlmlr opened this issue 7 years ago • 12 comments
trafficstars

# Good
f1 <- function(a, b) {
  code
}

f2 <- function(a, b,
               c) {

  code
}

# Bad
f3 <- function() {

  code
}

krlmlr avatar Mar 13 '18 16:03 krlmlr

Is there a corresponding rule in the tidyverse style or do you plan to add one?

lorenzwalthert avatar Mar 13 '18 16:03 lorenzwalthert

I remember Hadley suggested that an empty line after a long function header helps readability. I wouldn't formalize this as a rule yet, but I've seen this pattern a few times in dplyr.

krlmlr avatar Mar 13 '18 16:03 krlmlr

Ok. I think this is easy to implement. I just think for the sake of consistency, we should in general aim to keep the style guide in sync with styler.

lorenzwalthert avatar Mar 13 '18 16:03 lorenzwalthert

For a propper implementation, we'd need #357 because we want to add a line break dependent on whether a token is multi-line, which we cannot do if the multi-line attribute is not up-to date. The multi-line attribute is not up-to-date because it is only updated after all line break rules were applied.

lorenzwalthert avatar Mar 13 '18 16:03 lorenzwalthert

Blocked by #357.

lorenzwalthert avatar Mar 13 '18 16:03 lorenzwalthert

Implemented in mlr branch: https://github.com/lorenzwalthert/styler/commit/2a3fbc6fe278fbf9a09d229a49379d778536b5ed

lorenzwalthert avatar Jan 26 '19 12:01 lorenzwalthert

The implementation in the mlr branch now depends on the number of lines in the body.

lorenzwalthert avatar Apr 17 '19 15:04 lorenzwalthert

For a propper implementation, we'd need #357 because we want to add a line break dependent on whether a token is multi-line, which we cannot do if the multi-line attribute is not up-to date. The multi-line attribute is not up-to-date because it is only updated after all line break rules were applied.

Wrong. We need the exact line count, ~which we can compute ad-hoc within the transformer. It's as simple as in https://github.com/lorenzwalthert/styler/commit/d9f1b22f52437368446daf2c4c654481bf914784. Performance implications probably small for non-package code because only taking effect for nests that are function declarations. Implementing it otherwise is most likely not much faster (i.e. doing it with a visitor for all top-level tokens) because most function declarations are not nested.~ (apparently there are more nested cases where this is insufficient, see #773). This rule needs to be at the end of the line-break transformers because it depends on them.

lorenzwalthert avatar Apr 17 '19 15:04 lorenzwalthert

@krlmlr any specific examples of this pattern? :pray:

maelle avatar Oct 14 '21 13:10 maelle

For context I was looking into never removing the newline at the beginning of a function body if there was one, following a comment by @mpadge. :grin:

maelle avatar Oct 14 '21 13:10 maelle

I have tweaked the "bad" example in the original post.

Keeping an extra newline, at least in non-strict mode, doesn't sound too bad as a starter.

krlmlr avatar Oct 14 '21 15:10 krlmlr

I have tweaked the "bad" example in the original post.

That's what I had guessed it should be. :grin:

maelle avatar Oct 14 '21 16:10 maelle