Skip to content

Commit 15689bb

Browse files
Merge pull request #308 from r-lib/f-no-spacing-caret
- Avoid spaces around ^ (#308).
2 parents 49621d9 + 0c7e298 commit 15689bb

File tree

13 files changed

+66
-62
lines changed

13 files changed

+66
-62
lines changed

API

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention())
66
default_style_guide_attributes(pd_flat)
7-
specify_math_token_spacing(zero = NULL, one = c("'+'", "'-'", "'*'", "'/'", "'^'"))
7+
specify_math_token_spacing(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'"))
88
specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE)
99
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, exclude_files = NULL)
1010
style_file(path, ..., style = tidyverse_style, transformers = style(...))

R/rules-spacing.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ set_space_around_op <- function(pd_flat) {
2727
#' @param zero Character vector of tokens that should be surrounded with zero
2828
#' spaces.
2929
style_space_around_math_token <- function(strict, zero, one, pd_flat) {
30+
# We remove spaces for zero (e.g., around ^ in the tidyverse style guide)
31+
# even for strict = FALSE to be consistent with the : operator
3032
pd_flat %>%
31-
style_space_around_token(strict, zero, 0L) %>%
32-
style_space_around_token(strict, one, 1L)
33+
style_space_around_token(strict = TRUE, zero, 0L) %>%
34+
style_space_around_token(strict = strict, one, 1L)
3335
}
3436

3537
#' Set spacing of token to a certain level

R/style_guides.R

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,20 +289,15 @@ character_to_ordered <- function(x, levels, name = substitute(x)) {
289289
#' math_token_spacing = specify_math_token_spacing(zero = "'+'"),
290290
#' strict = TRUE
291291
#' )
292-
#' style_text(
293-
#' "1+1 -3",
294-
#' math_token_spacing = tidyverse_math_token_spacing(),
295-
#' strict = TRUE
296-
#' )
297292
#' @name math_token_spacing
298293
NULL
299294

300295
#' @describeIn math_token_spacing Allows to fully specify the math token
301296
#' spacing.
302297
#' @export
303298
specify_math_token_spacing <-
304-
function(zero = NULL,
305-
one = c("'+'", "'-'", "'*'", "'/'", "'^'")) {
299+
function(zero = "'^'",
300+
one = c("'+'", "'-'", "'*'", "'/'")) {
306301
assert_tokens(c(one, zero))
307302
lst(
308303
one = setdiff(c(math_token, one), zero),
@@ -317,9 +312,17 @@ specify_math_token_spacing <-
317312
#' style_text(
318313
#' "1+1 -3",
319314
#' math_token_spacing = tidyverse_math_token_spacing(),
315+
#' strict = FALSE
316+
#' )
317+
#' style_text(
318+
#' "1+1 -3",
319+
#' math_token_spacing = tidyverse_math_token_spacing(),
320320
#' strict = TRUE
321321
#' )
322322
#' @export
323323
tidyverse_math_token_spacing <- function() {
324-
specify_math_token_spacing(one = c("'+'", "'-'", "'*'", "'/'", "'^'"))
324+
specify_math_token_spacing(
325+
zero = "'^'",
326+
one = c("'+'", "'-'", "'*'", "'/'")
327+
)
325328
}

man/math_token_spacing.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/indention_multiple/overall-in.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ a <- function(x) {
1717
c(
1818
list(x + 2),
1919
c(c(
20-
26 ^ 2, # FIXME ^ operator has to be surrounded by one space (or none?!), never multiple
20+
26 ^ 2,
2121
8,
2222
7
2323
)))

tests/testthat/indention_multiple/overall-in_tree

Lines changed: 39 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/indention_multiple/overall-out.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ a <- function(x) {
1717
c(
1818
list(x + 2),
1919
c(c(
20-
26 ^ 2, # FIXME ^ operator has to be surrounded by one space (or none?!), never multiple
20+
26^2,
2121
8,
2222
7
2323
))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1 + +1 - 3 / 23 * 3 ^ 4
1+
1 + +1 - 3 / 23 * 3^4
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1 + +1 - 3 / 23 * 3 ^ 4
1+
1 + +1 - 3 / 23 * 3^4

tests/testthat/strict/non_strict-out.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test <- function() {
6565
a > b
6666
a * b
6767
a / b
68-
a ^ b
68+
a^b
6969
a & b
7070
a | b
7171
a := b

0 commit comments

Comments
 (0)