r-lib / cli

Tools for making beautiful & useful command line interfaces
https://cli.r-lib.org/
Other
653 stars 70 forks source link

Consistent way to open files with {.file} hyperlinks in RStudio project #604

Closed olivroy closed 1 year ago

olivroy commented 1 year ago

Hello,

I was wondering if it was possible to open files with a relative path. (Windows)

https://cli.r-lib.org/reference/links.html#files

I usually open files with 5 ways, the first 4 are consistent, but {.file } is not. (when in Project)

  1. Opening a file from Ctrl + .,

  2. Files pane in RStudio,

  3. Find in Files RStudio

  4. or with usethis::edit_file()

  5. With cli {.file } hyperlinks

  6. to 4. open with a relative path "~/path/to/file.R" (when hovering over the tab) in RStudio, so avoid opening the same file in 2 different tabs. Clicking on

    cli::cli_text("{.file file.R}

    opens a 'new' file (duplicate tab) When hovering over it, I see that the path is "C:/Users/.../path/to/file.R"

RStudio doesn't recognize that they are the same file, so they are open twice.

I would expect the cli hyperlink to just change the focus of the source, instead of opening 2 instances of the file.

gaborcsardi commented 1 year ago

cli converts hyperlinks to files to absolute file:// links, because relative file: links do not exist. They would not make sense anyway, because different processes have different working directories, and the process you run with the click might be different from the process creating the link.

cli also does not open anything, it only creates the link, and then it is the job of the terminal or IDE, or the OS to decide what happens if you click on it.

So I am afraid that you'll need to take this up with RStudio. It would make sense to avoid opening the same file twice.

olivroy commented 1 year ago

As a package developer, how do you cope with that when debugging? (or is it just an issue on Windows?) For example, you have a .R file opened with a short path.

short path: ~/path/to/file.R long path: C:/.../path/to/file.R

Workflow example:

  1. open and edit file.R with the short path (with Ctrl/Cmd + .)
  2. load_all()
  3. rlang throws an error with a link e.g. Error in f() at {.file file.R:320}
  4. Click on that link to debug (open and edit long path)
  5. Forget to save or close.
  6. Forget that two instances of the same file are open and edit the short path. Save
  7. lose changes to the previously opened file.

Any recommendation on how you avoid that?

Thanks

gaborcsardi commented 1 year ago

As a package developer, how do you cope with that when debugging?

I usually use a different editor, so I am afraid that I can't give you advice on this.

Any recommendation on how you avoid that?

Report an issue to RStudio. The IDE should recognize that the file is already open, especially because AFAIR you should not be able to open the same file twice.