private void hightlightLine(List <List <Replacement> > replacements, ref Place place, int lineNumber, List <TokenData.TokenDictionaryEntry> line, string programTextBoxLine, int trimmedOffset) { StringFlag = false; int lastPrepocCount = 0; foreach (var entry in line) { if (lastPrepocCount > 0) { lastPrepocCount--; continue; } if (entry.StringTerminator && StringFlag) { StringFlag = false; } else if (entry.StringStarter) { StringFlag = true; } string token = entry.Name; if (place.iChar < programTextBoxLine.Length && programTextBoxLine[place.iChar] == '\\') { if (place.iChar != programTextBoxLine.Length) { place = place + 1; } } string lineText = programTextBoxLine.ClippedSubstring(place.iChar, token.Length); Replacement replacement = null; if (lineNumber < replacements.Count) { List <Replacement> lineReplacements = replacements[lineNumber]; int charLocation = place.iChar; replacement = lineReplacements.FirstOrDefault(r => r.Location - trimmedOffset == charLocation); } if (replacement != null) { token = replacement.OldValue; TokenData.Tokenize(replacement.NewValue, out lastPrepocCount); lastPrepocCount--; } else if (lineText != token) { foreach (string alt in entry.Alts) { lineText = programTextBoxLine.ClippedSubstring(place.iChar, alt.Length); if (lineText == alt) { if (!PrettyPrint) { token = alt; } else { handlePrettyPrint(place, lineNumber, programTextBoxLine, trimmedOffset, token, alt); } break; } } } int offset; if (place.iChar != 0 && place.iChar < programTextBoxLine.Length && programTextBoxLine[place.iChar - 1] == '\\') { offset = -1; } else { offset = 0; } Range curRange = ProgramTextBox.GetRange(place + offset + trimmedOffset, place + token.Length + trimmedOffset); curRange.ClearStyle(styles.Values.ToArray()); string styleKey; if (StringFlag || entry.StringStarter) { styleKey = "String"; if (entry.StyleType == "Error") { styleKey = "Error" + styleKey; } } else { styleKey = entry.StyleType ?? "Default"; } TokenStyle style = styles[styleKey]; curRange.SetStyle(style ?? styles["Default"]); place += token.Length; } }
private void UpdateHighlight(List <List <TokenData.TokenDictionaryEntry> > tokens, Range range, List <List <Replacement> > replacements) { if (!LiveUpdate) { return; } int ifCount = 0; var ifFlag = new Stack <bool>(); // Do all the preproc up to this point? Dictionary <string, string> directives = new Dictionary <string, string>(); for (int i = 0; i < ProgramTextBox.LinesCount; i++) { HandlePreproc(ProgramTextBox.Lines[i].TrimStart(), directives, ref ifCount, ifFlag, false); //PreProcForward = PreProcForward.OrderByDescending(v => v.Key.Length).ToDictionary(v => v.Key, v => v.Value); } #if DEBUG List <AutocompleteItem> items = new List <AutocompleteItem>(); items.AddRange(autoCompleteItems); foreach (var d in directives) { items.Add(new AutocompleteItem() { Text = d.Key, ToolTipText = d.Value }); } autoCompleteMenu.Items.SetAutocompleteItems(items); #endif Place place = new Place(0, range.Start.iLine); for (int i = range.Start.iLine; i <= range.End.iLine; i++) { if (i < 0 || i > ProgramTextBox.Lines.Count - 1) { continue; } var line = tokens[i]; string programTextBoxLine = ProgramTextBox.Lines[i].TrimStart(); //TokenData.TrimStart ? ProgramTextBox.Lines[i].TrimStart() : ProgramTextBox.Lines[i]; int trimmedOffset = ProgramTextBox.Lines[i].Length - programTextBoxLine.Length; //string tokenizedLine = string.Join("", tokens[i].Select(t => t.Name)); if (ifFlag.Count > 0 && !ifFlag.Peek()) { Range curRange = ProgramTextBox.GetRange(place, place + ProgramTextBox.Lines[i].Length); curRange.ClearStyle(styles.Values.ToArray()); curRange.SetStyle(styles["Disabled"] ?? styles["Default"]); } else if (programTextBoxLine.StartsWith(CommentString)) { Range curRange = ProgramTextBox.GetRange(place, place + ProgramTextBox.Lines[i].Length); curRange.ClearStyle(styles.Values.ToArray()); curRange.SetStyle(styles["Comment"] ?? styles["Default"]); } else { hightlightLine(replacements, ref place, i, line, programTextBoxLine, trimmedOffset); } place.iLine++; place.iChar = 0; } //ProgramTextBox.Invalidate(); }