Created
August 16, 2016 06:28
-
-
Save zhenlinyang/dcb7431ebe87fc6fdaf5b5ccf4162165 to your computer and use it in GitHub Desktop.
Windows Forms RichTextBox Change Code Color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal static class ProtoChangeColorHandler | |
| { | |
| private static readonly string[] c_KeyWords = | |
| { | |
| "default", "enum", "message", "import", "group", "package", | |
| "extend", "extensions", "to", "max", | |
| "service", "rpc", "returns", | |
| "true", "false", | |
| "required", "optional", "repeated", | |
| "double", "float", "int32", "int64", "uint32", "uint64", | |
| "sint32", "sint64", "fixed32", "fixed64", "sfixed32", | |
| "sfixed64", "bool", "string", "bytes", | |
| "option", "packed", "deprecated", "optional" | |
| }; | |
| internal static void ProtoChangeColor(this RichTextBox richTextBox) | |
| { | |
| richTextBox.SelectAll(); | |
| richTextBox.SelectionColor = Color.Black; | |
| richTextBox.SelectionFont = richTextBox.Font; | |
| for (int i = 0; i < c_KeyWords.Length; i++) | |
| { | |
| ChangeKeyWordsColor(richTextBox, c_KeyWords[i]); | |
| } | |
| ChangeAnnotationColor(richTextBox); | |
| richTextBox.SelectionStart = richTextBox.TextLength; | |
| richTextBox.Focus(); | |
| richTextBox.SelectionColor = Color.Black; | |
| richTextBox.SelectionFont = richTextBox.Font; | |
| } | |
| private static void ChangeKeyWordsColor(RichTextBox richTextBox, string keyword) | |
| { | |
| int searchIndex = 0; | |
| while (true) | |
| { | |
| string text = richTextBox.Text; | |
| int index = richTextBox.Text.IndexOf(keyword, searchIndex); | |
| if (index != -1) | |
| { | |
| if (index > 0) | |
| { | |
| if (CheckContextLink(text[index - 1])) | |
| { | |
| searchIndex = index + keyword.Length; | |
| continue; | |
| } | |
| } | |
| if (index + keyword.Length < text.Length) | |
| { | |
| if (CheckContextLink(text[index + keyword.Length])) | |
| { | |
| searchIndex = index + keyword.Length; | |
| continue; | |
| } | |
| } | |
| richTextBox.Select(index, keyword.Length); | |
| richTextBox.SelectionColor = Color.Blue; | |
| searchIndex = index + keyword.Length; | |
| } | |
| else | |
| { | |
| break; | |
| } | |
| } | |
| } | |
| private static bool CheckContextLink(char c) | |
| { | |
| return c >= 'A' && c <= 'Z' || | |
| c >= 'a' && c <= 'z' || | |
| c >= '0' && c <= '9' || | |
| c == '_'; | |
| } | |
| private static void ChangeAnnotationColor(RichTextBox richTextBox) | |
| { | |
| int searchIndex = 0; | |
| for (int lineId = 0; lineId < richTextBox.Lines.Length; lineId++) | |
| { | |
| int index = richTextBox.Lines[lineId].IndexOf("//", 0); | |
| if (index != -1) | |
| { | |
| richTextBox.Select(searchIndex + index, richTextBox.Lines[lineId].Length - index); | |
| richTextBox.SelectionColor = Color.Green; | |
| } | |
| searchIndex += richTextBox.Lines[lineId].Length + 1; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment