private void UpdateTextColor(FilterName[] filterNames) { if(parser == null) { parser = new Parser(new ExpressionFilter()); } else { parser.FieldFilters.Clear(); } int position = ExpressionText.SelectionStart; ExpressionText.HideSelection = true; ExpressionText.SuspendLayout(); // add filter names Dictionary<string, FilterName> filterDictionary = new Dictionary<string, FilterName>(); for(int i = 0; i < filterNames.Length; i++) { parser.FieldFilters.Add(filterNames[i].Name); if(filterDictionary.ContainsKey(filterNames[i].Name) == false) { filterDictionary.Add(filterNames[i].Name, filterNames[i]); } } parser.Text = ExpressionText.Text + " "; parser.InitParsing(); TextBlock? block = parser.GetNextBlock(); StringBuilder builder = new StringBuilder(); StringBuilder textBuilder = new StringBuilder(ExpressionText.Text); int diff = 0; builder.Append("{\\rtf1\\ansi\\deff0"); // color table // // 1 2 3 4 5 // colors: black, blue, red, dark violet, gray builder.Append("{\\colortbl;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red255\\green0\\blue0;\\red148\\green0\\blue211;\\red128\\green128\\blue128;}"); // parse the text while(block.HasValue) { TextBlock b = block.Value; if(b.Type == TextBlockType.Keyword) { /*ExpressionText.SelectionStart = b.StartIndex; ExpressionText.SelectionLength = b.EndIndex - b.StartIndex + 1; ExpressionText.SelectionColor = Color.DarkViolet;*/ if(b.EndIndex - b.StartIndex > 0) { textBuilder.Replace(b.Text, "\\highlight0\\cf4" + b.Text, diff + b.StartIndex, b.EndIndex - b.StartIndex); diff += 15; } } else if(b.Type == TextBlockType.Field) { int color = 5; /*ExpressionText.SelectionStart = b.StartIndex; ExpressionText.SelectionLength = b.EndIndex - b.StartIndex; */ // set color if(filterDictionary.ContainsKey(b.Text) == false || (filterDictionary.ContainsKey(b.Text) && filterDictionary[b.Text].Enabled == false)) { //ExpressionText.SelectionColor = Color.Gray; color = 5; } else { //ExpressionText.SelectionColor = Color.Blue; color = 2; } // append if(b.EndIndex - b.StartIndex > 0) { textBuilder.Replace(b.Text, "\\highlight0\\cf" + color.ToString() + b.Text, diff + b.StartIndex, b.EndIndex - b.StartIndex); diff += 15; } } else if(ContainsParantesis(b.Text) == false && ContainsName(b.Text, filterNames) == false) { /*// mark as error ExpressionText.SelectionStart = b.StartIndex; ExpressionText.SelectionLength = b.EndIndex - b.StartIndex; ExpressionText.SelectionBackColor = Color.LightCoral;*/ if(b.EndIndex - b.StartIndex > 0) { textBuilder.Replace(b.Text, "\\highlight0\\cf3" + b.Text, diff + b.StartIndex, b.EndIndex - b.StartIndex); diff += 15; } } else { // unknown if(b.EndIndex - b.StartIndex > 0) { textBuilder.Replace(b.Text, "\\highlight0\\cf1" + b.Text, diff + b.StartIndex, b.EndIndex - b.StartIndex); diff += 15; } } block = parser.GetNextBlock(); } updatingColor = true; builder.Append(textBuilder.ToString()); ExpressionText.Rtf = builder.ToString(); ExpressionText.SelectionStart = position; ExpressionText.SelectionLength = 0; updatingColor = false; }
private bool ContainsName(string text, FilterName[] filterNames) { for(int i = 0; i < filterNames.Length; i++) { if(filterNames[i].Name.StartsWith(text)) { return true; } } return false; }