|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | | -#nullable disable |
6 | | - |
7 | 5 | using System; |
8 | 6 | using System.ComponentModel.Composition; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
9 | 8 | using System.Threading; |
10 | 9 | using Microsoft.CodeAnalysis.Editor; |
11 | 10 | using Microsoft.CodeAnalysis.Editor.Shared.Extensions; |
@@ -44,36 +43,50 @@ internal sealed partial class BraceCompletionSessionProvider( |
44 | 43 | private readonly IEditorOperationsFactoryService _editorOperationsFactoryService = editorOperationsFactoryService; |
45 | 44 | private readonly EditorOptionsService _editorOptionsService = editorOptionsService; |
46 | 45 |
|
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) |
48 | 49 | { |
49 | 50 | _threadingContext.ThrowIfNotOnUIThread(); |
50 | 51 |
|
51 | 52 | var responsiveCompletion = textView.Options.GetOptionValue(DefaultOptions.ResponsiveCompletionOptionId); |
52 | 53 | var cancellationToken = GetCancellationToken(responsiveCompletion); |
| 54 | + try |
| 55 | + { |
| 56 | + return TryCreateSessionWorker(out session); |
| 57 | + } |
| 58 | + catch (OperationCanceledException) |
| 59 | + { |
| 60 | + session = null; |
| 61 | + return false; |
| 62 | + } |
53 | 63 |
|
54 | | - var textSnapshot = openingPoint.Snapshot; |
55 | | - var document = textSnapshot.GetOpenDocumentInCurrentContextWithChanges(); |
56 | | - if (document != null) |
| 64 | + bool TryCreateSessionWorker([NotNullWhen(true)] out IBraceCompletionSession? session) |
57 | 65 | { |
58 | | - var editorSessionFactory = document.GetLanguageService<IBraceCompletionServiceFactory>(); |
59 | | - if (editorSessionFactory != null) |
| 66 | + var textSnapshot = openingPoint.Snapshot; |
| 67 | + var document = textSnapshot.GetOpenDocumentInCurrentContextWithChanges(); |
| 68 | + if (document != null) |
60 | 69 | { |
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) |
64 | 72 | { |
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 | + } |
71 | 84 | } |
72 | 85 | } |
73 | | - } |
74 | 86 |
|
75 | | - session = null; |
76 | | - return false; |
| 87 | + session = null; |
| 88 | + return false; |
| 89 | + } |
77 | 90 | } |
78 | 91 |
|
79 | 92 | private static CancellationToken GetCancellationToken(bool responsiveCompletion) |
|
0 commit comments