提交 0dbf0b99 编写于 作者: G Gen Lu

Address review comments

上级 b5d8348b
......@@ -1162,6 +1162,50 @@ public Task MoveToNamespace_MoveOneTypeInGlobalNamespace()
class MyClass2
{
}",
expectedSuccess: false);
[WpfFact, Trait(Traits.Feature, Traits.Features.MoveToNamespace)]
[WorkItem(980758, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/980758")]
public Task MoveToNamespace_PartialTypesInNamesapce_SelectType()
=> TestMoveToNamespaceAsync(
@"namespace NS
{
partial class MyClass[||]
{
}
partial class MyClass
{
}
}",
expectedSuccess: false);
[WpfFact, Trait(Traits.Feature, Traits.Features.MoveToNamespace)]
[WorkItem(980758, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/980758")]
public Task MoveToNamespace_PartialTypesInNamesapce_SelectNamespace()
=> TestMoveToNamespaceAsync(
@"namespace NS[||]
{
partial class MyClass
{
}
partial class MyClass
{
}
}",
expectedSuccess: false);
[WpfFact, Trait(Traits.Feature, Traits.Features.MoveToNamespace)]
[WorkItem(980758, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/980758")]
public Task MoveToNamespace_PartialTypesInGlobalNamesapce()
=> TestMoveToNamespaceAsync(
@"partial class MyClass[||]
{
}
partial class MyClass
{
}",
expectedSuccess: false);
}
......
......@@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.MoveToNamespace;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.MoveToNamespace
{
......@@ -20,19 +21,13 @@ internal class CSharpMoveToNamespaceService :
{
}
protected override string GetNamespaceName(NamespaceDeclarationSyntax namespaceSyntax)
=> namespaceSyntax.Name.ToString();
protected override string GetNamespaceName(TypeDeclarationSyntax typeDeclarationSyntax)
{
var namespaceDecl = typeDeclarationSyntax.FirstAncestorOrSelf<NamespaceDeclarationSyntax>();
if (namespaceDecl == null)
protected override string GetNamespaceName(SyntaxNode container)
=> container switch
{
return string.Empty;
}
return GetNamespaceName(namespaceDecl);
}
NamespaceDeclarationSyntax namespaceSyntax => namespaceSyntax.Name.ToString(),
CompilationUnitSyntax compilationUnit => string.Empty,
_ => throw ExceptionUtilities.UnexpectedValue(container)
};
protected override bool IsContainedInNamespaceDeclaration(NamespaceDeclarationSyntax namespaceDeclaration, int position)
{
......
......@@ -33,8 +33,7 @@ internal abstract class AbstractMoveToNamespaceService<TCompilationUnitSyntax, T
where TNamedTypeDeclarationSyntax : SyntaxNode
{
protected abstract string GetNamespaceName(TNamespaceDeclarationSyntax namespaceSyntax);
protected abstract string GetNamespaceName(TNamedTypeDeclarationSyntax namedTypeSyntax);
protected abstract string GetNamespaceName(SyntaxNode namespaceSyntax);
protected abstract bool IsContainedInNamespaceDeclaration(TNamespaceDeclarationSyntax namespaceSyntax, int position);
public IMoveToNamespaceOptionsService OptionsService { get; }
......@@ -95,16 +94,19 @@ protected AbstractMoveToNamespaceService(IMoveToNamespaceOptionsService moveToNa
}
// The underlying ChangeNamespace service doesn't support nested namespace decalration.
if (GetNamespaceInSpineCount(declarationSyntax) > 1)
if (GetNamespaceInSpineCount(declarationSyntax) == 1)
{
return MoveToNamespaceAnalysisResult.Invalid;
}
else
{
var namespaceName = GetNamespaceName(declarationSyntax);
var namespaces = await GetNamespacesAsync(document, cancellationToken).ConfigureAwait(false);
return new MoveToNamespaceAnalysisResult(document, declarationSyntax, namespaceName, namespaces.ToImmutableArray(), MoveToNamespaceAnalysisResult.ContainerType.Namespace);
var changeNamespaceService = document.GetLanguageService<IChangeNamespaceService>();
if (await changeNamespaceService.CanChangeNamespaceAsync(document, declarationSyntax, cancellationToken).ConfigureAwait(false))
{
var namespaceName = GetNamespaceName(declarationSyntax);
var namespaces = await GetNamespacesAsync(document, cancellationToken).ConfigureAwait(false);
return new MoveToNamespaceAnalysisResult(document, declarationSyntax, namespaceName, namespaces.ToImmutableArray(), MoveToNamespaceAnalysisResult.ContainerType.Namespace);
}
}
return MoveToNamespaceAnalysisResult.Invalid;
}
private async Task<MoveToNamespaceAnalysisResult> TryAnalyzeNamedTypeAsync(
......@@ -118,14 +120,16 @@ protected AbstractMoveToNamespaceService(IMoveToNamespaceOptionsService moveToNa
return MoveToNamespaceAnalysisResult.Invalid;
}
SyntaxNode container = null;
// Moving one of the many members declared in global namespace is not currently supported,
// but if it's the only member declared, then that's fine.
if (namespaceInSpineCount == 0)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
container = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
if (syntaxFacts.GetMembersOfCompilationUnit(root).Count > 1)
if (syntaxFacts.GetMembersOfCompilationUnit(container).Count > 1)
{
return MoveToNamespaceAnalysisResult.Invalid;
}
......@@ -133,14 +137,23 @@ protected AbstractMoveToNamespaceService(IMoveToNamespaceOptionsService moveToNa
if (node is TNamedTypeDeclarationSyntax namedTypeDeclarationSyntax)
{
var namespaceName = GetNamespaceName(namedTypeDeclarationSyntax);
var namespaces = await GetNamespacesAsync(document, cancellationToken).ConfigureAwait(false);
return new MoveToNamespaceAnalysisResult(document, namedTypeDeclarationSyntax, namespaceName, namespaces.ToImmutableArray(), MoveToNamespaceAnalysisResult.ContainerType.NamedType);
// If we are inside a namespace declaration, then find it as the container.
container ??= GetContainingNamespace(namedTypeDeclarationSyntax);
var changeNamespaceService = document.GetLanguageService<IChangeNamespaceService>();
if (await changeNamespaceService.CanChangeNamespaceAsync(document, container, cancellationToken).ConfigureAwait(false))
{
var namespaces = await GetNamespacesAsync(document, cancellationToken).ConfigureAwait(false);
return new MoveToNamespaceAnalysisResult(document, namedTypeDeclarationSyntax, GetNamespaceName(container), namespaces.ToImmutableArray(), MoveToNamespaceAnalysisResult.ContainerType.NamedType);
}
}
return null;
return MoveToNamespaceAnalysisResult.Invalid;
}
private static TNamespaceDeclarationSyntax GetContainingNamespace(TNamedTypeDeclarationSyntax namedTypeSyntax)
=> namedTypeSyntax.FirstAncestorOrSelf<TNamespaceDeclarationSyntax>();
private static int GetNamespaceInSpineCount(SyntaxNode node)
=> node.AncestorsAndSelf().OfType<TNamespaceDeclarationSyntax>().Count() + node.DescendantNodes().OfType<TNamespaceDeclarationSyntax>().Count();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册