Skip to content

Commit c53bdd8

Browse files
Update to platform background work pattern to make porting to their impl easier (#78469)
2 parents 0c003e9 + fc6d789 commit c53bdd8

File tree

6 files changed

+31
-15
lines changed

6 files changed

+31
-15
lines changed

src/EditorFeatures/CSharp/EventHookup/EventHookupCommandHandler_TabKeyCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ async Task<bool> TryExecuteCommandAsync()
167167
}
168168

169169
// We're about to make an edit ourselves. so disable the cancellation that happens on editing.
170-
waitContext.CancelOnEdit = false;
170+
using var _ = waitContext.SuppressAutoCancel();
171171

172172
var workspace = document.Project.Solution.Workspace;
173173
if (!workspace.TryApplyChanges(solutionAndRenameSpan.Value.solution))

src/EditorFeatures/Core/BackgroundWorkIndicator/BackgroundWorkIndicatorContext.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,25 @@ public void RemoveScope(BackgroundWorkIndicatorScope scope)
296296
string IUIThreadOperationContext.Description => BuildData().description;
297297

298298
bool IUIThreadOperationContext.AllowCancellation => true;
299+
300+
public IDisposable SuppressAutoCancel()
301+
{
302+
var disposer = new SuppressAutoCancelDisposable(this, this.CancelOnEdit, this.CancelOnFocusLost);
303+
this.CancelOnEdit = false;
304+
this.CancelOnFocusLost = false;
305+
return disposer;
306+
}
307+
}
308+
309+
private sealed class SuppressAutoCancelDisposable(
310+
BackgroundWorkIndicatorContext context,
311+
bool originalCancelOnEdit,
312+
bool originalCancelOnFocusLost) : IDisposable
313+
{
314+
public void Dispose()
315+
{
316+
context.CancelOnEdit = originalCancelOnEdit;
317+
context.CancelOnFocusLost = originalCancelOnFocusLost;
318+
}
299319
}
300320
}

src/EditorFeatures/Core/BackgroundWorkIndicator/IBackgroundWorkIndicatorContext.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using Microsoft.VisualStudio.Utilities;
67

78
namespace Microsoft.CodeAnalysis.Editor.BackgroundWorkIndicator;
89

910
internal interface IBackgroundWorkIndicatorContext : IUIThreadOperationContext
1011
{
1112
/// <summary>
12-
/// Whether or not this context should cancel work if a navigation happens. Clients that use this indicator can
13-
/// have this behavior set to true (so that they cancel if the user edits themselves), but then set this to
14-
/// false right before they edit themselves so that they do not self-cancel.
13+
/// Allows clients to temporarily suppress auto cancel behaviors when they want to apply edits or navigate without canceling.
1514
/// </summary>
16-
bool CancelOnEdit { get; set; }
17-
18-
/// <summary>
19-
/// Whether or not this context should cancel work if the text view it is attached to loses focus. Clients that use
20-
/// this indicator can have this behavior set to true (so that they cancel if the user navigates themselves), but
21-
/// then set this to false right before they navigate themselves so that they do not self-cancel.
22-
/// </summary>
23-
bool CancelOnFocusLost { get; set; }
15+
IDisposable SuppressAutoCancel();
2416
}

src/EditorFeatures/Core/ExtractMethod/ExtractMethodCommandHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ private void ApplyChange_OnUIThread(
201201
using var undoTransaction = _undoManager.GetTextBufferUndoManager(textBuffer).TextBufferUndoHistory.CreateTransaction("Extract Method");
202202

203203
// We're about to make an edit ourselves. so disable the cancellation that happens on editing.
204-
waitContext.CancelOnEdit = false;
204+
using var _ = waitContext.SuppressAutoCancel();
205+
205206
textBuffer.ApplyChanges(changes);
206207

207208
// apply changes

src/EditorFeatures/Core/GoToDefinition/GoToDefinitionCommandHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.CodeAnalysis.Shared.TestHooks;
1717
using Microsoft.CodeAnalysis.Text;
1818
using Microsoft.VisualStudio.Commanding;
19+
using Microsoft.VisualStudio.Language.Intellisense.Utilities;
1920
using Microsoft.VisualStudio.Text;
2021
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
2122
using Microsoft.VisualStudio.Threading;
@@ -128,7 +129,8 @@ private async Task ExecuteAsynchronouslyAsync(
128129

129130
// we're about to navigate. so disable cancellation on focus-lost in our indicator so we don't end up
130131
// causing ourselves to self-cancel.
131-
backgroundIndicator.CancelOnFocusLost = false;
132+
using var _ = backgroundIndicator.SuppressAutoCancel();
133+
132134
succeeded = definitionLocation != null && await definitionLocation.Location.TryNavigateToAsync(
133135
_threadingContext, new NavigationOptions(PreferProvisionalTab: true, ActivateTab: true), cancellationToken).ConfigureAwait(false);
134136
}

src/EditorFeatures/Core/Organizing/OrganizeDocumentCommandHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Microsoft.CodeAnalysis.Shared.TestHooks;
2020
using Microsoft.CodeAnalysis.Text;
2121
using Microsoft.VisualStudio.Commanding;
22+
using Microsoft.VisualStudio.Language.Intellisense.Utilities;
2223
using Microsoft.VisualStudio.Text;
2324
using Microsoft.VisualStudio.Text.Editor.Commanding;
2425
using Microsoft.VisualStudio.Threading;
@@ -152,7 +153,7 @@ private async Task ExecuteAsync(
152153
await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
153154

154155
// We're about to make an edit ourselves. so disable the cancellation that happens on editing.
155-
backgroundWorkContext.CancelOnEdit = false;
156+
using var _ = backgroundWorkContext.SuppressAutoCancel();
156157

157158
commandArgs.SubjectBuffer.ApplyChanges(changes);
158159
}

0 commit comments

Comments
 (0)