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

Conversation

olivroy
Copy link
Contributor

@olivroy olivroy commented Jan 18, 2024

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
Copy link
Contributor Author

olivroy commented Jan 18, 2024

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

@jeroen
Copy link
Member

jeroen commented Jan 30, 2024

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
Copy link
Contributor Author

olivroy commented Jan 31, 2024

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
Copy link
Contributor Author

olivroy commented Mar 10, 2024

Closing in favour of #81

@olivroy olivroy closed this Mar 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: Use clickable hyperlinks in spell_check_*()
2 participants