Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kjhealy/203d0a31ac8ee0f8bc4023f9e233b85a to your computer and use it in GitHub Desktop.
Save kjhealy/203d0a31ac8ee0f8bc4023f9e233b85a 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