Skip to content

Commit 93fe2ee

Browse files
Do not leak cancellation exception for our own operation (#78376)
2 parents e51a3c6 + 441aac4 commit 93fe2ee

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/EditorFeatures/Core/AutomaticCompletion/BraceCompletionSessionProvider.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
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-
#nullable disable
6-
75
using System;
86
using System.ComponentModel.Composition;
7+
using System.Diagnostics.CodeAnalysis;
98
using System.Threading;
109
using Microsoft.CodeAnalysis.Editor;
1110
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
@@ -44,36 +43,50 @@ internal sealed partial class BraceCompletionSessionProvider(
4443
private readonly IEditorOperationsFactoryService _editorOperationsFactoryService = editorOperationsFactoryService;
4544
private readonly EditorOptionsService _editorOptionsService = editorOptionsService;
4645

47-
public bool TryCreateSession(ITextView textView, SnapshotPoint openingPoint, char openingBrace, char closingBrace, out IBraceCompletionSession session)
46+
public bool TryCreateSession(
47+
ITextView textView, SnapshotPoint openingPoint, char openingBrace, char closingBrace,
48+
[NotNullWhen(true)] out IBraceCompletionSession? session)
4849
{
4950
_threadingContext.ThrowIfNotOnUIThread();
5051

5152
var responsiveCompletion = textView.Options.GetOptionValue(DefaultOptions.ResponsiveCompletionOptionId);
5253
var cancellationToken = GetCancellationToken(responsiveCompletion);
54+
try
55+
{
56+
return TryCreateSessionWorker(out session);
57+
}
58+
catch (OperationCanceledException)
59+
{
60+
session = null;
61+
return false;
62+
}
5363

54-
var textSnapshot = openingPoint.Snapshot;
55-
var document = textSnapshot.GetOpenDocumentInCurrentContextWithChanges();
56-
if (document != null)
64+
bool TryCreateSessionWorker([NotNullWhen(true)] out IBraceCompletionSession? session)
5765
{
58-
var editorSessionFactory = document.GetLanguageService<IBraceCompletionServiceFactory>();
59-
if (editorSessionFactory != null)
66+
var textSnapshot = openingPoint.Snapshot;
67+
var document = textSnapshot.GetOpenDocumentInCurrentContextWithChanges();
68+
if (document != null)
6069
{
61-
var parsedDocument = ParsedDocument.CreateSynchronously(document, cancellationToken);
62-
var editorSession = editorSessionFactory.TryGetService(parsedDocument, openingPoint, openingBrace, cancellationToken);
63-
if (editorSession != null)
70+
var editorSessionFactory = document.GetLanguageService<IBraceCompletionServiceFactory>();
71+
if (editorSessionFactory != null)
6472
{
65-
var undoHistory = _undoManager.GetTextBufferUndoManager(textView.TextBuffer).TextBufferUndoHistory;
66-
session = new BraceCompletionSession(
67-
this, textView, openingPoint.Snapshot.TextBuffer,
68-
openingPoint, openingBrace, closingBrace,
69-
undoHistory, editorSession, responsiveCompletion);
70-
return true;
73+
var parsedDocument = ParsedDocument.CreateSynchronously(document, cancellationToken);
74+
var editorSession = editorSessionFactory.TryGetService(parsedDocument, openingPoint, openingBrace, cancellationToken);
75+
if (editorSession != null)
76+
{
77+
var undoHistory = _undoManager.GetTextBufferUndoManager(textView.TextBuffer).TextBufferUndoHistory;
78+
session = new BraceCompletionSession(
79+
this, textView, openingPoint.Snapshot.TextBuffer,
80+
openingPoint, openingBrace, closingBrace,
81+
undoHistory, editorSession, responsiveCompletion);
82+
return true;
83+
}
7184
}
7285
}
73-
}
7486

75-
session = null;
76-
return false;
87+
session = null;
88+
return false;
89+
}
7790
}
7891

7992
private static CancellationToken GetCancellationToken(bool responsiveCompletion)

0 commit comments

Comments
 (0)