private void ProjectManager_Changed(object sender, ProjectChangeEventArgs e) { switch (e.Kind) { case ProjectChangeKind.DocumentAdded: { var key = new DocumentKey(e.ProjectFilePath, e.DocumentFilePath); var document = _documentManager.GetOrCreateDocument(key, _onChangedOnDisk, _onChangedOnDisk, _onOpened, _onClosed); if (document.IsOpenInEditor) { Document_Opened(document, EventArgs.Empty); } break; } case ProjectChangeKind.DocumentRemoved: { // This class 'owns' the document entry so it's safe for us to dispose it. if (_documentManager.TryGetDocument(new DocumentKey(e.ProjectFilePath, e.DocumentFilePath), out var document)) { document.Dispose(); } break; } } }
// Internal for testing. #pragma warning disable VSTHRD100 // Avoid async void methods internal async void ProjectManager_Changed(object sender, ProjectChangeEventArgs e) #pragma warning restore VSTHRD100 // Avoid async void methods { try { switch (e.Kind) { case ProjectChangeKind.DocumentAdded: { // Don't do any work if the solution is closing if (e.SolutionIsClosing) { return; } var key = new DocumentKey(e.ProjectFilePath, e.DocumentFilePath); // GetOrCreateDocument needs to be run on the UI thread await _joinableTaskContext.Factory.SwitchToMainThreadAsync(); var document = _documentManager.GetOrCreateDocument( key, _onChangedOnDisk, _onChangedInEditor, _onOpened, _onClosed); if (document.IsOpenInEditor) { _onOpened(document, EventArgs.Empty); } break; } case ProjectChangeKind.DocumentRemoved: { // Need to run this even if the solution is closing because document dispose cleans up file watchers etc. // TryGetDocument and Dispose need to be run on the UI thread await _joinableTaskContext.Factory.SwitchToMainThreadAsync(); var documentFound = _documentManager.TryGetDocument( new DocumentKey(e.ProjectFilePath, e.DocumentFilePath), out var document); // This class 'owns' the document entry so it's safe for us to dispose it. if (documentFound) { document.Dispose(); } break; } } } catch (Exception ex) { Debug.Fail("EditorDocumentManagerListener.ProjectManager_Changed threw exception:" + Environment.NewLine + ex.Message + Environment.NewLine + "Stack trace:" + Environment.NewLine + ex.StackTrace); } }