-
Notifications
You must be signed in to change notification settings - Fork 5
Init a guide about R snippets #67
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
478870b
Init a guide about R snippets
jennybc 3a2e3fc
Try this
jennybc b796a20
Add to listing of R migration guides
jennybc d3c284f
Update r-snippets.qmd
jennybc e3ee2f4
Apply suggestions from code review
jennybc 123871e
Apply suggestions from code review
jennybc d1fc0a5
Update r-snippets.qmd
jennybc 67024cb
Try to limit vale to files involved in the PR
jennybc a338f64
How about added files?
jennybc 6909623
Trying to not fail for warnings
jennybc 16f4c83
Trying a different reporter
jennybc dfeb6ff
Back to fail_on_error
jennybc 8c5e3f3
Update screenshot now that `for` appears in bare and snippet form
jennybc f91c30c
Go back to the vale linting workflow from `main`
jennybc 5f9c64c
Polishing up
jennybc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--- | ||
title: "Snippets for R" | ||
--- | ||
|
||
Code snippets let you insert ready-made templates for common coding patterns. | ||
For example, `for` is a reserved word in R that is used to create a loop. | ||
In actual usage, `for` is always part of a larger construct: | ||
|
||
```r | ||
for (variable in vector) { | ||
# code to repeat | ||
} | ||
``` | ||
|
||
A snippet for `for` inserts this whole skeleton, with special placeholders that let you use <kbd>Tab</kbd> to move through the positions for `variable` and `vector`, and `# code to repeat`. | ||
|
||
{width=700} | ||
|
||
It is common for IDEs to provide explicit support for code snippets and this certainly holds true for RStudio and Positron. | ||
This guide is intended to help users who have used snippets in RStudio adapt their approach for Positron, which inherits snippet behaviour from its VS Code roots. | ||
|
||
Documentation on snippets in: | ||
|
||
* [RStudio](https://docs.posit.co/ide/user/ide/guide/productivity/snippets.html) | ||
* [VS Code](https://code.visualstudio.com/docs/editing/userdefinedsnippets) | ||
|
||
## Default and custom snippets in RStudio | ||
|
||
RStudio ships with a default set of snippets. | ||
For our purposes, it's useful to break them into two broad classes: | ||
|
||
* Reserved words: These snippets relate to keywords that have special meaning. This includes control flow elements, such as `if`, `while`, and `for`, as well as the `function` keyword. | ||
* Everything else: These snippets relate to a selection of functions, mostly in the so-called base packages. Examples include `matrix()`, the `apply()` family of functions, and various functions around S4 classes, such as `setClass()`. | ||
|
||
You can customize your RStudio snippets via the *Edit Snippets* button in *Global Options > Code*. | ||
Under the hood, this initializes a user-level snippet file that is pre-populated with RStudio's default snippets. | ||
You can then apply your desired changes, such as adding or deleting snippets. | ||
Going forward, this user-level file powers all of your RStudio snippets, i.e. there is no combining of built-in and user-level snippets. | ||
|
||
## Default and custom snippets in Positron | ||
|
||
The completion experience in Positron arises from two components working together: | ||
|
||
* Positron: The IDE controls the overall completion experience, which is the heart of the so-called [IntelliSense features](https://code.visualstudio.com/docs/editing/intellisense#_intellisense-features) in VS Code. IntelliSense provides the user interface for completions and is also in charge of combining/filtering/sorting completions coming from multiple sources, including snippets. These snippets can come from various sources: | ||
- Extensions | ||
- User settings | ||
- Workspace settings | ||
* R language server: The [ark kernel](https://github.com/posit-dev/ark) provides an LSP server for R and, in particular, ark produces all (or almost all) of the completions you see for R. | ||
|
||
This division of labor means that the completion and snippet responsibilities are a bit different in Positron than in RStudio. | ||
|
||
### Reserved words | ||
|
||
In Positron, the completion items related to R's reserved words are built into ark. | ||
Each reserved word can be completed on its own and, in some cases, as part of a larger snippet. | ||
|
||
{width=700} | ||
|
||
### Everything else | ||
|
||
If you want snippets beyond those provided for R's reserved words, you'll need to configure that for yourself. | ||
Press <kbd>Cmd/Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> to open the command palette and type *Snippets: Configure Snippets*. | ||
|
||
Snippets can be stored in three types of files: | ||
|
||
1. "Global": contains snippets that are not limited to one language. Create with *New Global Snippets file...*. | ||
- Individual snippets can have a `scope` which specifies one or more languages. Include `"r"` in the `scope` for any snippet you want to use in R. | ||
- This writes a `.code-snippets` file alongside your other Positron user settings. It is pre-populated with example snippets for both R and Python. | ||
1. "Global" but for one workspace: contains snippets for multiple languages but tied to a specific workspace. Create with *New Snippets file for 'MyProject'*. | ||
- This writes a `.code-snippets` file alongside the other settings for the workspace, pre-populated with example snippets. | ||
|
||
{width=700} | ||
1. Language-specific: contains snippets for one specific language. Create by selecting the language from the drop-down menu, which includes entries for R, Python, and Quarto. | ||
|
||
In the case of R, this writes an `r.json` file alongside your other Positron user settings. It is pre-populated with an R example snippet. | ||
|
||
{width=700} | ||
|
||
Although both RStudio and Positron keep track of custom snippets in a file, they don't use the same syntax. | ||
Positron uses the TextMate syntax, which is inherited from VS Code. | ||
If you miss specific snippets from RStudio's defaults, you can consult this file to see what they look like when translated to the syntax used by Positron: [`r.code-snippets`](https://github.com/posit-dev/ark/blob/19337a1b41e8c5a3a77ac61db93b7c6bf6cdc8a3/crates/ark/resources/snippets/r.code-snippets). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.