Skip to content

add nonstrict version of set_space_before_comment #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions R/rules-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,18 @@ set_space_before_comments <- function(pd_flat) {
comment_before <- lead(comment_after, default = FALSE)
pd_flat$spaces[comment_before & (pd_flat$newlines == 0L)] <- 1L
pd_flat
}

add_space_before_comments <- function(pd_flat) {
comment_after <- (pd_flat$token == "COMMENT") & (pd_flat$lag_newlines == 0L)
if (!any(comment_after)) return(pd_flat)
comment_before <- lead(comment_after, default = FALSE)
pd_flat$spaces[comment_before & (pd_flat$newlines == 0L)] <-
pmax(pd_flat$spaces[comment_before], 1L)
pd_flat
}


remove_space_after_excl <- function(pd_flat) {
excl <- (pd_flat$token == "'!'") & (pd_flat$newlines == 0L)
pd_flat$spaces[excl] <- 0L
Expand Down
2 changes: 1 addition & 1 deletion R/style_guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ tidyverse_style <- function(scope = "tokens",
force_one = start_comments_with_one_space),

remove_space_after_unary_pm_nested,
set_space_before_comments,
if (strict) set_space_before_comments else add_space_before_comments,
set_space_between_levels,
set_space_between_eq_sub_and_comma,
)
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/spacing/comments-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a # comment
b #comment
c # comment
dejk #comment
13 changes: 13 additions & 0 deletions tests/testthat/spacing/comments-in_tree

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/spacing/comments-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a # comment
b # comment
c # comment
dejk # comment
7 changes: 7 additions & 0 deletions tests/testthat/test-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ test_that(":, ::, and :::", {
"spacing", "colon",
transformer = style_text), NA)
})


test_that("comments and strict = FALSE", {
expect_warning(test_collection(
"spacing", "comments",
transformer = style_text, stric = FALSE), NA)
})