-
Notifications
You must be signed in to change notification settings - Fork 94
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
Expression-level on-type-formatting #209
Expression-level on-type-formatting #209
Conversation
The dot strategy requires some rework because if under default styler settings, mtcars %>%
. becomes mtcars %>%
.() |
Another thing needs to be fixed is Hit enter before mtcars %>% # a comment |
Since And inserting |
It worked perfectly on ubuntu, but I got an error on windows:
|
What version of R do you have? |
@paulofelipe Didn't notice that See https://cran.r-project.org/doc/manuals/r-devel/NEWS.html.
|
@paulofelipe c3290f0 should fix it. |
It worked! Indeed I'm using and older R version (3.5.1). Thanks! |
Closes #208
This PR enhances the current implementation of on-type-formatting handler which only tries to format the single-line under editing.
Parsing a short chunk of code without generating source reference is super fast, which allows us to use
parse(text = ., keep.source = FALSE)
repeatedly to find the parse-able expression before current line to perform formatting.Currently, only
)
,]
,}
and\n
are used to trigger on type formatting. The closing brackets are usually with complete expressions before cursor, while the line break can be in the end of either a complete expression (a single line call likefoo()
) or an incomplete expression to be continued in the next line (typical use case is pipelines ending with ggplot's+
or magrittr's%>%
)To handle the case of
\n
in the end of an incomplete expression, we use a zero strategy, i.e. if the end line is empty or only whitespace, then we replace that line with0
to make the expression a complete one.internally becomes
Then we use a backward parsing strategy, i.e., we expand the code chunk backward line by line from the end line on request and try to parse the code. We stop backward parsing when
When we find the start line of the expression, and we format the code in the range and cut the zero if used.
Both backward parsing and formatting are called in
tryCatchTimeout()
which usessetTimeout()
to limit the number of seconds to evaluate an expression. We set the timeout of backward parsing to 0.1s and formatting to 1s at the moment.Now the on-type-formatting works nicely with the following use cases: