<commit_before>﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using DesperateDevs.CodeGeneration;
using DesperateDevs.Serialization;
using DesperateDevs.Utils;
using Entitas.CodeGeneration.Attributes;

namespace Entitas.CodeGeneration.Plugins {

    public class EventInterfaceGenerator : ICodeGenerator, IConfigurable {

        public string name { get { return "Event (Listener Interface)"; } }
        public int priority { get { return 0; } }
        public bool runInDryMode { get { return true; } }

        public Dictionary<string, string> defaultProperties { get { return _ignoreNamespacesConfig.defaultProperties; } }

        readonly IgnoreNamespacesConfig _ignoreNamespacesConfig = new IgnoreNamespacesConfig();

        const string INTERFACE_TEMPLATE =
            @"public interface I${OptionalContextName}${ComponentName}Listener {
    void On${ComponentName}${EventType}(${ContextName}Entity entity${memberArgs});
}
";

        const string MEMBER_ARGS_TEMPLATE =
            @"${MemberType} ${MemberName}";

        public void Configure(Preferences preferences) {
            _ignoreNamespacesConfig.Configure(preferences);
        }

        public CodeGenFile[] Generate(CodeGeneratorData[] data) {
            return data
                .OfType<ComponentData>()
                .Where(d => d.IsEvent())
                .SelectMany(generateInterfaces)
                .ToArray();
        }

        CodeGenFile[] generateInterfaces(ComponentData data) {
            return data.GetContextNames()
                .Select(contextName => generateInterface(contextName, data))
                .ToArray();
        }

        CodeGenFile generateInterface(string contextName, ComponentData data) {
            var optionalContextName = data.GetContextNames().Length > 1 ? contextName : string.Empty;
            var componentName = data.GetTypeName().ToComponentName(_ignoreNamespacesConfig.ignoreNamespaces);
            var memberData = data.GetMemberData();
            if (memberData.Length == 0) {
                memberData = new[] { new MemberData("bool", data.GetUniquePrefix().LowercaseFirst() + componentName) };
            }

            var eventTypeSuffix = string.Empty;
            var memberArgs = ", " + getMemberArgs(memberData);
            if (data.GetMemberData().Length == 0) {
                switch (data.GetEventType()) {
                    case EventType.Added:
                        eventTypeSuffix = "Added";
                        memberArgs = string.Empty;
                        break;
                    case EventType.Removed:
                        eventTypeSuffix = "Removed";
                        memberArgs = string.Empty;
                        break;
                }
            } else {
                switch (data.GetEventType()) {
                    case EventType.Removed:
                        eventTypeSuffix = "Removed";
                        memberArgs = string.Empty;
                        break;
                    case EventType.AddedOrRemoved:
                        eventTypeSuffix = "AddedOrRemoved";
                        break;
                }
            }

            var fileContent = INTERFACE_TEMPLATE
                .Replace("${ContextName}", contextName)
                .Replace("${OptionalContextName}", optionalContextName)
                .Replace("${ComponentName}", componentName)
                .Replace("${ComponentName}", componentName)
                .Replace("${EventType}", eventTypeSuffix)
                .Replace("${memberArgs}", memberArgs);

            return new CodeGenFile(
                "Events" + Path.DirectorySeparatorChar +
                "Interfaces" + Path.DirectorySeparatorChar +
                "I" + contextName + componentName + "Listener.cs",
                fileContent,
                GetType().FullName
            );
        }

        string getMemberArgs(MemberData[] memberData) {<commit_msg>EventInterfaceGenerator generates correct filename matching the class name. Thanks to @c0ffeeartc<commit_after>﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using DesperateDevs.CodeGeneration;
using DesperateDevs.Serialization;
using DesperateDevs.Utils;
using Entitas.CodeGeneration.Attributes;

namespace Entitas.CodeGeneration.Plugins {

    public class EventInterfaceGenerator : ICodeGenerator, IConfigurable {

        public string name { get { return "Event (Listener Interface)"; } }
        public int priority { get { return 0; } }
        public bool runInDryMode { get { return true; } }

        public Dictionary<string, string> defaultProperties { get { return _ignoreNamespacesConfig.defaultProperties; } }

        readonly IgnoreNamespacesConfig _ignoreNamespacesConfig = new IgnoreNamespacesConfig();

        const string INTERFACE_TEMPLATE =
            @"public interface I${OptionalContextName}${ComponentName}Listener {
    void On${ComponentName}${EventType}(${ContextName}Entity entity${memberArgs});
}
";

        const string MEMBER_ARGS_TEMPLATE =
            @"${MemberType} ${MemberName}";

        public void Configure(Preferences preferences) {
            _ignoreNamespacesConfig.Configure(preferences);
        }

        public CodeGenFile[] Generate(CodeGeneratorData[] data) {
            return data
                .OfType<ComponentData>()
                .Where(d => d.IsEvent())
                .SelectMany(generateInterfaces)
                .ToArray();
        }

        CodeGenFile[] generateInterfaces(ComponentData data) {
            return data.GetContextNames()
                .Select(contextName => generateInterface(contextName, data))
                .ToArray();
        }

        CodeGenFile generateInterface(string contextName, ComponentData data) {
            var optionalContextName = data.GetContextNames().Length > 1 ? contextName : string.Empty;
            var componentName = data.GetTypeName().ToComponentName(_ignoreNamespacesConfig.ignoreNamespaces);
            var memberData = data.GetMemberData();
            if (memberData.Length == 0) {
                memberData = new[] { new MemberData("bool", data.GetUniquePrefix().LowercaseFirst() + componentName) };
            }

            var eventTypeSuffix = string.Empty;
            var memberArgs = ", " + getMemberArgs(memberData);
            if (data.GetMemberData().Length == 0) {
                switch (data.GetEventType()) {
                    case EventType.Added:
                        eventTypeSuffix = "Added";
                        memberArgs = string.Empty;
                        break;
                    case EventType.Removed:
                        eventTypeSuffix = "Removed";
                        memberArgs = string.Empty;
                        break;
                }
            } else {
                switch (data.GetEventType()) {
                    case EventType.Removed:
                        eventTypeSuffix = "Removed";
                        memberArgs = string.Empty;
                        break;
                    case EventType.AddedOrRemoved:
                        eventTypeSuffix = "AddedOrRemoved";
                        break;
                }
            }

            var fileContent = INTERFACE_TEMPLATE
                .Replace("${ContextName}", contextName)
                .Replace("${OptionalContextName}", optionalContextName)
                .Replace("${ComponentName}", componentName)
                .Replace("${ComponentName}", componentName)
                .Replace("${EventType}", eventTypeSuffix)
                .Replace("${memberArgs}", memberArgs);

            return new CodeGenFile(
                "Events" + Path.DirectorySeparatorChar +
                "Interfaces" + Path.DirectorySeparatorChar +
                "I" + optionalContextName + componentName + "Listener.cs",
                fileContent,
                GetType().FullName
            );
        }

        string getMemberArgs(MemberData[] memberData) {
{'fraction_non_alphanumeric': 0.0920627021647176, 'fraction_numerical': 0.001119681512814133, 'mean_word_length': 1.8938084953203744, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1897043', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1830, 'n_words': 388}