Skip to content

Commit 7675e91

Browse files
outsource computation of token_to_update. Introduce a zero_ref.
1 parent 9f0b98c commit 7675e91

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

R/reindent.R

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,14 @@ apply_ref_indention <- function(flattened_pd) {
9191
#' @param target_token The index of the token from which the indention level
9292
#' should be applied to other tokens.
9393
apply_ref_indention_one <- function(flattened_pd, target_token) {
94-
token_points_to_ref <-
95-
flattened_pd$indention_ref_pos_id == flattened_pd$pos_id[target_token]
96-
first_token_on_line <- flattened_pd$lag_newlines > 0L
97-
token_to_update <- which(token_points_to_ref & first_token_on_line)
9894

95+
token_to_update <- find_tokens_to_update(flattened_pd, target_token)
9996
# udate spaces
10097
copied_spaces <- flattened_pd$col2[target_token]
10198
old_spaces <- flattened_pd$lag_spaces[token_to_update[1]]
102-
shift <- copied_spaces - old_spaces
99+
if (length(token_to_update) < 1) return(flattened_pd)
100+
zero_ref <- min(flattened_pd$lag_spaces[token_to_update])
101+
shift <- copied_spaces - zero_ref
103102
flattened_pd$lag_spaces[token_to_update] <-
104103
flattened_pd$lag_spaces[token_to_update] + shift
105104

@@ -110,6 +109,34 @@ apply_ref_indention_one <- function(flattened_pd, target_token) {
110109
flattened_pd
111110
}
112111

112+
#' Find the tokens to update when applying a reference indention
113+
#'
114+
#' Given a target token and a flattened parse table, the token for which the
115+
#' spacing information needs to be updated are computed. Since indention is
116+
#' already embeded in the column `lag_spaces`, only tokens at the beginning of
117+
#' a line are of concern.
118+
#' This function is currently tailored targeted at
119+
#' re-indention of function delcaration.
120+
#' @param flattened_pd A flattened parse table.
121+
#' @param token_to_update An integer vector with positions of tokens to update.
122+
#' @seealso apply_ref_indention_one()
123+
#' @examples
124+
#' style_text("function(a =
125+
#' b,
126+
#' dd
127+
#' ) {}", scope = "indention")
128+
#' style_text("function(a,
129+
#' b,
130+
#' dd
131+
#' ) {}", scope = "indention")
132+
find_tokens_to_update <- function(flattened_pd, target_token) {
133+
token_points_to_ref <-
134+
flattened_pd$indention_ref_pos_id == flattened_pd$pos_id[target_token]
135+
first_token_on_line <- flattened_pd$lag_newlines > 0L
136+
which(token_points_to_ref & first_token_on_line)
137+
}
138+
139+
113140
#' Set indention of tokens that match regex
114141
#'
115142
#' Force the level of indention of tokens whose text matches a regular

man/find_tokens_to_update.Rd

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)