From 3ed3e4dd8b5454535972f69be60e06030decd263 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 4 May 2020 08:01:45 -0700 Subject: [PATCH] Avoid constructing AutomaticLineEnderCommandHandler outside the MEF catalog --- .../AutomaticLineEnderCommandHandler.cs | 4 ++-- .../AutomaticCompletion/AutomaticLineEnderTests.cs | 10 +++++----- .../AbstractAutomaticLineEnderTests.cs | 9 ++------- .../TestUtilities/Workspaces/TestWorkspace.cs | 6 ++++++ .../AutomaticLineEnderCommandHandler.vb | 4 ++-- .../AutomaticCompletion/AutomaticLineEnderTests.vb | 11 +++++------ 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/EditorFeatures/CSharp/AutomaticCompletion/AutomaticLineEnderCommandHandler.cs b/src/EditorFeatures/CSharp/AutomaticCompletion/AutomaticLineEnderCommandHandler.cs index 526d530b2ef..0539ee7e127 100644 --- a/src/EditorFeatures/CSharp/AutomaticCompletion/AutomaticLineEnderCommandHandler.cs +++ b/src/EditorFeatures/CSharp/AutomaticCompletion/AutomaticLineEnderCommandHandler.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.ComponentModel.Composition; -using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading; using Microsoft.CodeAnalysis.CSharp; @@ -14,6 +13,7 @@ using Microsoft.CodeAnalysis.CSharp.Utilities; using Microsoft.CodeAnalysis.Editor.Implementation.AutomaticCompletion; using Microsoft.CodeAnalysis.Formatting; +using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.Commanding; @@ -34,7 +34,7 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.AutomaticCompletion internal class AutomaticLineEnderCommandHandler : AbstractAutomaticLineEnderCommandHandler { [ImportingConstructor] - [SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")] + [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] public AutomaticLineEnderCommandHandler( ITextUndoHistoryRegistry undoRegistry, IEditorOperationsFactoryService editorOperations) diff --git a/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticLineEnderTests.cs b/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticLineEnderTests.cs index 6ec6e16bc93..b6143587197 100644 --- a/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticLineEnderTests.cs +++ b/src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticLineEnderTests.cs @@ -9,7 +9,6 @@ using Microsoft.CodeAnalysis.Test.Utilities; using Microsoft.VisualStudio.Commanding; using Microsoft.VisualStudio.Text.Editor.Commanding.Commands; -using Microsoft.VisualStudio.Text.Operations; using Roslyn.Test.Utilities; using Xunit; @@ -846,11 +845,12 @@ protected override TestWorkspace CreateWorkspace(string code) protected override Action CreateNextHandler(TestWorkspace workspace) => () => { }; - internal override IChainedCommandHandler CreateCommandHandler( - ITextUndoHistoryRegistry undoRegistry, - IEditorOperationsFactoryService editorOperations) + internal override IChainedCommandHandler GetCommandHandler(TestWorkspace workspace) { - return new AutomaticLineEnderCommandHandler(undoRegistry, editorOperations); + return Assert.IsType( + workspace.GetService( + ContentTypeNames.CSharpContentType, + PredefinedCommandHandlerNames.AutomaticLineEnder)); } } } diff --git a/src/EditorFeatures/TestUtilities/AutomaticCompletion/AbstractAutomaticLineEnderTests.cs b/src/EditorFeatures/TestUtilities/AutomaticCompletion/AbstractAutomaticLineEnderTests.cs index a9424fbb36a..50a94010cfe 100644 --- a/src/EditorFeatures/TestUtilities/AutomaticCompletion/AbstractAutomaticLineEnderTests.cs +++ b/src/EditorFeatures/TestUtilities/AutomaticCompletion/AbstractAutomaticLineEnderTests.cs @@ -11,7 +11,6 @@ using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Text.Editor.Commanding.Commands; -using Microsoft.VisualStudio.Text.Operations; using Roslyn.Test.Utilities; using Xunit; @@ -23,9 +22,7 @@ public abstract class AbstractAutomaticLineEnderTests protected abstract TestWorkspace CreateWorkspace(string code); protected abstract Action CreateNextHandler(TestWorkspace workspace); - internal abstract IChainedCommandHandler CreateCommandHandler( - ITextUndoHistoryRegistry undoRegistry, - IEditorOperationsFactoryService editorOperations); + internal abstract IChainedCommandHandler GetCommandHandler(TestWorkspace workspace); protected void Test(string expected, string code, bool completionActive = false, bool assertNextHandlerInvoked = false) { @@ -37,9 +34,7 @@ protected void Test(string expected, string code, bool completionActive = false, view.Caret.MoveTo(new SnapshotPoint(buffer.CurrentSnapshot, workspace.Documents.Single(d => d.CursorPosition.HasValue).CursorPosition.Value)); - var commandHandler = CreateCommandHandler( - GetExportedValue(workspace), - GetExportedValue(workspace)); + var commandHandler = GetCommandHandler(workspace); commandHandler.ExecuteCommand(new AutomaticLineEnderCommandArgs(view, buffer), assertNextHandlerInvoked diff --git a/src/EditorFeatures/TestUtilities/Workspaces/TestWorkspace.cs b/src/EditorFeatures/TestUtilities/Workspaces/TestWorkspace.cs index c439ef62f28..b64cba4842d 100644 --- a/src/EditorFeatures/TestUtilities/Workspaces/TestWorkspace.cs +++ b/src/EditorFeatures/TestUtilities/Workspaces/TestWorkspace.cs @@ -258,6 +258,12 @@ public TServiceInterface GetService(string contentType) return values.Single(value => value.Metadata.ContentTypes.Contains(contentType)).Value; } + public TServiceInterface GetService(string contentType, string name) + { + var values = ExportProvider.GetExports(); + return values.Single(value => value.Metadata.Name == name && value.Metadata.ContentTypes.Contains(contentType)).Value; + } + public override bool CanApplyChange(ApplyChangesKind feature) { switch (feature) diff --git a/src/EditorFeatures/VisualBasic/AutomaticCompletion/AutomaticLineEnderCommandHandler.vb b/src/EditorFeatures/VisualBasic/AutomaticCompletion/AutomaticLineEnderCommandHandler.vb index 8b649fdbb5f..63b00dd245d 100644 --- a/src/EditorFeatures/VisualBasic/AutomaticCompletion/AutomaticLineEnderCommandHandler.vb +++ b/src/EditorFeatures/VisualBasic/AutomaticCompletion/AutomaticLineEnderCommandHandler.vb @@ -3,10 +3,10 @@ ' See the LICENSE file in the project root for more information. Imports System.ComponentModel.Composition -Imports System.Diagnostics.CodeAnalysis Imports System.Threading Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.Editor.Implementation.AutomaticCompletion +Imports Microsoft.CodeAnalysis.Host.Mef Imports Microsoft.CodeAnalysis.Text Imports Microsoft.VisualStudio.Commanding Imports Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion @@ -25,7 +25,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.AutomaticCompletion Inherits AbstractAutomaticLineEnderCommandHandler - + Public Sub New(undoRegistry As ITextUndoHistoryRegistry, editorOperations As IEditorOperationsFactoryService) diff --git a/src/EditorFeatures/VisualBasicTest/AutomaticCompletion/AutomaticLineEnderTests.vb b/src/EditorFeatures/VisualBasicTest/AutomaticCompletion/AutomaticLineEnderTests.vb index b30ff2463c9..5b6f3357282 100644 --- a/src/EditorFeatures/VisualBasicTest/AutomaticCompletion/AutomaticLineEnderTests.vb +++ b/src/EditorFeatures/VisualBasicTest/AutomaticCompletion/AutomaticLineEnderTests.vb @@ -9,7 +9,6 @@ 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 @@ -266,12 +265,12 @@ End Module Test(expected.NormalizedValue(), code.NormalizedValue()) End Sub - Friend Overrides Function CreateCommandHandler( - undoRegistry As ITextUndoHistoryRegistry, - editorOperations As IEditorOperationsFactoryService - ) As IChainedCommandHandler(Of AutomaticLineEnderCommandArgs) + Friend Overrides Function GetCommandHandler(workspace As TestWorkspace) As IChainedCommandHandler(Of AutomaticLineEnderCommandArgs) - Return New AutomaticLineEnderCommandHandler(undoRegistry, editorOperations) + Return Assert.IsType(Of AutomaticLineEnderCommandHandler)( + workspace.GetService(Of ICommandHandler)( + ContentTypeNames.VisualBasicContentType, + PredefinedCommandHandlerNames.AutomaticLineEnder)) End Function Protected Overrides Function CreateNextHandler(workspace As TestWorkspace) As Action -- GitLab