Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storing styler config in a config file / DESCRIPTION / other #319

Open
lorenzwalthert opened this issue Jan 7, 2018 · 10 comments
Open

storing styler config in a config file / DESCRIPTION / other #319

lorenzwalthert opened this issue Jan 7, 2018 · 10 comments

Comments

@lorenzwalthert
Copy link
Collaborator

lorenzwalthert commented Jan 7, 2018

Repo may include a file fallbacks.yaml that contains a configuration for styling, like this:

scope: tokens
strict: True
indent_by: 2
reindention: !expr styler::tidyverse_reindention()
math_token_spacing: !expr styler::tidyverse_math_token_spacing()
filetype: !expr c("r", "rmd")

When styler::style_pkg() is run, such a config file is looked for in the root directory and if one is found, the options specified there should be given precedence over what is specified in the function call. Or something like that. WIP @ https://github.com/lorenzwalthert/styler/tree/config

@lorenzwalthert
Copy link
Collaborator Author

Maybe also non-hidden styler.yaml. Also needs new infrastructure in root directory as #320.

@pat-s
Copy link
Contributor

pat-s commented Nov 21, 2018

Are there any plans to tackle this again in the future? The ideal case would be that RStudio also picks up this settings file.

@lorenzwalthert
Copy link
Collaborator Author

lorenzwalthert commented Jan 6, 2019

After some more thoughts on this, I felt like the whole idea of argument value resolution using defaults and config files should be handled by another package since it seems a rather generic problem. Hence, I created the package fallback. I also created a styler branch where we could test this: https://github.com/lorenzwalthert/styler/tree/config. Inspired by reprex, the syntax is:

# I changed the defaults in the styler source code for the arguments
tidyverse_style <- function(strict = fallback(TRUE), ...) {
  # ...
}

# the user can, as before:
style_text("1+1", style = tidyverse_style)
#> [details on resolution omitted]

Which basically means

check config files in working directory and home directory for strict. If you don't find it, use TRUE. More information in the package repo.

The user, of course, can override all defaults. Here is the trace with the most verbose output.

style_text("1+1", style = tidyverse_style, strict = FALSE)
declaring argument scope 
#> ● trying ./config.yaml: ✔ success (tokens)
#> declaring argument strict 
#> ● resorting to literal input value:  ✔ success (FALSE)
#> declaring argument indent_by 
#> ● trying ./config.yaml: ✔ success (2)
#> declaring argument start_comments_with_one_space 
#> ● trying ./config.yaml: ✖ failed (key does not exist in source file)
#> ● trying ~/config.yaml: ✖ failed (source file does not exist)
#> ● resorting to terminal fallback value: ✔success (FALSE)
#> declaring argument reindention 
#> ● trying ./config.yaml: ✔ success (NULL, 0, TRUE)
#> declaring argument math_token_spacing 
#> ● trying ./config.yaml: ✔ success (c("'+'", "'-'", "'*'", "'/'"), '^')

This is early alpha, all feedback welcomed, breaking changes too. @krlmlr If you can help me conceptually for the API / naming conventions / argument order ect, dropping a few comments in an issue in lorenzwalthert/fallback I would really appreciate it.

@lorenzwalthert
Copy link
Collaborator Author

Also, consider how other formatters have solved this problem, e.g. https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/

@lorenzwalthert
Copy link
Collaborator Author

lorenzwalthert commented May 3, 2019

This is also relevant for the save on styling functionality provided via an environment variable ($save_after_styling) without namespace or language prefix.

@lorenzwalthert
Copy link
Collaborator Author

lorenzwalthert commented Apr 18, 2020

Until we make progress here (if at all), you can also use RStudio snippets to store your preferred styler command, e.g. with

snippet style
	styler::style_file(${1:file}, strict = TRUE, scope = "spaces", indent_by = 4)

You can type style in the RStudio console and it would expand to the command above. Just to save typing.

@pat-s
Copy link
Contributor

pat-s commented Apr 18, 2020

Note that you can also run R code within a snippet. The placeholder inside the snippet could default to the file that is currently opened in your editor.

Then it almost behaves like a keybinding without the need to specify the file again 🙂

@eitsupi
Copy link

eitsupi commented Apr 2, 2022

Hello.
As with many auto-formatters, it would be great to be able to change the behavior in the definition file.
It looks like you haven't made progress for a while, do you have something like a todo list?

Thanks for all the great work you do.

@lorenzwalthert
Copy link
Collaborator Author

I think I’d prefer to solve this outside {styler}, eg with {fallback} as referenced above. I am happy to transfer the repo to someone else if they want to continue work on it, because I don’t have time.

@lorenzwalthert
Copy link
Collaborator Author

Maybe {renv} would want to expose their mechanism so we don't have to re-invent the wheel? Upvotes welcome 😀 rstudio/renv#1865

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants