﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Composition;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.MoveToNamespace;

namespace Microsoft.CodeAnalysis.CSharp.MoveToNamespace
{
    [ExportLanguageService(typeof(AbstractMoveToNamespaceService), LanguageNames.CSharp), Shared]
    internal class CSharpMoveToNamespaceService :
        AbstractMoveToNamespaceService<CompilationUnitSyntax, NamespaceDeclarationSyntax, TypeDeclarationSyntax>
    {
        [ImportingConstructor]
        [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
        public CSharpMoveToNamespaceService(
            [Import(AllowDefault = true)] IMoveToNamespaceOptionsService moveToNamespaceOptionsService)
            : base(moveToNamespaceOptionsService)
        {
        }

        protected override string GetNamespaceName(NamespaceDeclarationSyntax syntax)
            => syntax.Name.ToString();

        protected override string GetNamespaceName(TypeDeclarationSyntax syntax)
            => GetNamespaceName(syntax.FirstAncestorOrSelf<NamespaceDeclarationSyntax>());
    }
}

﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Composition;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.MoveToNamespace;

namespace Microsoft.CodeAnalysis.CSharp.MoveToNamespace
{
    [ExportLanguageService(typeof(AbstractMoveToNamespaceService), LanguageNames.CSharp), Shared]
    internal class CSharpMoveToNamespaceService :
        AbstractMoveToNamespaceService<CompilationUnitSyntax, NamespaceDeclarationSyntax, TypeDeclarationSyntax>
    {
        [ImportingConstructor]
        [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
        public CSharpMoveToNamespaceService(
            [Import(AllowDefault = true)] IMoveToNamespaceOptionsService moveToNamespaceOptionsService)
            : base(moveToNamespaceOptionsService)
        {
        }

        protected override string GetNamespaceName(NamespaceDeclarationSyntax syntax)
            => syntax.Name.ToString();

        protected override string GetNamespaceName(TypeDeclarationSyntax syntax)
        {
            var namespaceDecl = syntax.FirstAncestorOrSelf<NamespaceDeclarationSyntax>();
            if (namespaceDecl == null)
            {
                return string.Empty;
            }

            return GetNamespaceName(namespaceDecl);
        }
    }
}

Handle cases where a namespace declaration isn't found in the ancestors of a type declaration
Handle cases where a namespace declaration isn't found in the ancestors of a type declaration
