How to hilight python multiline comment from custom syntax hilight radsyntaxeditor

1 Answer 61 Views
SyntaxEditor
jack
Top achievements
Rank 1
jack asked on 30 Oct 2023, 03:41 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.Windows.Controls;

using Telerik.WinForms.Controls.SyntaxEditor.Taggers;
using Telerik.WinForms.SyntaxEditor.Core.Tagging;
using Telerik.WinControls.UI;
using Telerik.WinForms.Controls.SyntaxEditor.UI;
using System.IO;

namespace WindowsFormsApp12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class PythonTagger : WordTaggerBase
        {
            private static readonly string[] Keywords = new string[]
    {
        "False", "None", "True", "and", "as", "assert","break", "class",
        "continue", "def", "del", "elif", "else", "except", "for", "from",
        "global", "if", "import", "in", "is", "lambda", "nonlocal", "not",
        "or", "pass", "raise", "finally", "return", "try", "while", "with", "yield"
    };

            private static readonly string[] Comments = new string[]
    {
        "#"
    };

            private static readonly string[] Operators = new string[]
    {
        "+", "-",  "*", "/"
    };

            public static readonly ClassificationType FruitsClassificationType = new ClassificationType("Fruits");

            private static readonly string[] Fruits = new string[]
    {
        "apple", "banana",  "cherry"
    };

            private static readonly Dictionary<string, ClassificationType> WordsToClassificationType = new Dictionary<string, ClassificationType>();

            static PythonTagger()
            {
                WordsToClassificationType = new Dictionary<string, ClassificationType>();

                foreach (var keyword in Keywords)
                {
                    WordsToClassificationType.Add(keyword, ClassificationTypes.Keyword);
                }

                foreach (var preprocessor in Operators)
                {
                    WordsToClassificationType.Add(preprocessor, ClassificationTypes.Operator);
                }

                foreach (var comment in Comments)
                {
                    WordsToClassificationType.Add(comment, ClassificationTypes.Comment);
                }

                foreach (var comment in Fruits)
                {
                    WordsToClassificationType.Add(comment, FruitsClassificationType);
                }
            }

            public PythonTagger(RadSyntaxEditorElement editor)
                : base(editor)
            {
            }

            protected override Dictionary<string, ClassificationType> GetWordsToClassificationTypes()
            {
                return PythonTagger.WordsToClassificationType;
            }

            protected override bool TryGetClassificationType(string word, out ClassificationType classificationType)
            {
                int number;

                if (int.TryParse(word, out number))
                {
                    classificationType = ClassificationTypes.NumberLiteral;
                    return true;
                }

                return base.TryGetClassificationType(word, out classificationType);
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            PythonTagger pythonTagger = new PythonTagger(this.radSyntaxEditor1.SyntaxEditorElement);
            if (!this.radSyntaxEditor1.TaggersRegistry.IsTaggerRegistered(pythonTagger))
            {
                this.radSyntaxEditor1.TaggersRegistry.RegisterTagger(pythonTagger);
            }

            this.radSyntaxEditor1.TextFormatDefinitions.AddLast(ClassificationTypes.NumberLiteral, new TextFormatDefinition(new SolidBrush(Color.Red)));
            this.radSyntaxEditor1.TextFormatDefinitions.AddLast(ClassificationTypes.Operator, new TextFormatDefinition(new SolidBrush(Color.YellowGreen)));
            this.radSyntaxEditor1.TextFormatDefinitions.AddLast(PythonTagger.FruitsClassificationType, new TextFormatDefinition(new SolidBrush(Color.LightCoral)));


            StreamReader  reader = new StreamReader(@"C:\Python27amd64\Lib\msilib\text.py");
            {
                radSyntaxEditor1.Document = new     Telerik.WinForms.SyntaxEditor.Core.Text.TextDocument(reader);
            }


        }

        private void radMenuItem2_Click(object sender, EventArgs e)
        {
            StreamReader reader = new StreamReader(@"C:\Python27amd64\Lib\ctypes\util.py");
            {
                radSyntaxEditor1.Document = new Telerik.WinForms.SyntaxEditor.Core.Text.TextDocument(reader);
            }

        }

        private void radSyntaxEditor1_Click(object sender, EventArgs e)
        {

        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 01 Nov 2023, 08:34 AM

Hello, Jack,

The built-in tagger in WordTaggerBase already has an implementation for ClassificationTypes.Comment. Hence, when you add "//", these words will be classified as Comment. If you have multiline comment they would be marked as comments accordingly.

I have attached my test project for your reference. Can you give it a try and see how it works.

Please let me know if you have any other questions.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
SyntaxEditor
Asked by
jack
Top achievements
Rank 1
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or