Skip to content

Use rstudioapi::modifyRange() for Addin #178

Closed

Description

@dracodoc

I have this data.table code:

ranges[, non_white_char_total := {range_count = nchar(str_replace_all(text, "\\s*", ""))
                                         cumsum(range_count)}]

I selected it, used style active region addin menu and there is this error

Error in parse(text = text, keep.source = TRUE) : 
  <text>:3:0: unexpected end of input
1: ranges[, non_white_char_total := {
2:   range_count <- nchar(str_replace_all(text, "\\s*", ""))
  ^

Activity

  1. lorenzwalthert commented on Sep 4, 2017

    @lorenzwalthert
    Collaborator

    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.

  2. dracodoc commented on Sep 4, 2017

    @dracodoc
    Author

    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.

  3. lorenzwalthert commented on Sep 5, 2017

    @lorenzwalthert
    Collaborator

    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?

  4. lorenzwalthert commented on Sep 5, 2017

    @lorenzwalthert
    Collaborator

    You can see here how much we can simplify, it's about 100 lines.
    lorenzwalthert@1b74fb0

  5. krlmlr commented on Sep 5, 2017

    @krlmlr
    Member

    I haven't closely followed this thread, but modifyRange() seems fine with me. We still can mock it for testing.

  6. lorenzwalthert commented on Sep 5, 2017

    @lorenzwalthert
    Collaborator

    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".

  7. dracodoc commented on Sep 5, 2017

    @dracodoc
    Author

    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$start
      selection_end <- context$selection[[1]]$range$end
      if (any(selection_start != selection_end)) { # text selected
  8. changed the title [-]error on styling data.table usage[/-] [+]Use rstudioapi::modifyRange() for Addin[/+] on Sep 6, 2017
  9. lorenzwalthert commented on Sep 18, 2017

    @lorenzwalthert
    Collaborator

    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

    remotes::install_github("krlmlr/styler#205")

    Can now also style unsaved files.

  10. dracodoc commented on Sep 18, 2017

    @dracodoc
    Author

    Actually they should be same...

    ?rstudioapi::modifyRange

    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 styler
    ranges[tag == "non_literal" & str_detect(text, ";"),
    text := str_replace_all(text, ";", "\n")]
  11. lorenzwalthert commented on Sep 18, 2017

    @lorenzwalthert
    Collaborator

    That's what I thought too and implemented it in bb7a2cd (my version with modifyRange) but I got:

    Error: 'text' should either be length 1, or same length as 'ranges'
    

    When styling

    if (TRUE) {3} else {  1 + +1}

    Which was supposed to become

    if (TRUE) {
      3
    } else {  
      1 + +1
    }

    So I just worked around that using insertText().

  12. dracodoc commented on Sep 18, 2017

    @dracodoc
    Author

    Interesting, look at this

    modifyRange <- insertText
  13. dracodoc commented on Sep 18, 2017

    @dracodoc
    Author

    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.

  14. dracodoc commented on Sep 18, 2017

    @dracodoc
    Author

    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.

  15. lorenzwalthert commented on Sep 18, 2017

    @lorenzwalthert
    Collaborator

    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 😊

  16. lorenzwalthert commented on Sep 18, 2017

    @lorenzwalthert
    Collaborator

    Regarding the other problem with your example code: I will file an issue.

  17. lorenzwalthert commented on Sep 18, 2017

    @lorenzwalthert
    Collaborator

    Reference: #206

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions