-
Notifications
You must be signed in to change notification settings - Fork 73
Math token spacing #221
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
Math token spacing #221
Conversation
dfded2a
to
5560809
Compare
As a follow-up and for consistency, other spacing rules could also be transformed from |
5560809
to
c916d63
Compare
c916d63
to
698dc9f
Compare
698dc9f
to
eebf8f3
Compare
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.
Thanks. I haven't looked through all of the code, but the results make sense. Maybe we should consider an option that adds explicit parens to complex arithmetic expressions? In this case, the spacing wouldn't matter that much.
@@ -0,0 +1 @@ | |||
1++1-3/23*3 ^ 4 |
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.
I wonder if the other way round would be more usual, because of operator precedence:
+1 + +1 - 3 / 23 * 3^4
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.
Right, I can adapt the test. The default should remain as in the tidyverse style guide, so all operators have spacing around them I think.
R/style_guides.R
Outdated
#' strict = TRUE | ||
#' ) | ||
#' @export | ||
specify_math_token_spacing <- |
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.
Maybe default_math_token_spacing
?
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.
Not sure whether I understand you correctly. What does it have to do with a default? Everything will be specified unambiguously. We can also drop the specify
from the name in order to make it sound a bit more simplistic.
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.
Oh, I see. I only saw the use of the function as a default argument. Maybe implement two functions then, math_token_spacing()
and default_math_token_spacing()
, and have the latter forward to the former?
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.
Ok, we can do that.
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.
I am not sure whether we need the forwarder. I.e. if we only have one style guide, then that might be fine, but other style guides may have other defaults. So maybe call it tidyverse_math_token_spacing()
?
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.
We cannot use a function that has the same name as an argument (math_token_spacing()
):
tidyverse_style <- function(scope = "tokens",
strict = TRUE,
indent_by = 2,
start_comments_with_one_space = FALSE,
math_token_spacing = math_token_spacing()) {
# [...]
Because we get
#> Error: promise already under evaluation: recursive default argument reference or earlier
#> problems?
I suggest to revert to specify_math_token_spacing()
, since that is also a verb and functions are supposed to be verbs according to the tidyverse style guide I think.
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.
@krlmlr ok? We can keep tidyverse_math_token_spacing()
that forwards to specify_math_token_spaching()
.
Yes, maybe we can consider adding braces to complex expressions. However, I am not sure how straightforward that is. Depends a lot on the structure of the nested parse data I think. Maybe other PR or issue? |
740cb9b
to
8ecf731
Compare
Closes #86. |
- Hotfix: utf8 should not be verbose (#245). - Allow styling of Rmd files(#233). - Remove duplicate @family (#244). - Fixing token insertion (#242). - Capitalize Addin titles (#241). - Explicit `NULL` creation to make styler compatible with R3.2.0 (#237). - Improve vignettes (#232). - Allow exclusion of files with `style_pkg()` and `style_dir()`. - Correct styling with long strings (#230). - Add tools for re-indenting tokens (#217). - Math token spacing (#221). - Remove outdated line and col information (#218). - Empty input for styling does not cause an error (#227, #228). - Tools to insert tokens + application on `if`-`else` clauses (#212). - Improve example in documentation (#222). - Fix spacing around in (#214). - Maintenance: renaming functions / files, extend helper, documentation, if_else etc. (#204). - Disallow line break after ( for function calls (#210). - Preserve space between `!` and bang (#209). - Simplify RStudio Addin (#208, #211). - Indention based on square brackets (#207). - Add vignette on introducing styler (#203). - Indent function declaration without curly braces correctly (#202). - Fix indention in if-else statement (#200). - Sorting key (#196). - Use safe sequences (#197). - Fix space between two commas (#195). - Keep single-line pipes on one line (#74). - Remove tidyr and dplyr dependency (#182, #183, @jimhester). - Fix parsing inconsistency (#188). - Substitute create filler (#190). - Introducing class vertical (#189). - Adapt line break rules (#172). - Fix `R CMD check` (#185). - Force argument evaluation for proper error handling (#179). - Add nonstrict version of set_space_before_comment (#174). - Add installation instructions to README (#175). - Addin to style highlighted region (#143). - Improve spelling (#168). - Add coverage badge - Change badge from WIP to active - Add the number of files to message (#166). - Improve documentation (#164). - Add informative messages while styling files (#165). - More examples in help file (#161). - No line breaks after pipe if comment is next token (#160). - Fixing spacing around `!` (non-bang-bang) (#157). - Finalize function documentation (#154). - Review vignette (#158). - Update bang-bang rule (#156).
This PR gives granular control over spacing around math tokens, as envisioned in #82 and requested in tidyverse/lubridate#577 (comment).
Implements a new paradigm for transformer functions in the sense that
strict
is now an argument tothe transformer. This reduces duplication of code, since the only part that
set_*
andadd_*
functions for spacing rules are different is thatset_*
usespd_flat$spaces[...] <- level
whereaseadd_*
usespd_flat$spaces[...] <- pmax(1, pd_flat$spaces[...]
.Functions that follow this paradigm should be named
style_*
instead ofadd_*
andset_*
.pd_flat
is the last argument tostyle_space_around_math_token()
because this allows usingpurrr::partial()
without naming arguments.