3 ms·
I added Undo support to WordGrinder after the fact (<plug> http://cowlark.com/wordgrinder/ http://cowlark.com/wordgrinder/ </plug>). Wasn't easy. WordGrinder's
by david-given 10y ago
I added Undo support to WordGrinder after the fact (<plug> http://cowlark.com/wordgrinder/ http://cowlark.com/wordgrinder/ </plug>). Wasn't easy.
WordGrinder's data model is that a document is a Lua table (== array) of paragraphs; each paragraph is a table of words; each word is a string. This means that the data structures are all reasonably small in most common use so I can get away with O(n) insertion within them.
What I did for undo support was that I made paragraphs and words immutable; adding a word to a paragraph actually copied the paragraph, with the new one referring to the same words that were in the old one, plus the new word. Again, all quite cheap because the structures were small. But this means that I can now copy the entire document for each level in the undo stack, because I since paragraphs are immutable I can share them across multiple levels in the undo stack.
Surprisingly, it worked.
The tl;dr here is: immutable data structures are awesome, and I should totally have designed in undo right at the beginning.