Test and fix for #2464

This is a possible fix for the issue detailed in #2464. It also includes the test for it.

This feels more like a workaround than a real fix,  but may be good enough if codemirror token behavior is not to be changed.