void DetectAndSaveChange(string new_text, int new_sel_start, int new_sel_length) { if (m_PrevText != null) { // We are doing brutalforce change detection as we will work with at most a few hundred kilobytes of text in this project. int max_len = Math.Min(m_PrevText.Length, new_text.Length); // The Math.Min(m_PrevSelStart, max_len) is there to fix a bug that detected wrongly the actual location of // multiple pasted lines. If we pasted lines that are equivalent to the lines starting at the actual paste // location then this change detector found our newly pasted text as unchanged text and detected the next // block as the change. This caused problems especially in case of the optimized syntax highlighter. int common_prefix_len = FindCommonPrefixLength(m_PrevText, new_text, Math.Min(m_PrevSelStart, max_len)); if (m_PrevText.Length == new_text.Length && new_text.Length == common_prefix_len) { return; } int common_postfix_len = common_prefix_len >= max_len ? 0 : FindCommonPostfixLen(m_PrevText, new_text, max_len - common_prefix_len); UndoEntry entry = new UndoEntry(); entry.Id = m_NextUndoEntryId++; entry.PrevCaretPos = m_PrevSelStart + m_PrevSelLength; entry.NewCaretPos = new_sel_start + new_sel_length; entry.Pos = common_prefix_len; entry.CutText = m_PrevText.Substring(common_prefix_len, m_PrevText.Length - common_prefix_len - common_postfix_len); entry.PastedText = new_text.Substring(common_prefix_len, new_text.Length - common_prefix_len - common_postfix_len); m_UndoBuffer.AddChange(entry); if (UndoEntryAdded != null) { UndoEntryAdded(entry); } } m_PrevText = new_text; m_PrevSelStart = new_sel_start; m_PrevSelLength = new_sel_length; }