Skip to content

Braces in function calls #428

Closed
Closed

Description

@krlmlr

Can we keep the newline after ( here?

text <- '
eventReactive(
  {
    code()
  },
  {
    code()
  }
)
'

styler::style_text(text)
#> 
#> eventReactive({
#>   code()
#> }, {
#>   code()
#> })

Created on 2018-09-28 by the reprex package (v0.2.1)

Activity

  1. lorenzwalthert commented on Sep 28, 2018

    @lorenzwalthert
    Collaborator

    Maybe. I liked it that way more though, it's more compact. Is there a rule in the tidyverse style guide that discourages this use? Note that with strict = FALSE, the output is not as desired, i.e. there should be indention for the braces:

    text <- '
    eventReactive({
    
        code()
    },
        {
        code()
        }
        )
        '
    
    styler::style_text(text, strict = FALSE)
    #> 
    #> eventReactive({
    #> 
    #>   code()
    #> },    {
    #>   code()
    #> }
    #> )

    Created on 2018-09-28 by the reprex package (v0.2.1)

  2. krlmlr commented on Sep 29, 2018

    @krlmlr
    MemberAuthor

    It's more compact, but it's much less readable, because it's very easy to overlook the }, { . And when you find it, you wonder what the meaning is.

    I'm not aware of any specific rule.

  3. lorenzwalthert commented on Sep 30, 2018

    @lorenzwalthert
    Collaborator

    So then if you prefer another behavior, I suggest to propose/formalize a rule in tidyverse/style first :-). In the meantime, we nevertheless need to fix the brace indention with strict = FALSE, so I opened #430.

  4. krlmlr commented on Oct 1, 2018

    @krlmlr
    MemberAuthor

    We only have a contradicting rule in 2.4:

    Code that is related to a brace (e.g., an if clause, a function declaration, a trailing comma, …) must be on the same line as the opening brace.

    The two braces are not related, one could argue that they shouldn't be on the same line by negation.

    If we add additional arguments, indentation starts looking really weird, although no rule seems to specify this either:

    text <- '
    eventReactive(
      {
        code()
      },
      {
        code()
      },
      label = "label"
    )
    '
    
    styler::style_text(text)
    #> 
    #> eventReactive({
    #>   code()
    #> }, {
    #>   code()
    #> },
    #> label = "label"
    #> )

    Created on 2018-10-01 by the reprex package (v0.2.1.9000)

    The style guide seems to prefer brevity over comprehensiveness. If we want a comprehensive style guide, we could add an appendix where all the rules are formalized. Or we just use common sense and agreement :-o

  5. krlmlr commented on Oct 4, 2018

    @krlmlr
    MemberAuthor

    Also interesting:

    text <- '
    library(dplyr, warn.conflicts = FALSE)
    tibble(x = 1) %>% 
      mutate(y = { x <- 2; x }, z = x)
    '
    
    styler::style_text(text)
    #> 
    #> library(dplyr, warn.conflicts = FALSE)
    #> tibble(x = 1) %>%
    #>   mutate(y = {
    #>     x <- 2
    #>     x
    #>   }, z = x)

    Created on 2018-10-04 by the reprex package (v0.2.1.9000)

    I think that function arguments that consist of a braced expression always need to start on a new line, unless it's the last argument and all other arguments fit on the line of the function call:

    tryCatch(
      {
      },
      error = function(e) {
      }
    )
    
    with_envvar(c(ENV = "var"), {
    })
    
    test_that("test", {
    })
  6. krlmlr commented on Oct 14, 2018

    @krlmlr
    MemberAuthor

    Working on that Shiny code now, letting styler have its way even if I don't like the formatting because I'm tired of undoing... (And we called it "non-invasive" ;-) )

    Any chance we can achieve consensus here?

  7. lorenzwalthert commented on Oct 14, 2018

    @lorenzwalthert
    Collaborator

    Note that we pretty much had this discussion before: #125. Also, removing the rule remove_line_break_before_curly_opening() at first glance seems to result in the behavior you desire. However, the problem is that with that, we can't get a typical edge case right:

    test_that("xyz", {
      call()
    })

    I.e. in the above case, we'd move the opening { to the next line too. I think originally, for this reason, I decided to go for the more compact representation because it was simpler to implement.

    Because we'd need to set line break based on multi-line attribute, which is not easily possible currently, as outlined in #357.

  8. lorenzwalthert commented on Mar 15, 2019

    @lorenzwalthert
    Collaborator

    I think this is the rule that makes sense once #357 is resolve from above:

    function arguments that consist of a braced expression always need to start on a new line, unless it's the last argument and all other arguments fit on the line of the function call.

    Plus when they are named, they should start on the same line as the key and EQ_SUB:

    cjklkjl(x,
      y = {
        k
      }
    )
    
  9. lorenzwalthert commented on Sep 17, 2019

    @lorenzwalthert
    Collaborator

    It seems as we don't need #357 anyways because the line break is added in the same nest as the check for multi-line (via column lag_newlines) happens in other places. I opened #543.

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