<commit_before>﻿#region License
/*********************************************************************************
 * PgnStyleSelector.cs
 *
 * Copyright (c) 2004-2020 <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the 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.
 *
**********************************************************************************/
#endregion

using Eutherion.Win.AppTemplate;
using Sandra.Chess.Pgn;
using ScintillaNET;
using System.Drawing;

namespace Sandra.UI
{
    /// <summary>
    /// A style selector for PGN syntax highlighting.
    /// </summary>
    public class PgnStyleSelector<TSyntaxTree, TError> : PgnSymbolVisitor<SyntaxEditor<TSyntaxTree, IPgnSymbol, TError>, Style>
    {
        private const int tagNameStyleIndex = 8;
        private const int tagValueStyleIndex = 9;
        private const int errorTagValueStyleIndex = 10;
        private const int illegalCharacterStyleIndex = 11;
        private const int symbolStyleIndex = 12;
        private const int escapedLineStyleIndex = 13;

        private static readonly Font tagNameAndEscapeFont = new Font("Consolas", 10, FontStyle.Italic);

        private static readonly Color tagNameForeColor = Color.FromArgb(0x90, 0xff, 0xf0);
        private static readonly Color tagValueForeColor = Color.FromArgb(0xff, 0xbb, 0x9e);
        private static readonly Color errorTagValueForeColor = Color.FromArgb(0xbc, 0x82, 0x70);
        private static readonly Color illegalCharacterForeColor = Color.FromArgb(0xa0, 0xa0, 0xa0);
        private static readonly Color symbolForeColor = Color.FromArgb(0xff, 0xff, 0xa0);
        private static readonly Color escapeForeColor = Color.FromArgb(0x8c, 0x8c, 0x8c);

        public static readonly PgnStyleSelector<TSyntaxTree, TError> Instance = new PgnStyleSelector<TSyntaxTree, TError>();

        public static void InitializeStyles(SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
        {
            syntaxEditor.Styles[tagNameStyleIndex].ForeColor = tagNameForeColor;
            tagNameAndEscapeFont.CopyTo(syntaxEditor.Styles[tagNameStyleIndex]);

            syntaxEditor.Styles[tagValueStyleIndex].ForeColor = tagValueForeColor;

            syntaxEditor.Styles[errorTagValueStyleIndex].ForeColor = errorTagValueForeColor;

            syntaxEditor.Styles[illegalCharacterStyleIndex].ForeColor = illegalCharacterForeColor;

            syntaxEditor.Styles[symbolStyleIndex].ForeColor = symbolForeColor;

            syntaxEditor.Styles[escapedLineStyleIndex].ForeColor = escapeForeColor;
            tagNameAndEscapeFont.CopyTo(syntaxEditor.Styles[escapedLineStyleIndex]);
        }

        private PgnStyleSelector() { }

        public override Style DefaultVisit(IPgnSymbol node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.Styles[illegalCharacterStyleIndex];

        public override Style VisitCommentSyntax(PgnCommentSyntax node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.DefaultStyle;

        public override Style VisitEscapeSyntax(PgnEscapeSyntax node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.Styles[escapedLineStyleIndex];

        public override Style VisitIllegalCharacterSyntax(PgnIllegalCharacterSyntax node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.Styles[illegalCharacterStyleIndex];

        public override Style VisitUnterminatedCommentSyntax(PgnUnterminatedCommentSyntax node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.DefaultStyle;

        public override Style VisitPgnSymbol(PgnSymbol node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
        {
            switch (node.Green.SymbolType)
            {
                case PgnSymbolType.TagName:
                    return syntaxEditor.Styles[tagNameStyleIndex];
                case PgnSymbolType.TagValue:
                    return syntaxEditor.Styles[tagValueStyleIndex];
                case PgnSymbolType.ErrorTagValue:
                    return syntaxEditor.Styles[errorTagValueStyleIndex];
                case PgnSymbolType.Unknown:
                    return syntaxEditor.Styles[illegalCharacterStyleIndex];
            }

            return syntaxEditor.DefaultStyle;<commit_msg>Add syntax highlighting for moves, NAGs, move numbers and termination markers.<commit_after>﻿#region License
/*********************************************************************************
 * PgnStyleSelector.cs
 *
 * Copyright (c) 2004-2020 <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the 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.
 *
**********************************************************************************/
#endregion

using Eutherion.Win.AppTemplate;
using Sandra.Chess.Pgn;
using ScintillaNET;
using System.Drawing;

namespace Sandra.UI
{
    /// <summary>
    /// A style selector for PGN syntax highlighting.
    /// </summary>
    public class PgnStyleSelector<TSyntaxTree, TError> : PgnSymbolVisitor<SyntaxEditor<TSyntaxTree, IPgnSymbol, TError>, Style>
    {
        private const int tagNameStyleIndex = 8;
        private const int tagValueStyleIndex = 9;
        private const int errorTagValueStyleIndex = 10;
        private const int illegalCharacterStyleIndex = 11;
        private const int moveNumberStyleIndex = 12;
        private const int moveTextStyleIndex = 13;
        private const int errorNagStyleIndex = 14;
        private const int escapedLineStyleIndex = 15;

        private static readonly Font tagNameAndEscapeFont = new Font("Consolas", 10, FontStyle.Italic);

        private static readonly Color tagNameForeColor = Color.FromArgb(0x90, 0xff, 0xf0);
        private static readonly Color tagValueForeColor = Color.FromArgb(0xff, 0xbb, 0x9e);
        private static readonly Color errorTagValueForeColor = Color.FromArgb(0xbc, 0x82, 0x70);
        private static readonly Color illegalCharacterForeColor = Color.FromArgb(0xa0, 0xa0, 0xa0);
        private static readonly Color moveNumberForeColor = Color.FromArgb(0xcc, 0xcc, 0x92);
        private static readonly Color moveTextForeColor = Color.FromArgb(0xbb, 0xff, 0x9e);
        private static readonly Color errorNagForeColor = Color.FromArgb(0x82, 0xbc, 0x70);
        private static readonly Color escapeForeColor = Color.FromArgb(0x8c, 0x8c, 0x8c);

        public static readonly PgnStyleSelector<TSyntaxTree, TError> Instance = new PgnStyleSelector<TSyntaxTree, TError>();

        public static void InitializeStyles(SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
        {
            syntaxEditor.Styles[tagNameStyleIndex].ForeColor = tagNameForeColor;
            tagNameAndEscapeFont.CopyTo(syntaxEditor.Styles[tagNameStyleIndex]);

            syntaxEditor.Styles[tagValueStyleIndex].ForeColor = tagValueForeColor;
            syntaxEditor.Styles[errorTagValueStyleIndex].ForeColor = errorTagValueForeColor;
            syntaxEditor.Styles[illegalCharacterStyleIndex].ForeColor = illegalCharacterForeColor;
            syntaxEditor.Styles[moveNumberStyleIndex].ForeColor = moveNumberForeColor;
            syntaxEditor.Styles[moveTextStyleIndex].ForeColor = moveTextForeColor;
            syntaxEditor.Styles[errorNagStyleIndex].ForeColor = errorNagForeColor;

            syntaxEditor.Styles[escapedLineStyleIndex].ForeColor = escapeForeColor;
            tagNameAndEscapeFont.CopyTo(syntaxEditor.Styles[escapedLineStyleIndex]);
        }

        private PgnStyleSelector() { }

        public override Style DefaultVisit(IPgnSymbol node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.Styles[illegalCharacterStyleIndex];

        public override Style VisitCommentSyntax(PgnCommentSyntax node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.DefaultStyle;

        public override Style VisitEscapeSyntax(PgnEscapeSyntax node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.Styles[escapedLineStyleIndex];

        public override Style VisitIllegalCharacterSyntax(PgnIllegalCharacterSyntax node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.Styles[illegalCharacterStyleIndex];

        public override Style VisitUnterminatedCommentSyntax(PgnUnterminatedCommentSyntax node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
            => syntaxEditor.DefaultStyle;

        public override Style VisitPgnSymbol(PgnSymbol node, SyntaxEditor<TSyntaxTree, IPgnSymbol, TError> syntaxEditor)
        {
            switch (node.Green.SymbolType)
            {
                case PgnSymbolType.TagName:
                    return syntaxEditor.Styles[tagNameStyleIndex];
                case PgnSymbolType.TagValue:
                    return syntaxEditor.Styles[tagValueStyleIndex];
                case PgnSymbolType.ErrorTagValue:
                    return syntaxEditor.Styles[errorTagValueStyleIndex];
                case PgnSymbolType.Nag:
                    return syntaxEditor.Styles[moveTextStyleIndex];
                case PgnSymbolType.EmptyNag:
                    // Don't darken this one, got to go through this state before creating a valid NAG.
                    return syntaxEditor.Styles[moveTextStyleIndex];
                case PgnSymbolType.OverflowNag:
                    return syntaxEditor.Styles[errorNagStyleIndex];
                case PgnSymbolType.Period:
                case PgnSymbolType.MoveNumber:
                    return syntaxEditor.Styles[moveNumberStyleIndex];
                case PgnSymbolType.Move:
                    return syntaxEditor.Styles[moveTextStyleIndex];
                case PgnSymbolType.Asterisk:
                case PgnSymbolType.DrawMarker:
                case PgnSymbolType.WhiteWinMarker:
                case PgnSymbolType.BlackWinMarker:
                    return syntaxEditor.Styles[moveTextStyleIndex];
                case PgnSymbolType.Unknown:
                    return syntaxEditor.Styles[illegalCharacterStyleIndex];
            }

            return syntaxEditor.DefaultStyle;
{'fraction_non_alphanumeric': 0.0947799750579013, 'fraction_numerical': 0.011580260110457865, 'mean_word_length': 3.2883880825057297, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 43, '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': '1756179', 'n_tokens_mistral': 2967, 'n_tokens_neox': 2838, 'n_words': 708}