(fileUri?: vs.Uri)
| 496 | } |
| 497 | |
| 498 | export async function forceDocumentCloseEvents(fileUri?: vs.Uri) { |
| 499 | // To ensure that onDidCloseTextDocument fires for it, we need to fill |
| 500 | // VS Code's text model buffer with dummy files. This is a horrible hack, but |
| 501 | // otherwise, we'd have to wait 3 mins for the event to fire (which isn't practical) |
| 502 | // or have tests behave differently (for example even if we mock the close/open, |
| 503 | // we'd still see onDidChangeTextDocument events that might interfere with things |
| 504 | // like the position trackers). |
| 505 | const openedDummyUris: vs.Uri[] = []; |
| 506 | const MAX_DUMMY_FILES = 65; // In testing, it seems to require around 58 files to trigger the cleanup. Round up a little to give some margin. |
| 507 | for (let i = 0; i < MAX_DUMMY_FILES; i++) { |
| 508 | // If we had a file uri, we only care about that one and can stop creating dummies when we see it's gone. |
| 509 | if (fileUri && !vs.workspace.textDocuments.find((d) => d.uri.toString() === fileUri.toString())) { |
| 510 | // File is already gone so we don't need to open any more dummies. |
| 511 | break; |
| 512 | } |
| 513 | |
| 514 | // Otherwise, open a dummy. |
| 515 | // Unfortunately we have to also open the editor here, because otherwise |
| 516 | // tabgroups will not contain it (at least not immediately), despite the editor |
| 517 | // becoming visible. However this shouldn't interfere with anything because it's |
| 518 | // a non-Dart file that we don't do anything with. |
| 519 | const dummyUri = vs.Uri.parse(`untitled:dummy-${i}`); |
| 520 | await vs.window.showTextDocument(dummyUri); |
| 521 | openedDummyUris.push(dummyUri); |
| 522 | } |
| 523 | |
| 524 | closeTabs(openedDummyUris); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Closes a file, and ensures VS Code's onDidCloseTextDocument event fires. |
no test coverage detected