Improving the supporting of history #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
本PR改进了原Knife内部原始的撤销、回退的功能。Knife原有的机制是监听文本变化,然后把修改前的文本存到一个栈里面,undo的时候把栈顶的元素把当前的文本替换掉,而且只会在文本变化的时候进行历史记录。这种做法虽然简单粗暴,但是每次有文本修改,就把整一套文本复制一次,这种做法太低效了,如果文本内容特别大,『能耗』就更高了。
我在内部维护一个栈(
CapacityLimitedStack
),用于保存每次所做的修改动作(Action
),这个栈有一个最大的容量限制(historyMaxCapacity
),如果栈满之后继续push
,则新action压栈,旧(位于栈底)的Action会被丢弃。Action
是一个接口,也就意味着压栈的所有的Action都是可定制的。不管是文本的变动,还是Span的增删变化,undo和redo都能完美地被支持。每次undo时,栈顶Action被弹出,他的
undo()
方法会被调用。每次redo时,栈顶Action被弹出,他的
redo()
方法会被调用。注:Knife的
historySize
被更名为maxHistoryCapacity