<commit_before>﻿using Microsoft.CSharp;
using ScintillaNET;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ScintillaNET_Kitchen.Forms
{
    public partial class MainForm : Form
    {
        #region Constructors

        public MainForm()
        {
            InitializeComponent();

            scintilla1.Text = "";

            lexerToolStripMenuItem.DropDownItems.AddRange(
                Enum.GetNames(typeof(Lexer))
                    .OrderBy(m => m)
                    .Select(m => new ToolStripMenuRadioItem(m)
                    {
                        CheckState = m == "Null" ? CheckState.Checked : CheckState.Unchecked
                    })
                    .ToArray()
            );

            scintilla2.StyleResetDefault();
            scintilla2.Styles[Style.Default].Font = "Consolas";
            scintilla2.Styles[Style.Default].Size = 10;
            scintilla2.StyleClearAll();
            scintilla2.Styles[Style.Cpp.Default].ForeColor = Color.Silver;
            scintilla2.Styles[Style.Cpp.Comment].ForeColor = Color.FromArgb(0, 128, 0);
            scintilla2.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0);
            scintilla2.Styles[Style.Cpp.CommentLineDoc].ForeColor = Color.FromArgb(128, 128, 128);
            scintilla2.Styles[Style.Cpp.Number].ForeColor = Color.Olive;
            scintilla2.Styles[Style.Cpp.Word].ForeColor = Color.Blue;
            scintilla2.Styles[Style.Cpp.Word2].ForeColor = Color.Blue;
            scintilla2.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21);
            scintilla2.Styles[Style.Cpp.Character].ForeColor = Color.FromArgb(163, 21, 21);
            scintilla2.Styles[Style.Cpp.Verbatim].ForeColor = Color.FromArgb(163, 21, 21);
            scintilla2.Styles[Style.Cpp.StringEol].BackColor = Color.Pink;
            scintilla2.Styles[Style.Cpp.Operator].ForeColor = Color.Purple;
            scintilla2.Styles[Style.Cpp.Preprocessor].ForeColor = Color.Maroon;

            this.UpdateResult();
        }

        #endregion

        #region Public Methods

        public void UpdateResult()
        {
            var text = new StringBuilder();
            var lexerName = Enum.GetName(typeof(Lexer), scintilla1.Lexer);
            var defaultStyle = scintilla1.Styles[Style.Default];
            var styleType = defaultStyle.GetType();

            // TODO somehow force scintilla to refresh (otherwise ui doesn't match user selection for some reason)

            text.AppendLine("scintilla1.Lexer = ScintillaNET.Lexer." + lexerName + ";");
            text.AppendLine("");

            foreach (var item in this.GetLexerStyles(scintilla1.Lexer))
            {
                foreach (var styleKey in styleKeys)
                {
                    var prop = styleType.GetProperty(styleKey);
                    var oldValue = prop.GetValue(defaultStyle, null);
                    var newValue = prop.GetValue(scintilla1.Styles[item.Key], null);
                    if (oldValue.ToString() != newValue.ToString() && !(styleKey == "Font" && String.IsNullOrEmpty(newValue.ToString())))
                    {
                        var serializedValue = this.SerializeValue(newValue);
                        if (serializedValue != null)
                        {
                            text.AppendLine("scintilla1.Styles[ScintillaNET.Style." + lexerName + "." + item.Value + "]." + styleKey + " = " + serializedValue + ";");
                        }
                    }
                }
            }

            var keywordSets = new Dictionary<int, string>();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                var keywords = (row.Cells[1].Value ?? "").ToString();
                scintilla1.SetKeywords(row.Index, keywords);
                if (String.IsNullOrEmpty(keywords)) keywordSets.Add(row.Index, keywords);
            }

            if (keywordSets.Any())
            {
                text.AppendLine();
                foreach (var item in keywordSets)
                    if (!String.IsNullOrEmpty(item.Value))
                        text.AppendLine("scintilla1.SetKeywords(" + item.Key + ", @\"" + item.Value + "\");");
            }

            scintilla2.ReadOnly = false;
            scintilla2.Text = text.ToString();
            scintilla2.ReadOnly = true;
        }

        #endregion

        #region Utility Methods

        private void LoadLexerStyles(Lexer lexer)
        {
            toolStripComboBox1.Items.Clear();

            var lexerType = Type.GetType(typeof(Style).AssemblyQualifiedName.Replace(".Style", ".Style+" + lexer));

            if (lexerType != null)
            {
                toolStripComboBox1.Items
                    .AddRange(
                        this.GetLexerStyles(scintilla1.Lexer)
                            .Select(m => m.Key + ": " + m.Value)<commit_msg>Fixed not adding Keywords to the code editor<commit_after>﻿using Microsoft.CSharp;
using ScintillaNET;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ScintillaNET_Kitchen.Forms
{
    public partial class MainForm : Form
    {
        #region Constructors

        public MainForm()
        {
            InitializeComponent();

            scintilla1.Text = "";

            lexerToolStripMenuItem.DropDownItems.AddRange(
                Enum.GetNames(typeof(Lexer))
                    .OrderBy(m => m)
                    .Select(m => new ToolStripMenuRadioItem(m)
                    {
                        CheckState = m == "Null" ? CheckState.Checked : CheckState.Unchecked
                    })
                    .ToArray()
            );

            scintilla2.StyleResetDefault();
            scintilla2.Styles[Style.Default].Font = "Consolas";
            scintilla2.Styles[Style.Default].Size = 10;
            scintilla2.StyleClearAll();
            scintilla2.Styles[Style.Cpp.Default].ForeColor = Color.Silver;
            scintilla2.Styles[Style.Cpp.Comment].ForeColor = Color.FromArgb(0, 128, 0);
            scintilla2.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0);
            scintilla2.Styles[Style.Cpp.CommentLineDoc].ForeColor = Color.FromArgb(128, 128, 128);
            scintilla2.Styles[Style.Cpp.Number].ForeColor = Color.Olive;
            scintilla2.Styles[Style.Cpp.Word].ForeColor = Color.Blue;
            scintilla2.Styles[Style.Cpp.Word2].ForeColor = Color.Blue;
            scintilla2.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21);
            scintilla2.Styles[Style.Cpp.Character].ForeColor = Color.FromArgb(163, 21, 21);
            scintilla2.Styles[Style.Cpp.Verbatim].ForeColor = Color.FromArgb(163, 21, 21);
            scintilla2.Styles[Style.Cpp.StringEol].BackColor = Color.Pink;
            scintilla2.Styles[Style.Cpp.Operator].ForeColor = Color.Purple;
            scintilla2.Styles[Style.Cpp.Preprocessor].ForeColor = Color.Maroon;

            this.UpdateResult();
        }

        #endregion

        #region Public Methods

        public void UpdateResult()
        {
            var text = new StringBuilder();
            var lexerName = Enum.GetName(typeof(Lexer), scintilla1.Lexer);
            var defaultStyle = scintilla1.Styles[Style.Default];
            var styleType = defaultStyle.GetType();

            // TODO somehow force scintilla to refresh (otherwise ui doesn't match user selection for some reason)

            text.AppendLine("scintilla1.Lexer = ScintillaNET.Lexer." + lexerName + ";");
            text.AppendLine("");

            foreach (var item in this.GetLexerStyles(scintilla1.Lexer))
            {
                foreach (var styleKey in styleKeys)
                {
                    var prop = styleType.GetProperty(styleKey);
                    var oldValue = prop.GetValue(defaultStyle, null);
                    var newValue = prop.GetValue(scintilla1.Styles[item.Key], null);
                    if (oldValue.ToString() != newValue.ToString() && !(styleKey == "Font" && String.IsNullOrEmpty(newValue.ToString())))
                    {
                        var serializedValue = this.SerializeValue(newValue);
                        if (serializedValue != null)
                        {
                            text.AppendLine("scintilla1.Styles[ScintillaNET.Style." + lexerName + "." + item.Value + "]." + styleKey + " = " + serializedValue + ";");
                        }
                    }
                }
            }

            var keywordSets = new Dictionary<int, string>();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                var keywords = (row.Cells[1].Value ?? "").ToString();
                scintilla1.SetKeywords(row.Index, keywords);
                if (String.IsNullOrEmpty(keywords) == false) keywordSets.Add(row.Index, keywords);
            }

            if (keywordSets.Any())
            {
                text.AppendLine();
                foreach (var item in keywordSets)
                    if (!String.IsNullOrEmpty(item.Value))
                        text.AppendLine("scintilla1.SetKeywords(" + item.Key + ", @\"" + item.Value + "\");");
            }

            scintilla2.ReadOnly = false;
            scintilla2.Text = text.ToString();
            scintilla2.ReadOnly = true;
        }

        #endregion

        #region Utility Methods

        private void LoadLexerStyles(Lexer lexer)
        {
            toolStripComboBox1.Items.Clear();

            var lexerType = Type.GetType(typeof(Style).AssemblyQualifiedName.Replace(".Style", ".Style+" + lexer));

            if (lexerType != null)
            {
                toolStripComboBox1.Items
                    .AddRange(
                        this.GetLexerStyles(scintilla1.Lexer)
                            .Select(m => m.Key + ": " + m.Value)
{'fraction_non_alphanumeric': 0.11053440984236831, 'fraction_numerical': 0.014801999231064974, 'mean_word_length': 1.8243756786102063, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 11, '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': '6390435', 'n_tokens_mistral': 2992, 'n_tokens_neox': 2848, 'n_words': 489}