(ranges: number[][], id: string)
| 174 | } |
| 175 | |
| 176 | export async function setSelections(ranges: number[][], id: string): Promise<void> { |
| 177 | // Setting selections can only be done on TextEditors not TextDocuments, but |
| 178 | // it is the latter which are the things actually referred to by `id`. In |
| 179 | // VSCode it's not possible to get a list of the open text editors. it is not |
| 180 | // window.visibleTextEditors - this is only editors (tabs) with text showing. |
| 181 | // The only editors we know about are those that are visible and the last |
| 182 | // active (which may not be visible if it was overtaken by a WebViewPanel). |
| 183 | // This function looks to see if a text editor for the document id is amongst |
| 184 | // those known, and if not, it opens and shows that document, but in a |
| 185 | // texteditor 'beside' the current one. |
| 186 | // The rationale for this is: |
| 187 | // If an addin is trying to set selections in an editor that is not the active |
| 188 | // one it is most likely that it was active before the addin ran, but the addin |
| 189 | // opened a something that overtook its' focus. The most likely culprit for |
| 190 | // this is a shiny app. In the case that the target window is visible |
| 191 | // alongside the shiny app, it will be found and used. If it is not visible, |
| 192 | // there's a change it may be the last active, if the shiny app over took it. |
| 193 | // If it is neither of these things a new one needs to be opened to set |
| 194 | // selections and the question is whether open it in the same window as the |
| 195 | // shiny app, or the one 'beside'. 'beside' is preferred since it allows shiny |
| 196 | // apps that work interactively with an open document to behave more smoothly. |
| 197 | // {prefixer} is an example of one of these. |
| 198 | const target = findTargetUri(id); |
| 199 | const targetDocument = await workspace.openTextDocument(target); |
| 200 | const editor = await reuseOrCreateEditor(targetDocument); |
| 201 | |
| 202 | const selectionObjects = ranges.map(x => { |
| 203 | const newRange = parseRange(x, targetDocument); |
| 204 | const newSelection = new Selection(newRange.start, newRange.end); |
| 205 | return (newSelection); |
| 206 | }); |
| 207 | |
| 208 | editor.selections = selectionObjects; |
| 209 | } |
| 210 | |
| 211 | export async function documentSave(id: string): Promise<void> { |
| 212 | const target = findTargetUri(id); |
no test coverage detected