Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Imports:
xml2,
hunspell (>= 3.0),
knitr
Suggests: pdftools
Suggests:
cli,
pdftools
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Language: en-GB
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
dev
- spell_check_*() now display cli hyperlinks in console if your console
supports it (@olivroy, #74)

2.3.2
- Fix diff NOTE with spelling::spell_check_test() on R-devel

Expand Down
62 changes: 62 additions & 0 deletions R/spell-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ summarize_words <- function(file_names, found_line){
paste0(basename(file_names[i]), ":", found_line[[i]][word])
}, character(1))
})
# Gather full file names
out$file_names <- lapply(bad_words, function(word) {
index <- which(vapply(words_by_file, `%in%`, x = word, logical(1)))
reports <- vapply(index, function(i){
paste0("file://", file_names[i])
}, character(1))
})
# gather line numbers for each match for each file.
out$line_numbers <- lapply(bad_words, function(word) {
index <- which(vapply(words_by_file, `%in%`, x = word, logical(1)))
reports <- lapply(index, function(i){
as.integer(unlist(strsplit(found_line[[i]][word], split = ",", fixed = TRUE)))
})
})
structure(out, class = c("summary_spellcheck", "data.frame"))
}

Expand All @@ -136,6 +150,18 @@ print.summary_spellcheck <- function(x, ...){
fmt <- paste0("%-", max(nchar(words), 0) + 3, "s")
pretty_names <- sprintf(fmt, words)
cat(sprintf(fmt, " WORD"), " FOUND IN\n", sep = "")

display_hyperlinks <-
requireNamespace("cli", quietly = TRUE) &&
cli::ansi_has_hyperlink_support()

if (display_hyperlinks) {
# Diplay cli hyperlinks if console supports it.
# Show in RStudio interactively .
# https://github.com/ropensci/spelling/issues/74
display_hyperlinks(x, pretty_names, words, fmt)
return(invisible(x))
}
for(i in seq_len(nrow(x))){
cat(pretty_names[i])
cat(paste(x$found[[i]], collapse = paste0("\n", sprintf(fmt, ""))))
Expand All @@ -144,6 +170,42 @@ print.summary_spellcheck <- function(x, ...){
invisible(x)
}

display_hyperlinks <- function(x, pretty_names, words, fmt) {
for(i in seq_len(nrow(x))){
# print word
cat(pretty_names[i])
for (j in seq_along(x$file_names[[i]])) {
# each file name
# print separator only for subsequent files
if (j != 1) cat(paste0("\n", sprintf(fmt, "")))
found_str <- x$found[[i]][[j]]
first_match <- regmatches(found_str, m = regexpr(".+\\:\\d+", found_str))
lnk <- cli::style_hyperlink(
text = first_match,
url = x$file_names[[i]][[j]],
params = list(line = x$line_numbers[[i]][[j]][1L], col = 1)
)
cat(lnk) #file_name:<line>
n_matches <- length(x$line_numbers[[i]][[j]])
if (n_matches > 1) {
for (k in 2:n_matches) {
if (k == 2) cat(",")
# link for each line of each file.
lnk_line <- cli::style_hyperlink(
text = x$line_numbers[[i]][[j]][k],
url = x$file_names[[i]][[j]],
params = list(line = x$line_numbers[[i]][[j]][k], col = 1)
)
cat(lnk_line) # <line>
if (k != n_matches) cat(",")
}
}
}
cat("\n")
}

}

#' @export
#' @aliases spell_check_test
#' @rdname spell_check_package
Expand Down
Loading