Skip to content

styling with styler #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions R/modify_pd.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ indent_curly <- function(pd, indent_by) {
#' @describeIn update_indention Indents operators
indent_op <- function(pd,
indent_by,
token = c(math_token,
logical_token,
special_token,
"LEFT_ASSIGN",
"'$'")) {
token = c(
math_token,
logical_token,
special_token,
"LEFT_ASSIGN",
"'$'"
)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krlmlr desired?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

indent_indices <- compute_indent_indices(pd, token, indent_last = TRUE)
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
pd
Expand Down
23 changes: 13 additions & 10 deletions R/nested.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ tokenize <- function(text) {
#' @param pd A parse table.
enhance_mapping_special <- function(pd) {
pd$token <- with(pd, case_when(
token != "SPECIAL" ~ token,
text == "%>%" ~ special_and("PIPE"),
text == "%in%" ~ special_and("IN"),
TRUE ~ special_and("OTHER")
))
token != "SPECIAL" ~ token,
text == "%>%" ~ special_and("PIPE"),
text == "%in%" ~ special_and("IN"),
TRUE ~ special_and("OTHER")
))
pd
}

Expand All @@ -64,8 +64,10 @@ add_terminal_token_after <- function(pd_flat) {
filter(terminal) %>%
arrange(line1, col1)

data_frame(id = terminals$id,
token_after = lead(terminals$token, default = "")) %>%
data_frame(
id = terminals$id,
token_after = lead(terminals$token, default = "")
) %>%
left_join(pd_flat, ., by = "id")
}

Expand All @@ -75,8 +77,10 @@ add_terminal_token_before <- function(pd_flat) {
filter(terminal) %>%
arrange(line1, col1)

data_frame(id = terminals$id,
token_before = lag(terminals$token, default = "")) %>%
data_frame(
id = terminals$id,
token_before = lag(terminals$token, default = "")
) %>%
left_join(pd_flat, ., by = "id")
}

Expand Down Expand Up @@ -144,7 +148,6 @@ combine_children <- function(child, internal_child) {
bound <- bind_rows(child, internal_child)
if (nrow(bound) == 0) return(NULL)
bound[order(bound$line1, bound$col1), ]

}

#' Get the start right
Expand Down
2 changes: 0 additions & 2 deletions R/parsed.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#' spaces.
#' @importFrom utils tail
create_filler <- function(pd_flat) {

pd_flat$line3 <- lead(pd_flat$line1, default = tail(pd_flat$line2, 1))
pd_flat$col3 <- lead(pd_flat$col1, default = tail(pd_flat$col2, 1) + 1L)
pd_flat$newlines <- pd_flat$line3 - pd_flat$line2
Expand All @@ -29,4 +28,3 @@ create_filler <- function(pd_flat) {

ret
}

7 changes: 3 additions & 4 deletions R/reindent.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ update_indention_ref_fun_call <- function(pd_nested) {
non_comment <- which(pd_nested$token != "COMMENT")
first_non_comment_after_call <- non_comment[non_comment > 2][1]
if ((current_is_call) &&
nrow(pd_nested) > 3 &&
pd_nested$lag_newlines[first_non_comment_after_call] == 0) {
nrow(pd_nested) > 3 &&
pd_nested$lag_newlines[first_non_comment_after_call] == 0) {
Copy link
Collaborator Author

@lorenzwalthert lorenzwalthert Aug 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Desired?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

candidates <- 3:(nrow(pd_nested) - 1)

child_is_call <- map_lgl(pd_nested$child, is_function_call)
Expand Down Expand Up @@ -58,7 +58,7 @@ update_indention_ref_fun_call <- function(pd_nested) {
#' }
update_indention_ref_fun_dec <- function(pd_nested) {
if (pd_nested$token[1] == "FUNCTION" &&
nrow(pd_nested) > 3) {
nrow(pd_nested) > 3) {
seq <- 3:(nrow(pd_nested) - 1)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again.

pd_nested$indention_ref_id[seq] <- pd_nested$id[1]
}
Expand Down Expand Up @@ -130,5 +130,4 @@ apply_ref_indention_one <- function(flattened_pd, target_token) {
flattened_pd$col1[cols_to_update] <- flattened_pd$col1[cols_to_update] + shift
flattened_pd$col2[cols_to_update] <- flattened_pd$col2[cols_to_update] + shift
flattened_pd

}
3 changes: 1 addition & 2 deletions R/relevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ flatten_operators <- function(pd_nested) {
#' @include token.R
flatten_operators_one <- function(pd_nested) {
pd_token_left <- c(special_token, math_token, "'$'")
pd_token_right <- c(special_token, "LEFT_ASSIGN", "'+'", "'-'")
pd_token_right <- c(special_token, "LEFT_ASSIGN", "'+'", "'-'")
bound <- pd_nested %>%
flatten_pd(pd_token_left, left = TRUE) %>%
flatten_pd(pd_token_right, left = FALSE)
Expand Down Expand Up @@ -64,4 +64,3 @@ bind_with_child <- function(pd_nested, pos) {
bind_rows(pd_nested$child[[pos]]) %>%
arrange(line1, col1)
}

24 changes: 12 additions & 12 deletions R/rules-line_break.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ set_line_break_after_opening_if_call_is_multi_line <-
function(pd,
except_token_after = NULL,
except_text_before = NULL) {
if (!is_function_call(pd)) return(pd)
npd <- nrow(pd)
is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0)
if (!is_multi_line) return(pd)
exception_pos <- c(
which(pd$token %in% except_token_after),
ifelse(pd$child[[1]]$text[1] %in% except_text_before, 3L, NA)
)
pd$lag_newlines[3] <- 0L
pd$lag_newlines[setdiff(3, exception_pos)] <- 1L
pd
}
if (!is_function_call(pd)) return(pd)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indention desired if function() is on new line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See line 66.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, but we can postpone. For now, maybe move function one line up?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem is that the function name is so long that it does not all fit on one line...

npd <- nrow(pd)
is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0)
if (!is_multi_line) return(pd)
exception_pos <- c(
which(pd$token %in% except_token_after),
ifelse(pd$child[[1]]$text[1] %in% except_text_before, 3L, NA)
)
pd$lag_newlines[3] <- 0L
pd$lag_newlines[setdiff(3, exception_pos)] <- 1L
pd
}


#' @describeIn set_line_break_if_call_is_multi_line Sets line break before
Expand Down
30 changes: 15 additions & 15 deletions R/rules-other.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ add_brackets_in_pipe <- function(pd) {
has_no_brackets <- (pd$token_before == "SPECIAL-PIPE") &
(pd$token == "SYMBOL") & (pd$text != ".")
if (!any(has_no_brackets)) return(pd)
new <- data_frame(token = c("'('", "')'"),
text = c("(", ")"),
lag_newlines = rep(0, 2),
terminal = rep(TRUE, 2),
spaces = rep(0, 2),
multi_line = rep(FALSE, 2),
indention_ref_id = rep(NA, 2),
line1 = pd$line1[has_no_brackets],
line2 = line1,
col1 = pd$col1[has_no_brackets] + c(0.3, 0.6),
col2 = col1 + 1:2,
indent = rep(0, 2),
child = rep(list(NULL), 2)
)
new <- data_frame(
token = c("'('", "')'"),
text = c("(", ")"),
lag_newlines = rep(0, 2),
terminal = rep(TRUE, 2),
spaces = rep(0, 2),
multi_line = rep(FALSE, 2),
indention_ref_id = rep(NA, 2),
line1 = pd$line1[has_no_brackets],
line2 = line1,
col1 = pd$col1[has_no_brackets] + c(0.3, 0.6),
col2 = col1 + 1:2,
indent = rep(0, 2),
child = rep(list(NULL), 2)
)
pd <- bind_rows(pd, new)
pd

}
19 changes: 10 additions & 9 deletions R/rules-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ remove_space_after_unary_pm <- function(pd_flat) {

pm_after <- pd_flat$token %in% op_pm
pd_flat$spaces[pm_after & (pd_flat$newlines == 0L) &
(dplyr::lag(pd_flat$token) %in% op_pm_unary_after)] <- 0L
(dplyr::lag(pd_flat$token) %in% op_pm_unary_after)] <- 0L
pd_flat
}

Expand All @@ -48,7 +48,8 @@ fix_quotes <- function(pd_flat) {
vapply(
lapply(pd_flat$text[str_const][str_const_change], parse_text),
deparse,
character(1L))
character(1L)
)
pd_flat
}

Expand Down Expand Up @@ -108,7 +109,7 @@ remove_space_before_comma <- function(pd_flat) {
comma_after <- pd_flat$token == "','"
if (!any(comma_after)) return(pd_flat)
comma_before <- lead(comma_after, default = FALSE)
idx <- comma_before & (pd_flat$newlines == 0L)
idx <- comma_before & (pd_flat$newlines == 0L)
pd_flat$spaces[idx] <- 0L
pd_flat
}
Expand Down Expand Up @@ -149,11 +150,13 @@ start_comments_with_space <- function(pd, force_one = FALSE) {

comments <- pd[comment_pos, ]

non_comments <-pd[pd$token != "COMMENT", ]
non_comments <- pd[pd$token != "COMMENT", ]

comments <- extract(comments, text,
c("prefix", "space_after_prefix", "text"),
regex = "^(#+'*)( *)(.*)$")
comments <- extract(
comments, text,
c("prefix", "space_after_prefix", "text"),
regex = "^(#+'*)( *)(.*)$"
)
comments$space_after_prefix <- nchar(
comments$space_after_prefix, type = "width"
)
Expand Down Expand Up @@ -183,7 +186,6 @@ set_space_before_comments <- function(pd_flat) {
comment_before <- lead(comment_after, default = FALSE)
pd_flat$spaces[comment_before & (pd_flat$newlines == 0L)] <- 1L
pd_flat

}

remove_space_after_excl <- function(pd_flat) {
Expand Down Expand Up @@ -229,5 +231,4 @@ set_space_between_eq_sub_and_comma <- function(pd) {
op_before <- which(pd$token == "EQ_SUB" & lead(pd$token == "','"))
pd$spaces[op_before] <- 1L
pd

}
6 changes: 3 additions & 3 deletions R/serialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ serialize_parse_data_flattened <- function(flattened_pd) {
map(lag_newlines, add_newlines),
map(lag_spaces, add_spaces),
text,
collapse = "")) %>%
collapse = ""
)
) %>%
.[["text_ws"]] %>%
strsplit("\n", fixed = TRUE) %>%
.[[1L]]


}
Loading