Skip to content

Commit

Permalink
Use simple using statement
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Nov 20, 2021
1 parent df2586f commit 703bb7e
Showing 1 changed file with 80 additions and 101 deletions.
181 changes: 80 additions & 101 deletions src/EditorFeatures/Core/CommandHandlers/BufferedFindUsagesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,74 +47,64 @@ public BufferedFindUsagesContext()

public async Task<string?> GetMessageAsync(CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
{
Contract.ThrowIfTrue(_streamingPresenterContext != null, "Should not be called if we've switched over to the streaming presenter");
return _message;
}
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfTrue(_streamingPresenterContext != null, "Should not be called if we've switched over to the streaming presenter");
return _message;
}

public async Task<string?> GetInformationalMessageAsync(CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
{
Contract.ThrowIfTrue(_streamingPresenterContext != null, "Should not be called if we've switched over to the streaming presenter");
return _informationalMessage;
}
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfTrue(_streamingPresenterContext != null, "Should not be called if we've switched over to the streaming presenter");
return _informationalMessage;
}

public async Task<string?> GetSearchTitleAsync(CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
{
Contract.ThrowIfTrue(_streamingPresenterContext != null, "Should not be called if we've switched over to the streaming presenter");
return _searchTitle;
}
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfTrue(_streamingPresenterContext != null, "Should not be called if we've switched over to the streaming presenter");
return _searchTitle;
}

public async Task<ImmutableArray<DefinitionItem>> GetDefinitionsAsync(CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
{
Contract.ThrowIfNull(_definitions, "This should not be called if we switched over to the presenter to show results");
return _definitions.ToImmutable();
}
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(_definitions, "This should not be called if we switched over to the presenter to show results");
return _definitions.ToImmutable();
}

public async Task AttachToStreamingPresenterAsync(IFindUsagesContext presenterContext, CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
{
Contract.ThrowIfTrue(_streamingPresenterContext != null, "Trying to set the presenter multiple times.");
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfTrue(_streamingPresenterContext != null, "Trying to set the presenter multiple times.");

// Push all values we've buffered into the new presenter context.
// Push all values we've buffered into the new presenter context.

await presenterContext.ProgressTracker.AddItemsAsync(_totalItemCount, cancellationToken).ConfigureAwait(false);
await presenterContext.ProgressTracker.ItemsCompletedAsync(_itemsCompleted, cancellationToken).ConfigureAwait(false);
await presenterContext.ProgressTracker.AddItemsAsync(_totalItemCount, cancellationToken).ConfigureAwait(false);
await presenterContext.ProgressTracker.ItemsCompletedAsync(_itemsCompleted, cancellationToken).ConfigureAwait(false);

if (_searchTitle != null)
await presenterContext.SetSearchTitleAsync(_searchTitle, cancellationToken).ConfigureAwait(false);
if (_searchTitle != null)
await presenterContext.SetSearchTitleAsync(_searchTitle, cancellationToken).ConfigureAwait(false);

if (_message != null)
await presenterContext.ReportMessageAsync(_message, cancellationToken).ConfigureAwait(false);
if (_message != null)
await presenterContext.ReportMessageAsync(_message, cancellationToken).ConfigureAwait(false);

if (_informationalMessage != null)
await presenterContext.ReportInformationalMessageAsync(_informationalMessage, cancellationToken).ConfigureAwait(false);
if (_informationalMessage != null)
await presenterContext.ReportInformationalMessageAsync(_informationalMessage, cancellationToken).ConfigureAwait(false);

Contract.ThrowIfNull(_definitions);
foreach (var definition in _definitions)
await presenterContext.OnDefinitionFoundAsync(definition, cancellationToken).ConfigureAwait(false);

// Now swap over to the presenter being the sink for all future callbacks, and clear any buffered data.
_streamingPresenterContext = presenterContext;

_totalItemCount = -1;
_itemsCompleted = -1;
_searchTitle = null;
_message = null;
_informationalMessage = null;
_definitions = null;
}
Contract.ThrowIfNull(_definitions);
foreach (var definition in _definitions)
await presenterContext.OnDefinitionFoundAsync(definition, cancellationToken).ConfigureAwait(false);

// Now swap over to the presenter being the sink for all future callbacks, and clear any buffered data.
_streamingPresenterContext = presenterContext;

_totalItemCount = -1;
_itemsCompleted = -1;
_searchTitle = null;
_message = null;
_informationalMessage = null;
_definitions = null;
}

#region IStreamingProgressTracker
Expand All @@ -123,31 +113,27 @@ public async Task AttachToStreamingPresenterAsync(IFindUsagesContext presenterCo

async ValueTask IStreamingProgressTracker.AddItemsAsync(int count, CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
if (_streamingPresenterContext != null)
{
if (_streamingPresenterContext != null)
{
await _streamingPresenterContext.ProgressTracker.AddItemsAsync(count, cancellationToken).ConfigureAwait(false);
}
else
{
_totalItemCount += count;
}
await _streamingPresenterContext.ProgressTracker.AddItemsAsync(count, cancellationToken).ConfigureAwait(false);
}
else
{
_totalItemCount += count;
}
}

async ValueTask IStreamingProgressTracker.ItemsCompletedAsync(int count, CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
if (_streamingPresenterContext != null)
{
await _streamingPresenterContext.ProgressTracker.ItemsCompletedAsync(count, cancellationToken).ConfigureAwait(false);
}
else
{
if (_streamingPresenterContext != null)
{
await _streamingPresenterContext.ProgressTracker.ItemsCompletedAsync(count, cancellationToken).ConfigureAwait(false);
}
else
{
_itemsCompleted += count;
}
_itemsCompleted += count;
}
}

Expand All @@ -157,68 +143,61 @@ async ValueTask IStreamingProgressTracker.ItemsCompletedAsync(int count, Cancell

async ValueTask IFindUsagesContext.ReportMessageAsync(string message, CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
if (_streamingPresenterContext != null)
{
if (_streamingPresenterContext != null)
{
await _streamingPresenterContext.ReportMessageAsync(message, cancellationToken).ConfigureAwait(false);
}
else
{
_message = message;
}
await _streamingPresenterContext.ReportMessageAsync(message, cancellationToken).ConfigureAwait(false);
}
else
{
_message = message;
}
}

async ValueTask IFindUsagesContext.ReportInformationalMessageAsync(string message, CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
if (_streamingPresenterContext != null)
{
if (_streamingPresenterContext != null)
{
await _streamingPresenterContext.ReportInformationalMessageAsync(message, cancellationToken).ConfigureAwait(false);
}
else
{
_informationalMessage = message;
}
await _streamingPresenterContext.ReportInformationalMessageAsync(message, cancellationToken).ConfigureAwait(false);
}
else
{
_informationalMessage = message;
}
}

async ValueTask IFindUsagesContext.SetSearchTitleAsync(string title, CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
if (_streamingPresenterContext != null)
{
await _streamingPresenterContext.SetSearchTitleAsync(title, cancellationToken).ConfigureAwait(false);
}
else
{
if (_streamingPresenterContext != null)
{
await _streamingPresenterContext.SetSearchTitleAsync(title, cancellationToken).ConfigureAwait(false);
}
else
{
_searchTitle = title;
}
_searchTitle = title;
}
}

async ValueTask IFindUsagesContext.OnDefinitionFoundAsync(DefinitionItem definition, CancellationToken cancellationToken)
{
using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
if (_streamingPresenterContext != null)
{
if (_streamingPresenterContext != null)
{
await _streamingPresenterContext.OnDefinitionFoundAsync(definition, cancellationToken).ConfigureAwait(false);
}
else
{
Contract.ThrowIfNull(_definitions);
_definitions.Add(definition);
}
await _streamingPresenterContext.OnDefinitionFoundAsync(definition, cancellationToken).ConfigureAwait(false);
}
else
{
Contract.ThrowIfNull(_definitions);
_definitions.Add(definition);
}
}

ValueTask IFindUsagesContext.OnReferenceFoundAsync(SourceReferenceItem reference, CancellationToken cancellationToken)
{
// Entirely ignored. These features do not show references.
Contract.Fail("GoToImpl/Base should never report a reference.");
return ValueTaskFactory.CompletedTask;
}

Expand Down

0 comments on commit 703bb7e

Please sign in to comment.