From 5d724224e5c984d499dc48833894ce55cafc2e35 Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 18 Jan 2024 13:40:37 -0500 Subject: [PATCH 1/6] Redocument with Roxygen2 7.3.0 --- DESCRIPTION | 2 +- man/spell_check_files.Rd | 7 ++++--- man/spell_check_package.Rd | 5 +++-- man/wordlist.Rd | 5 +++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 53f88b1..644bc16 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,5 +22,5 @@ Imports: knitr Suggests: pdftools Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3.9000 +RoxygenNote: 7.3.0 Language: en-GB diff --git a/man/spell_check_files.Rd b/man/spell_check_files.Rd index a5f5b80..2821d9e 100644 --- a/man/spell_check_files.Rd +++ b/man/spell_check_files.Rd @@ -12,7 +12,7 @@ spell_check_text(text, ignore = character(), lang = "en_US") \arguments{ \item{path}{path to file or to spell check} -\item{ignore}{character vector with words which will be added to the \link[hunspell:dictionary]{hunspell::dictionary}} +\item{ignore}{character vector with words which will be added to the \link[hunspell:hunspell]{hunspell::dictionary}} \item{lang}{set \code{Language} field in \code{DESCRIPTION} e.g. \code{"en-US"} or \code{"en-GB"}. For supporting other languages, see the \href{https://docs.ropensci.org/hunspell/articles/intro.html#hunspell-dictionaries}{hunspell vignette}.} @@ -38,7 +38,8 @@ files <- list.files(system.file("examples", package = "knitr"), spell_check_files(files) } \seealso{ -Other spelling: \code{\link{spell_check_package}()}, - \code{\link{wordlist}} +Other spelling: +\code{\link{spell_check_package}()}, +\code{\link{wordlist}} } \concept{spelling} diff --git a/man/spell_check_package.Rd b/man/spell_check_package.Rd index 1a86139..f492e40 100644 --- a/man/spell_check_package.Rd +++ b/man/spell_check_package.Rd @@ -49,7 +49,8 @@ Hunspell includes dictionaries for \code{en_US} and \code{en_GB} by default. Oth require installation of a custom dictionary, see \link[hunspell:hunspell]{hunspell} for details. } \seealso{ -Other spelling: \code{\link{spell_check_files}()}, - \code{\link{wordlist}} +Other spelling: +\code{\link{spell_check_files}()}, +\code{\link{wordlist}} } \concept{spelling} diff --git a/man/wordlist.Rd b/man/wordlist.Rd index 1cc1591..fd1a925 100644 --- a/man/wordlist.Rd +++ b/man/wordlist.Rd @@ -31,7 +31,8 @@ removes words from the wordlist that no longer appear as spelling errors, either they have been removed from the documentation or added to the \code{lang} dictionary. } \seealso{ -Other spelling: \code{\link{spell_check_files}()}, - \code{\link{spell_check_package}()} +Other spelling: +\code{\link{spell_check_files}()}, +\code{\link{spell_check_package}()} } \concept{spelling} From ddfbe77a1e97d7759a7951e12c80ff56f0dd2067 Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 18 Jan 2024 13:42:13 -0500 Subject: [PATCH 2/6] Add support for cli hyperlinks --- DESCRIPTION | 2 +- NEWS | 4 ++++ R/spell-check.R | 17 +++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 644bc16..f4ce4ba 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,7 +20,7 @@ Imports: xml2, hunspell (>= 3.0), knitr -Suggests: pdftools +Suggests: pdftools, cli Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.0 Language: en-GB diff --git a/NEWS b/NEWS index 1325524..edef4bf 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +dev +dev + - If cli is installed and your terminal supports it, hyperlinks are now displayed. (@olivroy, #74) + 2.2.1 - Fix for commonmark 1.7.0 change for auto-linked markdown URLs diff --git a/R/spell-check.R b/R/spell-check.R index 8682594..548e144 100644 --- a/R/spell-check.R +++ b/R/spell-check.R @@ -109,7 +109,7 @@ as_package <- function(pkg){ structure(pkg, class = 'package') } -# Find all occurences for each word +# Find all occurrences for each word summarize_words <- function(file_names, found_line){ words_by_file <- lapply(found_line, names) bad_words <- sort(unique(unlist(words_by_file))) @@ -120,7 +120,20 @@ summarize_words <- function(file_names, found_line){ out$found <- lapply(bad_words, function(word) { index <- which(vapply(words_by_file, `%in%`, x = word, logical(1))) reports <- vapply(index, function(i){ - paste0(basename(file_names[i]), ":", found_line[[i]][word]) + if (requireNamespace("cli", quietly = TRUE)) { + the_line <- as.integer(found_line[[i]][word]) + link <- cli::style_hyperlink( + paste0(basename(file_names[i]), ":", the_line), + paste0("file://", file_names[i]), + params = c(line = the_line, col = 1) + ) + res <- cli::format_inline(link) + + } else { + res <- paste0(basename(file_names[i]), ":", found_line[[i]][word]) + } + + res }, character(1)) }) structure(out, class = c("summary_spellcheck", "data.frame")) From e019585e07731e563f14dd3b9038979191f370f0 Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 18 Jan 2024 13:44:14 -0500 Subject: [PATCH 3/6] Only change behavior interactively --- NEWS | 2 +- R/spell-check.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index edef4bf..c1c9989 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,6 @@ dev dev - - If cli is installed and your terminal supports it, hyperlinks are now displayed. (@olivroy, #74) + - If cli is installed and your terminal supports it, hyperlinks are now displayed in interactive sessions. (@olivroy, #74) 2.2.1 - Fix for commonmark 1.7.0 change for auto-linked markdown URLs diff --git a/R/spell-check.R b/R/spell-check.R index 548e144..c80411b 100644 --- a/R/spell-check.R +++ b/R/spell-check.R @@ -120,7 +120,7 @@ summarize_words <- function(file_names, found_line){ out$found <- lapply(bad_words, function(word) { index <- which(vapply(words_by_file, `%in%`, x = word, logical(1))) reports <- vapply(index, function(i){ - if (requireNamespace("cli", quietly = TRUE)) { + if (interactive() && requireNamespace("cli", quietly = TRUE)) { the_line <- as.integer(found_line[[i]][word]) link <- cli::style_hyperlink( paste0(basename(file_names[i]), ":", the_line), From 2a0b82d09d97da2d5af24a60d59abf137c4fa761 Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 18 Jan 2024 14:28:09 -0500 Subject: [PATCH 4/6] Make it work for multiple instances of the same word. --- NEWS | 1 - R/spell-check.R | 29 ++++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index c1c9989..3da32d4 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,3 @@ -dev dev - If cli is installed and your terminal supports it, hyperlinks are now displayed in interactive sessions. (@olivroy, #74) diff --git a/R/spell-check.R b/R/spell-check.R index c80411b..f0eedc4 100644 --- a/R/spell-check.R +++ b/R/spell-check.R @@ -121,14 +121,37 @@ summarize_words <- function(file_names, found_line){ index <- which(vapply(words_by_file, `%in%`, x = word, logical(1))) reports <- vapply(index, function(i){ if (interactive() && requireNamespace("cli", quietly = TRUE)) { - the_line <- as.integer(found_line[[i]][word]) + line_chr <- found_line[[i]][word] + word_on_many_lines <- grepl(",", line_chr) + + if (word_on_many_lines) { + first_line <- regmatches(line_chr, m = regexpr("\\d+", line_chr)) + first_line <- as.integer(first_line) + } else { + first_line <- as.integer(found_line[[i]][word]) + } + link <- cli::style_hyperlink( - paste0(basename(file_names[i]), ":", the_line), + paste0(basename(file_names[i]), ":", first_line), paste0("file://", file_names[i]), - params = c(line = the_line, col = 1) + params = c(line = first_line, col = 1) # line must be integer / numeric ) + res <- cli::format_inline(link) + if (word_on_many_lines) { + all_lines <- unlist(strsplit(line_chr, split = ",")) + for (i in 2:length(all_lines)) { + other_link <- cli::style_hyperlink( + paste0(all_lines[i]), + paste0("file://", file_names[i]), + params = c(line = as.integer(all_lines[i]), col = 1) + ) + line_num_link <- cli::format_inline(other_link) + res <- paste(res, line_num_link, sep = ",") + } + } + } else { res <- paste0(basename(file_names[i]), ":", found_line[[i]][word]) } From c66802e855b4f90c94aa84749524eb8351e18c86 Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 18 Jan 2024 14:31:18 -0500 Subject: [PATCH 5/6] Oopss, iterate correctly. --- R/spell-check.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/spell-check.R b/R/spell-check.R index f0eedc4..861d75f 100644 --- a/R/spell-check.R +++ b/R/spell-check.R @@ -141,11 +141,11 @@ summarize_words <- function(file_names, found_line){ if (word_on_many_lines) { all_lines <- unlist(strsplit(line_chr, split = ",")) - for (i in 2:length(all_lines)) { + for (j in 2:length(all_lines)) { other_link <- cli::style_hyperlink( - paste0(all_lines[i]), + paste0(all_lines[j]), paste0("file://", file_names[i]), - params = c(line = as.integer(all_lines[i]), col = 1) + params = c(line = as.integer(all_lines[j]), col = 1) ) line_num_link <- cli::format_inline(other_link) res <- paste(res, line_num_link, sep = ",") From f3ea7dec917a8ee1d59b553b4113fe30e5359520 Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 18 Jan 2024 15:42:18 -0500 Subject: [PATCH 6/6] Add condition + make new code conditional on hyperlink support --- R/spell-check.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/R/spell-check.R b/R/spell-check.R index 861d75f..adc00ff 100644 --- a/R/spell-check.R +++ b/R/spell-check.R @@ -120,7 +120,8 @@ summarize_words <- function(file_names, found_line){ out$found <- lapply(bad_words, function(word) { index <- which(vapply(words_by_file, `%in%`, x = word, logical(1))) reports <- vapply(index, function(i){ - if (interactive() && requireNamespace("cli", quietly = TRUE)) { + if (interactive() && requireNamespace("cli", quietly = TRUE) && cli::ansi_has_hyperlink_support()) { + # Support cli links to lines when available. line_chr <- found_line[[i]][word] word_on_many_lines <- grepl(",", line_chr) @@ -137,6 +138,8 @@ summarize_words <- function(file_names, found_line){ params = c(line = first_line, col = 1) # line must be integer / numeric ) + # The first part of the hyperlink will always be + # res <- cli::format_inline(link) if (word_on_many_lines) { @@ -148,11 +151,14 @@ summarize_words <- function(file_names, found_line){ params = c(line = as.integer(all_lines[j]), col = 1) ) line_num_link <- cli::format_inline(other_link) + # if many occurrences of the same word in the same file + # create a