Skip to content

Commit 43ac9ae

Browse files
Merge pull request #242 from lorenzwalthert/master
Fixing token insertion (#242).
2 parents 6084667 + 1eb4d1d commit 43ac9ae

13 files changed

+341
-5
lines changed

R/modify_pd.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ compute_indent_indices <- function(pd,
129129
if (is.null(token_closing)) {
130130
stop <- npd
131131
} else {
132-
stop <- which(pd$token %in% token_closing) - 1
132+
stop <- last(which(pd$token %in% token_closing)[needs_indention]) - 1
133133
}
134134

135135
seq2(start, stop)

R/token-create.R

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,31 @@ create_tokens <- function(tokens,
6565
#' @family token creators
6666
create_pos_ids <- function(pd, pos, by = 0.1, after = FALSE, n = 1) {
6767
direction <- ifelse(after, 1L, -1L)
68-
first <- pd$pos_id[pos] + by * direction
68+
first <- find_start_pos_id(pd, pos, by, direction, after)
6969
new_ids <- seq(first, to = first + direction * (n - 1) * by, by = by * direction)
7070
validate_new_pos_ids(new_ids, after)
7171
new_ids
7272
}
7373

74+
#' Find legit starting value for a new positional id
75+
#'
76+
#' Looks at the current nest as well as into its children (if necessary) to make
77+
#' sure the right id is returned. Otherise, ordering of tokens might not be
78+
#' preserved.
79+
#' @param direction Derived from `after`. `1` if `after = TRUE`, `-1` otherwise.
80+
#' @inheritParams create_pos_ids
81+
find_start_pos_id <- function(pd, pos, by, direction, after) {
82+
if (is.null(pd$child[[pos]])) {
83+
pd$pos_id[pos] + by * direction
84+
} else {
85+
86+
find_start_pos_id(
87+
pd$child[[pos]], if_else(after, nrow(pd$child[[pos]]), 1L),
88+
by, direction, after
89+
)
90+
}
91+
}
92+
7493
#' Validate sequence of new position ids
7594
#'
7695
#' Ids created with `after = TRUE` can have `pos_id` values between x.0 and

R/transform.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ make_transformer <- function(transformers) {
8383
#' @inheritParams compute_parse_data_nested
8484
#' @inheritParams apply_transformers
8585
parse_transform_serialize <- function(text, transformers) {
86+
text <- assert_text(text)
8687
pd_nested <- compute_parse_data_nested(text)
8788
start_line <- find_start_line(pd_nested)
8889
if (nrow(pd_nested) == 0) {

R/utils.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ calls_sys <- function(sys_call, ...) {
3939
error <- system(sys_call, ...)
4040
}
4141
}
42+
43+
#' Assert text to be of positive length and replace it with the empty
44+
#' string otherwise.
45+
#' @param text The input to style.
46+
assert_text <- function(text) {
47+
if (length(text) < 1) {
48+
text <- ""
49+
}
50+
text
51+
}

man/assert_text.Rd

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

man/find_start_pos_id.Rd

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

tests/testthat/test-token_adding_removing.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ test_that("other manipulations are correct (add braces, semi-colon etc.)", {
99
test_that("braces in if-else clause are added correctly", {
1010
expect_warning(test_collection("token_adding_removing", "if_else_strict",
1111
transformer = style_text), NA)
12-
})
13-
14-
test_that("braces in if-else clause are added correctly", {
1512
expect_warning(test_collection("token_adding_removing", "if_else_non_strict",
1613
transformer = style_text, strict = FALSE), NA)
1714
})
15+
16+
test_that("double braces are treated correctly", {
17+
expect_warning(test_collection("token_adding_removing", "double_braces",
18+
transformer = style_text), NA)
19+
})
20+
21+
test_that("double braces are treated correctly", {
22+
expect_warning(test_collection("token_adding_removing", "token_creation_find_pos",
23+
transformer = style_text), NA)
24+
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if (X)
2+
return(TRUE)
3+
4+
if (X) return(FALSE)

tests/testthat/token_adding_removing/double_braces-in_tree

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (X) {
2+
return(TRUE)
3+
}
4+
5+
if (X) return(FALSE)

0 commit comments

Comments
 (0)