Skip to content

Commit

Permalink
Use DocumentOpened for normal and source generated documents
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Feb 3, 2022
1 parent 4be55f5 commit f020e73
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 121 deletions.
49 changes: 12 additions & 37 deletions src/EditorFeatures/CSharpTest/Workspaces/WorkspaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -803,27 +803,18 @@ public async Task TestSourceGeneratedDocumentEvents()
// Creating two waiters that will allow us to know for certain if the events have fired.
using var closeWaiter = new EventWaiter();
using var openWaiter = new EventWaiter();
using var sourceGeneratedCloseWaiter = new EventWaiter();
using var sourceGeneratedOpenWaiter = new EventWaiter();

// Wrapping event handlers so they can notify us on being called.
var documentOpenedEventHandler = openWaiter.Wrap<DocumentEventArgs>(
(sender, args) => Assert.True(false, $"Unexpected raising of {nameof(Workspace.DocumentOpened)}."));

var documentClosedEventHandler = closeWaiter.Wrap<DocumentEventArgs>(
(sender, args) => Assert.True(false, $"Unexpected raising of {nameof(Workspace.DocumentClosed)}."));

var sourceGeneratedDocumentOpenedEventHandler = sourceGeneratedOpenWaiter.Wrap<DocumentEventArgs>(
(sender, args) => Assert.True(args.Document.Id == document.Id,
$"The document given to the '{nameof(Workspace.SourceGeneratedDocumentOpened)}' event handler did not have the same id as the one created for the test."));
$"The source generated document given to the '{nameof(Workspace.DocumentOpened)}' event handler did not have the same id as the one created for the test."));

var sourceGeneratedDocumentClosedEventHandler = sourceGeneratedCloseWaiter.Wrap<DocumentEventArgs>(
var documentClosedEventHandler = closeWaiter.Wrap<DocumentEventArgs>(
(sender, args) => Assert.True(args.Document.Id == document.Id,
$"The document given to the '{nameof(Workspace.SourceGeneratedDocumentClosed)}' event handler did not have the same id as the one created for the test."));
$"The source generated document given to the '{nameof(Workspace.DocumentClosed)}' event handler did not have the same id as the one created for the test."));

workspace.DocumentOpened += documentOpenedEventHandler;
workspace.DocumentClosed += documentClosedEventHandler;
workspace.SourceGeneratedDocumentOpened += sourceGeneratedDocumentOpenedEventHandler;
workspace.SourceGeneratedDocumentClosed += sourceGeneratedDocumentClosedEventHandler;

workspace.OpenSourceGeneratedDocument(document.Id);
var sourceGeneratedDocument = Assert.IsType<SourceGeneratedDocument>(document.GetOpenTextContainer().GetOpenDocumentInCurrentContext());
Expand All @@ -835,24 +826,16 @@ public async Task TestSourceGeneratedDocumentEvents()
await WaitForWorkspaceOperationsToComplete(workspace);

// Wait to receive signal that events have fired.
Assert.False(openWaiter.WaitForEventToFire(shortEventTimeout),
string.Format("event handler 'DocumentOpened' was called within {0} seconds though it was not expected.",
shortEventTimeout.Seconds));

Assert.False(closeWaiter.WaitForEventToFire(shortEventTimeout),
string.Format("event handler 'DocumentClosed' was called within {0} seconds though it was not expected.",
shortEventTimeout.Seconds));

Assert.True(sourceGeneratedOpenWaiter.WaitForEventToFire(longEventTimeout),
string.Format("event 'SourceGeneratedDocumentOpened' was not fired within {0} minutes.",
Assert.True(openWaiter.WaitForEventToFire(longEventTimeout),
string.Format("event 'DocumentOpened' was not fired within {0} minutes.",
longEventTimeout.Minutes));

Assert.True(sourceGeneratedCloseWaiter.WaitForEventToFire(longEventTimeout),
string.Format("event 'SourceGeneratedDocumentClosed' was not fired within {0} minutes.",
Assert.True(closeWaiter.WaitForEventToFire(longEventTimeout),
string.Format("event 'DocumentClosed' was not fired within {0} minutes.",
longEventTimeout.Minutes));

workspace.SourceGeneratedDocumentOpened -= sourceGeneratedDocumentOpenedEventHandler;
workspace.SourceGeneratedDocumentClosed -= sourceGeneratedDocumentClosedEventHandler;
workspace.DocumentOpened -= documentOpenedEventHandler;
workspace.DocumentClosed -= documentClosedEventHandler;

workspace.OpenSourceGeneratedDocument(document.Id);
workspace.CloseSourceGeneratedDocument(document.Id);
Expand All @@ -863,19 +846,11 @@ public async Task TestSourceGeneratedDocumentEvents()
// Verifying that an event has not been called is difficult to prove.
// All events should have already been called so we wait 5 seconds and then assume the event handler was removed correctly.
Assert.False(openWaiter.WaitForEventToFire(shortEventTimeout),
string.Format("event handler 'DocumentOpened' was called within {0} seconds though it was not expected.",
string.Format("event handler 'DocumentOpened' was called within {0} seconds though it was removed from the list.",
shortEventTimeout.Seconds));

Assert.False(closeWaiter.WaitForEventToFire(shortEventTimeout),
string.Format("event handler 'DocumentClosed' was called within {0} seconds though it was not expected.",
shortEventTimeout.Seconds));

Assert.False(sourceGeneratedOpenWaiter.WaitForEventToFire(shortEventTimeout),
string.Format("event handler 'SourceGeneratedDocumentOpened' was called within {0} seconds though it was removed from the list.",
shortEventTimeout.Seconds));

Assert.False(sourceGeneratedCloseWaiter.WaitForEventToFire(shortEventTimeout),
string.Format("event handler 'SourceGeneratedDocumentClosed' was called within {0} seconds though it was removed from the list.",
string.Format("event handler 'DocumentClosed' was called within {0} seconds though it was removed from the list.",
shortEventTimeout.Seconds));
}

Expand Down
8 changes: 0 additions & 8 deletions src/Features/Core/Portable/SolutionCrawler/WorkCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public WorkCoordinator(
_registration.Workspace.WorkspaceChanged += OnWorkspaceChanged;
_registration.Workspace.DocumentOpened += OnDocumentOpened;
_registration.Workspace.DocumentClosed += OnDocumentClosed;
_registration.Workspace.SourceGeneratedDocumentOpened += OnDocumentOpened;
_registration.Workspace.SourceGeneratedDocumentClosed += OnDocumentClosed;
}

// subscribe to option changed event after all required fields are set
Expand Down Expand Up @@ -110,8 +108,6 @@ public void Shutdown(bool blockingShutdown)
_registration.Workspace.WorkspaceChanged -= OnWorkspaceChanged;
_registration.Workspace.DocumentOpened -= OnDocumentOpened;
_registration.Workspace.DocumentClosed -= OnDocumentClosed;
_registration.Workspace.SourceGeneratedDocumentOpened -= OnDocumentOpened;
_registration.Workspace.SourceGeneratedDocumentClosed -= OnDocumentClosed;

// cancel any pending blocks
_shutdownNotificationSource.Cancel();
Expand Down Expand Up @@ -156,16 +152,12 @@ private void OnOptionChanged(object? sender, OptionChangedEventArgs e)
_registration.Workspace.WorkspaceChanged += OnWorkspaceChanged;
_registration.Workspace.DocumentOpened += OnDocumentOpened;
_registration.Workspace.DocumentClosed += OnDocumentClosed;
_registration.Workspace.SourceGeneratedDocumentOpened += OnDocumentOpened;
_registration.Workspace.SourceGeneratedDocumentClosed += OnDocumentClosed;
}
else
{
_registration.Workspace.WorkspaceChanged -= OnWorkspaceChanged;
_registration.Workspace.DocumentOpened -= OnDocumentOpened;
_registration.Workspace.DocumentClosed -= OnDocumentClosed;
_registration.Workspace.SourceGeneratedDocumentOpened -= OnDocumentOpened;
_registration.Workspace.SourceGeneratedDocumentClosed -= OnDocumentClosed;
}

SolutionCrawlerLogger.LogOptionChanged(CorrelationId, value);
Expand Down
4 changes: 0 additions & 4 deletions src/Features/Core/Portable/Workspace/BackgroundCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public BackgroundCompiler(Workspace workspace)
_workspace.WorkspaceChanged += OnWorkspaceChanged;
_workspace.DocumentOpened += OnDocumentOpened;
_workspace.DocumentClosed += OnDocumentClosed;
_workspace.SourceGeneratedDocumentOpened += OnDocumentOpened;
_workspace.SourceGeneratedDocumentClosed += OnDocumentClosed;
}

public void Dispose()
Expand All @@ -51,8 +49,6 @@ public void Dispose()

_workspace.DocumentClosed -= OnDocumentClosed;
_workspace.DocumentOpened -= OnDocumentOpened;
_workspace.SourceGeneratedDocumentClosed -= OnDocumentClosed;
_workspace.SourceGeneratedDocumentOpened -= OnDocumentOpened;
_workspace.WorkspaceChanged -= OnWorkspaceChanged;

_workspace = null!;
Expand Down
2 changes: 0 additions & 2 deletions src/Features/Core/Portable/Workspace/BackgroundParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public BackgroundParser(Workspace workspace)

workspace.DocumentOpened += OnDocumentOpened;
workspace.DocumentClosed += OnDocumentClosed;
workspace.SourceGeneratedDocumentOpened += OnDocumentOpened;
workspace.SourceGeneratedDocumentClosed += OnDocumentClosed;
}

private void OnActiveDocumentChanged(object sender, DocumentId activeDocumentId)
Expand Down
4 changes: 2 additions & 2 deletions src/Workspaces/Core/Portable/Workspace/Workspace_Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ internal void OnSourceGeneratedDocumentOpened(
static async Task RaiseSourceGeneratedDocumentOpenedAsync(Workspace workspace, Solution currentSolution, DocumentId documentId)
{
var document = await currentSolution.GetSourceGeneratedDocumentAsync(documentId, CancellationToken.None).ConfigureAwait(false);
await workspace.RaiseSourceGeneratedDocumentOpenedEventAsync(document).ConfigureAwait(false);
await workspace.RaiseDocumentOpenedEventAsync(document).ConfigureAwait(false);
}
}

Expand All @@ -457,7 +457,7 @@ internal void OnSourceGeneratedDocumentClosed(DocumentId documentId)
static async Task RaiseSourceGeneratedDocumentClosedAsync(Workspace workspace, Solution currentSolution, DocumentId documentId)
{
var document = await currentSolution.GetSourceGeneratedDocumentAsync(documentId, CancellationToken.None).ConfigureAwait(false);
await workspace.RaiseSourceGeneratedDocumentClosedEventAsync(document).ConfigureAwait(false);
await workspace.RaiseDocumentClosedEventAsync(document).ConfigureAwait(false);
}
}
}
Expand Down
68 changes: 0 additions & 68 deletions src/Workspaces/Core/Portable/Workspace/Workspace_Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public abstract partial class Workspace
private const string WorkspaceFailedEventName = "WorkspaceFailed";
private const string DocumentOpenedEventName = "DocumentOpened";
private const string DocumentClosedEventName = "DocumentClosed";
private const string SourceGeneratedDocumentOpenedEventName = "SourceGeneratedDocumentOpened";
private const string SourceGeneratedDocumentClosedEventName = "SourceGeneratedDocumentClosed";
private const string DocumentActiveContextChangedName = "DocumentActiveContextChanged";

/// <summary>
Expand Down Expand Up @@ -170,72 +168,6 @@ protected Task RaiseDocumentClosedEventAsync(Document document)
}
}

/// <summary>
/// An event that is fired when a documents is opened in the editor.
/// </summary>
internal event EventHandler<DocumentEventArgs> SourceGeneratedDocumentOpened
{
add
{
_eventMap.AddEventHandler(SourceGeneratedDocumentOpenedEventName, value);
}

remove
{
_eventMap.RemoveEventHandler(SourceGeneratedDocumentOpenedEventName, value);
}
}

private protected Task RaiseSourceGeneratedDocumentOpenedEventAsync(Document document)
{
var ev = GetEventHandlers<DocumentEventArgs>(SourceGeneratedDocumentOpenedEventName);
if (ev.HasHandlers && document != null)
{
return this.ScheduleTask(() =>
{
var args = new DocumentEventArgs(document);
ev.RaiseEvent(handler => handler(this, args));
}, SourceGeneratedDocumentOpenedEventName);
}
else
{
return Task.CompletedTask;
}
}

/// <summary>
/// An event that is fired when a document is closed in the editor.
/// </summary>
internal event EventHandler<DocumentEventArgs> SourceGeneratedDocumentClosed
{
add
{
_eventMap.AddEventHandler(SourceGeneratedDocumentClosedEventName, value);
}

remove
{
_eventMap.RemoveEventHandler(SourceGeneratedDocumentClosedEventName, value);
}
}

private protected Task RaiseSourceGeneratedDocumentClosedEventAsync(Document document)
{
var ev = GetEventHandlers<DocumentEventArgs>(SourceGeneratedDocumentClosedEventName);
if (ev.HasHandlers && document != null)
{
return this.ScheduleTask(() =>
{
var args = new DocumentEventArgs(document);
ev.RaiseEvent(handler => handler(this, args));
}, SourceGeneratedDocumentClosedEventName);
}
else
{
return Task.CompletedTask;
}
}

/// <summary>
/// An event that is fired when the active context document associated with a buffer
/// changes.
Expand Down

0 comments on commit f020e73

Please sign in to comment.