Skip to content

Commit e1b64da

Browse files
Merge pull request #304 from lorenzwalthert/fun-dec
- don't re-indent function declarations with scope < "indention" (#304).
2 parents a7fff7a + 0780c41 commit e1b64da

12 files changed

+96
-29
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export(tidyverse_reindention)
1414
export(tidyverse_style)
1515
import(tibble)
1616
importFrom(magrittr,"%>%")
17+
importFrom(purrr,compact)
1718
importFrom(purrr,flatten)
1819
importFrom(purrr,flatten_chr)
1920
importFrom(purrr,flatten_int)

R/style_guides.R

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ tidyverse_style <- function(scope = "tokens",
7171

7272
fix_quotes,
7373
remove_space_before_closing_paren,
74-
if (strict) remove_space_before_opening_paren else identity,
74+
remove_space_before_opening_paren = if (strict) remove_space_before_opening_paren,
7575
add_space_after_for_if_while,
7676
add_space_before_brace,
7777
remove_space_before_comma,
@@ -105,25 +105,21 @@ tidyverse_style <- function(scope = "tokens",
105105
line_break_manipulators <- if (scope >= "line_breaks") {
106106
lst(
107107
remove_line_break_before_curly_opening,
108-
if (strict) remove_line_break_before_round_closing_after_curly else identity,
109-
if (strict) remove_line_break_before_round_closing_fun_dec else identity,
108+
remove_line_break_before_round_closing_after_curly =
109+
if (strict) remove_line_break_before_round_closing_after_curly,
110+
remove_line_break_before_round_closing_fun_dec =
111+
if (strict) remove_line_break_before_round_closing_fun_dec,
110112
partial(style_line_break_around_curly, strict),
111-
if (strict) {
113+
set_line_break_after_opening_if_call_is_multi_line = if (strict)
112114
partial(
113115
set_line_break_after_opening_if_call_is_multi_line,
114116
except_token_after = "COMMENT",
115117
except_text_before = c("switch", "ifelse", "if_else")
116-
)
117-
} else {
118-
identity
119-
} ,
120-
if (strict) {
118+
),
119+
set_line_break_before_closing_call = if (strict)
121120
partial(
122121
set_line_break_before_closing_call, except_token_before = "COMMENT"
123-
)
124-
} else {
125-
identity
126-
} ,
122+
),
127123
remove_line_break_in_empty_fun_call,
128124
add_line_break_after_pipe
129125
)
@@ -135,15 +131,16 @@ tidyverse_style <- function(scope = "tokens",
135131
resolve_semicolon,
136132
add_brackets_in_pipe,
137133
remove_terminal_token_before_and_after,
138-
if (strict) wrap_if_else_multi_line_in_curly else identity
134+
wrap_if_else_multi_line_in_curly =
135+
if (strict) wrap_if_else_multi_line_in_curly
139136
)
140137
}
141138

142139

143140
indention_modifier <-
144-
c(
145-
update_indention_ref_fun_dec,
146-
NULL
141+
lst(
142+
update_indention_ref_fun_dec =
143+
if (scope >= "indention") update_indention_ref_fun_dec
147144
)
148145

149146
create_style_guide(
@@ -185,9 +182,10 @@ tidyverse_style <- function(scope = "tokens",
185182
#' pd_flat
186183
#' }
187184
#' set_line_break_before_curly_opening_style <- function() {
188-
#' create_style_guide(line_break = set_line_break_before_curly_opening)
185+
#' create_style_guide(line_break = tibble::lst(set_line_break_before_curly_opening))
189186
#' }
190187
#' style_text("a <- function(x) { x }", style = set_line_break_before_curly_opening_style)
188+
#' @importFrom purrr compact
191189
#' @export
192190
create_style_guide <- function(initialize = default_style_guide_attributes,
193191
line_break = NULL,
@@ -198,15 +196,16 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
198196
reindention = tidyverse_reindention()) {
199197
lst(
200198
# transformer functions
201-
initialize,
199+
initialize = lst(initialize),
202200
line_break,
203201
space,
204202
token,
205203
indention,
206204
# transformer options
207205
use_raw_indention,
208206
reindention
209-
)
207+
) %>%
208+
map(compact)
210209
}
211210

212211

man/create_style_guide.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
a <- function(x, #
2+
y
3+
) {
4+
x - 1
5+
}
6+
7+
8+
a <- function(x, #
9+
y) #
10+
{
11+
x
12+
}

tests/testthat/fun_dec/fun_dec_scope_spaces-in_tree

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
a <- function(x, #
2+
y
3+
) {
4+
x - 1
5+
}
6+
7+
8+
a <- function(x, #
9+
y) #
10+
{
11+
x
12+
}

tests/testthat/test-fun_dec.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test_that("reindent function declaration", {
2+
expect_warning(test_collection("fun_dec", "fun_dec_scope_spaces",
3+
transformer = style_text, scope = "spaces"), NA)
4+
5+
expect_warning(test_collection("fun_dec", "line_break_fun_dec",
6+
transformer = style_text), NA)
7+
})

0 commit comments

Comments
 (0)