Skip to content

Commit b9fed85

Browse files
Switch to jtf in end construct handling
1 parent d35882b commit b9fed85

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

src/EditorFeatures/VisualBasic/EndConstructGeneration/EndConstructCommandHandler.vb

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,33 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EndConstructGeneration
3232
Implements IChainedCommandHandler(Of TypeCharCommandArgs)
3333
Implements IChainedCommandHandler(Of AutomaticLineEnderCommandArgs)
3434

35+
Private ReadOnly _threadingContext As IThreadingContext
3536
Private ReadOnly _editorOperationsFactoryService As IEditorOperationsFactoryService
3637
Private ReadOnly _undoHistoryRegistry As ITextUndoHistoryRegistry
3738
Private ReadOnly _editorOptionsService As EditorOptionsService
3839

3940
<ImportingConstructor()>
4041
<SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification:="Used in test code: https://github.com/dotnet/roslyn/issues/42814")>
41-
Public Sub New(editorOperationsFactoryService As IEditorOperationsFactoryService,
42-
undoHistoryRegistry As ITextUndoHistoryRegistry,
43-
editorOptionsService As EditorOptionsService)
44-
42+
Public Sub New(
43+
threadingContext As IThreadingContext,
44+
editorOperationsFactoryService As IEditorOperationsFactoryService,
45+
undoHistoryRegistry As ITextUndoHistoryRegistry,
46+
editorOptionsService As EditorOptionsService)
47+
_threadingContext = threadingContext
4548
_editorOperationsFactoryService = editorOperationsFactoryService
4649
_undoHistoryRegistry = undoHistoryRegistry
4750
_editorOptionsService = editorOptionsService
4851
End Sub
4952

50-
Public ReadOnly Property DisplayName As String Implements INamed.DisplayName
51-
Get
52-
Return VBEditorResources.End_Construct
53-
End Get
54-
End Property
53+
Public ReadOnly Property DisplayName As String = VBEditorResources.End_Construct Implements INamed.DisplayName
5554

5655
Public Function GetCommandState_ReturnKeyCommandHandler(args As ReturnKeyCommandArgs, nextHandler As Func(Of CommandState)) As CommandState Implements IChainedCommandHandler(Of ReturnKeyCommandArgs).GetCommandState
5756
Return nextHandler()
5857
End Function
5958

6059
Public Sub ExecuteCommand_ReturnKeyCommandHandler(args As ReturnKeyCommandArgs, nextHandler As Action, context As CommandExecutionContext) Implements IChainedCommandHandler(Of ReturnKeyCommandArgs).ExecuteCommand
61-
ExecuteEndConstructOnReturn(args.TextView, args.SubjectBuffer, nextHandler)
60+
_threadingContext.JoinableTaskFactory.Run(Function() ExecuteEndConstructOnReturnAsync(
61+
args.TextView, args.SubjectBuffer, nextHandler, context.OperationContext.UserCancellationToken))
6262
End Sub
6363

6464
Public Function GetCommandState_TypeCharCommandHandler(args As TypeCharCommandArgs, nextHandler As Func(Of CommandState)) As CommandState Implements IChainedCommandHandler(Of TypeCharCommandArgs).GetCommandState
@@ -88,17 +88,25 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EndConstructGeneration
8888
End Function
8989

9090
Public Sub ExecuteCommand_AutomaticLineEnderCommandHandler(args As AutomaticLineEnderCommandArgs, nextHandler As Action, context As CommandExecutionContext) Implements IChainedCommandHandler(Of AutomaticLineEnderCommandArgs).ExecuteCommand
91-
ExecuteEndConstructOnReturn(args.TextView, args.SubjectBuffer, Sub()
92-
Dim operations = Me._editorOperationsFactoryService.GetEditorOperations(args.TextView)
93-
If operations Is Nothing Then
94-
nextHandler()
95-
Else
96-
operations.InsertNewLine()
97-
End If
98-
End Sub)
91+
_threadingContext.JoinableTaskFactory.Run(Function() ExecuteEndConstructOnReturnAsync(
92+
args.TextView,
93+
args.SubjectBuffer,
94+
Sub()
95+
Dim operations = Me._editorOperationsFactoryService.GetEditorOperations(args.TextView)
96+
If operations Is Nothing Then
97+
nextHandler()
98+
Else
99+
operations.InsertNewLine()
100+
End If
101+
End Sub,
102+
context.OperationContext.UserCancellationToken))
99103
End Sub
100104

101-
Private Sub ExecuteEndConstructOnReturn(textView As ITextView, subjectBuffer As ITextBuffer, nextHandler As Action)
105+
Private Async Function ExecuteEndConstructOnReturnAsync(
106+
textView As ITextView,
107+
subjectBuffer As ITextBuffer,
108+
nextHandler As Action,
109+
cancellationToken As CancellationToken) As Task
102110
If Not _editorOptionsService.GlobalOptions.GetOption(EndConstructGenerationOptionsStorage.EndConstruct, LanguageNames.VisualBasic) OrElse
103111
Not subjectBuffer.CanApplyChangeDocumentToWorkspace() Then
104112
nextHandler()
@@ -111,7 +119,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EndConstructGeneration
111119
Return
112120
End If
113121

114-
CleanupBeforeEndConstruct(textView, subjectBuffer, document, CancellationToken.None)
122+
Await CleanupBeforeEndConstructAsync(
123+
textView, subjectBuffer, document, cancellationToken).ConfigureAwait(True)
115124

116125
Dim endConstructService = document.GetLanguageService(Of IEndConstructGenerationService)()
117126
Dim result = endConstructService.TryDo(textView, subjectBuffer, vbLf(0), CancellationToken.None)
@@ -120,9 +129,13 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EndConstructGeneration
120129
nextHandler()
121130
Return
122131
End If
123-
End Sub
132+
End Function
124133

125-
Private Sub CleanupBeforeEndConstruct(view As ITextView, buffer As ITextBuffer, document As Document, cancellationToken As CancellationToken)
134+
Private Async Function CleanupBeforeEndConstructAsync(
135+
view As ITextView,
136+
buffer As ITextBuffer,
137+
document As Document,
138+
cancellationToken As CancellationToken) As Task
126139
Dim position = view.GetCaretPoint(buffer)
127140
If Not position.HasValue Then
128141
Return
@@ -141,15 +154,16 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EndConstructGeneration
141154
End Function)
142155

143156
Dim options = buffer.GetCodeCleanupOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), document.Project.Services, explicitFormat:=False, allowImportsInHiddenRegions:=document.AllowImportsInHiddenRegions())
144-
Dim cleanDocument = CodeCleaner.CleanupAsync(document, GetSpanToCleanup(statement), Options, codeCleanups, cancellationToken:=cancellationToken).WaitAndGetResult(cancellationToken)
157+
Dim cleanDocument = Await CodeCleaner.CleanupAsync(
158+
document, GetSpanToCleanup(statement), options, codeCleanups, cancellationToken).ConfigureAwait(True)
145159
Dim changes = cleanDocument.GetTextChangesSynchronously(document, cancellationToken)
146160

147161
Using transaction = New CaretPreservingEditTransaction(VBEditorResources.End_Construct, view, _undoHistoryRegistry, _editorOperationsFactoryService)
148162
transaction.MergePolicy = AutomaticCodeChangeMergePolicy.Instance
149163
buffer.ApplyChanges(changes)
150164
transaction.Complete()
151165
End Using
152-
End Sub
166+
End Function
153167

154168
Private Shared Function GetSpanToCleanup(statement As StatementSyntax) As TextSpan
155169
Dim firstToken = statement.GetFirstToken()

0 commit comments

Comments
 (0)