-
Notifications
You must be signed in to change notification settings - Fork 186
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
add assignment_as_infix = TRUE to indentation_linter() #1812
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0d62332
add assignment_as_infix = TRUE to indentation_linter()
AshesITR ecd6dcd
delint, fix more complex edge case
AshesITR 94ee333
refac, extend, add more tests
AshesITR c2de2d0
Merge branch 'main' into fix/1800-assign-double-indentation
AshesITR 9ad5ead
Merge branch 'main' into fix/1800-assign-double-indentation
AshesITR 3635347
Merge branch 'main' into fix/1800-assign-double-indentation
AshesITR 220c639
Merge branch 'main' into fix/1800-assign-double-indentation
AshesITR 1dc0d29
Merge branch 'main' into fix/1800-assign-double-indentation
AshesITR 75f5487
Merge branch 'main' into fix/1800-assign-double-indentation
AshesITR 05ea7b6
Merge branch 'main' into fix/1800-assign-double-indentation
AshesITR 59ec58d
Merge branch 'main' into fix/1800-assign-double-indentation
MichaelChirico e36addf
Merge branch 'main' into fix/1800-assign-double-indentation
IndrajeetPatil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,27 @@ | |
#' # body | ||
#' } | ||
#' ``` | ||
#' @param assignment_as_infix Treat `<-` as a regular (i.e. left-associative) infix operator? | ||
#' This means, that infix operators on the right hand side of an assignment do not trigger a second level of | ||
#' indentation: | ||
#' ```r | ||
#' # complies to any style | ||
#' variable <- a %+% | ||
#' b %+% | ||
#' c | ||
#' | ||
#' # complies to assignment_as_infix = TRUE | ||
#' variable <- | ||
#' a %+% | ||
#' b %+% | ||
#' c | ||
#' | ||
#' # complies to assignment_as_infix = FALSE | ||
#' variable <- | ||
#' a %+% | ||
#' b %+% | ||
#' c | ||
#' ``` | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
|
@@ -97,7 +118,8 @@ | |
#' - <https://style.tidyverse.org/functions.html#long-lines-1> | ||
#' | ||
#' @export | ||
indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "always", "never")) { | ||
indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "always", "never"), | ||
assignment_as_infix = TRUE) { | ||
paren_tokens_left <- c("OP-LEFT-BRACE", "OP-LEFT-PAREN", "OP-LEFT-BRACKET", "LBB") | ||
paren_tokens_right <- c("OP-RIGHT-BRACE", "OP-RIGHT-PAREN", "OP-RIGHT-BRACKET", "OP-RIGHT-BRACKET") | ||
infix_tokens <- setdiff(infix_metadata$xml_tag, c("OP-LEFT-BRACE", "OP-COMMA", paren_tokens_left)) | ||
|
@@ -118,6 +140,21 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al | |
} | ||
} | ||
|
||
if (isTRUE(assignment_as_infix)) { | ||
infix_condition <- glue::glue(" | ||
and not( | ||
ancestor::expr[ | ||
preceding-sibling::LEFT_ASSIGN[{xp_last_on_line}] or expr[SYMBOL_FUNCTION_CALL] | ||
AshesITR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
][1][ | ||
preceding-sibling::LEFT_ASSIGN[{xp_last_on_line}] | ||
] | ||
) | ||
") | ||
# suppress only infix operators that are not inside of a function call | ||
} else { | ||
infix_condition <- "" | ||
} | ||
|
||
xp_block_ends <- paste0( | ||
"number(", | ||
paste( | ||
|
@@ -141,7 +178,7 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al | |
@line2 > @line1 and | ||
({xp_or(paste0('descendant::', paren_tokens_left, '[', xp_last_on_line, ']'))}) | ||
]/@line1)]"), | ||
glue::glue("//{infix_tokens}[{xp_last_on_line}]"), | ||
glue::glue("//{infix_tokens}[{xp_last_on_line}{infix_condition}]"), | ||
glue::glue("//{no_paren_keywords}[{xp_last_on_line}]"), | ||
glue::glue("//{keyword_tokens}/following-sibling::OP-RIGHT-PAREN[ | ||
{xp_last_on_line} and | ||
|
@@ -186,17 +223,13 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al | |
change_end <- xml2::xml_find_num(change, xp_block_ends) | ||
if (isTRUE(change_begin <= change_end)) { | ||
to_indent <- seq(from = change_begin, to = change_end) | ||
if (change_type == "hanging") { | ||
expected_indent_levels[to_indent] <- as.integer(xml2::xml_attr(change, "col2")) | ||
is_hanging[to_indent] <- TRUE | ||
} else { # block or double | ||
if (change_type == "double") { | ||
expected_indent_levels[to_indent] <- expected_indent_levels[to_indent] + 2L * indent | ||
} else { | ||
expected_indent_levels[to_indent] <- expected_indent_levels[to_indent] + indent | ||
} | ||
is_hanging[to_indent] <- FALSE | ||
} | ||
expected_indent_levels[to_indent] <- find_new_indent( | ||
expected_indent_levels[to_indent], | ||
change_type, | ||
indent, | ||
as.integer(xml2::xml_attr(change, "col2")) | ||
AshesITR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
is_hanging[to_indent] <- change_type == "hanging" | ||
} | ||
} | ||
|
||
|
@@ -261,6 +294,16 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al | |
}) | ||
} | ||
|
||
find_new_indent <- function(current_indent, change_type, indent, hanging_indent) { | ||
AshesITR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (change_type == "hanging") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe a |
||
hanging_indent | ||
} else if (change_type == "double") { | ||
current_indent + 2L * indent | ||
} else { | ||
current_indent + indent | ||
} | ||
} | ||
|
||
build_indentation_style_tidy <- function() { | ||
paren_tokens_left <- c("OP-LEFT-BRACE", "OP-LEFT-PAREN", "OP-LEFT-BRACKET", "LBB") | ||
paren_tokens_right <- c("OP-RIGHT-BRACE", "OP-RIGHT-PAREN", "OP-RIGHT-BRACKET", "OP-RIGHT-BRACKET") | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: i usually prefer each argument with defaults on its own line once >1 line is used, WDYT? not sure it's a stated rule, but it's similar to the long lines rule for calls.