Skip to content

Commit b088598

Browse files
Merge pull request #588 from lorenzwalthert/issue-587
- Styling with styler (#588).
2 parents c991943 + 6bf8bbd commit b088598

18 files changed

+107
-95
lines changed

R/addins.R

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,17 @@ set_style_transformers <- function() {
120120
current_style
121121
)
122122
if (!is.null(new_style)) {
123-
parsed_new_style <- with_handlers({
124-
transformers <- eval(parse(text = new_style))
125-
style_text(c("a = 2", "function() {", "NULL", "}"))
126-
},
127-
error = function(e) {
128-
abort(paste0(
129-
"The selected style transformers \"",
130-
new_style, "\" is not valid: ", e$message
131-
))
132-
}
123+
parsed_new_style <- with_handlers(
124+
{
125+
transformers <- eval(parse(text = new_style))
126+
style_text(c("a = 2", "function() {", "NULL", "}"))
127+
},
128+
error = function(e) {
129+
abort(paste0(
130+
"The selected style transformers \"",
131+
new_style, "\" is not valid: ", e$message
132+
))
133+
}
133134
)
134135
options(styler.addins_style_transformer = new_style)
135136
}

R/detect-alignment.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@
3434
#' " a = 2",
3535
#' ")"
3636
#' )) %>%
37-
#' styler:::post_visit(transformers$initialize)
37+
#' styler:::post_visit(transformers$initialize)
3838
#' nest <- pd_nested$child[[1]]
3939
#' styler:::token_is_on_aligned_line(nest)
4040
token_is_on_aligned_line <- function(pd_flat) {
41-
4241
line_idx <- 1 + cumsum(pd_flat$lag_newlines)
4342
pd_flat$.lag_spaces <- lag(pd_flat$spaces)
4443
pd_by_line <- split(pd_flat, line_idx)

R/initialize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#' @export
1212
#' @keywords internal
1313
default_style_guide_attributes <- function(pd_flat) {
14-
initialize_newlines(pd_flat) %>%
14+
initialize_newlines(pd_flat) %>%
1515
initialize_spaces() %>%
1616
remove_attributes(c("line1", "line2", "col1", "col2", "parent", "id")) %>%
1717
initialize_multi_line() %>%

R/io.R

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@ transform_utf8 <- function(path, fun, write_back = TRUE) {
1515

1616
#' @importFrom rlang with_handlers warn
1717
transform_utf8_one <- function(path, fun, write_back) {
18-
with_handlers({
19-
file_with_info <- read_utf8(path)
20-
# only write back when changed OR when there was a missing newline
21-
new <- fun(file_with_info$text)
22-
identical_content <- identical(unclass(file_with_info$text), unclass(new))
23-
identical <- identical_content && !file_with_info$missing_EOF_line_break
24-
if (!identical && write_back) {
25-
xfun::write_utf8(new, path)
18+
with_handlers(
19+
{
20+
file_with_info <- read_utf8(path)
21+
# only write back when changed OR when there was a missing newline
22+
new <- fun(file_with_info$text)
23+
identical_content <- identical(unclass(file_with_info$text), unclass(new))
24+
identical <- identical_content && !file_with_info$missing_EOF_line_break
25+
if (!identical && write_back) {
26+
xfun::write_utf8(new, path)
27+
}
28+
!identical
29+
},
30+
error = function(e) {
31+
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
32+
NA
2633
}
27-
!identical
28-
}, error = function(e) {
29-
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
30-
NA
31-
})
34+
)
3235
}
3336

3437
#' Read UTF-8

R/nest.R

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,14 @@ add_cache_block <- function(pd_nested) {
8888
#' it is shallow in the right places.
8989
#' @keywords internal
9090
drop_cached_children <- function(pd) {
91-
9291
if (cache_is_activated()) {
93-
94-
pd_parent_first <- pd[order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal)),]
92+
pd_parent_first <- pd[order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal)), ]
9593
pos_ids_to_keep <- pd_parent_first %>%
9694
split(cumsum(pd_parent_first$parent == 0)) %>%
9795
map(find_pos_id_to_keep) %>%
9896
unlist() %>%
9997
unname()
100-
pd[pd$pos_id %in% pos_ids_to_keep,]
98+
pd[pd$pos_id %in% pos_ids_to_keep, ]
10199
} else {
102100
pd
103101
}
@@ -155,7 +153,7 @@ find_pos_id_to_keep <- function(pd) {
155153
#' # styler: on
156154
#' 1+1
157155
#' "
158-
#')
156+
#' )
159157
#'
160158
#' # if there is a stop marker before a start marker, styler won't be able
161159
#' # to figure out which lines you want to ignore and won't ignore anything,
@@ -169,8 +167,8 @@ find_pos_id_to_keep <- function(pd) {
169167
#' # styler: off
170168
#' 1+1
171169
#' "
172-
#')
173-
#'}
170+
#' )
171+
#' }
174172
#'
175173
NULL
176174

@@ -214,8 +212,9 @@ add_terminal_token_after <- function(pd_flat) {
214212

215213
new_tibble(list(
216214
pos_id = terminals$pos_id,
217-
token_after = lead(terminals$token, default = "")),
218-
nrow = nrow(terminals)
215+
token_after = lead(terminals$token, default = "")
216+
),
217+
nrow = nrow(terminals)
219218
) %>%
220219
left_join(pd_flat, ., by = "pos_id")
221220
}

R/parse.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
9797
parsed <- parse_safely(text, keep.source = TRUE)
9898
pd <- as_tibble(
9999
utils::getParseData(parsed, includeText = include_text),
100-
.name_repair = "minimal") %>%
100+
.name_repair = "minimal"
101+
) %>%
101102
add_id_and_short()
102103

103104
parser_version_set(parser_version_find(pd))

R/rules-line-break.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ NULL
207207
#' @keywords internal
208208
set_line_break_after_opening_if_call_is_multi_line <-
209209
function(pd,
210-
except_token_after = NULL,
211-
except_text_before = NULL) {
210+
except_token_after = NULL,
211+
except_text_before = NULL) {
212212
if (!is_function_call(pd) && !is_subset_expr(pd)) {
213213
return(pd)
214214
}
@@ -282,19 +282,18 @@ set_linebreak_after_ggplot2_plus <- function(pd) {
282282
next_non_comment <- next_non_comment(pd, first_plus)
283283
is_plus_or_comment_after_plus_before_fun_call <-
284284
lag(is_plus_raw, next_non_comment - first_plus - 1, default = FALSE) &
285-
(pd$token_after == "SYMBOL_FUNCTION_CALL" | pd$token_after == "SYMBOL_PACKAGE")
285+
(pd$token_after == "SYMBOL_FUNCTION_CALL" | pd$token_after == "SYMBOL_PACKAGE")
286286
if (any(is_plus_or_comment_after_plus_before_fun_call)) {
287287
gg_call <- pd$child[[previous_non_comment(pd, first_plus)]]$child[[1]]
288288
if (!is.null(gg_call) && isTRUE(gg_call$text[gg_call$token == "SYMBOL_FUNCTION_CALL"] == "ggplot")) {
289289
plus_without_comment_after <- setdiff(
290290
which(is_plus_raw),
291291
which(lead(pd$token == "COMMENT"))
292-
)
292+
)
293293

294294
pd$lag_newlines[plus_without_comment_after + 1] <- 1L
295295
}
296296
}
297-
298297
}
299298
pd
300299
}

R/rules-spacing.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ style_space_around_math_token <- function(strict, zero, one, pd_flat) {
5858
)
5959
}
6060
if (any(pd_flat$token %in% one)) {
61-
pd_flat <- pd_flat %>%
61+
pd_flat <- pd_flat %>%
6262
style_space_around_token(
6363
strict = strict, tokens = one, level_before = 1L, level_after = 1L
6464
)

R/style-guides.R

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,24 @@ tidyverse_style <- function(scope = "tokens",
119119
if (strict) remove_line_break_before_round_closing_after_curly,
120120
remove_line_break_before_round_closing_fun_dec =
121121
if (strict) remove_line_break_before_round_closing_fun_dec,
122-
style_line_break_around_curly = partial(style_line_break_around_curly,
122+
style_line_break_around_curly = partial(
123+
style_line_break_around_curly,
123124
strict
124125
),
125126
# must be after style_line_break_around_curly as it remove line
126127
# breaks again for {{.
127128
set_line_break_around_curly_curly,
128-
set_line_break_after_opening_if_call_is_multi_line = if (strict)
129+
set_line_break_after_opening_if_call_is_multi_line = if (strict) {
129130
partial(
130131
set_line_break_after_opening_if_call_is_multi_line,
131132
except_token_after = "COMMENT",
132133
except_text_before = c("switch", "ifelse", "if_else")
133-
),
134+
)
135+
},
134136
set_line_break_before_closing_call = if (strict) {
135137
partial(
136-
set_line_break_before_closing_call, except_token_before = "COMMENT"
138+
set_line_break_before_closing_call,
139+
except_token_before = "COMMENT"
137140
)
138141
},
139142
remove_line_break_in_empty_fun_call,
@@ -160,19 +163,19 @@ tidyverse_style <- function(scope = "tokens",
160163
update_indention_ref_fun_dec =
161164
if (scope >= "indention") update_indention_ref_fun_dec
162165
)
163-
166+
style_guide_name <- "styler::tidyverse_style@https://github.com/r-lib"
164167
create_style_guide(
165168
# transformer functions
166169
initialize = default_style_guide_attributes,
167-
line_break = line_break_manipulators,
168-
space = space_manipulators,
169-
token = token_manipulators,
170-
indention = indention_modifier,
170+
line_break = line_break_manipulators,
171+
space = space_manipulators,
172+
token = token_manipulators,
173+
indention = indention_modifier,
171174
# transformer options
172-
use_raw_indention = use_raw_indention,
173-
reindention = reindention,
174-
style_guide_name = "styler::tidyverse_style@https://github.com/r-lib",
175-
style_guide_version = styler_version
175+
use_raw_indention = use_raw_indention,
176+
reindention = reindention,
177+
style_guide_name = style_guide_name,
178+
style_guide_version = styler_version
176179
)
177180
}
178181

@@ -211,9 +214,14 @@ tidyverse_style <- function(scope = "tokens",
211214
#' pd_flat
212215
#' }
213216
#' set_line_break_before_curly_opening_style <- function() {
214-
#' create_style_guide(line_break = tibble::lst(set_line_break_before_curly_opening))
217+
#' create_style_guide(
218+
#' line_break = tibble::lst(set_line_break_before_curly_opening)
219+
#' )
215220
#' }
216-
#' style_text("a <- function(x) { x }", style = set_line_break_before_curly_opening_style)
221+
#' style_text(
222+
#' "a <- function(x) { x }",
223+
#' style = set_line_break_before_curly_opening_style
224+
#' )
217225
#' @importFrom purrr compact
218226
#' @export
219227
create_style_guide <- function(initialize = default_style_guide_attributes,
@@ -267,12 +275,13 @@ NULL
267275
#' @export
268276
specify_reindention <- function(regex_pattern = NULL,
269277
indention = 0,
270-
comments_only = TRUE)
278+
comments_only = TRUE) {
271279
lst(
272280
regex_pattern,
273281
indention,
274282
comments_only
275283
)
284+
}
276285

277286
#' @describeIn reindention Simple forwarder to
278287
#' `specify_reindention` with reindention according to the tidyverse style
@@ -330,7 +339,7 @@ NULL
330339
#' @export
331340
specify_math_token_spacing <-
332341
function(zero = "'^'",
333-
one = c("'+'", "'-'", "'*'", "'/'")) {
342+
one = c("'+'", "'-'", "'*'", "'/'")) {
334343
assert_tokens(c(one, zero))
335344
lst(
336345
one = setdiff(c(math_token, one), zero),

R/token-create.R

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,25 @@ create_tokens <- function(tokens,
4141
len_text <- length(texts)
4242
new_tibble(
4343
list(
44-
token = tokens,
45-
text = texts,
46-
short = substr(texts, 1, 5),
47-
lag_newlines = lag_newlines,
48-
newlines = lead(lag_newlines),
49-
pos_id = pos_ids,
50-
token_before = token_before,
51-
token_after = token_after,
52-
terminal = rep(terminal, len_text),
53-
internal = rep(FALSE, len_text),
54-
spaces = spaces,
55-
multi_line = rep(FALSE, len_text),
56-
indention_ref_pos_id = indention_ref_pos_ids,
57-
indent = indents,
58-
child = rep(list(child), len_text),
59-
stylerignore = stylerignore,
60-
block = block,
61-
is_cached = is_cached
62-
),
44+
token = tokens,
45+
text = texts,
46+
short = substr(texts, 1, 5),
47+
lag_newlines = lag_newlines,
48+
newlines = lead(lag_newlines),
49+
pos_id = pos_ids,
50+
token_before = token_before,
51+
token_after = token_after,
52+
terminal = rep(terminal, len_text),
53+
internal = rep(FALSE, len_text),
54+
spaces = spaces,
55+
multi_line = rep(FALSE, len_text),
56+
indention_ref_pos_id = indention_ref_pos_ids,
57+
indent = indents,
58+
child = rep(list(child), len_text),
59+
stylerignore = stylerignore,
60+
block = block,
61+
is_cached = is_cached
62+
),
6363
nrow = len_text
6464
)
6565
}

0 commit comments

Comments
 (0)