Skip to content

Commit 28d92df

Browse files
Merge pull request #935 from r-lib/issue-934
move if up if else is before
2 parents aa32091 + ac6952c commit 28d92df

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# styler 1.7.0
22

3+
* if `else` follows directly after `if`, line breaks are removed (#935).
4+
35
**API changes**
46

57
* new R option `styler.cache_root` (defaulting to `"styler"`) that determines

R/rules-line-breaks.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ style_line_break_around_curly <- function(strict, pd) {
153153
pd$lag_newlines[is_else] <- 0L
154154
pd$spaces[c(is_else, FALSE)[-1]] <- 1L
155155
}
156+
is_if_after_else <- pd$token == "ELSE" & pd$token_after == "IF"
157+
pd$lag_newlines[lag(is_if_after_else)] <- 0L
156158
}
157159
pd
158160
}

inst/hooks/require-news-update.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/local/bin/Rscript
22
args <- system2(
33
"git",
4-
c("diff", "upstream/main", "--name-only"),
4+
c("diff", "origin/main", "--name-only"),
55
stdout = TRUE
66
)
77

tests/testthat/indention_multiple/if_else_curly-in.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,18 @@ foo <- function(x) {
3737
2
3838
}
3939
}
40+
41+
42+
if (TRUE) {
43+
3
44+
} else
45+
if (FALSE) {
46+
4
47+
}
48+
49+
if (TRUE) {
50+
3
51+
} else # comment
52+
if (FALSE) {
53+
4
54+
}

tests/testthat/indention_multiple/if_else_curly-out.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,17 @@ foo <- function(x) {
3636
2
3737
}
3838
}
39+
40+
41+
if (TRUE) {
42+
3
43+
} else if (FALSE) {
44+
4
45+
}
46+
47+
if (TRUE) {
48+
3
49+
} else # comment
50+
if (FALSE) {
51+
4
52+
}

0 commit comments

Comments
 (0)