|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | +using System.Windows.Forms; |
| 5 | + |
| 6 | +namespace TrueRestrictive |
| 7 | +{ |
| 8 | + public partial class Agesoft_Codering : Form |
| 9 | + { |
| 10 | + public Agesoft_Codering() |
| 11 | + { |
| 12 | + InitializeComponent(); |
| 13 | + } |
| 14 | + |
| 15 | + public string DocumentText = ""; |
| 16 | + private void Agesoft_Codering_Load(object sender, EventArgs e) |
| 17 | + { |
| 18 | + CodeBox.Text = DocumentText; |
| 19 | + this.BringToFront(); |
| 20 | + } |
| 21 | + |
| 22 | + private void CodeBox_TextChanged(object sender, EventArgs e) |
| 23 | + { |
| 24 | + |
| 25 | + // getting keywords/functions |
| 26 | + string keywords = @"\b(public|private|protected|partial|static|namespace|class|struct|enum|using|void|string|int|bool|float|double|short|byte|char|const)\b"; |
| 27 | + MatchCollection keywordMatches = Regex.Matches(CodeBox.Text, keywords); |
| 28 | + |
| 29 | + // getting conds |
| 30 | + string conds = @"\b(if|else|switch|case|foreach)\b"; |
| 31 | + MatchCollection condsMatches = Regex.Matches(CodeBox.Text, conds); |
| 32 | + |
| 33 | + // getting types/classes from the text |
| 34 | + string types = @"\b(Console|Regex|Agesoft|Font|Application)\b"; |
| 35 | + MatchCollection typeMatches = Regex.Matches(CodeBox.Text, types); |
| 36 | + |
| 37 | + // getting comments (inline or multiline) |
| 38 | + string comments = @"(\/\/.+?$|\/\*.+?\*\/)"; |
| 39 | + MatchCollection commentMatches = Regex.Matches(CodeBox.Text, comments, RegexOptions.Multiline); |
| 40 | + |
| 41 | + // getting strings |
| 42 | + string strings = "\".+?\""; |
| 43 | + MatchCollection stringMatches = Regex.Matches(CodeBox.Text, strings); |
| 44 | + |
| 45 | + // saving the original caret position + forecolor |
| 46 | + int originalIndex = CodeBox.SelectionStart; |
| 47 | + int originalLength = CodeBox.SelectionLength; |
| 48 | + Color originalColor = Color.White; |
| 49 | + |
| 50 | + // MANDATORY - focuses a label before highlighting (avoids blinking) |
| 51 | + titleLabel.Focus(); |
| 52 | + |
| 53 | + // removes any previous highlighting (so modified words won't remain highlighted) |
| 54 | + CodeBox.SelectionStart = 0; |
| 55 | + CodeBox.SelectionLength = CodeBox.Text.Length; |
| 56 | + CodeBox.SelectionColor = originalColor; |
| 57 | + |
| 58 | + // scanning... |
| 59 | + foreach (Match m in keywordMatches) |
| 60 | + { |
| 61 | + CodeBox.SelectionStart = m.Index; |
| 62 | + CodeBox.SelectionLength = m.Length; |
| 63 | + CodeBox.SelectionColor = Color.FromArgb(86, 156, 214); |
| 64 | + } |
| 65 | + |
| 66 | + foreach (Match m in condsMatches) |
| 67 | + { |
| 68 | + CodeBox.SelectionStart = m.Index; |
| 69 | + CodeBox.SelectionLength = m.Length; |
| 70 | + CodeBox.SelectionColor = Color.FromArgb(197,134,192); |
| 71 | + } |
| 72 | + |
| 73 | + foreach (Match m in typeMatches) |
| 74 | + { |
| 75 | + CodeBox.SelectionStart = m.Index; |
| 76 | + CodeBox.SelectionLength = m.Length; |
| 77 | + CodeBox.SelectionColor = Color.FromArgb(78, 201, 176); |
| 78 | + } |
| 79 | + |
| 80 | + foreach (Match m in commentMatches) |
| 81 | + { |
| 82 | + CodeBox.SelectionStart = m.Index; |
| 83 | + CodeBox.SelectionLength = m.Length; |
| 84 | + CodeBox.SelectionColor = Color.Green; |
| 85 | + } |
| 86 | + |
| 87 | + foreach (Match m in stringMatches) |
| 88 | + { |
| 89 | + CodeBox.SelectionStart = m.Index; |
| 90 | + CodeBox.SelectionLength = m.Length; |
| 91 | + CodeBox.SelectionColor = Color.FromArgb(206,145,120); |
| 92 | + } |
| 93 | + |
| 94 | + // restoring the original colors, for further writing |
| 95 | + CodeBox.SelectionStart = originalIndex; |
| 96 | + CodeBox.SelectionLength = originalLength; |
| 97 | + CodeBox.SelectionColor = originalColor; |
| 98 | + |
| 99 | + // giving back the focus |
| 100 | + CodeBox.Focus(); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments