Skip to content

Commit 2051ab9

Browse files
Remove more cases of WaitAndGetResult
1 parent 5638aed commit 2051ab9

File tree

4 files changed

+92
-102
lines changed

4 files changed

+92
-102
lines changed

src/VisualStudio/Core/Def/Implementation/AbstractOleCommandTarget.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics.CodeAnalysis;
66
using System.Runtime.InteropServices;
77
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
8+
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
89
using Microsoft.VisualStudio.ComponentModelHost;
910
using Microsoft.VisualStudio.Editor;
1011
using Microsoft.VisualStudio.OLE.Interop;
@@ -35,6 +36,7 @@ public AbstractOleCommandTarget(
3536
}
3637

3738
public IComponentModel ComponentModel { get; }
39+
protected IThreadingContext ThreadingContext => ComponentModel.GetService<IThreadingContext>();
3840

3941
public IVsEditorAdaptersFactoryService EditorAdaptersFactory
4042
{

src/VisualStudio/Core/Def/Implementation/AbstractVsTextViewFilter.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System;
88
using System.Linq;
99
using System.Threading;
10+
using System.Threading.Tasks;
1011
using Microsoft.CodeAnalysis.BraceMatching;
1112
using Microsoft.CodeAnalysis.Debugging;
1213
using Microsoft.CodeAnalysis.Editor;
@@ -29,15 +30,10 @@
2930

3031
namespace Microsoft.VisualStudio.LanguageServices.Implementation;
3132

32-
internal abstract class AbstractVsTextViewFilter : AbstractOleCommandTarget, IVsTextViewFilter
33+
internal abstract class AbstractVsTextViewFilter(
34+
IWpfTextView wpfTextView,
35+
IComponentModel componentModel) : AbstractOleCommandTarget(wpfTextView, componentModel), IVsTextViewFilter
3336
{
34-
public AbstractVsTextViewFilter(
35-
IWpfTextView wpfTextView,
36-
IComponentModel componentModel)
37-
: base(wpfTextView, componentModel)
38-
{
39-
}
40-
4137
int IVsTextViewFilter.GetDataTipText(TextSpan[] pSpan, out string pbstrText)
4238
{
4339
try
@@ -144,41 +140,41 @@ protected int GetDataTipTextImpl(ITextBuffer subjectBuffer, TextSpan[] pSpan, ou
144140

145141
int IVsTextViewFilter.GetPairExtents(int iLine, int iIndex, TextSpan[] pSpan)
146142
{
147-
try
148-
{
149-
var result = VSConstants.S_OK;
150-
ComponentModel.GetService<IUIThreadOperationExecutor>().Execute(
151-
"Intellisense",
152-
defaultDescription: "",
153-
allowCancellation: true,
154-
showProgress: false,
155-
action: c => result = GetPairExtentsWorker(iLine, iIndex, pSpan, c.UserCancellationToken));
156-
157-
return result;
158-
}
159-
catch (Exception e) when (FatalError.ReportAndCatch(e) && false)
160-
{
161-
throw ExceptionUtilities.Unreachable();
162-
}
143+
return this.ThreadingContext.JoinableTaskFactory.Run(() => GetPairExtentsAsync(iLine, iIndex, pSpan));
163144
}
164145

165-
private int GetPairExtentsWorker(int iLine, int iIndex, TextSpan[] pSpan, CancellationToken cancellationToken)
146+
private async Task<int> GetPairExtentsAsync(int iLine, int iIndex, TextSpan[] pSpan)
166147
{
148+
using var waitContext = ComponentModel.GetService<IUIThreadOperationExecutor>().BeginExecute(
149+
"Intellisense",
150+
defaultDescription: "",
151+
allowCancellation: true,
152+
showProgress: false);
153+
167154
var braceMatcher = ComponentModel.GetService<IBraceMatchingService>();
168155
var globalOptions = ComponentModel.GetService<IGlobalOptionService>();
169-
return GetPairExtentsWorker(
156+
157+
return await GetPairExtentsAsync(
170158
WpfTextView,
171159
braceMatcher,
172160
globalOptions,
173161
iLine,
174162
iIndex,
175163
pSpan,
176164
(VSConstants.VSStd2KCmdID)this.CurrentlyExecutingCommand == VSConstants.VSStd2KCmdID.GOTOBRACE_EXT,
177-
cancellationToken);
165+
waitContext.UserCancellationToken).ConfigureAwait(true);
178166
}
179167

180168
// Internal for testing purposes
181-
internal static int GetPairExtentsWorker(ITextView textView, IBraceMatchingService braceMatcher, IGlobalOptionService globalOptions, int iLine, int iIndex, TextSpan[] pSpan, bool extendSelection, CancellationToken cancellationToken)
169+
internal static async Task<int> GetPairExtentsAsync(
170+
ITextView textView,
171+
IBraceMatchingService braceMatcher,
172+
IGlobalOptionService globalOptions,
173+
int iLine,
174+
int iIndex,
175+
TextSpan[] pSpan,
176+
bool extendSelection,
177+
CancellationToken cancellationToken)
182178
{
183179
pSpan[0].iStartLine = pSpan[0].iEndLine = iLine;
184180
pSpan[0].iStartIndex = pSpan[0].iEndIndex = iIndex;
@@ -202,7 +198,8 @@ internal static int GetPairExtentsWorker(ITextView textView, IBraceMatchingServi
202198
if (document != null)
203199
{
204200
var options = globalOptions.GetBraceMatchingOptions(document.Project.Language);
205-
var matchingSpan = braceMatcher.FindMatchingSpanAsync(document, position, options, cancellationToken).WaitAndGetResult(cancellationToken);
201+
var matchingSpan = await braceMatcher.FindMatchingSpanAsync(
202+
document, position, options, cancellationToken).ConfigureAwait(true);
206203

207204
if (matchingSpan.HasValue)
208205
{
@@ -236,7 +233,8 @@ internal static int GetPairExtentsWorker(ITextView textView, IBraceMatchingServi
236233
if (extendSelection)
237234
{
238235
// case a.
239-
var closingSpans = braceMatcher.FindMatchingSpanAsync(document, matchingSpan.Value.Start, options, cancellationToken).WaitAndGetResult(cancellationToken);
236+
var closingSpans = await braceMatcher.FindMatchingSpanAsync(
237+
document, matchingSpan.Value.Start, options, cancellationToken).ConfigureAwait(true);
240238
var vsClosingSpans = textView.GetSpanInView(closingSpans.Value.ToSnapshotSpan(subjectBuffer.CurrentSnapshot)).First().ToVsTextSpan();
241239
pSpan[0].iEndIndex = vsClosingSpans.iStartIndex;
242240
}
@@ -255,7 +253,8 @@ internal static int GetPairExtentsWorker(ITextView textView, IBraceMatchingServi
255253
pSpan[0].iEndIndex = vsTextSpan.iStartIndex;
256254

257255
// case b.
258-
var openingSpans = braceMatcher.FindMatchingSpanAsync(document, matchingSpan.Value.End, options, cancellationToken).WaitAndGetResult(cancellationToken);
256+
var openingSpans = await braceMatcher.FindMatchingSpanAsync(
257+
document, matchingSpan.Value.End, options, cancellationToken).ConfigureAwait(true);
259258
var vsOpeningSpans = textView.GetSpanInView(openingSpans.Value.ToSnapshotSpan(subjectBuffer.CurrentSnapshot)).First().ToVsTextSpan();
260259
pSpan[0].iStartIndex = vsOpeningSpans.iStartIndex;
261260
}

src/VisualStudio/Core/Def/Implementation/StandaloneCommandFilter.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,5 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation;
1010
/// <summary>
1111
/// A CommandFilter used for "normal" files, as opposed to Venus files which are special.
1212
/// </summary>
13-
internal sealed class StandaloneCommandFilter : AbstractVsTextViewFilter
14-
{
15-
/// <summary>
16-
/// Creates a new command handler that is attached to an IVsTextView.
17-
/// </summary>
18-
/// <param name="wpfTextView">The IWpfTextView of the view.</param>
19-
internal StandaloneCommandFilter(
20-
IWpfTextView wpfTextView,
21-
IComponentModel componentModel)
22-
: base(wpfTextView, componentModel)
23-
{
24-
}
25-
}
13+
internal sealed class StandaloneCommandFilter(
14+
IWpfTextView wpfTextView, IComponentModel componentModel) : AbstractVsTextViewFilter(wpfTextView, componentModel);

0 commit comments

Comments
 (0)