You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Did you highlight all code? I think there are some problems with rstudioapi, the package that powers this feature of styler. I can reproduce the error when I first style the code (no error), then revert the changes via the menu Edit -> Back and then try to style again. After that, I get the error you get.
Can you try to delete a letter (e.g. the first letter in your expression), manually inserting it again (instead of using Edit -> Back) so the code is again the same as the original code, save it, and try the Addin again?
Note that the file must be saved already for the Addins (styling of the active file or active region) to work, but I guess that was the case in your situation already.
Originally my code was in a code chunk of RMarkdown, maybe that also caused some problem. I moved the code into a .R script, just like what you experienced, if Edit->back is used, the 2nd time will have this error.
I saw your code need to read the whole script as context, which also set the limit that file must be saved first. Is the whole context really needed? If you don't have to read context, you can also support RMarkdown directly.
I just tried styling a highlighted region in markdown and it seems to work fine on my side. The styling of whole markdown documents is on the todo list (#13).
I actually overlooked the function rstudioapi::modifyRange() which could do what you propose. It could simplify our code quite a bit, because essentially all internal functions for handling parts of lines etc. would be gone. Thanks for the hint 😊.
However, using such an approach seems to make it impossible to test the function because we have to provide the ID of the document we want to modify to rstudioapi::modifyRange() and that ID is usually returned by getActiveDocumentContext(). R CMD Check tests are ran in an isolated directory as far as I know, so we cannot "foresee" the ID of our document to style.
@krlmlr Do you think we should use rstudioapi::modifyRange() which simplifies our code and allows styling unsaved files and just bear with the fact that we can't test it anymore?
I don't see how we can mock because we need the ID of the temporary *-in.R file for modifyRange() (instead of a path), which is usually obtained via getActiveDocumentContext() but as we are not in an interactive session, we cannot call this function. Calling it via source in the *-in.R file gives context "console".
I thought you need the whole file as context so I didn't suggest modifyRange directly at first. If you don't need it, then this definitely is much easier with a lot of advantages.
This part is interactive usage so it's naturally difficult to test automatically. I think as long as you have some test on the command line equivalent, this should be fine. The only part left untested is limited and easy to be tested by users.
You may want to test the selection first though, otherwise there might be some error if user clicked this menu without selecting anything. I have some code like this:
context<-rstudioapi::getActiveDocumentContext()
selection_start<-context$selection[[1]]$range$startselection_end<-context$selection[[1]]$range$endif (any(selection_start!=selection_end)) { # text selected
The thing is I think you can't use rstudioapi::modifyRange() because we might return a longer or shorter expression (in terms of the number of lines). However, we could simply use rstudioapi::insertText() to first clear the range and then insert the text. I created #205 so @dracodoc if you want you can test it with
modifyRange is a synonym for insertText, but makes its intent clearer when working with ranges, as performing text insertion with a range will replace the text previously existing in that range with new text.
#205 worked with the code example above without problem. I do find some other code result not optimal though:
ranges[tag=="non_literal"& str_detect(text, ";"),
text:= str_replace_all(text, ";", "\n")]
# become to this after stylerranges[tag=="non_literal"& str_detect(text, ";"),
text:= str_replace_all(text, ";", "\n")]
I looked the code, the real difference is not from modifyRange or insertText. The API is only expecting one string, so you need to concatenate character vector into single string. Your newer version did this so it worked.
And you should not need to clear the range first before inserting new content.
performing text insertion with a range will replace the text previously existing in that range with new text.
That means user will see deletion when pressing ctrl-Z. I wanted to switch between old version and new version to see the difference, and this deletion make that difficult to compare.
Activity
lorenzwalthert commentedon Sep 4, 2017
Did you highlight all code? I think there are some problems with
rstudioapi, the package that powers this feature of styler. I can reproduce the error when I first style the code (no error), then revert the changes via the menu Edit -> Back and then try to style again. After that, I get the error you get.Can you try to delete a letter (e.g. the first letter in your expression), manually inserting it again (instead of using Edit -> Back) so the code is again the same as the original code, save it, and try the Addin again?
Note that the file must be saved already for the Addins (styling of the active file or active region) to work, but I guess that was the case in your situation already.
dracodoc commentedon Sep 4, 2017
Originally my code was in a code chunk of RMarkdown, maybe that also caused some problem. I moved the code into a .R script, just like what you experienced, if Edit->back is used, the 2nd time will have this error.
I saw your code need to read the whole script as context, which also set the limit that file must be saved first. Is the whole context really needed? If you don't have to read context, you can also support RMarkdown directly.
lorenzwalthert commentedon Sep 5, 2017
I just tried styling a highlighted region in markdown and it seems to work fine on my side. The styling of whole markdown documents is on the todo list (#13).
I actually overlooked the function
rstudioapi::modifyRange()which could do what you propose. It could simplify our code quite a bit, because essentially all internal functions for handling parts of lines etc. would be gone. Thanks for the hint 😊.However, using such an approach seems to make it impossible to test the function because we have to provide the ID of the document we want to modify to
rstudioapi::modifyRange()and that ID is usually returned bygetActiveDocumentContext(). R CMD Check tests are ran in an isolated directory as far as I know, so we cannot "foresee" the ID of our document to style.@krlmlr Do you think we should use
rstudioapi::modifyRange()which simplifies our code and allows styling unsaved files and just bear with the fact that we can't test it anymore?lorenzwalthert commentedon Sep 5, 2017
You can see here how much we can simplify, it's about 100 lines.
lorenzwalthert@1b74fb0
krlmlr commentedon Sep 5, 2017
I haven't closely followed this thread, but
modifyRange()seems fine with me. We still can mock it for testing.lorenzwalthert commentedon Sep 5, 2017
I don't see how we can mock because we need the ID of the temporary *-in.R file for
modifyRange()(instead of a path), which is usually obtained viagetActiveDocumentContext()but as we are not in an interactive session, we cannot call this function. Calling it viasourcein the *-in.R file gives context "console".dracodoc commentedon Sep 5, 2017
I thought you need the whole file as context so I didn't suggest
modifyRangedirectly at first. If you don't need it, then this definitely is much easier with a lot of advantages.This part is interactive usage so it's naturally difficult to test automatically. I think as long as you have some test on the command line equivalent, this should be fine. The only part left untested is limited and easy to be tested by users.
You may want to test the selection first though, otherwise there might be some error if user clicked this menu without selecting anything. I have some code like this:
[-]error on styling data.table usage[/-][+]Use rstudioapi::modifyRange() for Addin[/+]lorenzwalthert commentedon Sep 18, 2017
The thing is I think you can't use
rstudioapi::modifyRange()because we might return a longer or shorter expression (in terms of the number of lines). However, we could simply userstudioapi::insertText()to first clear the range and then insert the text. I created #205 so @dracodoc if you want you can test it withCan now also style unsaved files.
dracodoc commentedon Sep 18, 2017
Actually they should be same...
?rstudioapi::modifyRange#205 worked with the code example above without problem. I do find some other code result not optimal though:
lorenzwalthert commentedon Sep 18, 2017
That's what I thought too and implemented it in bb7a2cd (my version with
modifyRange) but I got:When styling
Which was supposed to become
So I just worked around that using
insertText().dracodoc commentedon Sep 18, 2017
Interesting, look at this
dracodoc commentedon Sep 18, 2017
I looked the code, the real difference is not from
modifyRangeorinsertText. The API is only expecting one string, so you need to concatenate character vector into single string. Your newer version did this so it worked.dracodoc commentedon Sep 18, 2017
And you should not need to clear the range first before inserting new content.
That means user will see deletion when pressing
ctrl-Z. I wanted to switch between old version and new version to see the difference, and this deletion make that difficult to compare.lorenzwalthert commentedon Sep 18, 2017
Thanks. You are absolutely right obviously. Don't know why I can't solve this the right way. Looking forward to close this issue haha 😊
lorenzwalthert commentedon Sep 18, 2017
Regarding the other problem with your example code: I will file an issue.
lorenzwalthert commentedon Sep 18, 2017
Reference: #206