diff --git a/DESCRIPTION b/DESCRIPTION index 7a71f99..3556552 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.1 Language: en-GB diff --git a/NEWS b/NEWS index fb3bc17..eeccc3c 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,5 @@ dev + - If cli is installed and your terminal supports it, hyperlinks are now displayed in interactive sessions. (@olivroy, #74) - Support for spellchecking on Quarto files (@olivroy, #77) diff --git a/R/spell-check.R b/R/spell-check.R index efed040..cd196e3 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,49 @@ 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 (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) + + 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]), ":", first_line), + paste0("file://", file_names[i]), + 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) { + all_lines <- unlist(strsplit(line_chr, split = ",")) + for (j in 2:length(all_lines)) { + other_link <- cli::style_hyperlink( + paste0(all_lines[j]), + paste0("file://", file_names[i]), + 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