Repository navigation
Support ansi hyperlinks in the console and build - #10299
romainfrancois wants to merge 56 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| { | ||
| Event.sinkEvents(element, Event.ONCLICK); | ||
| Event.setEventListener(element, event -> { | ||
| // TODO: call the RemoteServer.consoleFollowHyperlink([url], [params]) method |
There was a problem hiding this comment.
I don't know how to call RemoteServer.consoleFollowHyperlink([url], [params]).
Probably something to @Inject somehow ?
There was a problem hiding this comment.
Right, you'd need to inject the appropriate "*ServerOperations" class, either in the class constructor, or via an explicit RStudioGinjector.injectMembers(this) call in the constructor, with an appropriate injected initialize() method defined. For example:
There was a problem hiding this comment.
Thanks. I'll try to go the initialize() route
There was a problem hiding this comment.
That seems to work, although the class already had an @Inject constructor, so perhaps I'll move that there, but I suppose I would have to make some sort of fake ConsoleServerOperations for the new VirtualConsole() code in tests ?
There was a problem hiding this comment.
I think that's true. I'm not sure what the cleanest way to mock that in our tests would be...
There was a problem hiding this comment.
So then maybe it's easier if it's extracted to a simpler VirtualConsoleOperations interface.
| int flags, | ||
| ServerRequestCallback<Void> requestCallback); | ||
|
|
||
| void consoleFollowHyperlink(String url, String link_params, ServerRequestCallback<Void> requestCallback); |
There was a problem hiding this comment.
Is this the right class for this ?
There was a problem hiding this comment.
I think this is fine, but if you felt strongly then a separate VirtualConsoleServerOperations class might be a better fit.
There was a problem hiding this comment.
At least as a learning exercise, I'll try to make it a separate class/interface.
|
Now able to display links and have them do things through and rpc call, i.e. hyperlink <- function(text, url, params = NULL) {
paste0("\u001B]8;", params,";", url, "\u0007", text, "\u001B]8;;\u0007")
}
library(dplyr)
rlang::inform(c(
x = paste0("Incorrect use of ", hyperlink("dplyr::summarise()", "-", "type=help:topic=summarise")),
i = glue::glue("More info in this {hyperlink('article', 'https://dplyr.tidyverse.org/reference/summarise.html' )}"),
">" = glue::glue("{hyperlink('DESCRIPTION', 'DESCRIPTION', 'type=file:line=5' )}")
))gives : clicking on the The hyperlink is made of 3 things: the url, the text and params, which is either "" or a .rs.addJsonRpcHandler("console_follow_hyperlink", function(url, text, params)
{
if (identical(params, "")){
utils::browseURL(url)
} else {
options <- strsplit(params, ":")[[1]]
names <- sapply(options, function(param) sub("=.*$", "", param))
options <- lapply(options, function(param) sub("^[^=]*=", "", param))
names(options) <- names
if (is.null(options$type)) {
options$type <- ""
}
switch(
options$type,
help = .rs.showHelpTopic(options$topic, NULL),
file = {
file <- url
line <- as.numeric(options$line)
if (length(line) == 0L) line <- -1L
col <- as.numeric(options$col)
if (length(col) == 0L) col <- -1L
.rs.api.navigateToFile(url, line = line, col = col)
},
stop(paste0("Can't handle link <", url, "> with params <", params, ">"))
)
}
})There are also some ideas in #2530 about various types of links. |
|
This works in the build pane too, i.e. see r-lib/testthat#1544 |
|
Awesome work! Let me know if you think the PR is ready for review. |
|
One other thing to consider: right now, this PR is tailored to hyperlinks. How hard would it be to make the existing code generic enough to handle other custom escapes? (I guess one of the bigger challenges would be handling these escapes when they're embedded in each other.) |
|
I'm asking @gtritchie to take a look as well since he's the most familiar with our VirtualConsole implementation. |
|
hyperlinks are actually in addition to other escapes that were already in use for colors, etc ... (bold, italic, ...). It would be worth reviewing other kinds of escapes. @gaborcsardi might have some ideas. For the hyperlinks, using
From the user's point of view, at the moment things would go through Perhaps also something to We probably also need to add a |
Yes, I think having it in the URI would be better. |
This comment has been minimized.
This comment has been minimized.
| } | ||
| } | ||
|
|
||
| .rs.api.navigateToFile(file, line = line, col = col, moveCursor = TRUE) |
There was a problem hiding this comment.
This opens the file at the right location, but this does not seem to always move the focus. Or more accurately, it does but then the focus goes back to the console.
| server_.consoleFollowHyperlink(hyperlink_.url_, text, hyperlink_.params_, new VoidServerRequestCallback()); | ||
| }); | ||
|
|
||
| // for now, perhaps a dedicated style would be better |
There was a problem hiding this comment.
I'm not sure where to add a new specific style for links ? cc @gtritchie
There was a problem hiding this comment.
The console styles (along with the terminal, aka xterm) are part of the themes, so I think you'd need to add a new style to all the various themes you see in Options / Appearances, and also deal with what to do with third-party themes that don't have the new one. For now, if it looks ok, I think using the existing xterm style is reasonable. @MariaSemple did a lot of the theming work so she might have thoughts (and know if I'm totally off base here).
There was a problem hiding this comment.
I'll look into adding a xtermHyperlink style. We might want to fine tune how they are displayed. For example cli (not using style_hyperlink() yet, until r-lib/cli#386) uses:
There was a problem hiding this comment.
Gary is on base :). We should have a fallback/default in case user-themes are missing a rule for this class, but we should add a rule for each of the themes we ship (see here: https://github.com/rstudio/rstudio/blob/main/src/cpp/session/resources/themes/)
| params_ = params; | ||
| } | ||
|
|
||
| public String getTitle() { |
There was a problem hiding this comment.
Should this adapt to the type of link ? "Open help page for ...", "Open the viewer ..." ?
Do we actually need the E.g. |
|
Yeah that's probably better. And potentially the links would be meaningful to other cli besides rstudio. We just then need a way to differentiate when we want a link to open in the viewer, e.g. |
Yeah, isn't there some RStudio config setting for that? Maybe we can reuse that for the default? I don't like the form because that interferes with passing query parameters to the URI. Maybe the |
This comment has been minimized.
This comment has been minimized.
|
Or maybe |
d62216e to
af3ae7e
Compare
|
Thanks @kevinushey and @gtritchie. This now uses |
kevinushey
left a comment
There was a problem hiding this comment.
LGTM other than one import. Are there any other changes we want to make for now, or is this ready for merge?
|
I think this needs some testing inspired from the tests related to colors. The other thing is that the pull request targets |
|
Yes, I think we should target for |
|
(replaced by #10416 that targets |


Intent
Adresses #1941
Approach
Automated Tests
QA Notes
This code:
will produce these links in the console:
Checklist
NEWS.md