Skip to content

Commit 51bb82f

Browse files
Merge pull request #782 from lorenzwalthert/issue-776
- Multi-line to carry number of lines instead of boolean (#776).
2 parents 4250d57 + 1774304 commit 51bb82f

12 files changed

+25
-20
lines changed

.pre-commit-config.yaml

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

55
repos:
66
- repo: https://github.com/lorenzwalthert/precommit
7-
rev: acc8657498d5bfb9e9891098ba00b36e82c7ebd6
7+
rev: 0cdf06628ef4ea54b20ed2feede10dad1fd9f304
88
hooks:
99
- id: style-files
1010
args: [--style_pkg=styler, --style_fun=tidyverse_style]

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# styler 1.4.1.9000 (Development version)
22

3-
* `#>` is recognized as an output marker and no space is added after `#` (#771).
43
* code with left alignment after `=` in function calls is now recognized as
54
aligned and won't be reformatted (#774, #777).
65
```
@@ -18,7 +17,9 @@
1817
```
1918
Also see `vignette("detect-alignment")`:
2019

20+
* `#>` is recognized as an output marker and no space is added after `#` (#771).
2121
* improve pkgdown author URLs (#775).
22+
* `multi_line` attribute in parse table is now integer, not boolean (#782).
2223

2324
# styler 1.4.1
2425

R/detect-alignment-utils.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ alignment_ensure_trailing_comma <- function(pd_by_line) {
5858
tokens <- create_tokens(
5959
tokens = "','",
6060
texts = ",",
61-
lag_newlines = 0,
62-
spaces = 0,
61+
lag_newlines = 0L,
62+
spaces = 0L,
6363
pos_ids = NA,
6464
)
6565
tokens$.lag_spaces <- 0

R/detect-alignment.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#' }
4444
#' )
4545
token_is_on_aligned_line <- function(pd_flat) {
46-
line_idx <- 1 + cumsum(pd_flat$lag_newlines)
46+
line_idx <- 1L + cumsum(pd_flat$lag_newlines)
4747
# cannot use lag_newlines anymore since we removed tokens
4848
# pos_id too expensive to construct in alignment_ensure_trailing_comma()
4949
pd_flat$lag_newlines <- pd_flat$pos_id <- NULL
@@ -80,7 +80,7 @@ token_is_on_aligned_line <- function(pd_flat) {
8080
if (any(starting_with_comma)) {
8181
return(FALSE)
8282
}
83-
pd_is_multi_line <- map_lgl(pd_by_line, ~ any(.x$multi_line, na.rm = TRUE))
83+
pd_is_multi_line <- map_lgl(pd_by_line, ~ any(.x$multi_line > 0L, na.rm = TRUE))
8484
if (any(pd_is_multi_line)) {
8585
return(FALSE)
8686
}

R/indent.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ needs_indention <- function(pd,
163163
needs_indention_one <- function(pd,
164164
potential_trigger_pos,
165165
other_trigger_tokens) {
166-
before_first_break <- which(pd$lag_newlines > 0)[1] - 1
166+
before_first_break <- which(pd$lag_newlines > 0)[1] - 1L
167167
if (is.na(before_first_break)) {
168168
return(FALSE)
169169
}
@@ -201,7 +201,7 @@ needs_indention_one <- function(pd,
201201
#' @importFrom purrr map_lgl
202202
#' @keywords internal
203203
set_multi_line <- function(pd) {
204-
pd$multi_line <- map_lgl(pd$child, pd_is_multi_line)
204+
pd$multi_line <- unname(map_int(pd$child, pd_multi_line))
205205
pd
206206
}
207207

@@ -214,7 +214,11 @@ set_multi_line <- function(pd) {
214214
#' @param pd A parse table.
215215
#' @keywords internal
216216
pd_is_multi_line <- function(pd) {
217-
any(pd$multi_line, pd$lag_newlines > 0)
217+
pd_multi_line(pd) > 0
218+
}
219+
220+
pd_multi_line <- function(pd) {
221+
sum(pd$multi_line, pd$lag_newlines)
218222
}
219223

220224
#' Update the newlines attribute

R/initialize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ remove_attributes <- function(pd_flat, attributes) {
6666
initialize_multi_line <- function(pd_flat) {
6767
nrow <- nrow(pd_flat)
6868
pd_flat$multi_line <- ifelse(pd_flat$terminal,
69-
rep(FALSE, nrow),
69+
rep(0L, nrow),
7070
rep(NA, nrow)
7171
)
7272
pd_flat

R/rules-line-breaks.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ add_line_break_after_pipe <- function(pd) {
195195
set_line_break_after_assignment <- function(pd) {
196196
is_assignment <- lag(pd$token, default = FALSE) %in% c("LEFT_ASSIGN", "EQ_ASSIGN")
197197
if (any(is_assignment)) {
198-
pd$lag_newlines[is_assignment] <- min(1, pd$lag_newlines[is_assignment])
198+
pd$lag_newlines[is_assignment] <- min(1L, pd$lag_newlines[is_assignment])
199199
}
200200
pd
201201
}
@@ -289,7 +289,7 @@ set_line_break_before_closing_call <- function(pd, except_token_before) {
289289
}
290290
npd <- nrow(pd)
291291
is_multi_line <- any(pd$lag_newlines[seq2(3L, npd - 1L)] > 0)
292-
if (!is_multi_line) {
292+
if (is_multi_line == 0) {
293293
exception <- which(pd$token_before %in% except_token_before)
294294
pd$lag_newlines[setdiff(npd, exception)] <- 0L
295295
return(pd)

R/rules-tokens.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ add_brackets_in_pipe_one <- function(pd, pos) {
3131
tokens = c("'('", "')'"),
3232
texts = c("(", ")"),
3333
pos_ids = new_pos_ids,
34-
lag_newlines = rep(0, 2)
34+
lag_newlines = rep(0L, 2)
3535
)
3636
pd$child[[next_non_comment]] <- bind_rows(
3737
pd$child[[next_non_comment]],

R/serialize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @keywords internal
66
serialize_parse_data_flattened <- function(flattened_pd) {
77
flattened_pd <- apply_stylerignore(flattened_pd)
8-
flattened_pd$lag_newlines[1] <- 0 # resolve start_line elsewhere
8+
flattened_pd$lag_newlines[1] <- 0L # resolve start_line elsewhere
99
with(
1010
flattened_pd,
1111
paste0(

R/token-create.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
#' @keywords internal
2727
create_tokens <- function(tokens,
2828
texts,
29-
lag_newlines = 0,
30-
spaces = 0,
29+
lag_newlines = 0L,
30+
spaces = 0L,
3131
pos_ids,
3232
token_before = NA,
3333
token_after = NA,
3434
indention_ref_pos_ids = NA,
35-
indents = 0,
35+
indents = 0L,
3636
terminal = TRUE,
3737
child = NULL,
3838
stylerignore = FALSE,
@@ -52,7 +52,7 @@ create_tokens <- function(tokens,
5252
terminal = rep(terminal, len_text),
5353
internal = rep(FALSE, len_text),
5454
spaces = spaces,
55-
multi_line = rep(FALSE, len_text),
55+
multi_line = rep(0L, len_text),
5656
indention_ref_pos_id = indention_ref_pos_ids,
5757
indent = indents,
5858
child = rep(list(child), len_text),

R/unindent.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ set_unindention_child <- function(pd, token = "')'", unindent_by) {
1515
return(pd)
1616
}
1717

18-
first_on_last_line <- last(c(1, which(pd$lag_newlines > 0 | pd$multi_line)))
18+
first_on_last_line <- last(c(1, which(pd$lag_newlines > 0 | pd$multi_line > 0)))
1919
on_same_line <- seq2(first_on_last_line, closing - 1)
2020
cand_ind <- setdiff(on_same_line, which(pd$terminal))
2121

R/visit.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ context_towards_terminals <- function(pd_nested,
110110
pd_nested$indent <- pd_nested$indent + ifelse(
111111
is.na(pd_nested$indention_ref_pos_id),
112112
outer_indent,
113-
0
113+
0L
114114
)
115115
ref_pos_id_is_na <- !is.na(pd_nested$indention_ref_pos_id)
116116
pd_nested$indention_ref_pos_id[!ref_pos_id_is_na] <- outer_indention_refs

0 commit comments

Comments
 (0)