ropensci / spelling

Tools for Spell Checking in R
https://docs.ropensci.org/spelling
Other
105 stars 27 forks source link

Display hyperlinks in console. #75

Closed olivroy closed 4 months ago

olivroy commented 6 months ago

Fix #74.

I used the same technique as the one used in roxygen2:::warn_roxy().

While this is not very useful for packages with generated man pages with roxygen2, it can be useful for vignettes at least.

I could look into scanning #' comments, especially when doing interactive spell-check. (may not be worth adding this for automated checks)

https://github.com/r-lib/roxygen2/blob/main/R/utils-warn.R#L24

Please let me know what you think.

I made a test and it seems to work. image

And then clicking took me to the correct place in the .Rd file.

olivroy commented 6 months ago

Seems to work pretty well now. Please let me know if there are other things that I should test. image

jeroen commented 5 months ago

I don't think this is the right place to implement this. Perhaps it could be a separate function that is applied in print.summary_spellcheck?

olivroy commented 5 months ago

I agree. Do you think it would be helpful to have the object be a data frame with the following structure

data.frame(
  word,
  found,
  full_path,
   lines_found = list()
)

It would be much easier to create the hyperlinks without having to play with regexps

Currently, it only sends

Word Found In

Edit: here a prospective code, but that would require other output from the data.frane

df <- tibble::tibble(
  # 1 row by word found
  word = c("aaa", "bbb"),
  # found_in is a nested list of n elements
  # where n is the number of files where word is found
  found_in = list(
    c(
      "C:/Users/full/path/rrr/proj/R/r-file.R",
      "C:/Users/full/path/rrr/proj/R/r-file2.R"
    ),
    c("C:/Users/full/path/rrr/proj/quarto-file.qmd")
  ),
  # line_number is a list of lists
  # where each element is a vector of the occurences in each file
  line_number = list(
    list( # aaa
      c(1, 4), # line 1 and 4 of First R file
      c(6) # line 6 of second R file
    ),
    list( # bbb
      c(3) # line 3 quarto-file.qmd
    ) # bbb found at line 3 of the first file
  )
)

df

hyperlink_support <- FALSE

for (i in seq_len(nrow(df))) {
  word <- df$word[i]
  full_files <- df$found_in[[i]]
  line_numbers <- df$line_number[[i]]
  cat(word, "   ")

  for (j in seq_along(full_files)) {
    test_label <- paste0(
      word,
      " at ",
      basename(full_files[j]),
      ":",
      paste0(line_numbers[[j]], collapse = ",")
    )

    if (hyperlink_support) {
      # First hyperlink is different contains the file name + 1st line
      first_hyperlink <- cli::style_hyperlink(
        text = paste0(basename(full_files[j]), ":", line_numbers[[j]][1]),
        url = paste0("file://", full_files[j]),
        params = list(line = line_numbers[[j]][1L], col = 1L)
      )

      if (j > 1) {
        cat("      ")
      }

      cat(first_hyperlink)
      multiple_lines_in_same_file <- length(line_numbers[[j]]) > 1

      if (multiple_lines_in_same_file) {
        # the subsequent hyperlinks show only the line number.
        for (k in 2:length(line_numbers[[j]])) {
          cat(",")

          hyperlink <- cli::style_hyperlink(
            text = as.character(line_numbers[[j]][k]),
            url = paste0("file://", full_files[j]),
            params = list(line = line_numbers[[j]][k], col = 1L)
          )

          cat(hyperlink)
        }
      }
    } else {
      cat(test_label)
    }

    cat("\n")
  }
}

I would use the spacing logic, but this is just a small self-contained example that displays all possibilities I encountered so far.

olivroy commented 4 months ago

Closing in favour of #81