<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.ComponentModel;
using SyMath;

namespace LiveSPICE
{
    /// <summary>
    /// Interaction logic for ComponentLibrary.xaml
    /// </summary>
    public partial class ComponentLibrary : UserControl
    {
        private static Tuple<Type, Key[]>[] CommonTypes = 
        {
            new Tuple<Type, Key[]> (typeof(Circuit.Conductor), new Key[] { Key.W }),
            new Tuple<Type, Key[]> (typeof(Circuit.Ground), new Key[] { Key.G }),
            new Tuple<Type, Key[]> (typeof(Circuit.Resistor), new Key[] { Key.R }),
            new Tuple<Type, Key[]> (typeof(Circuit.Capacitor), new Key[] { Key.F }),
            new Tuple<Type, Key[]> (typeof(Circuit.Inductor), new Key[] { Key.L, Key.H }),
            new Tuple<Type, Key[]> (typeof(Circuit.VoltageSource), null),
            new Tuple<Type, Key[]> (typeof(Circuit.NamedWire), null),
        };
                
        public ComponentLibrary()
        {
            InitializeComponent();
        }
                
        public static IEnumerable<Type> GetComponentTypes()
        {
            Type root = typeof(Circuit.Component);
            return Assembly.GetAssembly(root).GetTypes().Where(t => !t.IsAbstract && root.IsAssignableFrom(t)).Concat(new Type[] { typeof(Circuit.Conductor) });
        }

        public void Init(Window ShortcutKeys, RoutedEventHandler OnClick)
        {
            Expander common = GetCategory("Common");
            foreach (Tuple<Type, Key[]> i in CommonTypes)
            {
                Button item = AddItem(common, i.Item1, OnClick);

                if (i.Item2 != null)
                {
                    RoutedCommand command = new RoutedCommand();
                    foreach (Key j in i.Item2)
                        command.InputGestures.Add(new KeyGesture(j, ModifierKeys.Control));

                    CommandBinding binding = new CommandBinding(command, (o, e) => item.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)));
                    ShortcutKeys.CommandBindings.Add(binding);
                }
            }
            common.IsExpanded = true;

            foreach (Type i in GetComponentTypes())
                foreach (CategoryAttribute j in i.GetCustomAttributes(typeof(CategoryAttribute), false).Cast<CategoryAttribute>())
                    AddItem(GetCategory(j.Category), i, OnClick);
        }
        
        private Button AddItem(Expander Group, Type T, RoutedEventHandler OnClick)
        {
            try
            {
                StackPanel content = new StackPanel() { Orientation = Orientation.Vertical };

                // Add image to the button.
                ComponentControl symbol = new ComponentControl((Circuit.Component)Activator.CreateInstance(T))
                {
                    Width = 48,
                    Height = 32,
                    ShowText = false,
                    Margin = new Thickness(2),
                };
                content.Children.Add(symbol);

                TextBlock name = new TextBlock()
                {
                    Text = GetDisplayName(T),
                    MaxWidth = symbol.Width,
                    TextTrimming = TextTrimming.CharacterEllipsis,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    FontWeight = FontWeights.Normal
                };
                content.Children.Add(name);

                Button button = new Button() { Tag = T, ToolTip = GetDisplayName(T), Content = content };
                button.Click += OnClick;
                ((Panel)Group.Content).Children.Add(button);

                return button;
            }
            catch (System.Exception) 
            {
                return null;
            }
        }
        
        private Expander GetCategory(string Category)
        {
            IEnumerable<Expander> expanders = list.Children.OfType<Expander>();
            Expander category = expanders.SingleOrDefault(i => (string)i.Header == Category);
            if (category == null)
            {
                category = new Expander()
                {
                    Header = Category,
                    Content = new WrapPanel(),
                    Focusable = false,
                    FontWeight = FontWeights.Bold
                };
                list.Children.Add(category);
            }

            return category;
        }

        private static string GetDisplayName(Type T)
        {
            DisplayNameAttribute name = T.GetCustomAttribute<DisplayNameAttribute>();
            return name != null ? name.DisplayName : T.ToString();
        }

        private static string GetDescription(Type T)
        {
            DescriptionAttribute desc = T.GetCustomAttribute<DescriptionAttribute>();
            return desc != null ? desc.Description : GetDisplayName(T);
        }
    }
}
<commit_msg>Fixed Wire appearing twice in the component library<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.ComponentModel;
using SyMath;

namespace LiveSPICE
{
    /// <summary>
    /// Interaction logic for ComponentLibrary.xaml
    /// </summary>
    public partial class ComponentLibrary : UserControl
    {
        private static Tuple<Type, Key[]>[] CommonTypes = 
        {
            new Tuple<Type, Key[]> (typeof(Circuit.Conductor), new Key[] { Key.W }),
            new Tuple<Type, Key[]> (typeof(Circuit.Ground), new Key[] { Key.G }),
            new Tuple<Type, Key[]> (typeof(Circuit.Resistor), new Key[] { Key.R }),
            new Tuple<Type, Key[]> (typeof(Circuit.Capacitor), new Key[] { Key.F }),
            new Tuple<Type, Key[]> (typeof(Circuit.Inductor), new Key[] { Key.L, Key.H }),
            new Tuple<Type, Key[]> (typeof(Circuit.VoltageSource), null),
            new Tuple<Type, Key[]> (typeof(Circuit.NamedWire), null),
        };
                
        public ComponentLibrary()
        {
            InitializeComponent();
        }
                
        public static IEnumerable<Type> GetComponentTypes()
        {
            Type root = typeof(Circuit.Component);
            return Assembly.GetAssembly(root).GetTypes().Where(t => !t.IsAbstract && root.IsAssignableFrom(t));
        }

        public void Init(Window ShortcutKeys, RoutedEventHandler OnClick)
        {
            Expander common = GetCategory("Common");
            foreach (Tuple<Type, Key[]> i in CommonTypes)
            {
                Button item = AddItem(common, i.Item1, OnClick);

                if (i.Item2 != null)
                {
                    RoutedCommand command = new RoutedCommand();
                    foreach (Key j in i.Item2)
                        command.InputGestures.Add(new KeyGesture(j, ModifierKeys.Control));

                    CommandBinding binding = new CommandBinding(command, (o, e) => item.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)));
                    ShortcutKeys.CommandBindings.Add(binding);
                }
            }
            common.IsExpanded = true;

            foreach (Type i in GetComponentTypes())
                foreach (CategoryAttribute j in i.GetCustomAttributes(typeof(CategoryAttribute), false).Cast<CategoryAttribute>())
                    AddItem(GetCategory(j.Category), i, OnClick);
        }
        
        private Button AddItem(Expander Group, Type T, RoutedEventHandler OnClick)
        {
            try
            {
                StackPanel content = new StackPanel() { Orientation = Orientation.Vertical };

                // Add image to the button.
                ComponentControl symbol = new ComponentControl((Circuit.Component)Activator.CreateInstance(T))
                {
                    Width = 48,
                    Height = 32,
                    ShowText = false,
                    Margin = new Thickness(2),
                };
                content.Children.Add(symbol);

                TextBlock name = new TextBlock()
                {
                    Text = GetDisplayName(T),
                    MaxWidth = symbol.Width,
                    TextTrimming = TextTrimming.CharacterEllipsis,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    FontWeight = FontWeights.Normal
                };
                content.Children.Add(name);

                Button button = new Button() { Tag = T, ToolTip = GetDisplayName(T), Content = content };
                button.Click += OnClick;
                ((Panel)Group.Content).Children.Add(button);

                return button;
            }
            catch (System.Exception) 
            {
                return null;
            }
        }
        
        private Expander GetCategory(string Category)
        {
            IEnumerable<Expander> expanders = list.Children.OfType<Expander>();
            Expander category = expanders.SingleOrDefault(i => (string)i.Header == Category);
            if (category == null)
            {
                category = new Expander()
                {
                    Header = Category,
                    Content = new WrapPanel(),
                    Focusable = false,
                    FontWeight = FontWeights.Bold
                };
                list.Children.Add(category);
            }

            return category;
        }

        private static string GetDisplayName(Type T)
        {
            DisplayNameAttribute name = T.GetCustomAttribute<DisplayNameAttribute>();
            return name != null ? name.DisplayName : T.ToString();
        }

        private static string GetDescription(Type T)
        {
            DescriptionAttribute desc = T.GetCustomAttribute<DescriptionAttribute>();
            return desc != null ? desc.Description : GetDisplayName(T);
        }
    }
}

{'fraction_non_alphanumeric': 0.10012302451026782, 'fraction_numerical': 0.0015141478186807987, 'mean_word_length': 1.7237113402061857, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 43, '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': '6529642', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2559, 'n_words': 601}