rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
165 stars 35 forks source link

Get editor context by document ID #251

Closed gadenbuie closed 2 years ago

gadenbuie commented 2 years ago

Could rstudioapi include a method for obtaining the editor context by document ID? AFAICT, getSourceEditorContext() and getActiveDocumentContext() are the only way to get the current selection and contents for a document. In theory, one could use navigateToFile() to activate the required editor, read the source editor context, then switch back to the previously active editor... but there are a few problems:

  1. It's not possible to switch to an editor by context ID, meaning it may not be possible to switch back to the previously open editor (#250)
  2. navigateToFile() is currently asynchronous, which introduces subtle timing issues that mean that one cannot simply call

    navigateToFile("code.R")
    getSourceEditorContext()

    since the second call will almost certainly return the editor context of the document that was active, not the context from the desired file code.R. (This will be fixed by https://github.com/rstudio/rstudio/issues/8938)