Skip to content

Commit

Permalink
Implement full file lint caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimhester committed Jan 27, 2015
1 parent 83135d8 commit 2fbfee2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
20 changes: 19 additions & 1 deletion R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ save_cache <- function(cache, file, path = getOption("lintr.cache_directory")) {
save(file = file.path(path, file), envir = cache, list=ls(envir=cache))
}

cache_file <- function(cache, filename, linters, lints) {
assign(envir = cache,
x = digest::digest(list(linters, readLines(filename)), algo = "sha1"),
value = lints
)
}
retrieve_file <- function(cache, filename, linters) {
key <- digest::digest(list(linters, readLines(filename)), algo = "sha1")
if (exists(key, envir = cache)) {
get(
envir = cache,
x = digest::digest(list(linters, readLines(filename)), algo = "sha1")
)
} else {
NULL
}
}

cache_lint <- function(cache, expr, linter, lints) {
assign(
envir = cache,
Expand All @@ -44,7 +62,7 @@ cache_lint <- function(cache, expr, linter, lints) {
retrieve_lint <- function(cache, expr, linter, lines) {
lints <- get(
envir = cache,
x = digest::digest(list(linter, expr$content), algo="sha1"),
x = digest::digest(list(linter, expr$content), algo="sha1")
)
lints[] <- lapply(lints, function(lint) {
lint$line_number <- find_new_line(lint$line_number, unname(lint$line), lines)
Expand Down
18 changes: 13 additions & 5 deletions R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ lint <- function(filename, linters = default_linters, cache = FALSE) {
lints <- list()
itr <- 0

if (cache) {
if (isTRUE(cache)) {
lint_cache <- load_cache(filename)
lints <- retrieve_file(lint_cache, filename, linters)
if (!is.null(lints)) {
return(lints)
}
}

for (expr in source_expressions$expressions) {
Expand All @@ -54,15 +58,19 @@ lint <- function(filename, linters = default_linters, cache = FALSE) {
if (inherits(source_expressions$error, "lint")) {
lints[[itr <- itr + 1L]] <- source_expressions$error

if (cache) {
if (isTRUE(cache)) {
cache_lint(lint_cache, list(filename=filename, content=""), "error", source_expressions$error)
}
}

if (cache) {
lints <- structure(reorder_lints(flatten_lints(lints)), class = "lints")

if (isTRUE(cache)) {
cache_file(lint_cache, filename, linters, lints)
save_cache(lint_cache, filename)
}
structure(reorder_lints(flatten_lints(lints)), class = "lints")

lints
}

reorder_lints <- function(lints) {
Expand Down Expand Up @@ -97,7 +105,7 @@ lint_package <- function(path = NULL, relative_path = TRUE, ...) {
full.names = TRUE)

lints <- flatten_lints(lapply(files,
function(file, ...) {
function(file) {
if (interactive()) {
message(".", appendLF = FALSE)
}
Expand Down

0 comments on commit 2fbfee2

Please sign in to comment.