Skip to content

Do not leak cancellation exception for our own operation #78376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
Expand Down Expand Up @@ -44,36 +43,50 @@ internal sealed partial class BraceCompletionSessionProvider(
private readonly IEditorOperationsFactoryService _editorOperationsFactoryService = editorOperationsFactoryService;
private readonly EditorOptionsService _editorOptionsService = editorOptionsService;

public bool TryCreateSession(ITextView textView, SnapshotPoint openingPoint, char openingBrace, char closingBrace, out IBraceCompletionSession session)
public bool TryCreateSession(
ITextView textView, SnapshotPoint openingPoint, char openingBrace, char closingBrace,
[NotNullWhen(true)] out IBraceCompletionSession? session)
{
_threadingContext.ThrowIfNotOnUIThread();

var responsiveCompletion = textView.Options.GetOptionValue(DefaultOptions.ResponsiveCompletionOptionId);
var cancellationToken = GetCancellationToken(responsiveCompletion);
try
{
return TryCreateSessionWorker(out session);
}
catch (OperationCanceledException)
{
session = null;
return false;
}

var textSnapshot = openingPoint.Snapshot;
var document = textSnapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document != null)
bool TryCreateSessionWorker([NotNullWhen(true)] out IBraceCompletionSession? session)
{
var editorSessionFactory = document.GetLanguageService<IBraceCompletionServiceFactory>();
if (editorSessionFactory != null)
var textSnapshot = openingPoint.Snapshot;
var document = textSnapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document != null)
{
var parsedDocument = ParsedDocument.CreateSynchronously(document, cancellationToken);
var editorSession = editorSessionFactory.TryGetService(parsedDocument, openingPoint, openingBrace, cancellationToken);
if (editorSession != null)
var editorSessionFactory = document.GetLanguageService<IBraceCompletionServiceFactory>();
if (editorSessionFactory != null)
{
var undoHistory = _undoManager.GetTextBufferUndoManager(textView.TextBuffer).TextBufferUndoHistory;
session = new BraceCompletionSession(
this, textView, openingPoint.Snapshot.TextBuffer,
openingPoint, openingBrace, closingBrace,
undoHistory, editorSession, responsiveCompletion);
return true;
var parsedDocument = ParsedDocument.CreateSynchronously(document, cancellationToken);
var editorSession = editorSessionFactory.TryGetService(parsedDocument, openingPoint, openingBrace, cancellationToken);
if (editorSession != null)
{
var undoHistory = _undoManager.GetTextBufferUndoManager(textView.TextBuffer).TextBufferUndoHistory;
session = new BraceCompletionSession(
this, textView, openingPoint.Snapshot.TextBuffer,
openingPoint, openingBrace, closingBrace,
undoHistory, editorSession, responsiveCompletion);
return true;
}
}
}
}

session = null;
return false;
session = null;
return false;
}
}

private static CancellationToken GetCancellationToken(bool responsiveCompletion)
Expand Down
Loading