From 23944a5b067737333f34524f1c16ba609a06a63c Mon Sep 17 00:00:00 2001 From: olivroy Date: Sun, 10 Mar 2024 17:02:30 -0400 Subject: [PATCH 1/5] Add required information about line numbers and full file names --- R/spell-check.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/R/spell-check.R b/R/spell-check.R index e7afffd..7952935 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){ + 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")) } From 051af1cfd922b88ea831056e7d26ebef3f8e4833 Mon Sep 17 00:00:00 2001 From: olivroy Date: Sun, 10 Mar 2024 17:03:49 -0400 Subject: [PATCH 2/5] Reproduce output cli vs not-cli --- R/spell-check.R | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/R/spell-check.R b/R/spell-check.R index 7952935..4087952 100644 --- a/R/spell-check.R +++ b/R/spell-check.R @@ -150,10 +150,28 @@ 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 = "") - for(i in seq_len(nrow(x))){ - cat(pretty_names[i]) - cat(paste(x$found[[i]], collapse = paste0("\n", sprintf(fmt, "")))) - cat("\n") + if (cli::ansi_has_hyperlink_support()) { + 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, ""))) + + for (k in seq_along(x$line_numbers[[i]][[j]])) { + # link for each line of each file. + if (k == 1) cat(x$found[[i]][[j]]) + } + } + cat("\n") + } + } else { + for(i in seq_len(nrow(x))){ + cat(pretty_names[i]) + cat(paste(x$found[[i]], collapse = paste0("\n", sprintf(fmt, "")))) + cat("\n") + } } invisible(x) } From 578ec12b9f8db96a4d7138cd98129505f75ae464 Mon Sep 17 00:00:00 2001 From: olivroy Date: Sun, 10 Mar 2024 17:43:36 -0400 Subject: [PATCH 3/5] Finalize implementation. --- DESCRIPTION | 4 +++- R/spell-check.R | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 54a8db2..c91bc1a 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/R/spell-check.R b/R/spell-check.R index 4087952..5bebb42 100644 --- a/R/spell-check.R +++ b/R/spell-check.R @@ -127,7 +127,7 @@ summarize_words <- function(file_names, found_line){ out$file_names <- lapply(bad_words, function(word) { index <- which(vapply(words_by_file, `%in%`, x = word, logical(1))) reports <- vapply(index, function(i){ - file_names[i] + paste0("file://", file_names[i]) }, character(1)) }) # gather line numbers for each match for each file. @@ -150,18 +150,43 @@ 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 = "") - if (cli::ansi_has_hyperlink_support()) { + + # Diplay cli hyperlinks if console supports it. + # Show in RStudio interactively . + # https://github.com/ropensci/spelling/issues/74 + display_hyperlinks <- + requireNamespace("cli", quietly = TRUE) && + cli::ansi_has_hyperlink_support() + + if (display_hyperlinks) { 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, ""))) - - for (k in seq_along(x$line_numbers[[i]][[j]])) { - # link for each line of each file. - if (k == 1) cat(x$found[[i]][[j]]) + 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") From d405403a69d11974c8c89d99bf97eb1f15f32e7b Mon Sep 17 00:00:00 2001 From: olivroy Date: Sun, 10 Mar 2024 17:44:50 -0400 Subject: [PATCH 4/5] Add news --- NEWS | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS b/NEWS index 0e33986..aa761c0 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.0 - Support for spellchecking on Quarto files (@olivroy, #77) - You can now specify a global wordlist file via the SPELLING_WORDLIST envvar From 024c93e943f740eb45417dede19b761bf6ddb928 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 7 May 2024 10:50:33 -0400 Subject: [PATCH 5/5] Refactor out hyperlinks in own function --- R/spell-check.R | 83 ++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/R/spell-check.R b/R/spell-check.R index 5bebb42..787f210 100644 --- a/R/spell-check.R +++ b/R/spell-check.R @@ -151,54 +151,59 @@ print.summary_spellcheck <- function(x, ...){ pretty_names <- sprintf(fmt, words) cat(sprintf(fmt, " WORD"), " FOUND IN\n", sep = "") - # Diplay cli hyperlinks if console supports it. - # Show in RStudio interactively . - # https://github.com/ropensci/spelling/issues/74 display_hyperlinks <- requireNamespace("cli", quietly = TRUE) && cli::ansi_has_hyperlink_support() if (display_hyperlinks) { - 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(",") - } + # 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, "")))) + cat("\n") + } + 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") - } - } else { - for(i in seq_len(nrow(x))){ - cat(pretty_names[i]) - cat(paste(x$found[[i]], collapse = paste0("\n", sprintf(fmt, "")))) - cat("\n") } + cat("\n") } - invisible(x) + } #' @export