~hrbrmstr/cloc

ca0798ae57ae61c738970b0afb0a24829c99c821 — boB Rudis 6 years ago 0554026
addin
7 files changed, 48 insertions(+), 0 deletions(-)

M DESCRIPTION
M NAMESPACE
M NEWS.md
M R/cloc-package.r
A R/strip-rmd.R
M R/utils.R
A inst/rstudio/addins.dcf
M DESCRIPTION => DESCRIPTION +1 -0
@@ 14,6 14,7 @@ Description: Counts blank lines, comment lines, and physical lines of source cod
SystemRequirements: perl
Copyright: file inst/COPYRIGHTS
Imports:
    rstudioapi (>= 0.5),
    git2r,
    utils, 
    dplyr,

M NAMESPACE => NAMESPACE +1 -0
@@ 10,6 10,7 @@ export(cloc_os)
export(cloc_remove_comments)
export(cloc_reognized_languages)
export(cloc_version)
import(rstudioapi)
importFrom(git2r,clone)
importFrom(processx,run)
importFrom(utils,contrib.url)

M NEWS.md => NEWS.md +1 -0
@@ 10,6 10,7 @@
- URLs as sources work in more funtions now
- cloc_git() can now clone repo when using git:// URLs
- removed dplyr dependency
- created an RStudio addin to enable reducing the active Rmd document to just code (in a new document)

# cloc 0.1.0


M R/cloc-package.r => R/cloc-package.r +1 -0
@@ 54,6 54,7 @@
#' @name cloc-package
#' @docType package
#' @author Bob Rudis (bob@@rud.is)
#' @import rstudioapi
#' @importFrom git2r clone
#' @importFrom processx run
#' @importFrom utils read.table contrib.url download.file download.packages tail

A R/strip-rmd.R => R/strip-rmd.R +38 -0
@@ 0,0 1,38 @@
#' @keywords internal
strip_rmd <- function() {

  ctx <- rstudioapi::getActiveDocumentContext()

  if (!is.null(ctx)) {

    if (is_rmd_file(ctx$path)) {

      x <- basename(ctx$path)
      x <- tools::file_path_sans_ext(x)

      tf1 <- tempfile(pattern = x, fileext = ".Rmd")
      on.exit(unlink(tf1), add = TRUE)

      tf2 <- tempfile(pattern = x, fileext = ".R")

      cat(ctx$contents, file = tf1, sep = "\n")

      nc <- cloc_remove_comments(tf1)

      cat(nc, file = tf2)

      navigateToFile(tf2, line = -1L, column = -1L)

      message(
        sprintf(
          "Temporary file generated is in [%s]", tf2
        )
      )

    } else {
      stop("Can only style .R and .Rmd files.", call. = FALSE)
    }

  }

}

M R/utils.R => R/utils.R +2 -0
@@ 13,3 13,5 @@ find_perl <- function() {
  return(perl)

}

is_rmd_file <- function(path) { grepl("\\.Rmd$", path, ignore.case = TRUE) }

A inst/rstudio/addins.dcf => inst/rstudio/addins.dcf +4 -0
@@ 0,0 1,4 @@
Name: Strip Rmd
Description: Removes everything but code blocks from the active Rmd file
Binding: strip_rmd
Interactive: false