Skip to content

Enforcing a 80-character line length rule? #247

Open

Description

@jarodmeng

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.

Activity

  1. lorenzwalthert commented on Oct 22, 2017

    @lorenzwalthert
    Collaborator

    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.

  2. krlmlr commented on Oct 23, 2017

    @krlmlr
    Member

    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?

  3. jarodmeng commented on Oct 23, 2017

    @jarodmeng
    Author

    Thanks for the reply! I agree that it's not easy to implement such a rule. @krlmlr 's suggestion looks good to me.

  4. lorenzwalthert commented on Oct 23, 2017

    @lorenzwalthert
    Collaborator

    So do you @krlmlr suggest to take the 80 character limit from the (raw) unstyled expression, right?

  5. krlmlr commented on Oct 24, 2017

    @krlmlr
    Member

    No, that would be after styling, when we know the final width -- we'd just add linebreaks but not change much otherwise.

  6. lorenzwalthert commented on Nov 3, 2017

    @lorenzwalthert
    Collaborator

    Issues that can't be resolved soonish will be closed and labelled "Status: Postponed".

  7. lorenzwalthert commented on Feb 18, 2018

    @lorenzwalthert
    Collaborator

    Reference: #65.

  8. krivit commented on Apr 9, 2018

    @krivit

    @lorenzwalthert , I've implemented a version of this, by adding a loop to parse_transform_serialize to do the following:

    1. Run through the formatting and flattening process to obtain flattened_pd.
    2. 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.
    3. 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.
    4. 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.

  9. 19 remaining items

  10. lorenzwalthert commented on Apr 22, 2019

    @lorenzwalthert
    Collaborator

    @yech1990 sorry I can't follow. Can you make elaborate on this a bit more?

  11. y9c commented on Apr 22, 2019

    @y9c

    @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.

  12. lorenzwalthert commented on Apr 22, 2019

    @lorenzwalthert
    Collaborator

    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?

  13. added a commit that references this issue on May 19, 2020
  14. RobertMyles commented on Jun 30, 2020

    @RobertMyles

    Hi folks, just wondering what the status is on this issue? It would be a helpful addition :-)

    Thanks.

  15. lorenzwalthert commented on Jun 30, 2020

    @lorenzwalthert
    Collaborator

    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.

  16. RobertMyles commented on Jun 30, 2020

    @RobertMyles

    Ok, thanks!

  17. lorenzwalthert commented on Jun 30, 2020

    @lorenzwalthert
    Collaborator

    But I admit, the growing number of 👍 in the issue is motivating 😄

  18. added a commit that references this issue on Apr 13, 2021
  19. euklid321 commented on Jul 16, 2021

    @euklid321

    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.

  20. lorenzwalthert commented on Sep 11, 2021

    @lorenzwalthert
    Collaborator

    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.

  21. moritzpschwarz commented on Oct 4, 2021

    @moritzpschwarz

    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:

    ---
    title: "R Notebook"
    output: pdf_document
    header-includes:
      - |
        ```{=latex}
        \usepackage{fvextra}
        \DefineVerbatimEnvironment{Highlighting}{Verbatim}{
          breaksymbolleft={}, 
          showspaces = false,
          showtabs = false,
          breaklines,
          commandchars=\\\{\}
        }
        ```
    ---
    
    ```{r}
    knitr::opts_chunk$set(tidy="styler")
    ```
  22. added a commit that references this issue on Aug 24, 2023
  23. ssh352 commented on Jan 15, 2024

    @ssh352

    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?

    ❯ Rscript -e "styler::style_file(commandArgs(trailingOnly = TRUE),indent_by=4)" script.R
    
  24. maikol-solis commented on Apr 20, 2024

    @maikol-solis

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions