Skip to content

Commit 33fdf56

Browse files
Merge 5b049c7 into a760cfb
2 parents a760cfb + 5b049c7 commit 33fdf56

14 files changed

+21
-307
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ License: MIT + file LICENSE
2323
URL: https://github.com/r-lib/styler, https://styler.r-lib.org
2424
BugReports: https://github.com/r-lib/styler/issues
2525
Depends:
26-
R (>= 3.5.0)
26+
R (>= 3.6.0)
2727
Imports:
2828
cli (>= 3.1.1),
2929
magrittr (>= 2.0.0),

R/environments.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ parser_version_get <- function() {
4343

4444
#' @rdname parser_version_set
4545
parser_version_find <- function(pd) {
46-
ifelse(any(pd$token == "equal_assign"),
47-
2,
48-
ifelse(any(pd$token == "expr_or_assign_or_help"),
49-
3,
50-
1
51-
)
52-
)
46+
if (any(pd$token == "equal_assign")) {
47+
2L
48+
} else {
49+
3L
50+
}
5351
}
5452

5553

R/nest.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ compute_parse_data_nested <- function(text,
1616
pd_nested <- parse_data %>%
1717
nest_parse_data() %>%
1818
flatten_operators() %>%
19-
when(any(parse_data$token == "EQ_ASSIGN") ~ relocate_eq_assign(.), ~.) %>%
2019
add_cache_block()
2120

2221
pd_nested

R/parse.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
109109
pd <- pd %>%
110110
add_id_and_short()
111111

112-
parser_version_set(parser_version_find(pd))
113112
pd
114113
}
115114

R/relevel.R

Lines changed: 4 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ flatten_operators <- function(pd_nested) {
2525
#' @keywords internal
2626
flatten_operators_one <- function(pd_nested) {
2727
pd_token_left <- c(special_token, "PIPE", math_token, "'$'")
28-
pd_token_right <- c(
29-
special_token, "PIPE", "LEFT_ASSIGN",
30-
if (parser_version_get() > 1) "EQ_ASSIGN",
31-
"'+'", "'-'", "'~'"
32-
)
28+
pd_token_right <- c(special_token, "PIPE", "LEFT_ASSIGN", "EQ_ASSIGN", "'+'", "'-'", "'~'")
3329
pd_nested %>%
3430
flatten_pd(pd_token_left, left = TRUE) %>%
3531
flatten_pd(pd_token_right, left = FALSE)
@@ -56,12 +52,14 @@ flatten_pd <- function(pd_nested, token, child_token = token, left = TRUE) {
5652
if (length(token_pos_candidates) == 0L) {
5753
return(pd_nested)
5854
}
55+
5956
token_pos_idx <- if (left) {
6057
1L
6158
} else {
6259
length(token_pos_candidates)
6360
}
6461
token_pos <- token_pos_candidates[token_pos_idx]
62+
6563
if (left) {
6664
pos <- previous_non_comment(pd_nested, token_pos)
6765
} else {
@@ -99,150 +97,10 @@ bind_with_child <- function(pd_nested, pos) {
9997
wrap_expr_in_expr <- function(pd) {
10098
create_tokens(
10199
"expr", "",
102-
pos_ids = create_pos_ids(pd, 1, after = FALSE),
100+
pos_ids = create_pos_ids(pd, 1L, after = FALSE),
103101
child = pd,
104102
terminal = FALSE,
105103
stylerignore = pd$stylerignore[1L],
106104
indents = pd$indent[1L]
107105
)
108106
}
109-
110-
111-
# ____________________________________________________________________________
112-
# Relocate EQ_ASSIGN ####
113-
114-
#' Relocate the expressions containing the token `EQ_ASSIGN` within the nested
115-
#' parse table
116-
#'
117-
#' Although syntactically identical, [utils::getParseData()] does not produce
118-
#' the same hierarchy of the parse table (parent and id relationship) for `<-`
119-
#' and `=` (See 'Examples').
120-
#' This is considered to be a bug and causes problems because the
121-
#' nested parse table constructed with [compute_parse_data_nested()] is not
122-
#' consistent if `EQ_ASSIGN` occurs in the expression to style. In particular,
123-
#' `EQ_ASSIGN` and the tokens to its left and right are located too high up in
124-
#' the hierarchy of the nested parse data. Hence, this function wraps the
125-
#' sub-expression into an expression, similar to [wrap_expr_in_curly()].
126-
#' Since `wrap_expr_in_curly()` is called from within a visitor
127-
#' (and `relocate_eq_assign()` not), we need to
128-
#' wrap the the implementation [relocate_eq_assign_nest()] that operates on
129-
#' *nests* into a visitor call.
130-
#' @param pd A parse table.
131-
#' @examples
132-
#' styler:::get_parse_data("a <- b <- 3")
133-
#' styler:::get_parse_data("a = b = 3")
134-
#' styler:::get_parse_data(
135-
#' "x = 5
136-
#' if(x >= 5)
137-
#' y = TRUE else
138-
#' y = FALSE",
139-
#' )
140-
#' styler:::get_parse_data(
141-
#' "x <- 5
142-
#' if(x >= 5)
143-
#' y <- TRUE else
144-
#' y <- FALSE",
145-
#' )
146-
#' @keywords internal
147-
relocate_eq_assign <- function(pd) {
148-
if (parser_version_get() < 2) {
149-
post_visit_one(pd, relocate_eq_assign_nest)
150-
} else {
151-
pd
152-
}
153-
}
154-
155-
156-
#' Relocate all assignment expressions that contain `EQ_ASSIGN` within a *nest*
157-
#'
158-
#' Implements the relocation of an `EQ_ASSIGN` and associated tokens
159-
#' within a *nest* (nested parse table at one level of nesting).
160-
#' Note that one assignment expression (such as "a = b = c") can include
161-
#' multiple assignment operators, an assignment involves just one assignment
162-
#' operator.
163-
#' For the relocation of assignment expressions that contain `EQ_ASSIGN` within
164-
#' a *nest*, we need to first find the expressions that contain `=` and then
165-
#' split the *nest* into parse tables each containing one such assignment
166-
#' expression and then relocate each of them separately.
167-
#' We can't do all of them together because:
168-
#'
169-
#' * An assignment can contain more than just three tokens, e.g. (a <- b <- c).
170-
#' * Two assignments can be in the same nest although they don't belong to the
171-
#' same assignment (if-else statement).
172-
#'
173-
#' Please refer to the section 'Examples' in [relocate_eq_assign()] for details.
174-
#' @param pd A parse table.
175-
#' @importFrom rlang seq2
176-
#' @keywords internal
177-
relocate_eq_assign_nest <- function(pd) {
178-
idx_eq_assign <- which(pd$token == "EQ_ASSIGN")
179-
if (length(idx_eq_assign) > 0L) {
180-
block_id <- find_block_id(pd)
181-
blocks <- split(pd, block_id)
182-
pd <- map_dfr(blocks, relocate_eq_assign_one)
183-
}
184-
pd
185-
}
186-
187-
#' Find the block to which a token belongs
188-
#'
189-
#' Two assignment tokens `EQ_ASSIGN` belong to the same block if they are not
190-
#' separated by more than one token. Token between `EQ_ASSIGN` tokens belong
191-
#' to the `EQ_ASSIGN` token occurring before them, except the token right before
192-
#' `EQ_ASSING` already belongs to the `EQ_ASSING` after it. Note that this
193-
#' notion is unrelated to the column *block* in the parse table, which is used
194-
#' to [parse_transform_serialize_r()] code blocks and leave out the ones that
195-
#' are cached.
196-
#' @param pd A parse table.
197-
#' @keywords internal
198-
find_block_id <- function(pd) {
199-
idx_eq_assign <- which(pd$token == "EQ_ASSIGN")
200-
eq_belongs_to_block <- c(0, diff(idx_eq_assign) > 2)
201-
202-
empty_seq <- rep(0, nrow(pd))
203-
empty_seq[idx_eq_assign - 1] <- eq_belongs_to_block
204-
block_id <- cumsum(empty_seq)
205-
block_id
206-
}
207-
208-
#' Relocate an assignment expression
209-
#'
210-
#' Relocates an assignment expression within a parse table containing one
211-
#' assignment expression. Note that one assignment can include multiple
212-
#' assignment operators such as "a = b = c".
213-
#' @param pd A parse table with one assignment expression to relocate.
214-
#' @keywords internal
215-
relocate_eq_assign_one <- function(pd) {
216-
idx_eq_assign <- which(pd$token == "EQ_ASSIGN")
217-
eq_ind <- seq2(idx_eq_assign[1L] - 1L, last(idx_eq_assign) + 1L)
218-
# initialize because wrap_expr_in_expr -> create_tokens -> requires it
219-
pd$indent <- 0
220-
eq_expr <- pd[eq_ind, ] %>%
221-
wrap_expr_in_expr() %>%
222-
add_line_col_to_wrapped_expr() %>%
223-
remove_attributes(c(
224-
"multi_line", "indention_ref_pos_id",
225-
"newlines", "indent", "spaces", "lag_newlines"
226-
))
227-
eq_expr$id <- NA
228-
eq_expr$parent <- NA
229-
pd$indent <- NULL
230-
non_eq_expr <- pd[-eq_ind, ]
231-
pd <- bind_rows(eq_expr, non_eq_expr) %>%
232-
arrange_pos_id()
233-
pd
234-
}
235-
236-
#' Adds line and col information to an expression from its child
237-
#'
238-
#' @param pd A parse table.
239-
#' @importFrom rlang abort
240-
#' @keywords internal
241-
add_line_col_to_wrapped_expr <- function(pd) {
242-
if (nrow(pd) > 1) abort("pd must be a wrapped expression that has one row.")
243-
pd$line1 <- pd$child[[1L]]$line1[1L]
244-
pd$line2 <- last(pd$child[[1L]]$line2)
245-
pd$col1 <- pd$child[[1L]]$col1[1L]
246-
pd$col2 <- last(pd$child[[1L]]$col2)
247-
pd
248-
}

R/style-guides.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,6 @@ tidyverse_style <- function(scope = "tokens",
238238
)
239239
)
240240

241-
if (getRversion() < "3.6") {
242-
transformers_drop$token$force_assignment_op <- NULL
243-
}
244-
245241
style_guide_name <- "styler::tidyverse_style@https://github.com/r-lib"
246242
create_style_guide(
247243
# transformer functions

R/styler.R

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
#' style_text("a%>%b; a", scope = "tokens")
1919
"_PACKAGE"
2020

21-
if (getRversion() >= "2.15.1") {
22-
utils::globalVariables(c(
23-
".",
24-
"pd", "pd_nested", "pd_flat", "flattened_pd",
25-
"line1", "line2", "col1", "col2", "parent",
26-
"terminal", "text", "short",
27-
"spaces", "lag_spaces",
28-
"newlines", "lag_newlines",
29-
"pos_id",
30-
NULL
31-
))
32-
}
21+
utils::globalVariables(c(
22+
".",
23+
"pd", "pd_nested", "pd_flat", "flattened_pd",
24+
"line1", "line2", "col1", "col2", "parent",
25+
"terminal", "text", "short",
26+
"spaces", "lag_spaces",
27+
"newlines", "lag_newlines",
28+
"pos_id",
29+
NULL
30+
))

R/utils-navigate-nest.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#' Find the index of the next or previous non-comment in a parse table.
32
#' @param pd A parse table.
43
#' @param pos The position of the token to start the search from.

man/add_line_col_to_wrapped_expr.Rd

Lines changed: 0 additions & 15 deletions
This file was deleted.

man/find_block_id.Rd

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)