WPF RichTextBox TextChanged事件-如何查找已删除或已插入的文本? [英] WPF RichTextBox TextChanged event - how to find deleted or inserted text?

查看:89
本文介绍了WPF RichTextBox TextChanged事件-如何查找已删除或已插入的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用RichTextBox创建自定义编辑器时,我遇到的问题是使用TextChanged事件查找带有已提供信息的已删除/插入的文本。

While creating a customized editor with RichTextBox, I've face the problem of finding deleted/inserted text with the provided information with TextChanged event.

< href = http://msdn.microsoft.com/zh-cn/library/system.windows.controls.textchangedeventargs_members.aspx rel = nofollow noreferrer> TextChangedEventArgs 有一些有用的数据,但我想它不能满足所有需求。假设插入了多个段落,并且同时删除了所选文本(其本身跨越了多个段落)。

The instance of TextChangedEventArgs has some useful data, but I guess it does not cover all the needs. Suppose a scenario which multiple paragraphs are inserted, and at the same time, the selected text (which itself spanned multiple paragraphs) has been deleted.

对于TextChangedEventArgs实例,您有一组文本更改,并且每个更改仅为您提供已删除或添加的符号的数量及其位置。

With the instance of TextChangedEventArgs, you have a collection of text changes, and each change only provides you with the number of removed or added symbols and the position of it.

我想到的唯一解决方案是,保留一份文档副本,并在文档上应用给定的更改列表。但是由于TextChange的实例仅提供了插入/删除的符号(而不是符号)的数量,因此,当我们转换的原始副本时,我们需要放置一些特殊符号(例如?)来表示未知符号

The only solution I have in mind is, to keep a copy of document, and apply the given list of changes on it. But as the instances of TextChange only give us the number of inserted/removed symbols (and not the symbols), so we need to put some special symbol (for example, '?') to denote unknown symbols while we transform our original copy of document.

将所有更改应用到文档的原始副本之后,我们可以将其与Richtextbox的更新文档进行比较,并找到未知符号和真实符号之间的映射。最后,得到我们想要的东西!!!

After applying all changes to the original copy of document, we can then compare it with the richtextbox's updated document and find the mappings between unknown symbols and the real ones. And finally, get what we want !!!

有人尝试过吗?我需要您对整个策略以及您对该方法的看法提出建议。

Anybody has tried this before? I need your suggestions on the whole strategy, and what you think about this approach.

问候

推荐答案

这主要取决于您使用的文本更改。当序列同时包含插入和删除操作时,理论上不可能知道每个插入操作的详细信息,因为插入的某些符号可能随后已被删除。因此,您必须选择真正想要的结果:

It primarily depends on your use of the text changes. When the sequence includes both inserts and deletes it is theoretically impossible to know the details of each insert, since some of the symbols inserted may have subsequently been deleted. Therefore you have to choose what results you really want:


  • 出于某些目的,您必须知道确切的顺序

  • 出于其他目的,您必须确切知道新文本与旧文本有何不同,但不能确切知道其中的确切顺序

我将使用技术来实现每个结果。我过去曾经使用过这两种技术,所以我知道它们是有效的。

I will techniques to achieve each of these results. I have used both techniques in the past, so I know they are effective.

获取确切的序列

如果您要实现历史记录或撤消日志或搜索特定操作,则更合适。

This is more appropriate if you are implementing a history or undo log or searching for specific actions.

对于这些用途,您描述的过程可能是最好的选择,并且可能会有一个变化:与其在未知符号和真实符号之间查找映射,不如简单地向前运行扫描以找到每个删除的文本,然后向后运行以查找每个删除的文本。插入。

For these uses, the process you describe is probably best, with one possible change: Instead of "finding the mappings between the unknown symbols and the real ones", simply run the scan forward to find the text of each "Delete" then run it backward to find the text of each "Insert".

换句话说:


  1. 从初始文本并按顺序处理更改。对于每次插入,请插入?符号。对于每个删除,删除指定数量的符号并将它们记录为删除的文本。

  1. Start with the initial text and process the changes in order. For each insert, insert '?' symbols. For each delete, remove the specified number of symbols and record them as the text deleted.

从最终文本开始,以相反的顺序处理更改。对于每个删除,插入?符号。对于每个插入,删除指定数量的符号并将其记录为插入的文本。

Start with the final text and process the changes in reverse order. For each delete, insert '?' symbols. For each insert, remove the specified number of symbols and record them as the text inserted.

完成此操作后,就我们所知,所有插入和删除更改条目都将具有关联的文本,并且任何插入并立即删除的文本均为'?'符号。

When this is complete, all of your "Insert" and "Delete" change entries will have the associated text to the best of our knowledge, and any text that was inserted and immediately deleted will be '?' symbols.

获得差异

这更适合于版本标记或版本比较。

This is more appropriate for revision marking or version comparison.

对于这些用途,只需使用文本更改信息来计算一组可能在其中找到更改的整数范围,然后使用标准的diff算法查找实际更改。在处理增量更改时,这往往非常有效,但仍可以为您提供最佳的更新。

For these uses, simply use the text change information to compute a set of integer ranges in which changes might be found, then use a standard diff algorithm to find the actual changes. This tends to be very efficient in processing incremental changes but still gives you the best updates.

当您粘贴与原始的:使用文本更改信息将指示整个段落是新的,但是使用diff(即此技术)将仅标记那些实际上不同的符号行。

This is particularly nice when you paste in a replacement paragraph that is almost identical to the original: Using the text change information will indicate the whole paragraph is new, but using diff (ie. this technique) will mark only those symbol runs that are actually different.

计算更改范围的代码很简单:将更改表示为四个整数(oldstart,oldend,newstart,newend)。运行每个更改:

The code for computing the change range is simple: Represent the change as four integers (oldstart, oldend, newstart, newend). Run through each change:


  1. 如果changestart在newstart之前,则将newstart减少为changestart并减少oldstart相等的金额

  2. 如果changeend在newend之后,则将newend增加到changeend,并增加oldend等量

完成此操作后,提取范围
比较旧文档中的[oldstart,oldend]和新文档中的范围[newstart,newend],然后使用标准的diff算法进行比较。

Once this is done, extract range [oldstart, oldend] from the old document and the range [newstart, newend] from the new document, then use the standard diff algorithm to compare them.

这篇关于WPF RichTextBox TextChanged事件-如何查找已删除或已插入的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆