From afcedeb55ebdb554b785840cd7d4154a08db389d Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sat, 2 Mar 2019 13:53:14 -0600 Subject: [PATCH] Restore original behavior of Shift+Enter during completion Fixes #33823 --- .../AutomaticLineEnderCommandHandler.cs | 5 ++-- .../AutomaticLineEnderTests.cs | 6 +++-- ...bstractAutomaticLineEnderCommandHandler.cs | 23 ++++++++++++++++++- .../AbstractAutomaticLineEnderTests.cs | 16 ++++--------- .../AutomaticLineEnderCommandHandler.vb | 5 ++-- .../AutomaticLineEnderTests.vb | 6 +++-- .../CSharp/CSharpIntelliSense.cs | 3 +-- 7 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/EditorFeatures/CSharp/AutomaticCompletion/AutomaticLineEnderCommandHandler.cs b/src/EditorFeatures/CSharp/AutomaticCompletion/AutomaticLineEnderCommandHandler.cs index 507f8e0e55952..e94778bf85540 100644 --- a/src/EditorFeatures/CSharp/AutomaticCompletion/AutomaticLineEnderCommandHandler.cs +++ b/src/EditorFeatures/CSharp/AutomaticCompletion/AutomaticLineEnderCommandHandler.cs @@ -34,8 +34,9 @@ internal class AutomaticLineEnderCommandHandler : AbstractAutomaticLineEnderComm [ImportingConstructor] public AutomaticLineEnderCommandHandler( ITextUndoHistoryRegistry undoRegistry, - IEditorOperationsFactoryService editorOperations) - : base(undoRegistry, editorOperations) + IEditorOperationsFactoryService editorOperations, + IAsyncCompletionBroker asyncCompletionBroker) + : base(undoRegistry, editorOperations, asyncCompletionBroker) { } diff --git a/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticLineEnderTests.cs b/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticLineEnderTests.cs index a1aa2682f2fce..67fe477f261ed 100644 --- a/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticLineEnderTests.cs +++ b/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticLineEnderTests.cs @@ -6,6 +6,7 @@ using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces; using Microsoft.CodeAnalysis.Test.Utilities; using Microsoft.VisualStudio.Commanding; +using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; using Microsoft.VisualStudio.Text.Editor.Commanding.Commands; using Microsoft.VisualStudio.Text.Operations; using Roslyn.Test.Utilities; @@ -848,9 +849,10 @@ protected override Action CreateNextHandler(TestWorkspace workspace) internal override IChainedCommandHandler CreateCommandHandler( ITextUndoHistoryRegistry undoRegistry, - IEditorOperationsFactoryService editorOperations) + IEditorOperationsFactoryService editorOperations, + IAsyncCompletionBroker asyncCompletionBroker) { - return new AutomaticLineEnderCommandHandler(undoRegistry, editorOperations); + return new AutomaticLineEnderCommandHandler(undoRegistry, editorOperations, asyncCompletionBroker); } } } diff --git a/src/EditorFeatures/Core/Implementation/AutomaticCompletion/AbstractAutomaticLineEnderCommandHandler.cs b/src/EditorFeatures/Core/Implementation/AutomaticCompletion/AbstractAutomaticLineEnderCommandHandler.cs index 2575642018952..a721757be2839 100644 --- a/src/EditorFeatures/Core/Implementation/AutomaticCompletion/AbstractAutomaticLineEnderCommandHandler.cs +++ b/src/EditorFeatures/Core/Implementation/AutomaticCompletion/AbstractAutomaticLineEnderCommandHandler.cs @@ -8,6 +8,8 @@ using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.Commanding; +using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; +using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion.Data; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor.Commanding.Commands; using Microsoft.VisualStudio.Text.Operations; @@ -20,15 +22,18 @@ internal abstract class AbstractAutomaticLineEnderCommandHandler : { private readonly ITextUndoHistoryRegistry _undoRegistry; private readonly IEditorOperationsFactoryService _editorOperationsFactoryService; + private readonly IAsyncCompletionBroker _asyncCompletionBroker; public string DisplayName => EditorFeaturesResources.Automatic_Line_Ender; public AbstractAutomaticLineEnderCommandHandler( ITextUndoHistoryRegistry undoRegistry, - IEditorOperationsFactoryService editorOperationsFactoryService) + IEditorOperationsFactoryService editorOperationsFactoryService, + IAsyncCompletionBroker asyncCompletionBroker) { _undoRegistry = undoRegistry; _editorOperationsFactoryService = editorOperationsFactoryService; + _asyncCompletionBroker = asyncCompletionBroker; } /// @@ -58,6 +63,22 @@ public VSCommanding.CommandState GetCommandState(AutomaticLineEnderCommandArgs a public void ExecuteCommand(AutomaticLineEnderCommandArgs args, Action nextHandler, CommandExecutionContext context) { + // Completion will only be active here if this command wasn't handled by the completion controller itself. + if (_asyncCompletionBroker.IsCompletionActive(args.TextView)) + { + var session = _asyncCompletionBroker.GetSession(args.TextView); + var computedItems = session.GetComputedItems(context.OperationContext.UserCancellationToken); + var softSelection = computedItems.SuggestionItemSelected || computedItems.UsesSoftSelection; + var behavior = session.Commit('\n', context.OperationContext.UserCancellationToken); + session.Dismiss(); + + if (behavior != CommitBehavior.CancelCommit && !softSelection) + { + // Skip the automatic line handling in this case for behavior parity with legacy completion. + return; + } + } + // get editor operation var operations = _editorOperationsFactoryService.GetEditorOperations(args.TextView); if (operations == null) diff --git a/src/EditorFeatures/TestUtilities/AutomaticCompletion/AbstractAutomaticLineEnderTests.cs b/src/EditorFeatures/TestUtilities/AutomaticCompletion/AbstractAutomaticLineEnderTests.cs index cd6e4c2689e73..8cc54f056952e 100644 --- a/src/EditorFeatures/TestUtilities/AutomaticCompletion/AbstractAutomaticLineEnderTests.cs +++ b/src/EditorFeatures/TestUtilities/AutomaticCompletion/AbstractAutomaticLineEnderTests.cs @@ -1,25 +1,17 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Linq; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Editor.Host; using Microsoft.CodeAnalysis.Editor.UnitTests.Utilities; using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces; -using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Test.Utilities; -using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.Commanding; -using Microsoft.VisualStudio.Language.Intellisense; +using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Text.Editor.Commanding.Commands; using Microsoft.VisualStudio.Text.Operations; -using Moq; using Roslyn.Test.Utilities; -using Roslyn.Utilities; using Xunit; namespace Microsoft.CodeAnalysis.Editor.UnitTests.AutomaticCompletion @@ -32,7 +24,8 @@ public abstract class AbstractAutomaticLineEnderTests internal abstract IChainedCommandHandler CreateCommandHandler( ITextUndoHistoryRegistry undoRegistry, - IEditorOperationsFactoryService editorOperations); + IEditorOperationsFactoryService editorOperations, + IAsyncCompletionBroker asyncCompletionBroker); protected void Test(string expected, string code, bool completionActive = false, bool assertNextHandlerInvoked = false) { @@ -46,7 +39,8 @@ protected void Test(string expected, string code, bool completionActive = false, var commandHandler = CreateCommandHandler( GetExportedValue(workspace), - GetExportedValue(workspace)); + GetExportedValue(workspace), + GetExportedValue(workspace)); commandHandler.ExecuteCommand(new AutomaticLineEnderCommandArgs(view, buffer), assertNextHandlerInvoked diff --git a/src/EditorFeatures/VisualBasic/AutomaticCompletion/AutomaticLineEnderCommandHandler.vb b/src/EditorFeatures/VisualBasic/AutomaticCompletion/AutomaticLineEnderCommandHandler.vb index fcc7b8a9a596a..363ff7a402aa7 100644 --- a/src/EditorFeatures/VisualBasic/AutomaticCompletion/AutomaticLineEnderCommandHandler.vb +++ b/src/EditorFeatures/VisualBasic/AutomaticCompletion/AutomaticLineEnderCommandHandler.vb @@ -24,9 +24,10 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.AutomaticCompletion Friend Sub New(undoRegistry As ITextUndoHistoryRegistry, - editorOperations As IEditorOperationsFactoryService) + editorOperations As IEditorOperationsFactoryService, + asyncCompletionBroker As IAsyncCompletionBroker) - MyBase.New(undoRegistry, editorOperations) + MyBase.New(undoRegistry, editorOperations, asyncCompletionBroker) End Sub Protected Overrides Sub NextAction(editorOperation As IEditorOperations, nextAction As Action) diff --git a/src/EditorFeatures/VisualBasicTest/AutomaticCompletion/AutomaticLineEnderTests.vb b/src/EditorFeatures/VisualBasicTest/AutomaticCompletion/AutomaticLineEnderTests.vb index fb59e33730630..3f1c97e2f7e30 100644 --- a/src/EditorFeatures/VisualBasicTest/AutomaticCompletion/AutomaticLineEnderTests.vb +++ b/src/EditorFeatures/VisualBasicTest/AutomaticCompletion/AutomaticLineEnderTests.vb @@ -7,6 +7,7 @@ Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces Imports Microsoft.CodeAnalysis.Editor.VisualBasic.AutomaticCompletion Imports Microsoft.CodeAnalysis.Editor.VisualBasic.EndConstructGeneration Imports Microsoft.VisualStudio.Commanding +Imports Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion Imports Microsoft.VisualStudio.Text.Editor.Commanding.Commands Imports Microsoft.VisualStudio.Text.Operations @@ -245,10 +246,11 @@ End Module Friend Overrides Function CreateCommandHandler( undoRegistry As ITextUndoHistoryRegistry, - editorOperations As IEditorOperationsFactoryService + editorOperations As IEditorOperationsFactoryService, + asyncCompletionBroker As IAsyncCompletionBroker ) As IChainedCommandHandler(Of AutomaticLineEnderCommandArgs) - Return New AutomaticLineEnderCommandHandler(undoRegistry, editorOperations) + Return New AutomaticLineEnderCommandHandler(undoRegistry, editorOperations, asyncCompletionBroker) End Function Protected Overrides Function CreateNextHandler(workspace As TestWorkspace) As Action diff --git a/src/VisualStudio/IntegrationTest/IntegrationTests/CSharp/CSharpIntelliSense.cs b/src/VisualStudio/IntegrationTest/IntegrationTests/CSharp/CSharpIntelliSense.cs index a7ab157fe67d3..0f9e7552b8641 100644 --- a/src/VisualStudio/IntegrationTest/IntegrationTests/CSharp/CSharpIntelliSense.cs +++ b/src/VisualStudio/IntegrationTest/IntegrationTests/CSharp/CSharpIntelliSense.cs @@ -250,8 +250,7 @@ void Main(string[] args) assertCaretPosition: true); } - // 🐛 This should work with async completion, but currently does not. - [ConditionalWpfFact(typeof(LegacyCompletionCondition)), Trait(Traits.Feature, Traits.Features.Completion)] + [WpfFact, Trait(Traits.Feature, Traits.Features.Completion)] [WorkItem(33823, "https://github.com/dotnet/roslyn/issues/33823")] public void CommitOnShiftEnter() {