Skip to content

Commit 13b0e7c

Browse files
Merge pull request #297 from lorenzwalthert/style-with-styler
- Style with styler (#297).
2 parents a68fdce + 4f904a6 commit 13b0e7c

22 files changed

+150
-124
lines changed

R/addins.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ try_transform_as_r_file <- function(context, transformer) {
5050
tryCatch(
5151
transformer(context$contents),
5252
error = function(e) stop(
53-
paste(
54-
"Styling of unsaved files is only supported for R files with valid code.",
55-
"Please save the file (as .R or .Rmd) and make sure that the R code in it",
56-
"can be parsed. Then, try to style again.",
57-
"The error was \n", e
58-
), call. = FALSE
59-
))
53+
paste(
54+
"Styling of unsaved files is only supported for R files with valid code.",
55+
"Please save the file (as .R or .Rmd) and make sure that the R code in it",
56+
"can be parsed. Then, try to style again.",
57+
"The error was \n", e
58+
), call. = FALSE
59+
)
60+
)
6061
}
6162

6263
#' @describeIn styler_addins Styles the highlighted selection in a `.R` or

R/communicate.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@ communicate_summary <- function(changed, ruler_width) {
2323
cli::cat_bullet("\t", sum(changed, na.rm = TRUE), "\tFile changed.", bullet = "info")
2424
cli::cat_bullet(bullet = "cross", "\t", sum(is.na(changed)), "\tStyling threw an eror.")
2525
cli::cat_rule(width = max(40, ruler_width))
26-
2726
}

R/dplyr.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ arrange <- function(.data, ...) {
2424
stopifnot(is.data.frame(.data))
2525
ord <- eval(substitute(order(...)), .data, parent.frame())
2626
if (length(ord) != nrow(.data)) {
27-
stop("Length of ordering vectors don't match data frame size",
28-
call. = FALSE)
27+
stop(
28+
"Length of ordering vectors don't match data frame size",
29+
call. = FALSE
30+
)
2931
}
3032
.data[ord, , drop = FALSE]
3133
}
@@ -83,7 +85,7 @@ left_join <- function(x, y, by, ...) {
8385
res
8486
}
8587

86-
nth <- function (x, n, order_by = NULL, default = x[NA_real_]) {
88+
nth <- function(x, n, order_by = NULL, default = x[NA_real_]) {
8789
stopifnot(length(n) == 1, is.numeric(n))
8890
n <- trunc(n)
8991
if (n == 0 || n > length(x) || n < -length(x)) {
@@ -101,7 +103,7 @@ nth <- function (x, n, order_by = NULL, default = x[NA_real_]) {
101103
}
102104

103105

104-
last <- function (x, order_by = NULL, default = x[NA_real_]) {
106+
last <- function(x, order_by = NULL, default = x[NA_real_]) {
105107
nth(x, -1L, order_by = order_by, default = default)
106108
}
107109

R/expr-is.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ contains_else_expr_that_needs_braces <- function(pd) {
4747
non_comment_after_else <- next_non_comment(pd, else_idx)
4848
sub_expr <- pd$child[[non_comment_after_else]]
4949
# needs braces if NOT if_condition, NOT curly expr
50-
!is_cond_expr(sub_expr) && !is_curly_expr(sub_expr)
50+
!is_cond_expr(sub_expr) && !is_curly_expr(sub_expr)
5151
} else {
5252
FALSE
5353
}

R/indent.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ indent_op <- function(pd,
2828
special_token,
2929
"LEFT_ASSIGN",
3030
"EQ_ASSIGN",
31-
"'$'")
32-
) {
31+
"'$'"
32+
)) {
3333
indent_indices <- compute_indent_indices(pd, token)
3434
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
3535
pd
@@ -62,8 +62,8 @@ indent_assign <- function(pd, indent_by, token = NULL) {
6262
#' statements that do not have curly parenthesis.
6363
indent_without_paren <- function(pd, indent_by = 2) {
6464
pd %>%
65-
indent_without_paren_for_while_fun(indent_by) %>%
66-
indent_without_paren_if_else(indent_by)
65+
indent_without_paren_for_while_fun(indent_by) %>%
66+
indent_without_paren_if_else(indent_by)
6767
}
6868

6969
#' @describeIn update_indention Is used to indent for and statements and function
@@ -90,8 +90,8 @@ indent_without_paren_if_else <- function(pd, indent_by) {
9090
expr_after_else_idx <- next_non_comment(pd, else_idx)
9191
has_else_without_curly_or_else_chid <-
9292
any(pd$token == "ELSE") &&
93-
pd$child[[expr_after_else_idx]]$token[1] != "'{'" &&
94-
pd$child[[expr_after_else_idx]]$token[1] != "IF"
93+
pd$child[[expr_after_else_idx]]$token[1] != "'{'" &&
94+
pd$child[[expr_after_else_idx]]$token[1] != "IF"
9595
if (has_else_without_curly_or_else_chid) {
9696
pd$indent[seq(else_idx + 1, nrow(pd))] <- indent_by
9797
}

R/initialize.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#' styler:::pre_visit(pd, c(default_style_guide_attributes))
1111
#' @export
1212
default_style_guide_attributes <- function(pd_flat) {
13-
1413
init_pd <-
1514
initialize_newlines(pd_flat) %>%
1615
initialize_spaces() %>%

R/nest.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ add_terminal_token_before <- function(pd_flat) {
6565
filter(terminal) %>%
6666
arrange(pos_id)
6767

68-
data_frame(id = terminals$id,
69-
token_before = lag(terminals$token, default = "")) %>%
68+
data_frame(
69+
id = terminals$id,
70+
token_before = lag(terminals$token, default = "")
71+
) %>%
7072
left_join(pd_flat, ., by = "id")
7173
}
7274

@@ -144,7 +146,6 @@ combine_children <- function(child, internal_child) {
144146
bound <- bind_rows(child, internal_child)
145147
if (nrow(bound) == 0) return(NULL)
146148
bound[order(bound$pos_id), ]
147-
148149
}
149150

150151
#' Get the start right

R/nested_to_tree.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ create_node_from_nested_root <- function(pd_nested, structure_only) {
4343
#' @param parent The parent of the node to be created.
4444
#' @importFrom purrr map2 map
4545
create_node_from_nested <- function(pd_nested, parent, structure_only) {
46-
if (is.null(pd_nested))
46+
if (is.null(pd_nested)) {
4747
return()
48+
}
4849

4950
node_info <- create_node_info(pd_nested, structure_only)
5051

@@ -62,6 +63,6 @@ create_node_info <- function(pd_nested, structure_only) {
6263
pd_nested$short, " [",
6364
pd_nested$lag_newlines, "/",
6465
pd_nested$spaces, "] {",
65-
pd_nested$pos_id, "}")
66-
66+
pd_nested$pos_id, "}"
67+
)
6768
}

R/parse.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ add_id_and_short <- function(pd) {
5757
#' for potential reparsing.
5858
verify_str_txt <- function(pd_with_terminal_text, text) {
5959
string_ind <- pd_with_terminal_text$token == "STR_CONST"
60-
strings <- pd_with_terminal_text[string_ind,]
60+
strings <- pd_with_terminal_text[string_ind, ]
6161
parent_of_strings_ind <- pd_with_terminal_text$id %in% strings$parent
6262
other_ind <- !(string_ind | parent_of_strings_ind)
6363
if (nrow(strings) == 0 || !any(substr(strings$text, 1, 1) == "[")) {
@@ -71,8 +71,7 @@ verify_str_txt <- function(pd_with_terminal_text, text) {
7171
bind_rows(
7272
new_strings,
7373
pd_with_terminal_text[other_ind, ],
74-
pd_with_terminal_text[parent_of_strings_ind,]
74+
pd_with_terminal_text[parent_of_strings_ind, ]
7575
) %>%
7676
arrange(pos_id)
77-
7877
}

R/reindent.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ update_indention_ref_fun_call <- function(pd_nested) {
3030
non_comment <- which(pd_nested$token != "COMMENT")
3131
first_non_comment_after_call <- non_comment[non_comment > 2][1]
3232
if ((current_is_call) &&
33-
pd_nested$lag_newlines[first_non_comment_after_call] == 0) {
33+
pd_nested$lag_newlines[first_non_comment_after_call] == 0) {
3434
candidates <- seq2(3, nrow(pd_nested) - 1)
3535

3636
child_is_call <- map_lgl(pd_nested$child, is_function_call)
@@ -109,7 +109,6 @@ apply_ref_indention_one <- function(flattened_pd, target_token) {
109109
flattened_pd$col1[cols_to_update] <- flattened_pd$col1[cols_to_update] + shift
110110
flattened_pd$col2[cols_to_update] <- flattened_pd$col2[cols_to_update] + shift
111111
flattened_pd
112-
113112
}
114113

115114

@@ -131,16 +130,16 @@ apply_ref_indention_one <- function(flattened_pd, target_token) {
131130
#' the tokens that match `regex.`
132131
#' @importFrom purrr map flatten_int
133132
set_regex_indention <- function(flattened_pd,
134-
pattern,
135-
target_indention = 0,
136-
comments_only = TRUE) {
133+
pattern,
134+
target_indention = 0,
135+
comments_only = TRUE) {
137136
if (comments_only) {
138137
cond <- which(
139138
(flattened_pd$token == "COMMENT") & (flattened_pd$lag_newlines > 0)
140139
)
141140
if (length(cond) < 1) return(flattened_pd)
142-
to_check <- flattened_pd[cond,]
143-
not_to_check <- flattened_pd[-cond,]
141+
to_check <- flattened_pd[cond, ]
142+
not_to_check <- flattened_pd[-cond, ]
144143
} else {
145144
to_check <- flattened_pd
146145
not_to_check <- NULL

0 commit comments

Comments
 (0)