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:
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)
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)
Could rstudioapi include a method for obtaining the editor context by document ID? AFAICT,
getSourceEditorContext()
andgetActiveDocumentContext()
are the only way to get the current selection and contents for a document. In theory, one could usenavigateToFile()
to activate the required editor, read the source editor context, then switch back to the previously active editor... but there are a few problems:navigateToFile()
is currently asynchronous, which introduces subtle timing issues that mean that one cannot simply callsince 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)