-
Notifications
You must be signed in to change notification settings - Fork 73
Fix eq assign parsing #276
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
Fix eq assign parsing #276
Conversation
Codecov Report
@@ Coverage Diff @@
## master #276 +/- ##
==========================================
+ Coverage 92.95% 93.17% +0.22%
==========================================
Files 28 28
Lines 1136 1173 +37
==========================================
+ Hits 1056 1093 +37
Misses 80 80
Continue to review full report at Codecov.
|
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. What a job to work around a nasty minor upstream problem...
R/relevel.R
Outdated
#' | ||
#' Although syntactically identical, [utils::getParseData()] does not produce | ||
#' the same hierarchy of the parse table (parent and id relationship) for `<-` | ||
#' and `=` (See 'Examles'). |
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.
Examples
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.
Uups.
R/relevel.R
Outdated
cumsum((is_eq_assign - lag(is_eq_assign, default = is_eq_assign[1])) > 2) | ||
empty_seq <- rep(0, nrow(pd)) | ||
empty_seq[lead(pd$token == "EQ_ASSIGN", default = FALSE)] <- eq_belongs_to_block | ||
block_id <- cumsum(empty_seq) |
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.
Is this the value this function returns?
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.
Yup. Lets add
block_id
So it is explicit.
eq_expr$parent <- NA | ||
non_eq_expr <- pd[-eq_ind,] | ||
pd <- bind_rows(eq_expr, non_eq_expr) %>% | ||
arrange(pos_id) |
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.
Same here?
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.
See above
R/relevel.R
Outdated
find_block_id <- function(pd) { | ||
is_eq_assign <- which(pd$token == "EQ_ASSIGN") | ||
eq_belongs_to_block <- | ||
cumsum((is_eq_assign - lag(is_eq_assign, default = is_eq_assign[1])) > 2) |
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.
x - lag(x)
isn't much different from diff(x)
, and cumsum(diff(x))
is about the same as x
. Does this need to be that complicated?
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.
Well I checked diff(x)
from a previous comment from you but the thing is we also need 0 to be the first element. Because with diff()
, you get n - 1
elements back, not n
. So we could do
c(0, diff(x)) # instad of
x - lag(x, default = x[1])
Then, cumsum(c(0, diff(x)) > 2)
can be simplified by pushing the zero out of cumsum
, i.e.
c(0, cumsum(diff(is_eq_assign)) > 2)
Which is, I admit, simpler, but it's maybe less clear to the programmer how we arrived at that... Anyways I can change it accordingly.
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.
Or can you simplify it even further? Cause it's not immediately clear to me how one could 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.
Maybe simplify a bit further?
R/relevel.R
Outdated
#' @param pd A parse table. | ||
find_block_id <- function(pd) { | ||
is_eq_assign <- which(pd$token == "EQ_ASSIGN") | ||
eq_belongs_to_block <- c(0, cumsum(diff(is_eq_assign)) > 2) |
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.
Are you sure it's not c(0, cumsum(diff(is_eq_assign) > 2))
?
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.
uups yes.
R/relevel.R
Outdated
#' `EQ_ASSING` already belongs to the `EQ_ASSING` after it. | ||
#' @param pd A parse table. | ||
find_block_id <- function(pd) { | ||
is_eq_assign <- which(pd$token == "EQ_ASSIGN") |
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.
Naming: is_
hints to a logical, maybe idx_
?
R/relevel.R
Outdated
eq_belongs_to_block <- c(0, cumsum(diff(is_eq_assign)) > 2) | ||
|
||
empty_seq <- rep(0, nrow(pd)) | ||
empty_seq[lead(pd$token == "EQ_ASSIGN", default = FALSE)] <- eq_belongs_to_block |
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.
empty_seq[idx_eq_assign + 1] <- ...
?
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.
it's a minus 😄 .
26fd015
to
245c483
Compare
245c483
to
c51460b
Compare
- Adapt documentation (#290). - Add roundtrip (#287). - Fix AppVeyor builds. - Fix token insertion / comment interaction (#279). - Clarify labelling strategy (#285). - Fixing and extending Rstudioaddins (#283). - Fix eq assign parsing (#276). - style_files -> vectorized style_file (#273). - Refactoring (#270). - Fix CI (#275). - Fix covr (#274). - Renaming files (#271). - Handle styling of an unsaved active file (#243). - Test R 3.1 and R 3.2 (#249). - Allow empty {} without line break (#261). - Wrap expr in expr before enclosing with curly braces (#263). - Avoid checking for hard-coded dot (#262). - Account for dependency renaming (utf8 changed to enc) (#264). - Indention of function declaration and closing braces (#260). - Only remove line break before closing with strict option (#252).
Closes #259. As outlined in #259, we
a = b = c
) at most.However, we only do that if there is an
EQ_ASSIGN
token in the flat parse data since the whole procedure is rather costly and we can expect the probability that there is anEQ_ASSIGN
in the parse data to be small.