Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display hyperlinks in console. #75

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
46 changes: 44 additions & 2 deletions R/spell-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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
# <file name hyperlink:##1>
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 <file name hyperlink:##1,##2, etc.
res <- paste(res, line_num_link, sep = ",")
}
}

} else {
# original behavior if no hyperlink support, or non-interactive.
res <- paste0(basename(file_names[i]), ":", found_line[[i]][word])
}

res
}, character(1))
})
structure(out, class = c("summary_spellcheck", "data.frame"))
Expand Down
Loading