Skip to content

Instantly share code, notes, and snippets.

@gadenbuie
Created December 30, 2022 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gadenbuie/7fe84038a5bfd39bbcc6ef13b47a3420 to your computer and use it in GitHub Desktop.
Save gadenbuie/7fe84038a5bfd39bbcc6ef13b47a3420 to your computer and use it in GitHub Desktop.
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