Forked from gadenbuie/migrate-rstudio-snippets-to-vscode.R
Created
September 27, 2024 00:35
-
-
Save kjhealy/203d0a31ac8ee0f8bc4023f9e233b85a to your computer and use it in GitHub Desktop.
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
snps <- | |
list(r = "~/.config/rstudio/snippets/r.snippets") %>% | |
purrr::map(readLines, warn = FALSE) %>% | |
purrr::map(paste, collapse = "\n") %>% | |
purrr::map(trimws) %>% | |
purrr::map(strsplit, split = "(^|\n)snippet ") %>% | |
purrr::map_depth(2, ~ .x[.x != ""]) %>% | |
purrr::map_depth(2, ~ { | |
nm <- gsub("^([^\n\t ]+).*", "\\1", .x) | |
names(.x) <- nm | |
.x | |
}) %>% | |
purrr::flatten() | |
# write out the snippets in the vscode json format | |
snps$r %>% | |
purrr::imap(function(body, prefix) { | |
body <- sub("^\t", "", strsplit(trimws(body), "\n")[[1]][-1]) | |
list( | |
prefix = prefix, | |
body = body | |
) | |
}) %>% | |
jsonlite::toJSON(pretty = 2, auto_unbox = TRUE) %>% | |
clipr::write_clip() | |
# Open "Configure user snippets" then "r.json" in vscode | |
# Paste the JSON into the template file | |
# Remove outer braces `{` and `}` from pasted text | |
# Remove any comments that were in the source snippets (if you have any) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment