-
Notifications
You must be signed in to change notification settings - Fork 73
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
Option to remove comments? #334
Comments
HI @wlandau, thanks for this suggestion. library(styler)
tidyverse_style_with_comments_removed <- function() {
remove_comments <- function(pd) {
is_comment <- pd$token == "COMMENT"
pd <- pd[!is_comment,]
pd
}
tidyverse_with_comments_removed <- tidyverse_style()
tidyverse_with_comments_removed$token$remove_comments <- remove_comments
tidyverse_with_comments_removed
}
style_text(
"a <- 3 # hi
# there
2
list(
#
hi
)
", transformers = tidyverse_style_with_comments_removed()
)
#> a <- 3
#> 2
#> list(
#> hi
#> ) Created on 2018-01-30 by the reprex package (v0.1.1.9000). If you instead want comments to be replaced with the empty string, this seems a bit more complicated due to line breaks, but I think I managed to find a solution for this too, check out the penultimate commit in the aforementioned branch. We may consider adding this feature at some point in some form to styler, I am just not sure if it should be a styling option on the same level as whether or not you want to modify just spaces or also indention since it has (potentially detrimental) effects if used without version control. |
Thanks, @lorenzwalthert! I really appreciate your consideration, especially given that this is probably not a common use case for the tidyverse. My use of Currently, I am using |
Ok, great. So is it ok for you to implement that style guide yourself in the drake package along the lines outlined above? styler was created with flexibility in mind, but we have not really decided on how to handle "third-party" style guides, i.e. whether we want to add them to styler or whether we want people to distribute them in their own packages. However, with respect to your use case, I am not sure if styler is all you need. Consider this library(styler)
style_text("a \nb")
#> a
#> b
style_text("a \n\nb")
#> a
#>
#> b Created on 2018-01-30 by the reprex package (v0.1.1.9000). It's the same code but it does not give the same styling because styler does not remove the line breaks here. I think what you want to check is whether, given all comments are dropped, the code evaluates to the same (i.e. the expression is the same). Hence, you probably want to parse the code and check for identity library(styler)
(one_line_break <- style_text("a \nb"))
#> a
#> b
(two_line_breaks <- style_text("a \n\nb"))
#> a
#>
#> b
identical(
parse(text = one_line_break, keep.source = FALSE),
parse(text = two_line_breaks, keep.source = FALSE)
)
#> [1] TRUE Created on 2018-01-30 by the reprex package (v0.1.1.9000). Obviously this does not check for the values of a and b but that's probably not required if I understand you correctly. |
Yes, I think if I do go with You bring up a great point about line breaks. That's one area where I still rely on
|
You could even rely on
|
As far as blank lines go, I think that might be worth a separate issue. |
Reference: #335 |
Yes, I did rely on |
Normalizing code (with removing comments) may be useful in other cases too. I don't mind if we support this, given that it seems easy to implement. |
This is a candidate for https://github.com/lorenzwalthert/styler.yours. |
Here is a style guide that does just that: remove comments: https://github.com/lorenzwalthert/styler.nocomments. |
For my use case, I need to remove comments from code too. Desired behavior:
The text was updated successfully, but these errors were encountered: