Skip to content

Commit

Permalink
drop id, use pos_id only. Also, fix find_start_pos_id.
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Oct 27, 2017
1 parent 8aad35c commit 94cb7bc
Show file tree
Hide file tree
Showing 110 changed files with 6,442 additions and 6,441 deletions.
3 changes: 2 additions & 1 deletion R/initialize.R
Expand Up @@ -39,7 +39,8 @@ initialize_spaces <- function(pd_flat) {
}

remove_unused_attributes <- function(pd_flat) {
pd_flat[c("line1", "line2", "col1", "col2", "parent")] <- rep(list(NULL), 4)
pd_flat[c("line1", "line2", "col1", "col2", "parent", "id")] <-
rep(list(NULL), 4)
pd_flat
}

Expand Down
5 changes: 2 additions & 3 deletions R/nested.R
Expand Up @@ -53,9 +53,8 @@ add_terminal_token_after <- function(pd_flat) {
filter(terminal) %>%
arrange(pos_id)

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

#' @rdname add_token_terminal
Expand Down
4 changes: 2 additions & 2 deletions R/nested_to_tree.R
Expand Up @@ -22,7 +22,7 @@ create_tree <- function(text) {
#' initialized <- styler:::pre_visit(nested_pd, c(styler:::initialize_attributes))
#' styler:::create_node_from_nested_root(initialized)
create_node_from_nested_root <- function(pd_nested) {
n <- data.tree::Node$new("ROOT (token: short_text [lag_newlines/spaces] {id})")
n <- data.tree::Node$new("ROOT (token: short_text [lag_newlines/spaces] {pos_id})")
create_node_from_nested(pd_nested, n)
n
}
Expand All @@ -35,7 +35,7 @@ create_node_from_nested <- function(pd_nested, parent) {
if (is.null(pd_nested))
return()

node_info <- paste0(pd_nested$token, ": ", pd_nested$short, " [", pd_nested$lag_newlines, "/", pd_nested$spaces, "] {", pd_nested$id, "}")
node_info <- paste0(pd_nested$token, ": ", pd_nested$short, " [", pd_nested$lag_newlines, "/", pd_nested$spaces, "] {", pd_nested$pos_id, "}")

child_nodes <-
node_info %>%
Expand Down
23 changes: 12 additions & 11 deletions R/token-create.R
Expand Up @@ -8,13 +8,11 @@
#' @param spaces Character vector with spaces corresponding to the tokens.
#' @param pos_ids Character vector with positional id corresponding to the
#' tokens.
#' @param parents Vector with `id` corresponding to the
#' parent of the tokens we want to create.
#' @param token_before Character vector corresponding to the columns
#' `token_before`.
#' @param token_after Character vector corresponding to the columns
#' `token_after`.
#' @param indention_ref_ids Character vector with indention ref ids
#' @param indention_ref_pos_ids Character vector with indention ref ids
#' corresponding to the tokens.
#' @param indents Vector with indents corresponding to the tokens.
#' @param terminal Boolean vector indicating whether a token is a terminal or
Expand All @@ -28,7 +26,7 @@ create_tokens <- function(tokens,
pos_ids,
token_before = NA,
token_after = NA,
indention_ref_ids = NA,
indention_ref_pos_ids = NA,
indents = 0,
terminal = TRUE,
child = NULL) {
Expand All @@ -42,12 +40,11 @@ create_tokens <- function(tokens,
pos_id = pos_ids,
token_before = token_before,
token_after = token_after,
id = NA,
terminal = rep(terminal, len_text),
internal = rep(FALSE, len_text),
spaces = spaces,
multi_line = rep(FALSE, len_text),
indention_ref_id = indention_ref_ids,
indention_ref_pos_id = indention_ref_pos_ids,
indent = indents,
child = rep(list(child), len_text)
)
Expand Down Expand Up @@ -80,19 +77,23 @@ create_pos_ids <- function(pd, pos, by = 0.1, after = FALSE, n = 1) {
#' sure the right id is returned. Otherise, ordering of tokens might not be
#' preserved.
#' @param direction Derived from `after`. `1` if `after = TRUE`, `-1` otherwise.
#' @param candidates The `pos_ids` of the candidates that origin from other
#' nests.
#' @inheritParams create_pos_ids
find_start_pos_id <- function(pd, pos, by, direction, after) {
find_start_pos_id <- function(pd, pos, by, direction, after, candidates = NULL) {
candidates <- append(candidates, pd$pos_id[pos])
if (is.null(pd$child[[pos]])) {
pd$pos_id[pos] + by * direction
ifelse(after, max(candidates), min(candidates)) + by * direction
} else {

find_start_pos_id(
pd$child[[pos]], if_else(after, nrow(pd$child[[pos]]), 1L),
by, direction, after
by, direction, after, candidates
)
}
}



#' Validate sequence of new position ids
#'
#' Ids created with `after = TRUE` can have `pos_id` values between x.0 and
Expand Down Expand Up @@ -131,7 +132,7 @@ wrap_expr_in_curly <- function(pd, stretch_out = FALSE) {
)
opening <- create_tokens(
"'{'", "{",
pos_ids = create_pos_ids(pd, 1, after = FALSE)
pos_ids = create_pos_ids(expr, 1, after = FALSE)
)

closing <- create_tokens(
Expand Down
2 changes: 1 addition & 1 deletion man/context_to_terminals.Rd

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

2 changes: 1 addition & 1 deletion man/context_towards_terminals.Rd

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

7 changes: 2 additions & 5 deletions man/create_tokens.Rd

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

5 changes: 4 additions & 1 deletion man/find_start_pos_id.Rd

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

6 changes: 3 additions & 3 deletions man/initialize_attributes.Rd

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

4 changes: 2 additions & 2 deletions man/update_indention_ref.Rd

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

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

0 comments on commit 94cb7bc

Please sign in to comment.