Skip to content

Commit

Permalink
Merge pull request dotnet#54965 from CyrusNajmabadi/fileScopedNamespa…
Browse files Browse the repository at this point in the history
…cesSimplifyTypeNames

Fix 'simplify type names' with file scoped namespaces
  • Loading branch information
CyrusNajmabadi authored Jul 20, 2021
2 parents 8acbcc2 + b1b296c commit 21d77e7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ class A
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyTypeNames)]
public async Task UseAlias00_FileScopedNamespace()
{
await TestInRegularAndScriptAsync(
@"namespace Root;
using MyType = System.IO.File;
class A
{
[|System.IO.File|] c;
}
",
@"namespace Root;
using MyType = System.IO.File;
class A
{
MyType c;
}
");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyTypeNames)]
public async Task UseAlias()
{
Expand Down Expand Up @@ -754,6 +778,40 @@ class A
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyTypeNames)]
public async Task SimplifyTypeName1_FileScopedNamespace()
{
var source =
@"using System;
namespace Root;
class A
{
[|System.Exception|] c;
}";

await TestInRegularAndScriptAsync(source,
@"using System;
namespace Root;
class A
{
Exception c;
}");
await TestActionCountAsync(source, 1);
await TestSpansAsync(
@"using System;
namespace Root;
class A
{
[|System|].Exception c;
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyTypeNames)]
public async Task SimplifyTypeName2()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
Expand Down Expand Up @@ -92,13 +91,11 @@ private static ImmutableHashSet<string> GetAliasedNames(CompilationUnitSyntax? c
return aliasedNames;

foreach (var usingDirective in compilationUnit.Usings)
{
AddAliasedName(usingDirective);
}

foreach (var member in compilationUnit.Members)
{
if (member is NamespaceDeclarationSyntax namespaceDeclaration)
if (member is BaseNamespaceDeclarationSyntax namespaceDeclaration)
AddAliasedNames(namespaceDeclaration);
}

Expand All @@ -119,16 +116,14 @@ void AddAliasedName(UsingDirectiveSyntax usingDirective)
}
}

void AddAliasedNames(NamespaceDeclarationSyntax namespaceDeclaration)
void AddAliasedNames(BaseNamespaceDeclarationSyntax namespaceDeclaration)
{
foreach (var usingDirective in namespaceDeclaration.Usings)
{
AddAliasedName(usingDirective);
}

foreach (var member in namespaceDeclaration.Members)
{
if (member is NamespaceDeclarationSyntax memberNamespace)
if (member is BaseNamespaceDeclarationSyntax memberNamespace)
AddAliasedNames(memberNamespace);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,37 +196,23 @@ private static bool IsAliasReplaceableExpression(ExpressionSyntax expression)

private static bool HasUsingAliasDirective(SyntaxNode syntax)
{
SyntaxList<UsingDirectiveSyntax> usings;
SyntaxList<MemberDeclarationSyntax> members;
if (syntax.IsKind(SyntaxKind.NamespaceDeclaration, out NamespaceDeclarationSyntax namespaceDeclaration))
var (usings, members) = syntax switch
{
usings = namespaceDeclaration.Usings;
members = namespaceDeclaration.Members;
}
else if (syntax.IsKind(SyntaxKind.CompilationUnit, out CompilationUnitSyntax compilationUnit))
{
usings = compilationUnit.Usings;
members = compilationUnit.Members;
}
else
{
return false;
}
BaseNamespaceDeclarationSyntax ns => (ns.Usings, ns.Members),
CompilationUnitSyntax compilationUnit => (compilationUnit.Usings, compilationUnit.Members),
_ => default,
};

foreach (var usingDirective in usings)
{
if (usingDirective.Alias != null)
{
return true;
}
}

foreach (var member in members)
{
if (HasUsingAliasDirective(member))
{
return true;
}
}

return false;
Expand Down Expand Up @@ -317,7 +303,7 @@ private static int GetNamespaceIdForAliasSearch(SemanticModel semanticModel, Syn
}

// check whether I am under a namespace
var @namespace = startNode.GetAncestorOrThis<NamespaceDeclarationSyntax>();
var @namespace = startNode.GetAncestorOrThis<BaseNamespaceDeclarationSyntax>();
if (@namespace != null)
{
// since we have node inside of the root, root should be already there
Expand Down Expand Up @@ -345,34 +331,28 @@ private static SyntaxNode GetStartNodeForNamespaceId(SemanticModel semanticModel
return token.Parent;
}

private static int GetNamespaceId(SyntaxNode container, NamespaceDeclarationSyntax target, ref int index)
private static int GetNamespaceId(SyntaxNode container, BaseNamespaceDeclarationSyntax target, ref int index)
=> container switch
{
CompilationUnitSyntax compilation => GetNamespaceId(compilation.Members, target, ref index),
NamespaceDeclarationSyntax @namespace => GetNamespaceId(@namespace.Members, target, ref index),
BaseNamespaceDeclarationSyntax @namespace => GetNamespaceId(@namespace.Members, target, ref index),
_ => throw ExceptionUtilities.UnexpectedValue(container)
};

private static int GetNamespaceId(SyntaxList<MemberDeclarationSyntax> members, NamespaceDeclarationSyntax target, ref int index)
private static int GetNamespaceId(SyntaxList<MemberDeclarationSyntax> members, BaseNamespaceDeclarationSyntax target, ref int index)
{
foreach (var member in members)
{
if (!(member is NamespaceDeclarationSyntax childNamespace))
{
if (member is not BaseNamespaceDeclarationSyntax childNamespace)
continue;
}

if (childNamespace == target)
{
return index;
}

index++;
var result = GetNamespaceId(childNamespace, target, ref index);
if (result > 0)
{
return result;
}
}

return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ private static bool IsPartOfNamespaceDeclarationName(SyntaxNode node)
break;

case SyntaxKind.NamespaceDeclaration:
var namespaceDeclaration = (NamespaceDeclarationSyntax)parent;
case SyntaxKind.FileScopedNamespaceDeclaration:
var namespaceDeclaration = (BaseNamespaceDeclarationSyntax)parent;
return object.Equals(namespaceDeclaration.Name, node);

default:
Expand Down

0 comments on commit 21d77e7

Please sign in to comment.