r-lib / styler

Non-invasive pretty printing of R code
https://styler.r-lib.org
Other
728 stars 71 forks source link

Styler reformat comments at on the first column #944

Closed stefanoborini closed 2 years ago

stefanoborini commented 2 years ago

Not sure if it's a bug or a feature that can be configured, but I currently have code like this

f <- function() {
  code
  # this is a comment
  code
}

Which gets reformatted as

f <- function() {
  code
# this is a comment
  code
}

Is it possible to leave the comment at the same original indentation level?

lorenzwalthert commented 2 years ago

I can't reproduce current dev version of {styler}:

styler::cache_activate()
#> Using cache 1.7.0.9001 at /Users/lorenz/Library/Caches/org.R-project.R/R/R.cache/styler/1.7.0.9001.
styler::style_text('# this is a comment')
#> Warning: Could not use colored = TRUE, as the package prettycode is not
#> installed. Please install it if you want to see colored output or see `?
#> print.vertical` for more information.
#> # this is a comment
styler::style_text('cache2')
#> Warning: Could not use colored = TRUE, as the package prettycode is not
#> installed. Please install it if you want to see colored output or see `?
#> print.vertical` for more information.
#> cache2
styler::style_text('cache')
#> Warning: Could not use colored = TRUE, as the package prettycode is not
#> installed. Please install it if you want to see colored output or see `?
#> print.vertical` for more information.
#> cache
styler::style_text('
f <- function() {
  code2
  # this is a comment
  code
}
')
#> Warning: Could not use colored = TRUE, as the package prettycode is not
#> installed. Please install it if you want to see colored output or see `?
#> print.vertical` for more information.
#> 
#> f <- function() {
#>   code2
#>   # this is a comment
#>   code
#> }

Created on 2022-05-17 by the reprex package (v2.0.1) Please 1) update {styler} 2) if not working, clear cache.

stefanoborini commented 2 years ago

I am using styler as part of the pre-commit hooks. Not sure exactly how it works, but I suspect it installs its own styler?

I got them from here:

https://github.com/lorenzwalthert/precommit

which apparently it's using 1.7.0.9001

lorenzwalthert commented 2 years ago

It does, but the cache is shared. Without a reproducible examle, it's hard to fix the issue. So I recommend clearing the cache:

styler::cache_clear()
stefanoborini commented 2 years ago

Sorry sorry sorry my mistake. I had left a rogue setting --reindention=specify_reindention('#') in the pre-commit hook. Thank you for your help!