@@ -14,6 +14,7 @@ get_transformers <- function(flat = FALSE, ...) {
14
14
get_transformers_nested(... )
15
15
}
16
16
}
17
+
17
18
# ' Get the transformer functions for flat styling
18
19
# '
19
20
# ' @param strict A logical value indicating whether a set of strict
@@ -36,7 +37,6 @@ get_transformers_flat <- function(strict = TRUE,
36
37
if (strict ) set_space_around_op else add_space_around_op ,
37
38
if (strict ) set_space_after_comma else add_space_after_comma ,
38
39
remove_space_before_comma ,
39
- remove_space_after_unary_pm ,
40
40
remove_space_after_opening_paren ,
41
41
partial(start_comments_with_space ,
42
42
force_one = start_comments_with_one_space ),
@@ -47,23 +47,90 @@ get_transformers_flat <- function(strict = TRUE,
47
47
# '
48
48
# ' Similar to [get_transformers_flat()], but additionally, returns some
49
49
# ' functions needed due the fact that styling is done in a nested way.
50
+ # ' @param scope The extent of manipulation. Can range from "none" (least
51
+ # ' invasive) to "token" (most invasive). See 'Details'. This argument is a
52
+ # ' vector of length one.
50
53
# ' @param indent_by How many spaces of indention should be inserted after
51
54
# ' operators such as '('.
52
55
# ' @inheritParams get_transformers_flat
56
+ # ' @details The following options for `scope` are available.
57
+ # '
58
+ # ' * "none": Performs no transformation at all.
59
+ # ' * "spaces": Manipulates spacing between token on the same line.
60
+ # ' * "line_breaks": In addition to "spaces", this option also manipulates
61
+ # ' line breaks.
62
+ # ' * "tokens": In addition to "line_breaks", this option also manipulates
63
+ # ' tokens.
64
+ # '
65
+ # ' As it becomes clear from this description, more invasive operations can only
66
+ # ' be performed if all less invasive operations are performed too.
53
67
# ' @family obtain transformers
54
68
# ' @importFrom purrr partial
55
69
# ' @export
56
- get_transformers_nested <- function (strict = TRUE ,
57
- indent_by = 2 ,
58
- start_comments_with_one_space = FALSE ) {
59
- c(create_filler ,
60
- partial(indent_round , indent_by = indent_by ),
61
- partial(indent_curly , indent_by = indent_by ),
62
- partial(indent_op , indent_by = indent_by ),
63
- strip_eol_spaces ,
64
- get_transformers_flat(strict , start_comments_with_one_space ),
65
- remove_space_after_unary_pm_nested ,
66
- set_space_before_comments ,
67
- set_space_between_levels
70
+ get_transformers_nested <- function (
71
+ scope = " tokens" ,
72
+ strict = TRUE ,
73
+ indent_by = 2 ,
74
+ start_comments_with_one_space = FALSE ) {
75
+
76
+ lvls_scope <- c(" none" , " spaces" , " line_breaks" , " tokens" )
77
+
78
+ scope <- character_to_ordered(scope , lvls_scope )
79
+
80
+ space_manipulators <- if (scope > = lvls_scope [2 ])
81
+ c(
82
+ partial(indent_round , indent_by = indent_by ),
83
+ partial(indent_curly , indent_by = indent_by ),
84
+ partial(indent_op , indent_by = indent_by ),
85
+ partial(indent_without_paren , indent_by = indent_by ),
86
+ partial(indent_assign , indent_by = indent_by ),
87
+ get_transformers_flat(strict , start_comments_with_one_space ),
88
+ remove_space_after_unary_pm_nested ,
89
+ set_space_before_comments ,
90
+ set_space_between_levels
91
+ )
92
+
93
+ line_break_manipulators <- if (scope > = lvls_scope [3 ])
94
+ c(
95
+ remove_line_break_before_curly_opening ,
96
+ remove_line_break_before_round_closing ,
97
+ add_line_break_afer_curly_opening ,
98
+ add_line_break_before_curly_closing ,
99
+ add_line_break_after_pipe
100
+ )
101
+
102
+ token_manipulators <- if (scope > = lvls_scope [4 ])
103
+ c(
104
+ force_assignment_op ,
105
+ resolve_semicolon ,
106
+ add_brackets_in_pipe
68
107
)
108
+
109
+
110
+ list (
111
+ filler = create_filler ,
112
+ line_break = line_break_manipulators ,
113
+ space = space_manipulators ,
114
+ token = token_manipulators ,
115
+ eol = strip_eol_spaces ,
116
+ NULL
117
+ )
69
118
}
119
+
120
+
121
+ # ' Convert a character vector to an ordered factor
122
+ # '
123
+ # ' Convert a vector to an ordered factor but stop if any of the values in
124
+ # ' `x` does not match the predefined levels in `levels.`
125
+ # ' @param x A character vector.
126
+ # ' @param levels A vector with levels.
127
+ # ' @param name The name of the character vector to be dispayed if the
128
+ # ' construction of the factor fails.
129
+ character_to_ordered <- function (x , levels , name = substitute(x )) {
130
+ if (! all((x %in% levels ))) {
131
+ stop(" all values in " , name , " must be one of the following: " ,
132
+ paste(levels , collapse = " , " ))
133
+ }
134
+ factor (x , levels = levels , ordered = TRUE )
135
+ }
136
+
0 commit comments