I have a fix for this in this plugin I just put together GitHub - nothingislost/obsidian-codemirror-options
The option which addresses this issue is called “Style Active Selection” and it behaves like this:
Do note that it relies on the deprecated CodeMirror 5 direct API and will break when WYSIWYG is rolled out. I’ll do what I can to make this work with CM6 once the APIs are available.