Skip to content

Issue 26: Added detection of bracket and pipe blocks.#82

Merged
Ikuyadeu merged 1 commit intoREditorSupport:masterfrom
andycraig:issue-26-brackets-and-pipe-blocks
Aug 16, 2018
Merged

Issue 26: Added detection of bracket and pipe blocks.#82
Ikuyadeu merged 1 commit intoREditorSupport:masterfrom
andycraig:issue-26-brackets-and-pipe-blocks

Conversation

@andycraig
Copy link
Copy Markdown
Collaborator

Closes #26

If no editor text is selected, r.runSelection will check around the line on which the cursor lies and send to the R console the chunk of code necessary to make a complete code line (by matching brackets and following pipes and other operators).

Some examples of code chunks it will send to the console:

  1. Multi-lined bracketed expressions:
a = list(x = 1,
         y = 2,
         z = 3)

a[1
    ]
  1. Lines joined by pipes and other operators:
library(magrittr) # For %>%
library(ggplot2)

1 + 1 %>%
    print()

ggplot(aes(speed, dist), data = cars) +
    geom_point()
  1. Combinations thereof:
library(magrittr) # For %>%

list(x = 1,
        y = 2,
        z = 3) %>%
    print()

If the algorithm encounters unmatched brackets, it just sends the current line.

Behaviour differs from that of RStudio in some cases. Roughly, vscode-R will send the smallest valid block of code, whereas RStudio sends the largest () or [] expression within the smallest {}. Here is an example:

(                     # 1. RStudio and vscode-R send lines 1-9
    {                 # 2. RStudio and vscode-R send lines 2-8
        (             # 3. RStudio and vscode-R send lines 3-7
            (         # 4. RStudio sends lines 3-7; vscode-R sends lines 4-6 
                1     # 5. RStudio sends lines 3-7; vscode-R sends line 5
            )         # 6. RStudio sends lines 3-7; vscode-R sends lines 4-6
        )             # 7. RStudio and vscode-R send lines 3-7
    }                 # 8. RStudio sends lines 1-9; vscode-R sends lines 2-8
)                     # 9. RStudio and vscode-R send lines 1-9

The algorithm would ideally ignore brackets in strings, but I haven't implemented that feature. If it proves to be a problem in practice then it could be dealt with in a separate issue/PR.

@Ikuyadeu
Copy link
Copy Markdown
Member

@andycraig , Thank you awesome Pull Request!
I think this change can be merged.
@Ladvien , how do you think this?

@Ladvien
Copy link
Copy Markdown
Collaborator

Ladvien commented Aug 16, 2018

@Ikuyadeu, it looks great! Much better than what I had. Sadly, I’ve not had a chance to run it. But if it works, I say merge it.

Thanks @andycraig

@Ikuyadeu
Copy link
Copy Markdown
Member

Ikuyadeu commented Aug 16, 2018

@Ladvien Thank you for your review!
I'll merge and check this tonight.
And, if it does not have some problems, publish as the new version!

@Ikuyadeu Ikuyadeu merged commit 8122c91 into REditorSupport:master Aug 16, 2018
@Ikuyadeu
Copy link
Copy Markdown
Member

@andycraig OK, now little fixed for maintainability, and published as the v0.6.1!
Thank you for your good fix!

@andycraig
Copy link
Copy Markdown
Collaborator Author

@Ikuyadeu Thank you for your readability/maintainability fixes, I like them and I learnt a couple of things from them.

@Ikuyadeu @Ladvien Thank you both very much! I love this extension and I'm happy to be able to make a contribution.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants