-
Notifications
You must be signed in to change notification settings - Fork 71
/
initialize.R
94 lines (86 loc) · 2.6 KB
/
initialize.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#' Initialize default style guide attributes
#'
#' This function initializes and removes various variables from the parse
#' table.
#' @param pd_flat A parse table.
#' @importFrom utils tail
#' @examples
#' string_to_format <- "call( 3)"
#' pd <- styler:::compute_parse_data_nested(string_to_format)
#' styler:::pre_visit(pd, c(default_style_guide_attributes))
#' @export
#' @keywords internal
default_style_guide_attributes <- function(pd_flat) {
init_pd <-
initialize_newlines(pd_flat) %>%
initialize_spaces() %>%
remove_attributes(c("line1", "line2", "col1", "col2", "parent", "id")) %>%
initialize_multi_line() %>%
initialize_indention_ref_pos_id() %>%
initialize_indent() %>%
validate_parse_data()
init_pd
}
#' Initialize attributes
#'
#' @name initialize_attributes
#' @inheritParams default_style_guide_attributes
#' @keywords internal
NULL
#' @describeIn initialize_attributes Initializes `newlines` and `lag_newlines`.
#' @keywords internal
initialize_newlines <- function(pd_flat) {
pd_flat$line3 <- lead(pd_flat$line1, default = tail(pd_flat$line2, 1))
pd_flat$newlines <- pd_flat$line3 - pd_flat$line2
pd_flat$lag_newlines <- lag(pd_flat$newlines, default = 0L)
pd_flat$line3 <- NULL
pd_flat
}
#' @describeIn initialize_attributes Initializes `spaces`.
#' @keywords internal
initialize_spaces <- function(pd_flat) {
pd_flat$col3 <- lead(pd_flat$col1, default = tail(pd_flat$col2, 1) + 1L)
pd_flat$col2_nl <- if_else(pd_flat$newlines > 0L,
rep(0L, nrow(pd_flat)), pd_flat$col2
)
pd_flat$spaces <- pd_flat$col3 - pd_flat$col2_nl - 1L
pd_flat$col3 <- NULL
pd_flat$col2_nl <- NULL
pd_flat
}
remove_attributes <- function(pd_flat, attributes) {
pd_flat[attributes] <- rep(list(NULL), length(attributes))
pd_flat
}
#' @describeIn initialize_attributes Initializes `multi_line`.
#' @keywords internal
initialize_multi_line <- function(pd_flat) {
nrow <- nrow(pd_flat)
pd_flat$multi_line <- if_else(pd_flat$terminal,
rep(FALSE, nrow),
rep(NA, nrow)
)
pd_flat
}
#' @describeIn initialize_attributes Initializes `indention_ref_ind`.
#' @keywords internal
initialize_indention_ref_pos_id <- function(pd_flat) {
pd_flat$indention_ref_pos_id <- NA
pd_flat
}
#' @describeIn initialize_attributes Initializes `indent`.
#' @keywords internal
initialize_indent <- function(pd_flat) {
if (!("indent" %in% names(pd_flat))) {
pd_flat$indent <- 0
}
pd_flat
}
#' @describeIn initialize_attributes validates the parse data.
#' @keywords internal
validate_parse_data <- function(pd_flat) {
if (any(pd_flat$spaces < 0L)) {
stop("Invalid parse data")
}
pd_flat
}