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

Don't require dplyr anywhere #1131

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Imports:
Suggests:
data.tree (>= 0.1.6),
digest,
dplyr,
here,
knitr,
prettycode,
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-cache-low-level-api.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
test_that("caching utils make right blocks with semi-colon", {
blocks_simple_uncached <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>%
dplyr::mutate(is_cached = FALSE) %>%
base::transform(is_cached = FALSE) %>%
cache_find_block()
expect_equal(blocks_simple_uncached, c(1, 1, 1, 1))

blocks_simple_cached <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>%
dplyr::mutate(is_cached = TRUE) %>%
base::transform(is_cached = TRUE) %>%
cache_find_block()
expect_equal(blocks_simple_cached, c(1, 1, 1, 1))

blocks_edge <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>%
dplyr::mutate(is_cached = c(TRUE, TRUE, FALSE, FALSE)) %>%
base::transform(is_cached = c(TRUE, TRUE, FALSE, FALSE)) %>%
cache_find_block()
expect_equal(blocks_edge, c(1, 2, 2, 2))
})
Expand All @@ -30,7 +30,7 @@ test_that("caching utils make right blocks with comments", {


blocks_simple_uncached <- compute_parse_data_nested(text) %>%
dplyr::mutate(is_cached = c(
base::transform(is_cached = c(
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE,
TRUE, FALSE, FALSE, FALSE
)) %>%
Expand All @@ -48,7 +48,7 @@ test_that("caching utils make right blocks with comments", {
tau1 = 1 # here?
"
blocks_simple_cached <- compute_parse_data_nested(text) %>%
dplyr::mutate(is_cached = c(
base::transform(is_cached = c(
FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE
)) %>%
cache_find_block()
Expand Down Expand Up @@ -101,17 +101,17 @@ test_that("blank lines are correctly identified", {

test_that("caching utils make right blocks with comments", {
blocks_simple_uncached <- compute_parse_data_nested(c("1 + 1", "2 # comment")) %>%
dplyr::mutate(is_cached = FALSE) %>%
base::transform(is_cached = FALSE) %>%
cache_find_block()
expect_equal(blocks_simple_uncached, c(1, 1, 1))

blocks_simple_cached <- compute_parse_data_nested(c("1 + 1", "2 # comment2")) %>%
dplyr::mutate(is_cached = TRUE) %>%
base::transform(is_cached = TRUE) %>%
cache_find_block()
expect_equal(blocks_simple_cached, c(1, 1, 1))

blocks_edge <- compute_parse_data_nested(c("1 + 1", "2 # 1+1")) %>%
dplyr::mutate(is_cached = c(TRUE, TRUE, FALSE)) %>%
base::transform(is_cached = c(TRUE, TRUE, FALSE)) %>%
cache_find_block()
expect_equal(blocks_edge, c(1, 2, 2))
})
Expand Down
10 changes: 5 additions & 5 deletions vignettes/customizing_styler.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ The `transformers` argument is, apart from the code to style, the key argument o

```{r, message = FALSE}
library("styler")
library("magrittr")
cache_deactivate()
library("dplyr")
names(tidyverse_style())
str(tidyverse_style(), give.attr = FALSE, list.len = 3)
```
Expand All @@ -50,17 +50,17 @@ As the name says, this function removes spaces after the opening parenthesis. Bu
string_to_format <- "call( 3)"
pd <- styler:::compute_parse_data_nested(string_to_format) %>%
styler:::pre_visit_one(default_style_guide_attributes)
pd$child[[1]] %>%
select(token, terminal, text, newlines, spaces)

cols <- c('token', 'terminal', 'text', 'newlines', 'spaces')
pd$child[[1]][, cols]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe subset(pd$child[[1]], select = cols)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or since we've magrittr:

pd$child[[1]] %>%
  subset(select = c(...))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know... the audience of the vignette is probably R developers who know subset() but I think it's one of these functions that are not used often anymore these days... ] is not very pretty here I know but probably easier to understand for most.

```

`default_style_guide_attributes()` is called to initialize some variables, it does not actually transform the parse table.

All the function `remove_space_after_opening_paren()` now does is to look for the opening bracket and set the column `spaces` of the token to zero. Note that it is very important to check whether there is also a line break following after that token. If so, `spaces` should not be touched because of the way `spaces` and `newlines` are defined. `spaces` are the number of spaces after a token and `newlines`. Hence, if a line break follows, spaces are not EOL spaces, but rather the spaces directly before the next token. If there was a line break after the token and the rule did not check for that, indention for the token following `(` would be removed. This would be unwanted for example if `use_raw_indention` is set to `TRUE` (which means indention should not be touched). If we apply the rule to our parse table, we can see that the column `spaces` changes and is now zero for all tokens:

```{r}
styler:::remove_space_after_opening_paren(pd$child[[1]]) %>%
select(token, terminal, text, newlines, spaces)
styler:::remove_space_after_opening_paren(pd$child[[1]])[, cols]
```

All top-level styling functions have a `style` argument (which defaults to `tidyverse_style`). If you check out the help file, you can see that the argument `style` is only used to create the default `transformers` argument, which defaults to `style(...)`. This allows for the styling options to be set without having to specify them inside the function passed to `transformers`.
Expand Down