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

run formatR::tidy_source() on deparsed code before styler #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cpsievert
Copy link
Collaborator

This updates styleText() to use formatR::tidy_source() before styler::style_text().

The latter doesn't seem to be quite conservative with introducing line breaks, but perhaps that will improve with time. Comparatively, formatR seems to be a more invasive will its line breaking.

@gadenbuie
Copy link
Member

Hi @cpsievert, I found this PR while looking for an easy way to enforce line length with styler. Just FYI, formatR doesn't work well with the pipe operator due to its dependence on deparse(). In general, if you're piping code through formatR::tidy_source() %>% styler::style_text() then styler can undo what formatR breaks, with the exception of comments included at the end of a line between pipe steps. Here's a small reprex that causes formatR::tidy_source() to throw an error. I think it's likely to be a common code style -- I ran into this problem on the first file from a collaborator that I tested.

library(dplyr)

mtcars %>% 
  mutate(mpg = mpg * 2) %>% # double mpg
  head()
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 1 42.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> 2 42.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> 3 45.6   4  108  93 3.85 2.320 18.61  1  1    4    1
#> 4 42.8   6  258 110 3.08 3.215 19.44  1  0    3    1
#> 5 37.4   8  360 175 3.15 3.440 17.02  0  0    3    2
#> 6 36.2   6  225 105 2.76 3.460 20.22  1  0    3    1

This style of code commenting causes a show-stopping error in formatR.

formatR::tidy_source(text = "
  mtcars %>% 
  mutate(mpg = mpg * 2) %>% # double mpg
  head()"
)
#> Error in base::parse(text = code, keep.source = FALSE): <text>:2:30: unexpected SPECIAL
#> 1: mtcars %>%
#> 2: mutate ( mpg = mpg * 2 ) %>% %InLiNe_IdEnTiFiEr%
#>                                 ^

But changing to this style of code commenting does work.

formatR::tidy_source(text = "
  mtcars %>% 
  # double mpg
  mutate(mpg = mpg * 2) %>%
  head()"
)
#> 
#> mtcars %>% # double mpg
#> mutate(mpg = mpg * 2) %>% head()

This section of the formatR docs explains how comments are handled behind the scenes.

Created on 2019-07-31 by the reprex package (v0.2.1)

@lorenzwalthert
Copy link

styler only adds /removes line breaks based on tokens, not based on the position of the token until r-lib/styler#247 is implemented.

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

Successfully merging this pull request may close these issues.

3 participants