You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to enforce a rule to limit code to 80 character per line? styler currently doesn't seem to do that. The closest issue I can find is #118, but it's not clear if that option is available.
Reacted by Lluís Revilla, A. Domingues, Will Landau, Yuri Dias, Antoine Drouhin, Garrick Aden-Buie, Mike Williamson, Doug Manuel, Chang Ye, Miha Gazvoda and 88 more
Thanks for having consulted open issues first. #118 is an edge case as far as indention goes and not quite what you are referring to. The task of splitting lines of code with more than 80 characters is non-trivial as it is not a-priori clear at which position lines should be split. I think this may also not follow a general and abstract rule (as the other styling rules) but is best done manually and decided on a case-to-case basis. Therefore, this feature is currently no supported in styler and will probably no be so in the near future. We suggest to use the packages lintr to detect lines with more than 80 characters and rearrange manually.
Edit: I think we aim to support this in the long run.
I'm working on (finally!) releasing utf8, this means we could release styler soon. After this I think we could start thinking about that 80-character rule, because editing code from "wide" to "long" takes quite a lot of time (measured in seconds, but still). I'd like to suggest to do it at the level of function calls and arithmetic expressions first:
If a single-line function call hits the 80-character boundary, add a line break after the opening paren, and proceed with usual rules.
If an arithmetic expression is too wide, add line breaks after lowest-prio operators (i.e., the + in 1 * 2 + 3 * 4)
Maybe start with only function calls -- if we add a set of rules after all other rules (because we need to be sure about the width), and then do only reindention of function call arguments?
@lorenzwalthert , I've implemented a version of this, by adding a loop to parse_transform_serialize to do the following:
Run through the formatting and flattening process to obtain flattened_pd.
See if any of the tokens have an end column greater than the limit. If any do, note the first token with this property and add its pos_id to a list. If none do, render the text and exit.
Prepend a formatter (I call it add_line_break_at_pos_id constructed by mk_add_line_break_at_pos_id) to the line_break formatters. This formatter increments lag_newlines for the elements which either have one of the stored pos_ids or have a child that does.
Repeat from Step 1 with the additional formatter, until exit.
I've committed the implementation to a fork (4017b39). It's not pull-request-ready, and I would appreciate your input as to whether it's worth developing into something that is.
@lorenzwalthert
I can't find enough of R language formatter implementation to illustrate this problem. You can do the formattting test of the following python code with two different tools.
The input:
(The first line of the code exceed 80 characters)
if ((a>2) and (b>3) and (c>4) and (d>5) and (e>6) and (f>7) and (g>8) and (h>9)):
print("Yes!")
formatted by yapf
if ((a>2) and (b>3) and (c>4) and (d>5) and (e>6) and (f>7)
and (g>8) and (h>9)):
print("Yes!")
formatted by black
if (
(a>2)
and (b>3)
and (c>4)
and (d>5)
and (e>6)
and (f>7)
and (g>8)
and (h>9)
):
print("Yes!")
Both Implementation solve the problem.
The first tool break the long line in the furthest 'breakable' position within the limit. formatR, which use the base::deparse() method, do the formatting in this way.
The second tool breaks the line by 'grammar unite' (that is a judgment in this example). Break long line in the second way make the code nicer and much more readable. Meanwhile, it is helpful for editing the code, since you you delete a whole line (dd in vim) quickly for editing the conditions.
Thus, I wish styler can format ggplot2 or dplyr one line express in the smarter way. It is very common to remove and add new operations (eg, + geom_bar()) in writing ggplot2/dplyr code.
Reacted by Lluís Revilla, Mike Williamson, Salim B, Chang Ye, Robert Myles McDonnell and j
Ok, now I understand better, thanks. Since this is going to slow down styler, there must be a way to turn it off globally. I.e. one should be able to turn width = Inf.
I think to begin with, we could have a look at function calls (ggplot + calls are resolved with #544). I think they are 90% of the cases that are longer than 80 characters. If the input function call is longer than 80 characters, we could simply put each argument on a separate line. Also, we should probably not consider removing line breaks, just adding. We must make sure styling remains ideompetent, otherwise this will potentially lead to some troubles with caching. Also, it's a problem that line breaks at a character width depend on the spacing. And spacing comes after line breaks in the transformer order because indention is a spacing topic and depends on line breaks.
Edit: This might have simplified with #705, where we factored indention out of space transformers. Indention formatting is now potentially completely independent of space formatting.
Edit: we should think of other styler functionally that will help us speeding things up. Maybe stylerignore could be used as some sort of cache? Or base indention and then only style the expression that needs re-arrangement?
I am not currently working on that. We first need to outline conceptually what the implementation (or a first version) will look like. I think what I outlined in #247 (comment) is a good start.
But I admit, the growing number of 👍 in the issue is motivating 😄
Reacted by Janosch Linkersdörfer, Robert Myles McDonnell, Patrick Schratz, Salim B, Lluís Revilla, Korhan Kocak, Michael, Jack Wasey, Evgeni Chasnovski, Moritz Schwarz and 66 more
Hi guys, one year after the last question... 😉 is there any chance that this would be implemented in the near future? I've been using styler quite actively for a couple of months now and I think this is the only major missing piece.
Reacted by Sarah Endicott, Will Langdale, Robert Myles McDonnell, Tyrone DeSilva, Pavel N. Krivitsky, Lukas Erhard, Derek Schinke, Sono Shah, Raphaël Nussbaumer, wdkrnls and 4 more
I know this is high up on the wish list for many users but I anticipate this to be quite complex and time consuming to implement. Given other priorities in my life as well as in {styler}, I won't build this soon unfortunately. I hope you can understand this.
Reacted by Lluís Revilla, Derek Schinke and Douglas Ezra Morrison
For knitting PDF documents Carlos Luis Rivera posted a very useful answer on my StackOverflow post, which might be helpful for some people. It requires including the following to your header:
I use styler to format R code. Lintr gives me warning on lines on 80 characters line limit. However it seems styler has no option to specify max number of characters per line. I'd rather enforce the line width automatically instead of manually. How do R users enforce line width limit in Rstudio or in vim?
Activity
lorenzwalthert commentedon Oct 22, 2017
Thanks for having consulted open issues first. #118 is an edge case as far as indention goes and not quite what you are referring to. The task of splitting lines of code with more than 80 characters is non-trivial as it is not a-priori clear at which position lines should be split. I think this may also not follow a general and abstract rule (as the other styling rules) but is best done manually and decided on a case-to-case basis. Therefore, this feature is currently no supported in styler and will probably no be so in the near future. We suggest to use the packages lintr to detect lines with more than 80 characters and rearrange manually.
Edit: I think we aim to support this in the long run.
krlmlr commentedon Oct 23, 2017
I'm working on (finally!) releasing utf8, this means we could release styler soon. After this I think we could start thinking about that 80-character rule, because editing code from "wide" to "long" takes quite a lot of time (measured in seconds, but still). I'd like to suggest to do it at the level of function calls and arithmetic expressions first:
+in1 * 2 + 3 * 4)Maybe start with only function calls -- if we add a set of rules after all other rules (because we need to be sure about the width), and then do only reindention of function call arguments?
jarodmeng commentedon Oct 23, 2017
Thanks for the reply! I agree that it's not easy to implement such a rule. @krlmlr 's suggestion looks good to me.
lorenzwalthert commentedon Oct 23, 2017
So do you @krlmlr suggest to take the 80 character limit from the (raw) unstyled expression, right?
krlmlr commentedon Oct 24, 2017
No, that would be after styling, when we know the final width -- we'd just add linebreaks but not change much otherwise.
lorenzwalthert commentedon Nov 3, 2017
Issues that can't be resolved soonish will be closed and labelled "Status: Postponed".
lorenzwalthert commentedon Feb 18, 2018
Reference: #65.
Lines longer than option "width" are now warped. References r-lib#247.
krivit commentedon Apr 9, 2018
@lorenzwalthert , I've implemented a version of this, by adding a loop to
parse_transform_serializeto do the following:flattened_pd.pos_idto a list. If none do, render the text and exit.add_line_break_at_pos_idconstructed bymk_add_line_break_at_pos_id) to theline_breakformatters. This formatter incrementslag_newlinesfor the elements which either have one of the storedpos_ids or have a child that does.I've committed the implementation to a fork (4017b39). It's not pull-request-ready, and I would appreciate your input as to whether it's worth developing into something that is.
19 remaining items
lorenzwalthert commentedon Apr 22, 2019
@yech1990 sorry I can't follow. Can you make elaborate on this a bit more?
y9c commentedon Apr 22, 2019
@lorenzwalthert
I can't find enough of R language formatter implementation to illustrate this problem. You can do the formattting test of the following python code with two different tools.
The input:
(The first line of the code exceed 80 characters)
formatted by
yapfformatted by
blackBoth Implementation solve the problem.
formatR, which use thebase::deparse()method, do the formatting in this way.ddin vim) quickly for editing the conditions.Thus, I wish styler can format
ggplot2ordplyrone line express in the smarter way. It is very common to remove and add new operations (eg,+ geom_bar()) in writingggplot2/dplyrcode.lorenzwalthert commentedon Apr 22, 2019
Ok, now I understand better, thanks. Since this is going to slow down styler, there must be a way to turn it off globally. I.e. one should be able to turn width = Inf.
I think to begin with, we could have a look at function calls (ggplot + calls are resolved with #544). I think they are 90% of the cases that are longer than 80 characters. If the input function call is longer than 80 characters, we could simply put each argument on a separate line. Also, we should probably not consider removing line breaks, just adding. We must make sure styling remains ideompetent, otherwise this will potentially lead to some troubles with caching. Also, it's a problem that line breaks at a character width depend on the spacing. And spacing comes after line breaks in the transformer order because indention is a spacing topic and depends on line breaks.
Reference: https://github.com/gadenbuie/grkstyle/blob/master/R/style.R#L50
Edit: This might have simplified with #705, where we factored indention out of space transformers. Indention formatting is now potentially completely independent of space formatting.
Edit: we should think of other styler functionally that will help us speeding things up. Maybe stylerignore could be used as some sort of cache? Or base indention and then only style the expression that needs re-arrangement?
v1.25.1 -- Resolve Bioconductor#57
RobertMyles commentedon Jun 30, 2020
Hi folks, just wondering what the status is on this issue? It would be a helpful addition :-)
Thanks.
lorenzwalthert commentedon Jun 30, 2020
I am not currently working on that. We first need to outline conceptually what the implementation (or a first version) will look like. I think what I outlined in #247 (comment) is a good start.
RobertMyles commentedon Jun 30, 2020
Ok, thanks!
lorenzwalthert commentedon Jun 30, 2020
But I admit, the growing number of 👍 in the issue is motivating 😄
🐞fix(tarscript): simplify works
euklid321 commentedon Jul 16, 2021
Hi guys, one year after the last question... 😉 is there any chance that this would be implemented in the near future? I've been using styler quite actively for a couple of months now and I think this is the only major missing piece.
lorenzwalthert commentedon Sep 11, 2021
I know this is high up on the wish list for many users but I anticipate this to be quite complex and time consuming to implement. Given other priorities in my life as well as in {styler}, I won't build this soon unfortunately. I hope you can understand this.
moritzpschwarz commentedon Oct 4, 2021
For knitting PDF documents Carlos Luis Rivera posted a very useful answer on my StackOverflow post, which might be helpful for some people. It requires including the following to your header:
v1.25.1 -- Resolve #57
ssh352 commentedon Jan 15, 2024
I use styler to format R code. Lintr gives me warning on lines on 80 characters line limit. However it seems styler has no option to specify max number of characters per line. I'd rather enforce the line width automatically instead of manually. How do R users enforce line width limit in Rstudio or in vim?
maikol-solis commentedon Apr 20, 2024
Hi everyone, I opened the issue #1192, and I just realized it's a duplicate of this.
Should I close my issue, or not?
Thanks.