Skip to content

Commit

Permalink
Trim long comments to avoid an error
Browse files Browse the repository at this point in the history
Fixes #55
  • Loading branch information
jimhester committed Feb 26, 2015
1 parent e17ae08 commit 9b5a6b2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# lintr 0.3.0.9000 #

* Now trims long comments (#55, reported by @paulstaab)
* Automatic commenting of Github commits and pull requests when linting on Travis-CI
* expect_lint_free expectation can be added to testthat unit tests.
* Robust configuration system and exclusion logic
* Emacs and Sublime Text 3 plugins now available from their respective package repositories.
* add `names.lints`, `split.lints` (#49, @ttriche)
* Fixed bug that caused vim syntatic plugin not to work properly in windows (#46, @abossenbroek)
Expand Down
39 changes: 35 additions & 4 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ print.lints <- function(x, ...) {

info <- travis_build_info()

lint_output <- paste0(collapse = "\n",
capture.output(
invisible(lapply(x, markdown, info, ...)))
)
lint_output <-
trim_output(paste0(collapse = "\n",
capture.output(invisible(lapply(x, markdown, info, ...)))
)
)

github_comment(lint_output, info, ...)
}
}
Expand All @@ -73,6 +75,35 @@ print.lints <- function(x, ...) {
invisible(x)
}

trim_output <- function(x, max = 65535) {

# if x is less than the max, just return it
if (length(x) <= 0 || nchar(x) <= max) {
return(x)
}

# otherwise trim x to the max, then search for the lint starts
x <- substr(x, 1, max)

re <- rex::rex("[", except_some_of(":"), ":", numbers, ":", numbers, ":", "]",
"(", except_some_of(")"), ")",
space,
"*", or("style", "warning", "error"), ":", "*",
except_some_of(newline), newline, except_some_of(newline),
newline, except_some_of(newline), newline)

lint_starts <- rex::re_matches(x, re, global = TRUE, locations = TRUE)[[1]]

# if at least one lint ends before the cutoff, cutoff there, else just use
# the cutoff
last_end <- lint_starts[NROW(lint_starts), "end"]
if (!is.na(last_end)) {
substr(x, 1, last_end)
} else {
x
}
}

#' @export
names.lints <- function(x, ...) {
vapply(x, `[[`, character(1), "filename")
Expand Down
Loading

0 comments on commit 9b5a6b2

Please sign in to comment.