Obsidian is overriding my `setCursor` position in one edge case

Hey everyone! I’m building an auto-formatting plugin using the JS Engine in Obsidian and ran into a strange issue where Obsidian seems to override the caret position I’m setting manually with setCursor() — but only in one very specific scenario.


:memo: What’s the goal?

If I type the note title in the note body, it automatically formats that word in bold:

Note title: T
Initial content:

*0123 T 789*

After formatting:

*0123 **T** 789*

My plugin handles formatting and repositions the cursor correctly after the inserted markdown (**), so it ends up right after the second **, before the space that comes before the 7 — all works fine… in most cases.


:red_exclamation_mark:The issue:

In this version (works fine):

*0123 **T** 789*

Also fine (even with an empty line before):

\n
0123 **T** 789

But this one breaks the caret positioning:

\n
*0123 **T** 789*

For some reason, when the text is wrapped in bold and inside another formatting template (like the outer * for italic), Obsidian overrides the cursor I set and moves it back to right after the T, before the closing **.


:magnifying_glass_tilted_left: Debugging

Looking through the runtime behavior, I noticed Obsidian later runs this (from its internal dispatch):

s && t.view.dom.win.setTimeout(() => {
  o === i.selection && r.dispatch({
    selection: de.create(c, o.mainIndex)
  })
})

This forcibly updates the selection — and only in this edge case.
I’ve tried adjusting timing and order of execution, but it still resets the caret incorrectly.


:red_question_mark:Question

Has anyone faced this issue with setCursor() being overridden like this?

Is there any reliable way to “lock” the cursor position or avoid it being reset after setValue() in Obsidian?

I’d really appreciate any tips — this one’s been hard to track down :sweat_smile:

Thanks in advance!

Had to use GPT to write it cause of my poor english :frowning: