bpmn-visualization-R icon indicating copy to clipboard operation
bpmn-visualization-R copied to clipboard

[INFRA] Add a R/JS formater

Open csouchet opened this issue 3 years ago • 4 comments

For now, we have to manage formatting manually. In RStudio, select the file content and then do Ctrl + Shift + A

See this article for general guidelines: https://blog.r-hub.io/2022/03/21/code-style/

For formatting rules

  • https://cran.r-project.org/doc/manuals/R-ints.html#R-coding-standards
  • https://stackoverflow.com/questions/3017877/tool-to-auto-format-r-code
  • rmarkdown tidy: https://bookdown.org/yihui/rmarkdown-cookbook/opts-tidy.html

tooling candidates

  • lintr: https://github.com/r-lib/lintr and proposal for a github action https://github.com/r-lib/actions/issues/567
  • styler: https://github.com/r-lib/styler and workflow in action https://github.com/cynkra/dm/blob/main/.github/workflows/style/action.yml

IDE extension supporting autoformating

  • VSCode https://marketplace.visualstudio.com/items?itemName=Ikuyadeu.r search for 'formatting'
  • VSCode https://marketplace.visualstudio.com/items?itemName=Mikhail-Arkhipov.r formatting + litting (via lintr)

csouchet avatar Aug 30 '21 13:08 csouchet

For {golem}, we have the following formatter grkstyle::grk_style_transformer()

I suggest all {golem} contributor to add the following code to their .Rprofile.

# If you work in RStudio
options(styler.addins_style_transformer = "grkstyle::grk_style_transformer()")

# If you work in VSCode
options(languageserver.formatting_style = function(options) {
  grkstyle::grk_style_transformer()
})

This is a piece of my personal .Rprofile that does format the code every time I open VScode :

if (file.exists("~/.Rprofile") { 
	source("~/.Rprofile") 
}

cli::cat_rule("[.Rprofile] Sourcing project-wide RProfile")

options(styler.addins_style_transformer = "grkstyle::grk_style_transformer()")

options(languageserver.formatting_style = function(options) {
  grkstyle::grk_style_transformer()
})


try(
  {
    grkstyle::grk_style_pkg(".")
  },
  silent = TRUE
)

options(styler.cache_root = "styler-perm")

Given that the package is in a folder, you can enforce the formatting with a project Rprofile

ColinFay avatar Oct 12 '22 15:10 ColinFay

For the JS, I would suggest minifying too, for example with https://github.com/ColinFay/minifyr (requires nodejs)

ColinFay avatar Oct 12 '22 15:10 ColinFay

@ColinFay thanks for the information, we will review quickly. I have seen some formatting changes in your PR #117 probably because of the configuration you use. We definitively need to setup shared linting settings for all contributors.

tbouffard avatar Oct 12 '22 15:10 tbouffard

Ah, yes, I've turned on "Format on save" in VSCode :)

ColinFay avatar Oct 12 '22 15:10 ColinFay