styler
styler copied to clipboard
Decide and implement double indent for function definitions
https://github.com/tidyverse/style/pull/174, and in particular the addin of @lionel- to toggle styles.
Is there a workaround for the double indent style ?
In #919, @lorenzwalthert said
but as far as double indent goes, you can't customize styler
So, before diving into customization and bringing my own style just for such as a small issue, I'd prefer asking here.
I'm so used to what Prettier does that I'm probably a bit biased, but I think that this is strange :
a_function_quite_long <- function(arg_a = 0,
arg_b = 0,
arg_c = 0,
arg_d = TRUE,
arg_e = 0) {
}
function_quite_long <- function(arg_a = 0,
arg_b = 0,
arg_c = 0,
arg_d = TRUE,
arg_e = 0) {
}
wrapper <- function(arg_a = 0,
arg_b = 0,
arg_c = 0,
arg_d = TRUE,
arg_e = 0) {
}
wrapper_with_other_defaults <- function(arg_a = 0,
arg_b = 1,
arg_c = 2,
arg_d = FALSE,
arg_e = 0) {
}
..and this is clean :
a_function_quite_long <- function(
arg_a = 0,
arg_b = 0,
arg_c = 0,
arg_d = TRUE,
arg_e = 0) {
}
function_quite_long <- function(
arg_a = 0 ,
arg_b = 0,
arg_c = 0,
arg_d = TRUE,
arg_e = 0) {
}
wrapper <- function(
arg_a = 0,
arg_b = 0,
arg_c = 0,
arg_d = TRUE,
arg_e = 0) {
}
wrapper_with_other_default <- function(
arg_a = 0,
arg_b = 1,
arg_c = 2,
arg_d = FALSE,
arg_e = 0) {
}
Anyway, very useful package to clean up the mess in my early projects :/ thanks
{styler} won't probably ever support single indent for function declarations as part of the default style guide because it's against the tidyverse style guide. Once this issue is closed, we'll support double indention though. You have to modify the tidyverse style guide manually, to only use the ( as a trigger for indention, then it should work):
code <- "wrapper_with_other_default <- function(
arg_a = 0,
arg_b = 1,
arg_c = 2,
arg_d = FALSE,
arg_e = 0) {
}"
sg <- styler::tidyverse_style()
sg$indention$unindent_fun_dec <- NULL
sg$indention$update_indention_ref_fun_dec <- NULL
sg$line_break$remove_line_breaks_in_fun_dec <- NULL
styler::style_text(code, transformers = sg)
#> 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.
#> wrapper_with_other_default <- function(
#> arg_a = 0,
#> arg_b = 1,
#> arg_c = 2,
#> arg_d = FALSE,
#> arg_e = 0) {
#> }
Created on 2022-03-21 by the reprex package (v2.0.1)
You can pack these modifications into a new style guide if you think that's a good idea, as described in the various vignettes for developers.
Ok, thanks.
So I do the "one line per arg", styler does the rest.
I've added this to my .Rprofile, as I use styler through coc-r-lsp, which uses REditorSupport/languageserver
options(languageserver.formatting_style = function(options) {
style <- styler::tidyverse_style()
style$indention$unindent_fun_dec <- NULL
style$indention$update_indention_ref_fun_dec <- NULL
style$line_break$remove_line_breaks_in_fun_dec <- NULL
style
})
Ok, good. That will work for language server based formatting only though, not for style_file() and friends or third-party integrations like {precommit}.