From 2fbfee237c39c47cda15e93e96ef74a16b4bc40d Mon Sep 17 00:00:00 2001 From: Jim Hester Date: Tue, 27 Jan 2015 13:18:34 -0500 Subject: [PATCH] Implement full file lint caching. --- R/cache.R | 20 +++++++++++++++++++- R/lint.R | 18 +++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/R/cache.R b/R/cache.R index 08a590234..c541d17e8 100644 --- a/R/cache.R +++ b/R/cache.R @@ -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, @@ -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) diff --git a/R/lint.R b/R/lint.R index b2952a6d8..e24a9ca59 100644 --- a/R/lint.R +++ b/R/lint.R @@ -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) { @@ -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) { @@ -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) }