Breakthrough: Leo's excellent new undo pattern

26 views
Skip to first unread message

Edward K. Ream

unread,
Nov 13, 2020, 8:13:15 AM11/13/20
to leo-editor
Yesterday marked an important milestone in the never-ending effort to simplify Leo's code base. Leo now has a new pattern that makes implementing undo/redo straightforward.

I won't show the new pattern here, because of the new limitations on syntax coloring in this group. However, the first comment of #1413 shows the pattern. Please take a look. The indent-region and unindent-region commands in the ekr-undo branch show this pattern in actual use.

Discussion

For the first time ever, it is now straightforward to define Leo commands without using either c.updateBodyPane or LeoBody.onBodyChanged. I now see clearly that these methods are evil. They have no easily stated purpose. They create unnecessary linkages between commands that should be entirely self-contained. They are faux helpers.

Thank you Vitalije

None of this would have happened without Vitalije's criticism of Leo's code. His suggestions:

1) Eliminate kwargs where possible.
2) Duplicate code to avoid unwanted dependencies and faux helpers.

Even with these hints firmly in mind, it took several days to see the way forward. Such was the mess that the old undo code was in.

Summary

The new undo pattern is a big milestone for Leo:

- Each command is fully responsible for its actions.
- It's now dead easy to understand what the code is doing.
  - No need to understand faux helpers.
  - Everything is explicit.
- The new u.before/afterChangeBody methods do all the undo/redo grunt work.

The new pattern lengthens the code for each command, but the advantages just stated are overwhelming.

Edward
Reply all
Reply to author
Forward
0 new messages