diff --git a/DESCRIPTION b/DESCRIPTION index 065944e..7935d4e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/NEWS b/NEWS index 7f769c7..060f133 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/R/spell-check.R b/R/spell-check.R index 58d715f..39b273a 100644 --- a/R/spell-check.R +++ b/R/spell-check.R @@ -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")) } @@ -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, "")))) @@ -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: + 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) # + if (k != n_matches) cat(",") + } + } + } + cat("\n") + } + +} + #' @export #' @aliases spell_check_test #' @rdname spell_check_package