Skip to content

Sorting key #196

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

Merged
merged 3 commits into from
Sep 17, 2017
Merged
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
23 changes: 17 additions & 6 deletions R/nested.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#' representation into a nested parse table with
#' [nest_parse_data()].
#' @param text A character vector to parse.
#' @return A nested parse table. Apart from the columns provided by
#' `utils::getParseData()`, a column "short" with the first five characters of
#' "text" is added, the nested subtibbles are in column "child".
#' @return A nested parse table. See [tokenize()] for details on the columns
#' of the parse table.
compute_parse_data_nested <- function(text) {
parse_data <- tokenize(text) %>%
add_terminal_token_before() %>%
Expand All @@ -23,14 +22,26 @@ compute_parse_data_nested <- function(text) {
#' Obtain token table from text
#'
#' [utils::getParseData()] is used to obtain a flat parse table from `text`.
#'
#' Apart from the columns provided by `utils::getParseData()`, the following
#' columns are added:
#'
#' * A column "short" with the first five characters of "text".
#' * A column "pos_id" for (positional id) which can be used for sorting
#' (because "id" cannot be used in general). Note that the nth value of this
#' column corresponds to n as long as no tokens are inserted.
#' * A column "child" that contains the nested subtibbles.
#'
#' @param text A character vector.
#' @return A flat parse table
#' @importFrom rlang seq2
tokenize <- function(text) {
# avoid https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16041
parse(text = text, keep.source = TRUE)
parsed <- parse(text = text, keep.source = TRUE)
parse_data <- as_tibble(utils::getParseData(parsed, includeText = NA)) %>%
enhance_mapping_special()
parse_data$pos_id <- seq2(1L, nrow(parse_data))
parse_data$short <- substr(parse_data$text, 1, 5)
parse_data
}
Expand Down Expand Up @@ -67,7 +78,7 @@ NULL
add_terminal_token_after <- function(pd_flat) {
terminals <- pd_flat %>%
filter(terminal) %>%
arrange(line1, col1)
arrange(pos_id)

data_frame(id = terminals$id,
token_after = lead(terminals$token, default = "")) %>%
Expand All @@ -78,7 +89,7 @@ add_terminal_token_after <- function(pd_flat) {
add_terminal_token_before <- function(pd_flat) {
terminals <- pd_flat %>%
filter(terminal) %>%
arrange(line1, col1)
arrange(pos_id)

data_frame(id = terminals$id,
token_before = lag(terminals$token, default = "")) %>%
Expand Down Expand Up @@ -149,7 +160,7 @@ nest_parse_data <- function(pd_flat) {
combine_children <- function(child, internal_child) {
bound <- bind_rows(child, internal_child)
if (nrow(bound) == 0) return(NULL)
bound[order(bound$line1, bound$col1), ]
bound[order(bound$pos_id), ]

}

Expand Down
2 changes: 1 addition & 1 deletion R/relevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ bind_with_child <- function(pd_nested, pos) {
pd_nested %>%
slice(-pos) %>%
bind_rows(pd_nested$child[[pos]]) %>%
arrange(line1, col1)
arrange(pos_id)
}

4 changes: 2 additions & 2 deletions R/rules-line_break.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ set_line_break_after_opening_if_call_is_multi_line <-
except_text_before = NULL) {
if (!is_function_call(pd)) return(pd)
npd <- nrow(pd)
seq_x <- seq2(3, npd - 1)
seq_x <- seq2(3L, npd - 1L)
is_multi_line <- any(
(pd$lag_newlines[seq_x] > 0) |
(pd$token[seq_x] == "COMMENT")
Expand All @@ -94,7 +94,7 @@ set_line_break_after_opening_if_call_is_multi_line <-
set_line_break_before_closing_call <- function(pd, except_token_before) {
if (!is_function_call(pd)) return(pd)
npd <- nrow(pd)
is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0)
is_multi_line <- any(pd$lag_newlines[seq2(3L, npd - 1L)] > 0)
if (!is_multi_line) {
exception <- which(pd$token_before %in% except_token_before)
pd$lag_newlines[setdiff(npd, exception)] <- 0L
Expand Down
2 changes: 2 additions & 0 deletions R/rules-other.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ add_brackets_in_pipe <- function(pd) {
col1 = pd$col1[has_no_brackets] + c(0.3, 0.6),
col2 = col1 + 1:2,
indent = rep(0, 2),
id = rep(NA, 2),
pos_id = pd$pos_id[has_no_brackets] + c(0.3, 0.6),
child = rep(list(NULL), 2)
)
pd <- bind_rows(pd, new)
Expand Down
1 change: 0 additions & 1 deletion R/rules-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ start_comments_with_space <- function(pd, force_one = FALSE) {
) %>%
trimws("right")
pd$short[comment_pos] <- substr(pd$text[comment_pos], 1, 5)

pd
}

Expand Down
1 change: 1 addition & 0 deletions R/styler.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ if (getRversion() >= "2.15.1") {
"terminal", "text", "short",
"spaces", "lag_spaces",
"newlines", "lag_newlines",
"pos_id",
NULL
))
}
2 changes: 1 addition & 1 deletion R/unindent.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ set_unindention_child <- function(pd, token = "')'", unindent_by) {
unindent_by = abs(pd$indent[closing] - pd$indent[closing-1]))

bind_rows(candidates, non_candidates) %>%
arrange(line1, col1)
arrange(pos_id)
}

#' Unindent a child
Expand Down
5 changes: 2 additions & 3 deletions man/compute_parse_data_nested.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions man/tokenize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.