styler icon indicating copy to clipboard operation
styler copied to clipboard

Aligning function parameters when a parameter has c()

Open armenic opened this issue 7 months ago • 3 comments

Dear developers and the community,

I generate the following function programmatically (please see the reprex below) and I wish to format it in a way that all parameters are aligned. Unfortunately, as you can see from the reprex the arg3 and arg4 are placed after the comma of the previous argument. I also provide the desired format. Thank you so much for your time and help 🙏

Desired format

  • option 1
my_fun(
  arg1 = "val1",
  arg2 = "val2",
  arg3 = c(
    "val3",
    "val4"
  ),
  arg4 = c(
    "val5",
    "val6"
  )
)
  • option 2
  • interestingly in RStudio the CTRL+SHIFT+A makes this format and I am not sure whether it is using styler behind the scene.
my_fun(
  arg1 = "val1",
  arg2 = "val2",
  arg3 = c("val3", "val4"),
  arg4 = c("val5", "val6")
)

Reprex

library(rlang)
library(styler)

args_l <- list(
  arg1 = "val1",
  arg2 = "val2",
  arg3 = c("val3", "val4"),
  arg4 = c("val5", "val6")
)

call1 <- rlang::call2(
  "my_fun",
  !!!args_l
)

txt <- rlang::expr_text(call1, width = 20)
styler::style_text(txt)
#> my_fun(
#>   arg1 = "val1",
#>   arg2 = "val2", arg3 = c(
#>     "val3",
#>     "val4"
#>   ), arg4 = c(
#>     "val5",
#>     "val6"
#>   )
#> )

Created on 2025-05-23 with reprex v2.0.2

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 (2024-06-14)
#>  os       Ubuntu 22.04.5 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  C
#>  ctype    en_US.UTF-8
#>  tz       UTC
#>  date     2025-05-23
#>  pandoc   3.5 @ /opt/conda/bin/ (via rmarkdown)
#>  quarto   1.5.57 @ /usr/local/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.4   2025-02-13 [1] RSPM (R 4.4.0)
#>  crayon        1.5.3   2024-06-20 [1] RSPM (R 4.4.0)
#>  digest        0.6.37  2024-08-19 [1] RSPM (R 4.4.0)
#>  evaluate      1.0.3   2025-01-10 [1] RSPM (R 4.4.0)
#>  fastmap       1.2.0   2024-05-15 [1] RSPM (R 4.4.0)
#>  fs            1.6.6   2025-04-12 [1] RSPM (R 4.4.0)
#>  glue          1.8.0   2024-09-30 [1] RSPM (R 4.4.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] RSPM (R 4.4.0)
#>  knitr         1.50    2025-03-16 [1] RSPM (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] RSPM (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] RSPM (R 4.4.0)
#>  prettycode    1.1.0   2019-12-16 [1] RSPM (R 4.4.0)
#>  purrr         1.0.4   2025-02-05 [1] RSPM (R 4.4.0)
#>  R.cache       0.16.0  2022-07-21 [1] RSPM (R 4.4.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] RSPM (R 4.4.0)
#>  R.oo          1.27.0  2024-11-01 [1] RSPM (R 4.4.0)
#>  R.utils       2.13.0  2025-02-24 [1] RSPM (R 4.4.0)
#>  reprex        2.0.2   2022-08-17 [1] RSPM (R 4.4.1)
#>  rlang       * 1.1.6   2025-04-11 [1] RSPM (R 4.4.0)
#>  rmarkdown     2.29    2024-11-04 [1] RSPM (R 4.4.0)
#>  rstudioapi    0.17.1  2024-10-22 [1] RSPM (R 4.4.0)
#>  sessioninfo   1.2.3   2025-02-05 [1] RSPM (R 4.4.0)
#>  styler      * 1.10.3  2024-04-07 [1] RSPM (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] RSPM (R 4.4.0)
#>  withr         3.0.2   2024-10-28 [1] RSPM (R 4.4.0)
#>  xfun          0.52    2025-04-02 [1] RSPM (R 4.4.0)
#>  yaml          2.3.10  2024-07-26 [1] RSPM (R 4.4.0)
#> 
#>  [1] /home/manukyae/rlibs
#>  [2] /home/manukyae/R/x86_64-pc-linux-gnu-library/4.4
#>  [3] /usr/local/lib/R/site-library
#>  [4] /usr/lib/R/site-library
#>  [5] /usr/lib/R/library
#>  * ── Packages attached to the search path.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

armenic avatar May 23 '25 17:05 armenic

Hi, thanks for the detailed explanation and reprex. The problem stems from using width = 20 in rlang::expr_text(). This already results in line breaks within the function signature. After that, {styler} breaks no lines inside the function signatures. To do that, we could either detect if there is any line break within the signature and then break after every argument or check if there are line breaks between the arguments (i.e. without checking if there are line breaks within, say arg1 = c(1, 2, 3)) and then add a line break after every argument. I don't have time to work on that though.

lorenzwalthert avatar May 26 '25 07:05 lorenzwalthert

Thanks so much @lorenzwalthert for looking into this. I assume that there is no workaround for this.

armenic avatar May 26 '25 13:05 armenic

Well you can play with with or just manually insert a line break before passing the text to styler (which is difficult if you don't parse the code, since you can have code like arg1 = "this, then somethign", where the , does not necessarily signal a separator of your arguments.

lorenzwalthert avatar May 26 '25 14:05 lorenzwalthert