Skip to content

{ / } and line breaks in function calls #125

Closed

Description

@lorenzwalthert

The tidyverse style guide states in section 2.4 that

A } should always go on its own line, unless it’s followed by else or ).

So that's why we implemented remove_line_break_before_round_closing() which is responsible for
the line breaks removed in tikzDevice.

I personally think that's fine, but we may only include this rule if strict = TRUE.

Also it says that a { should never go on its own line and should always be followed by a new line, so

tryCatch(
{
  ...
} 

from the above-referenced commit is in my eyes not compliant with the tidyverse style guide.

Activity

  1. changed the title [-]keep line break between } and )?[/-] [+]{ / } and line breaks[/+] on Aug 14, 2017
  2. krlmlr commented on Aug 14, 2017

    @krlmlr
    Member

    The rules might need a refinement. In this example, the closing brace } belongs to an argument that is indented already, which is why indenting the closing parenthesis looks wrong:

    # good
    fun(arg, {
      block
    })
    
    fun(
      arg,
      {
        block
      }
    )
    
    fun(
      {
        block
      },
      arg
    )
    
    # bad
    
    fun({
      block
    },
    {
      another block
    })
    
    fun(
      arg,
      {
        another block
      })
  3. lorenzwalthert commented on Aug 14, 2017

    @lorenzwalthert
    CollaboratorAuthor

    I am not sure whether I understand you correctly. Why is

    fun(
      arg,
      {
        block
      }
    )

    a good example with regard to the second rule cited? Shouldn't it be

    fun(
      arg, {
        block
      }
    )

    or even

    fun(
      arg, {
        block
    })

    Also

    fun(
      arg,
      {
        another block
      })

    is bad, I agree, but I thought

    fun(
      arg, {
        another block
    })

    would be correct, so } is unindented since a ) follows.

  4. krlmlr commented on Aug 14, 2017

    @krlmlr
    Member

    I haven't found a specification for long function calls in the tidyverse guide. Maybe we could contribute one, that also covers complex function arguments that consists of whole blocks?

    No need to hurry though, I'm fine with this limitation for now.

  5. added a commit that references this issue on Aug 17, 2017
  6. krlmlr commented on Aug 17, 2017

    @krlmlr
    Member

    Attempt to formulate a line-break rule:

    A multi-line argument to a function must start on a new line, unless it's the last argument (like test_that(..., {}) and all other arguments fit on one line.

    A function call must either fit on one line, or have a line break after the opening ( with the arguments indented. Exceptions are switch() and ifelse()/if_else(), and calls where the only multi-line argument is the last argument.

    Example: https://github.com/krlmlr/styler/issues/125#issuecomment-322282812

    # good
    switch(var,
      case1 = ,
      case2 = "val",
      "default_val"
    )
    
    switch(var,
      case1 = {
      },
      case2 = {
      },
      {
      }
    )
    
    if_else(cond,
      true_val,
      false_val,
      na_val
    )
    
    regular_call(
      usually,
      all,
      arguments,
      go,
      on,
      a,
      separate,
      line,
      maybe,
      only,
      `if`,
      strict = TRUE
    )
    
    regular_call(all, args, fit)
    
    # bad
    regular_call(usually, all,
      arguments, ...)
    
    regular_call(
      usually, all,
      arguments, ...)
  7. krlmlr commented on Aug 17, 2017

    @krlmlr
    Member

    Another potential exception, perhaps if all arguments are multi-line?

    tryCatch({
      block
    },
    error = function(e) {
      handler
    },
    finally = {
      cleanup
    })

    Maybe not for the tidyverse style, but it looks like a valid option for other styles.

  8. added a commit that references this issue on Aug 17, 2017
  9. lorenzwalthert commented on Aug 22, 2017

    @lorenzwalthert
    CollaboratorAuthor

    I suggested a function to add a line break after the opening parenthesis if the expression is a function call, see https://github.com/krlmlr/styler/issues/118#issuecomment-322052062. Should we implement that for the CRAN milestone or not? Otherwise,

    long_call(1, 
              k * 3)

    Will become just

    long_call(1, 
      k * 3)
  10. krlmlr commented on Aug 22, 2017

    @krlmlr
    Member

    Did you mean

    long_call(
      1,
      k * 3
    )

    ?

  11. lorenzwalthert commented on Aug 22, 2017

    @lorenzwalthert
    CollaboratorAuthor

    In the meanwhile, I implemented #149, which does what you wrote, yes. Otherwise, just removing the rule we had (update_indention_ref_fun_dec()) would result in what I wrote.

  12. changed the title [-]{ / } and line breaks[/-] [+]{ / } and line breaks in function calls[/+] on Aug 24, 2017
  13. added a commit that references this issue on Aug 24, 2017
  14. lorenzwalthert commented on Oct 25, 2017

    @lorenzwalthert
    CollaboratorAuthor

    Reference: #254.

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