We’re relying on the macOS standard text view behavior here. I’m not sure what its calculations are based on, I assume it may have something to do with time and/or significant application events.
If you fire up TextEdit you should be seeing the same behavior. (though you may need to modify the text system preferences to disable the substitutions, as that seems to create undo points).