提交 3ed3e4dd 编写于 作者: S Sam Harwell

Avoid constructing AutomaticLineEnderCommandHandler outside the MEF catalog

上级 09968ff5
......@@ -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)
......
......@@ -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<AutomaticLineEnderCommandArgs> CreateCommandHandler(
ITextUndoHistoryRegistry undoRegistry,
IEditorOperationsFactoryService editorOperations)
internal override IChainedCommandHandler<AutomaticLineEnderCommandArgs> GetCommandHandler(TestWorkspace workspace)
{
return new AutomaticLineEnderCommandHandler(undoRegistry, editorOperations);
return Assert.IsType<AutomaticLineEnderCommandHandler>(
workspace.GetService<ICommandHandler>(
ContentTypeNames.CSharpContentType,
PredefinedCommandHandlerNames.AutomaticLineEnder));
}
}
}
......@@ -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<AutomaticLineEnderCommandArgs> CreateCommandHandler(
ITextUndoHistoryRegistry undoRegistry,
IEditorOperationsFactoryService editorOperations);
internal abstract IChainedCommandHandler<AutomaticLineEnderCommandArgs> 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<ITextUndoHistoryRegistry>(workspace),
GetExportedValue<IEditorOperationsFactoryService>(workspace));
var commandHandler = GetCommandHandler(workspace);
commandHandler.ExecuteCommand(new AutomaticLineEnderCommandArgs(view, buffer),
assertNextHandlerInvoked
......
......@@ -258,6 +258,12 @@ public TServiceInterface GetService<TServiceInterface>(string contentType)
return values.Single(value => value.Metadata.ContentTypes.Contains(contentType)).Value;
}
public TServiceInterface GetService<TServiceInterface>(string contentType, string name)
{
var values = ExportProvider.GetExports<TServiceInterface, OrderableContentTypeMetadata>();
return values.Single(value => value.Metadata.Name == name && value.Metadata.ContentTypes.Contains(contentType)).Value;
}
public override bool CanApplyChange(ApplyChangesKind feature)
{
switch (feature)
......
......@@ -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
<ImportingConstructor>
<SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification:="Used in test code: https://github.com/dotnet/roslyn/issues/42814")>
<Obsolete(MefConstruction.ImportingConstructorMessage, True)>
Public Sub New(undoRegistry As ITextUndoHistoryRegistry,
editorOperations As IEditorOperationsFactoryService)
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册