Skip to content

Commit

Permalink
first version of todor plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dokato committed Sep 6, 2017
1 parent 4619211 commit b07bf39
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 0 deletions.
14 changes: 14 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Package: todor
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("Dominik", "Krzemiński",
email = "first.last@example.com",
role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.3.3)
Imports: stringr,
rex
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(todor)
import(rstudioapi)
37 changes: 37 additions & 0 deletions R/files.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
find_package <- function(path = getwd()) {
start_wd <- getwd()
on.exit(setwd(start_wd))
setwd(path)

prev_path <- ""
while (!file.exists(file.path(prev_path, "DESCRIPTION"))) {
if (prev_path == getwd()) {
return(NULL)
}
prev_path <- getwd()
setwd("..")
}
prev_path
}

process_file <- function(filepath, patterns) {
con = file(filepath, "r")
n <- 1
markers <- list()
while (TRUE) {
line = readLines(con, n = 1)
if (length(line) == 0) {
break
}
pattern_check <- find_pattern(line, patterns = patterns)
if (!is.null(pattern_check))
markers[[length(markers) + 1]] <- list(nr = n,
type = pattern_check,
text = stringr::str_replace(
line, pattern_check, "")
)
n <- n + 1
}
close(con)
markers
}
11 changes: 11 additions & 0 deletions R/patterns.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
find_pattern <- function(text, patterns = c("TODO", "FIXIT")) {
pattern <- paste(patterns, collapse="|")
pattern <- sprintf("(%s)", pattern)
extr <- stringr::str_extract(text, pattern)
if (!is.na(extr)) {
extr <- stringr::str_extract(extr, "[a-zA-Z]+")
}
else
extr <- NULL
extr
}
68 changes: 68 additions & 0 deletions R/todor.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#' TODOR
#' This package helps you to find all places in your code with places
#' to be filled in the future.
#'
#' @name todor
NULL

create_markers <- function(todo.list) {
markers <- list()
for (name in names(todo.list)) {
if (length(todo.list[[name]]) == 0) next
for (i in 1:length(todo.list[[name]])) {
x <- todo.list[[name]][[i]]
markers[[length(markers) + 1]] <- list(
type = "info",
file = name,
line = x$nr,
column = 1,
message = sprintf("[ %s ] %s", x$type, x$text)
)
}
}
markers
}

#' Build Rstudio Markers
#'
#' @param markers list of markers
#'
#' @import rstudioapi
build_rstudio_markers <- function(markers){
rstudioapi::callFun("sourceMarkers",
name = "todor",
markers = markers,
basePath = NULL,
autoSelect = "first")
}

#' @export
todor <- function(todo_types = NULL) {
pkg_path <- find_package()
files <- dir(
path = file.path(pkg_path,
c("R", "tests", "inst")
),
pattern = rex::rex(".", one_of("Rr"), end),
recursive = TRUE,
full.names = TRUE
)
# Default TODO types
patterns <- c("FIXME", "TODO", "CHANGED", "IDEA",
"HACK", "NOTE", "REVIEW", "BUG",
"QUESTION", "COMBAK", "TEMP")
if (is.null(todo_types))
todo_types <- patterns
else {
if (!all(todo_types %in% patterns))
stop(paste("One or more 'todo_types' were not recognized.",
"See documentation of 'todor' for more information"))
}
processed <- sapply(files, function(x) process_file(x, todo_types))
markers <- create_markers(processed)
build_rstudio_markers(markers)
}

todorAddin <- function() {
todor()
}
4 changes: 4 additions & 0 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Name: Find TODOs
Description: Find all places in your package which require more work.
Binding: todorAddin
Interactive: false
14 changes: 14 additions & 0 deletions man/build_rstudio_markers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions man/todor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions todor.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace

0 comments on commit b07bf39

Please sign in to comment.