R/vscode.R

Defines functions installVSCodeAddinsBindings suggestSnippets suggestFunctions readSnippets getSnippetPath transformSnippets

Documented in getSnippetPath installVSCodeAddinsBindings readSnippets suggestFunctions suggestSnippets transformSnippets

# Todolist
# [ ] Get familiar with language server and vscode, and potentially write these newShortcuts with languageserver functionality
# [ ] Insert Roxygen skeleton
# [ ] Extract function
# [ ] Extract variable
# [ ] Run code section
#   * Fork the vscode-R repository and implement the functionality or create an issue
# [ ] alt+- shortcut
# [ ] Collapse variables in outline keeping only section names
# [ ] Map newShortcuts to keyboard shortcuts
# [ ] Transform this into a proper package
# [ ] Radian complete while typing
#   * ~/.config/radian/profile
#   * options(radian.complete_while_typing=FALSE)
# [ ] https://github.com/REditorSupport/vscode-R/wiki/Keyboard-shortcuts

#' Title
#'
#' @param filename 
#'
#' @return
#' @export
#'
#' @examples
transformSnippets <- function(filename) {
  stop("Implement todos first")
  # [ ] Escaping of dollars and dots
  # ..  -----
  snippets <- readSnippets()
  
  formatJson <- function(snippetName, snippetContent) {
    snippetJson <- c(
      paste0('"', snippetName, '": {'),
      paste0('  "prefix": ', '"', snippetName, '",'),
      paste0('  "body": ['),
      paste0(paste0('    "', gsub('"' , '\\\\"', snippetContent), '"'), collapse = ",\n"),
      paste0('  ],'),
      paste0('  "description": ', '"', snippetName, '"'),
      paste0("}")
      )
    paste0(snippetJson, collapse = "\n")
  }
  snippets <- sapply(snippets, function(x) formatJson(snippetName = x$snippetName, snippetContent = x$snippetContent))
  snippets <- paste0(snippets, collapse = ",\n")
  writeLines(c("{", snippets, "}"), "~/.config/Code/User/snippets/r.json")
  
}


#' get file path of snippet file
#'
#' @return character to the snippet file
#' @export
#' @author Daniel Lill (daniel.lill@intiquan.com)
#' @md
#'
#' @examples
#' getSnippetPath()
getSnippetPath <- function() {
  snippetPath <- c(
    "~/PROJECTS/SHARE/PROJTOOLS/conveniencefunctions/inst/setup_rstudio/snippets/r.snippets",
    "~/Promotion/Promotion/Projects/conveniencefunctions/inst/setup_rstudio/snippets/r.snippets"
  )
  snippetPath <- snippetPath[file.exists(snippetPath)]
  if (!length(snippetPath)) stop("Snippet file cannot be found")
  snippetPath <- snippetPath[1] # Take the first of the available ptions
  snippetPath
}


#' Title
#'
#' @return
#' @export
#'
#' @examples
readSnippets <- function() {
  snippetPath <- getSnippetPath()
  sn <- readLines(snippetPath)
  sn <- sn[sn != ""]
  sn <- grep("^#", sn, value = TRUE, invert = TRUE)
  sn
  idxsn <- grep("^snippet", sn)
  idxsncontent <- lapply(seq_along(idxsn), function(i) {
    nextsn <- ifelse(i<length(idxsn), idxsn[i+1]-1, length(sn))
    seq((idxsn[i] + 1),nextsn)
  })
  
  snippets <- lapply(seq_along(idxsn), function(i) {
    list(snippetName = gsub("snippet ", "", sn[idxsn[i]]),
         snippetContent = gsub("\t", "", sn[idxsncontent[[i]]])
    )
  })
}

#' Suggest some functions/Snippets
#' @return Called for side-effect
#' @export
#'
#' @examples
#' suggestFunctions()
suggestFunctions <- function() {
  sn <- ls(asNamespace("conveniencefunctions"))
  sn <- sample(sn, 5)
  cat("\033[1m\033[32m*** Today's function suggestions ***\033[39m\033[22m\n")
  cat("* ")
  cat(sn, sep = "\n* ")
}

#' @export
#' @rdname suggestFunctions
suggestSnippets <- function() {
  sn <- readSnippets()
  sn <- sapply(sn, function(x) x$snippetName)
  sn <- sample(sn, 5)
  cat("\033[1m\033[32m*** Today's snippet suggestions ***\033[39m\033[22m\n")
  cat("* ")
  cat(sn, sep = "\n* ")
}




#' get file path of snippet file
#'
#' @return character to the snippet file
#' @export
#' @author Daniel Lill (daniel.lill@intiquan.com)
#' @md
#'
#' @examples
installVSCodeAddinsBindings <- function() {
  fl <- system.file("setup_rstudio/keybindings/addins.json", package = "conveniencefunctions")
  newShortcuts <- jsonlite::read_json(fl)
  newShortcuts <- lapply(names(newShortcuts), function(nm) {
    list(
      description = gsub("conveniencefunctions::","",nm), 
      key = paste0("ctrl+k ",tolower(newShortcuts[[nm]])),
      command = "r.runCommand",
      when = "editorTextFocus",
      args = paste0(nm,"()")
    )
  })
  newDescriptions <- lapply(newShortcuts, function(x) {x$description})
  newDescriptions <- do.call(c, newDescriptions)
  
  oldShortcuts <- jsonlite::fromJSON("~/.config/Code/User/keybindings.json", simplifyVector = FALSE)
  oldDescriptions <- lapply(oldShortcuts, function(x) {x$description})
  oldDescriptions <- do.call(c, oldDescriptions)
  shortcuts <- c(oldShortcuts, newShortcuts[!newDescriptions %in% oldDescriptions])

  writeLines(jsonlite::toJSON(shortcuts, auto_unbox = TRUE, pretty = TRUE), con =  "~/.config/Code/User/keybindings.json")
  
}
dlill/conveniencefunctions documentation built on Sept. 30, 2022, 4:40 a.m.