This is a migrated thread and some comments may be shown as answers.

Custom Language Words with symbols

1 Answer 82 Views
SyntaxEditor
This is a migrated thread and some comments may be shown as answers.
Fabrice
Top achievements
Rank 2
Iron
Iron
Fabrice asked on 29 Jan 2021, 01:38 PM

Hi,

I'm trying to adapt this fantastic user control to display a peculiar syntax that we use to let our clients write "code-like" functions.

 

But I'm facing a problem. Our syntax contains symbols like this:

!IF, !ELSE, !OR, !WHEN and so on.

 

I discovered that symbols are not considered part of a word in TryGetClassificationType().

 

I tried a userControl that works with regex and I achieve what I wanted, but telerik comes with styles, Localization, and I'd really love to use RadSyntaxEditor.

 

Thanks in advance for all the help you provide.

 

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Feb 2021, 12:13 PM
Hello, Fabrice, 

According to the provided information, it is not clear what is the exact custom implementation that you have on your end. However, please have in mind that RadSyntaxEditor works with the underlying document with the help of taggers. Taggers are used to identify spans of text and assign them a specific tag if they match a specific condition. The identification process occurs in the GetTags method which can be overridden in a custom tagger: https://docs.telerik.com/devtools/winforms/controls/syntax-editor/features/taggers/custom-taggers 
In the internal implementation of the GetTags method the text is split into words. Here is the place where you can control how exactly to divide the text into words.

A common requirement when using the RadSyntaxEditor is to highlight words which belong to a specific language classification type - such as keywords, operators and comments. Such predefined taggers include the CSharpTagger for example. By using the WordTaggerBase as a base, you can create custom syntax highlighting for any custom language. A sample example is demonstrated in the following help article: https://docs.telerik.com/devtools/winforms/controls/syntax-editor/features/taggers/custom-language 

However, indeed, special characters, like "!" are not considered as parts of the words by default because they are split as a separate word. I would recommend you to download the Telerik source code and have a look at the default implementation of the WordTaggerBase class. In its SplitIntoWords method you will notice that there is a GetCharType method: 
        internal static int GetCharType(char c)
        {
            // TODO: Remove this workaround.
            if (c == '#')
            {
                return 0;
            }

            if (char.IsWhiteSpace(c))
            {
                return 1;
            }

            if (char.IsPunctuation(c) || char.IsSymbol(c))
            {
                return 2;
            }

            return 0;
        }

"!" is considered as a punctuation. That is why it is not considered as a part of the word.

        protected virtual IList<string> SplitIntoWords(string value)
        {
            List<string> words = new List<string>();
            string word;
            int lastCharType = -1;
            int startIndex = 0;
            for (int i = 0; i < value.Length; i++)
            {
                int charType = GetCharType(value[i]);
                if (charType != lastCharType)
                {
                    word = value.Substring(startIndex, i - startIndex);
                    words.Add(word);
                    startIndex = i;
                    lastCharType = charType;
                }
            }

            word = value.Substring(startIndex, value.Length - startIndex);
            words.Add(word);

            return words;
        }

You can override the SplitIntoWords method and introduce any logic for splitting the words according to your custom requirement. Thus, you can achieve "!word" to be considered as a single word.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
SyntaxEditor
Asked by
Fabrice
Top achievements
Rank 2
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or