Keyword dyeing in the Code Editor, Optimized Solution

Source: Internet
Author: User
CodeKeyword dyeing in the editor
Zhou yinhui, class 2, class 03, software College, University of Electronic Science and Technology

In the Code Editor, although the selectioncolor attribute of RichTextBox can be easily used for keyword dyeing, many details may occur, resulting in poor dyeing effect, for example, the text blinks badly during dyeing. it cannot even be correctly colored, for example, "inty" is dyed black, but the user suddenly inserts the character 'T' after the letter 'T', but cannot dye 'int' into the blue of the keyword. here is the dyeing scheme I implemented with RichTextBox today. The effect is quite good.

1. Flashing during Dyeing:
The color flashes sharply because the number of times the select (INT, INT) function is called (or the number of times the selectionstart attribute is set) is too large.
If you use the following dyeing method: when the text changes, analyze the changed lines of the text. If there are keywords, the keywords are stained. Then your dyeing method will cause serious flashes. If the current row of the text contains many keywords, each character is dyed once in sequence. Each dyeing operation calls select (INT, INT) function (or set the selectionstart attribute ). Of course, this will also lead to incorrect dyeing, because one input may affect two lines of text (for example, when a line break is inserted in a line of text ).
My method is: Reduce the number of dyeing times. When you type the character "yes" in the text basket to obtain the word affected by this typing, if the word is a keyword, then dye it into the color of the keyword (blue ), otherwise, the text will be colored in black ). Because typing at one time affects at most two words, typing at most two dyeing times each time (and no duplicate dyeing is performed );

2. Words affected by typing:
If you are typing a letter, number, or underline, the word at the cursor is the affected word. You only need to determine whether the word is a keyword. If the word is a keyword, then, dye it into the keyword color (blue); otherwise, the plain text color (black ). If you are typing a character other than letters, numbers, or underscores, the affected two words are bounded by this character, for example, inserting a space in the "intint" is the two "int" which is decomposed into spaces. Then, you can judge the two words.
The following code describes the above idea (getcurrentword () is used to obtain the word at the specified position, and dye () is used to dye the specified text) 1 Private   Void Richtextbox_codearea_textchanged ( Object Sender, eventargs E)
2 {
3 RichTextBox RBx = (RichTextBox) sender;
4 Int Start, length;
5 Int INS =   This . Richtextbox_codearea.selectionstart;
6
7 Char C =   '   ' ;
8 String Word =   "" ;
9 If (INS >   0 )
10 {
11 C =   This . Richtextbox_codearea.text [INS -   1 ];
12 }
13 Else
14 {
15 If ( This . Richtextbox_codearea.text.length >   0 )
16 {
17 C =   This . Richtextbox_codearea.text [ 0 ];
18 }
19 }
20
21 // Obtain the two words on both sides of the insert point and determine whether the two words are keywords.
22 If ( ! Char . Isletter (c) &&   ! Char. isdigit (c) && C ! =   ' _ ' )
23 {
24 // Dye character C
25 This . Dye (INS -   1 , 1 , This . Textcolor, This . Textcolor );
26
27 Word =   This . Getcurrentword (INS -   1 , Out Start, Out Length );
28
29 Color Cl = Iskey (word) ?   This . Keycolor: This . Textcolor;
30 This . Dye (START, length, Cl, This . Textcolor );
31
32 Word =   This . Getcurrentword (INS +   1 , Out Start, Out Length );
33 Cl = Iskey (word) ?   This . Keycolor: This . Textcolor;
34 This . Dye (START, length, Cl, This . Textcolor );
35
36 }
37 Else // Obtain the word at the insertion point and determine whether it is a keyword.
38 {
39 Word =   This . Getcurrentword (INS, Out Start, Out Length );
40 Color Cl = Iskey (word) ?   This . Keycolor: This . Textcolor;
41 This . Dye (START, length, Cl, This . Textcolor );
42 }
43
44 }
45

3. Incorrect dyeing:
The reason for dyeing is sometimes incorrect becauseAlgorithmIt cannot cope with special situations. For example, after "INTIF" is dyed black, when a line break is inserted before 'I', it is divided into two lines. At this time, both "int" and "if" should be dyed blue, the "int" is always black. Dyeing according to the method described in "2. About Words affected by typing" will solve this problem.

---------------------------------
Download demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.