<commit_before>﻿//----------------------------------------------------------------------- 
// <copyright file="CollectAllMembers.cs" company="Copacetic Software"> 
// Copyright (c) Copacetic Software.  
// <author><NAME></author> 
// <date>Wednesday, July 23, 2014 10:15:58 AM</date> 
// Licensed under the Apache License, Version 2.0,
// you may not use this file except in compliance with this License.
//  
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright> 
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using CopaceticSoftware.CodeGenerator.StarterKit.Extensions;
using CopaceticSoftware.Common.Patterns;
using CopaceticSoftware.pMixins.Attributes;
using CopaceticSoftware.pMixins.CodeGenerator.Infrastructure;
using CopaceticSoftware.pMixins.CodeGenerator.Pipelines.ResolveAttributes.Infrastructure;
using ICSharpCode.NRefactory.TypeSystem;

namespace CopaceticSoftware.pMixins.CodeGenerator.Pipelines.CreateCodeGenerationPlan.Steps
{
    public class CollectAllMembers : IPipelineStep<ICreateCodeGenerationPlanPipelineState>
    {
        public bool PerformTask(ICreateCodeGenerationPlanPipelineState manager)
        {
            foreach (var target in manager.CommonState.SourcePartialClassDefinitions)
            {
                foreach (var mixin in manager.GetAllPMixinAttributes(target))
                {
                    var memberFilter = BuildMemberFilterFunction(
                        mixin,
                        manager.CommonState.Context.TypeResolver.Compilation);

                    var mixinMembers = 
                        CollectMemberWrappers(mixin, memberFilter)
                        //TODO: At this point should duplicates be removed or marked as 'don't implement'?
                        .DistinctMemberWrappers()
                        .ToList();

                    //Save Members
                    manager.CodeGenerationPlans[target].MixinGenerationPlans[mixin].Members = mixinMembers;
                }
            }

            return true;
        }

        private IEnumerable<MemberWrapper> CollectMemberWrappers(
            pMixinAttributeResolvedResult mixinAttribute,
            Func<IMember, bool> memberFilter)
        {
            return 
                //Collect all applicable types
                mixinAttribute.Mixin.GetAllBaseTypes()
                    .Union(new[] {mixinAttribute.Mixin})
                    //Collect all members
                    .SelectMany(t => CollectMemberWrappers(t, mixinAttribute, memberFilter));
        }

        private IEnumerable<MemberWrapper> CollectMemberWrappers(
            IType parentType, 
            pMixinAttributeResolvedResult mixinAttribute,
            Func<IMember, bool> memberFilter)
        {
            return
                parentType.GetMembers()
                    .Where(memberFilter)
                    .Select(m =>
                        new MemberWrapper
                        {
                            DeclaringType = parentType,
                            Member = m,
                            MixinAttribute = mixinAttribute
                        });

        }

        private Func<IMember, bool> BuildMemberFilterFunction(
            pMixinAttributeResolvedResult mixinAttribute,
            ICompilation compilation)
        {
            var includeInternalMembers =
                    mixinAttribute.Mixin.GetDefinition().ParentAssembly.Equals(<commit_msg>Collect Mixin members first and then base types.  This is fragile - by collecting mixin members first, they'll be chosen instead of base types when Distinct is called.  But maybe there is a better way to do this.<commit_after>﻿//----------------------------------------------------------------------- 
// <copyright file="CollectAllMembers.cs" company="Copacetic Software"> 
// Copyright (c) Copacetic Software.  
// <author><NAME></author> 
// <date>Wednesday, July 23, 2014 10:15:58 AM</date> 
// Licensed under the Apache License, Version 2.0,
// you may not use this file except in compliance with this License.
//  
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright> 
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using CopaceticSoftware.CodeGenerator.StarterKit.Extensions;
using CopaceticSoftware.Common.Patterns;
using CopaceticSoftware.pMixins.Attributes;
using CopaceticSoftware.pMixins.CodeGenerator.Infrastructure;
using CopaceticSoftware.pMixins.CodeGenerator.Pipelines.ResolveAttributes.Infrastructure;
using ICSharpCode.NRefactory.TypeSystem;

namespace CopaceticSoftware.pMixins.CodeGenerator.Pipelines.CreateCodeGenerationPlan.Steps
{
    public class CollectAllMembers : IPipelineStep<ICreateCodeGenerationPlanPipelineState>
    {
        public bool PerformTask(ICreateCodeGenerationPlanPipelineState manager)
        {
            foreach (var target in manager.CommonState.SourcePartialClassDefinitions)
            {
                foreach (var mixin in manager.GetAllPMixinAttributes(target))
                {
                    var memberFilter = BuildMemberFilterFunction(
                        mixin,
                        manager.CommonState.Context.TypeResolver.Compilation);

                    var mixinMembers = 
                        CollectMemberWrappers(mixin, memberFilter)
                        //TODO: At this point should duplicates be removed or marked as 'don't implement'?
                        .DistinctMemberWrappers()
                        .ToList();

                    //Save Members
                    manager.CodeGenerationPlans[target].MixinGenerationPlans[mixin].Members = mixinMembers;
                }
            }

            return true;
        }

        private IEnumerable<MemberWrapper> CollectMemberWrappers(
            pMixinAttributeResolvedResult mixinAttribute,
            Func<IMember, bool> memberFilter)
        {
            return 
                
                new[] {mixinAttribute.Mixin}
                    //Collect all applicable types
                    .Union(mixinAttribute.Mixin.GetAllBaseTypes())

                    //Collect all members
                    .SelectMany(t => CollectMemberWrappers(t, mixinAttribute, memberFilter));
        }

        private IEnumerable<MemberWrapper> CollectMemberWrappers(
            IType parentType, 
            pMixinAttributeResolvedResult mixinAttribute,
            Func<IMember, bool> memberFilter)
        {
            return
                parentType.GetMembers()
                    .Where(memberFilter)
                    .Select(m =>
                        new MemberWrapper
                        {
                            DeclaringType = parentType,
                            Member = m,
                            MixinAttribute = mixinAttribute
                        });

        }

        private Func<IMember, bool> BuildMemberFilterFunction(
            pMixinAttributeResolvedResult mixinAttribute,
            ICompilation compilation)
        {
            var includeInternalMembers =
                    mixinAttribute.Mixin.GetDefinition().ParentAssembly.Equals(
{'fraction_non_alphanumeric': 0.10225713929417633, 'fraction_numerical': 0.003990522509041028, 'mean_word_length': 1.8219563687543983, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2657522', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1759, 'n_words': 469}