The Undo function of the text editor has some very unexpected behaviour - and in some cases quite critical behaviour. It's a bit difficult to describe the expected behaviour exactly, but I have two examples. Enter "abc", delete the "b", Undo - I would expect the "b" to reappear, but instead the line is removed. Another, and more critical, example: make a change on one line, make a change on another line, Undo - I would expect only the last change to be undone, but instead both changes are undone - this way it's very likely to undo a change without noticing.
This is a known problem.
The issue you are hinting at is the granularity of a change. You mention that in your last case, both changes are undone. In reality, only the last change is undone, however the last change covers both lines. If you continue experimenting, you will notice, that all changes in a text-box until that text-box looses focus, are considered to be one change. The undo mechanism was really designed to work on the automaton level, and the behavior is really inappropriate for the declaration sections.
The planned fix for this was to use a timer to commit changes in a declaration second every second or so. Maybe it is time to finally make this change :-)
At least a partial fix has been committed to the 4.0 branch in rev. 3065. The text in a text area is now committed to the underlying model representation after at most one second. This will limit how much is undone at a time.