提交 17a9ee46 编写于 作者: M Manish Vasani

Port MisplacedUsingDirectives to shared layer

上级 13532741
......@@ -24,6 +24,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ConvertSwitchStatementToExpression\ConvertSwitchStatementToExpressionDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FileHeaders\CSharpFileHeaderDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FileHeaders\CSharpFileHeaderHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MisplacedUsingDirectives\MisplacedUsingDirectivesDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NamingStyle\CSharpNamingStyleDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)OrderModifiers\CSharpOrderModifiersDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)OrderModifiers\CSharpOrderModifiersHelper.cs" />
......
......@@ -19,16 +19,16 @@ namespace Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives
internal sealed class MisplacedUsingDirectivesDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
{
private static readonly LocalizableResourceString s_localizableTitle = new LocalizableResourceString(
nameof(CSharpFeaturesResources.Misplaced_using_directive), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources));
nameof(CSharpAnalyzersResources.Misplaced_using_directive), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources));
private static readonly LocalizableResourceString s_localizableOutsideMessage = new LocalizableResourceString(
nameof(CSharpFeaturesResources.Using_directives_must_be_placed_outside_of_a_namespace_declaration), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources));
nameof(CSharpAnalyzersResources.Using_directives_must_be_placed_outside_of_a_namespace_declaration), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources));
private static readonly DiagnosticDescriptor s_outsideDiagnosticDescriptor = CreateDescriptorWithId(
IDEDiagnosticIds.MoveMisplacedUsingDirectivesDiagnosticId, s_localizableTitle, s_localizableOutsideMessage);
private static readonly LocalizableResourceString s_localizableInsideMessage = new LocalizableResourceString(
nameof(CSharpFeaturesResources.Using_directives_must_be_placed_inside_of_a_namespace_declaration), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources));
nameof(CSharpAnalyzersResources.Using_directives_must_be_placed_inside_of_a_namespace_declaration), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources));
private static readonly DiagnosticDescriptor s_insideDiagnosticDescriptor = CreateDescriptorWithId(
IDEDiagnosticIds.MoveMisplacedUsingDirectivesDiagnosticId, s_localizableTitle, s_localizableInsideMessage);
......
......@@ -20,6 +20,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ConvertSwitchStatementToExpression\ConvertSwitchStatementToExpressionCodeFixProvider.Rewriter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FileHeaders\CSharpFileHeaderCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeFieldReadonly\CSharpMakeFieldReadonlyCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MisplacedUsingDirectives\MisplacedUsingDirectivesCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)OrderModifiers\CSharpOrderModifiersCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PopulateSwitch\CSharpPopulateSwitchExpressionCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PopulateSwitch\CSharpPopulateSwitchStatementCodeFixProvider.cs" />
......
......@@ -20,11 +20,15 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;
using Roslyn.Utilities;
using static Microsoft.CodeAnalysis.CodeActions.CodeAction;
#if CODE_STYLE
using OptionSet = Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions;
#else
using Microsoft.CodeAnalysis.Options;
#endif
namespace Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives
{
......@@ -58,7 +62,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var compilationUnit = (CompilationUnitSyntax)syntaxRoot;
#if CODE_STYLE
var options = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(syntaxRoot.SyntaxTree, cancellationToken);
#else
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
#endif
// Read the preferred placement option and verify if it can be applied to this code file.
// There are cases where we will not be able to fix the diagnostic and the user will need to resolve
......@@ -72,7 +81,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
foreach (var diagnostic in context.Diagnostics)
{
context.RegisterCodeFix(
new MoveMisplacedUsingsCodeAction(token => GetTransformedDocumentAsync(document, compilationUnit, options, placement, token)),
new MoveMisplacedUsingsCodeAction(token => GetTransformedDocumentAsync(document, compilationUnit, placement, token)),
diagnostic);
}
}
......@@ -80,7 +89,6 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task<Document> GetTransformedDocumentAsync(
Document document,
CompilationUnitSyntax compilationUnit,
OptionSet options,
AddImportPlacement placement,
CancellationToken cancellationToken)
{
......@@ -93,7 +101,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var (compilationUnitWithoutHeader, fileHeader) = RemoveFileHeader(compilationUnitWithExpandedUsings, syntaxFactsService);
// A blanket warning that this codefix may change code so that it does not compile.
var warningAnnotation = WarningAnnotation.Create(CSharpFeaturesResources.Warning_colon_Moving_using_directives_may_change_code_meaning);
var warningAnnotation = WarningAnnotation.Create(CSharpAnalyzersResources.Warning_colon_Moving_using_directives_may_change_code_meaning);
var newCompilationUnit = placement == AddImportPlacement.InsideNamespace
? MoveUsingsInsideNamespace(compilationUnitWithoutHeader, warningAnnotation)
......@@ -104,6 +112,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var newDocument = document.WithSyntaxRoot(newCompilationUnitWithHeader);
// Simplify usings now that they have been moved and are in the proper context.
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
return await Simplifier.ReduceAsync(newDocument, Simplifier.Annotation, options, cancellationToken).ConfigureAwait(false);
}
......@@ -389,10 +398,10 @@ private static CompilationUnitSyntax AddFileHeader(CompilationUnitSyntax compila
return compilationUnit.ReplaceToken(firstToken, newFirstToken);
}
private class MoveMisplacedUsingsCodeAction : DocumentChangeAction
private class MoveMisplacedUsingsCodeAction : CustomCodeActions.DocumentChangeAction
{
public MoveMisplacedUsingsCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument)
: base(CSharpFeaturesResources.Move_misplaced_using_directives, createChangedDocument)
: base(CSharpAnalyzersResources.Move_misplaced_using_directives, createChangedDocument)
{
}
}
......
......@@ -15,6 +15,7 @@
<Compile Include="$(MSBuildThisFileDirectory)AddRequiredParentheses\AddRequiredParenthesesTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ConvertAnonymousTypeToTuple\ConvertAnonymousTypeToTupleTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeFieldReadonly\MakeFieldReadonlyTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MisplacedUsingDirectives\MisplacedUsingDirectivesCodeFixProviderTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)OrderModifiers\OrderModifiersCompilerErrorTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)OrderModifiers\OrderModifiersTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnreachableCode\RemoveUnreachableCodeTests.cs" />
......
......@@ -17,11 +17,11 @@
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.MisplacedUsingDirectives
{
/// <summary>
/// Unit tests for the <see cref="MisplacedUsingDirectivesDiagnosticAnalyzer"/> and <see cref="MisplacedUsingDirectivesCodeFixProvider"/>.
/// </summary>
public class MisplacedUsingDirectivesInCompilationUnitCodeFixProviderTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
{
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new MisplacedUsingDirectivesDiagnosticAnalyzer(), new MisplacedUsingDirectivesCodeFixProvider());
internal static readonly CodeStyleOption2<AddImportPlacement> OutsidePreferPreservationOption =
new CodeStyleOption2<AddImportPlacement>(AddImportPlacement.OutsideNamespace, NotificationOption2.None);
......@@ -54,17 +54,14 @@ public class MisplacedUsingDirectivesInCompilationUnitCodeFixProviderTests : Abs
protected const string DelegateDefinition = @"public delegate void TestDelegate();";
private static TestParameters GetTestParameters(CodeStyleOption2<AddImportPlacement> preferredPlacementOption)
=> new TestParameters(options: OptionsSet((new OptionKey2(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement), preferredPlacementOption)));
private protected Task TestDiagnosticMissingAsync(string initialMarkup, CodeStyleOption2<AddImportPlacement> preferredPlacementOption)
{
var options = OptionsSet(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, preferredPlacementOption);
return TestDiagnosticMissingAsync(initialMarkup, new TestParameters(options: options));
}
=> TestDiagnosticMissingAsync(initialMarkup, GetTestParameters(preferredPlacementOption));
private protected Task TestMissingAsync(string initialMarkup, CodeStyleOption2<AddImportPlacement> preferredPlacementOption)
{
var options = OptionsSet(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, preferredPlacementOption);
return TestMissingAsync(initialMarkup, new TestParameters(options: options));
}
=> TestMissingAsync(initialMarkup, GetTestParameters(preferredPlacementOption));
private protected Task TestInRegularAndScriptAsync(string initialMarkup, string expectedMarkup, CodeStyleOption2<AddImportPlacement> preferredPlacementOption, bool placeSystemNamespaceFirst)
{
......@@ -76,9 +73,6 @@ private protected Task TestInRegularAndScriptAsync(string initialMarkup, string
return TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: options);
}
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new MisplacedUsingDirectivesDiagnosticAnalyzer(), new MisplacedUsingDirectivesCodeFixProvider());
#region Test Preserve
/// <summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册