Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives;
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.MoveMisplacedUsingDirectives), Shared]
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed partial class MisplacedUsingDirectivesCodeFixProvider() : CodeFixProvider
internal sealed class MisplacedUsingDirectivesCodeFixProvider() : CodeFixProvider
{
private static readonly SyntaxAnnotation s_usingPlacementCodeFixAnnotation = new(nameof(s_usingPlacementCodeFixAnnotation));

Expand Down Expand Up @@ -73,10 +73,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

foreach (var diagnostic in context.Diagnostics)
{
context.RegisterCodeFix(
CodeAction.Create(
context.RegisterCodeFix(CodeAction.Create(
CSharpAnalyzersResources.Move_misplaced_using_directives,
token => GetTransformedDocumentAsync(document, compilationUnit, GetAllUsingDirectives(compilationUnit), placement, simplifierOptions, token),
cancellationToken => GetTransformedDocumentAsync(document, compilationUnit, placement, simplifierOptions, cancellationToken),
nameof(CSharpAnalyzersResources.Move_misplaced_using_directives)),
diagnostic);
}
Expand All @@ -101,7 +100,7 @@ internal static async Task<Document> TransformDocumentIfRequiredAsync(
return document;

return await GetTransformedDocumentAsync(
document, compilationUnit, allUsingDirectives, placement, simplifierOptions, cancellationToken).ConfigureAwait(false);
document, compilationUnit, placement, simplifierOptions, cancellationToken).ConfigureAwait(false);
}

private static ImmutableArray<UsingDirectiveSyntax> GetAllUsingDirectives(CompilationUnitSyntax compilationUnit)
Expand All @@ -123,7 +122,7 @@ void Recurse(SyntaxList<MemberDeclarationSyntax> members)
{
foreach (var member in members)
{
if (member is NamespaceDeclarationSyntax namespaceDeclaration)
if (member is BaseNamespaceDeclarationSyntax namespaceDeclaration)
{
result.AddRange(namespaceDeclaration.Usings);
Recurse(namespaceDeclaration.Members);
Expand All @@ -135,13 +134,14 @@ void Recurse(SyntaxList<MemberDeclarationSyntax> members)
private static async Task<Document> GetTransformedDocumentAsync(
Document document,
CompilationUnitSyntax compilationUnit,
ImmutableArray<UsingDirectiveSyntax> allUsingDirectives,
AddImportPlacement placement,
SimplifierOptions simplifierOptions,
CancellationToken cancellationToken)
{
var bannerService = document.GetRequiredLanguageService<IFileBannerFactsService>();

var allUsingDirectives = GetAllUsingDirectives(compilationUnit);

// Expand usings so that they can be properly simplified after they are relocated.
var compilationUnitWithExpandedUsings = await ExpandUsingDirectivesAsync(
document, compilationUnit, allUsingDirectives, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public enum TestEnum
private const string DelegateDefinition = @"public delegate void TestDelegate();";

private TestParameters GetTestParameters(CodeStyleOption2<AddImportPlacement> preferredPlacementOption)
=> new(options: new OptionsCollection(GetLanguage()) { { CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, preferredPlacementOption } });
=> new(options: new(GetLanguage()) { { CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, preferredPlacementOption } });

private Task TestDiagnosticMissingAsync(
[StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] string initialMarkup,
Expand Down Expand Up @@ -657,6 +657,44 @@ namespace N1
""", OutsideNamespaceOption, placeSystemNamespaceFirst: true);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75961")]
public Task WhenOutsidePreferred_AliasToLocalType_FileScopedNamespace()
{
return TestInRegularAndScriptAsync("""
namespace Goo;

[|using Alias = C;|]

class C;
""", """

{|Warning:using Alias = Goo.C;|}

namespace Goo;
class C;
""", OutsideNamespaceOption, placeSystemNamespaceFirst: true);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75961")]
public Task WhenOutsidePreferred_AliasToLocalType_BlockNamespace()
{
return TestInRegularAndScriptAsync("""
namespace Goo
{
[|using Alias = C;|]

class C;
}
""", """
{|Warning:using Alias = Goo.C;|}

namespace Goo
{
class C;
}
""", OutsideNamespaceOption, placeSystemNamespaceFirst: true);
}

#endregion

#region OutsideNamespaceIgnoringAliases
Expand Down
Loading