Fixed: cursor and stray space when numbering a freshly-created empty heading / 修复:给刚生成的空标题编号时的光标错位与多余空格
English
A small but annoying bug: turn an empty line into a heading (e.g. with a heading shortcut) and wait for the plugin to number it — the cursor used to land before the number instead of right after it, so the next character you typed got inserted into the middle of the number. On top of that, the line ended up with two trailing spaces instead of one.
Both symptoms shared the same root cause — this only happens when the heading's title is still empty at the moment numbering is written:
- The duplicate space came from the logic that preserves a trailing space you just typed. It checks whether the newly numbered line "already ends with" that space, but the check was tripped up by an invisible marker character the plugin appends right after the number when there's no title text yet, so it wrongly concluded the space was missing and added a second one.
- The misplaced cursor came from how the number gets inserted: when the line is empty, the insertion point and your cursor sit at the exact same spot, and the editor's default behavior in that case is to leave the cursor before the inserted text rather than after it.
Both are fixed. Numbering a fresh, empty heading now leaves exactly one trailing space, with the cursor sitting right after it — ready for you to type the title. Headings that already have title text are unaffected.
中文
一个不大但很碍事的 bug:把一个空行转成标题(比如用标题快捷键)、等插件写入编号后,光标本该落在编号后面,却卡在了编号前面——接着打的字直接插进了编号中间。同时行尾还会多出一个空格,变成两个。
两个症状同源——只在编号写入那一刻标题正文仍为空时才会发生:
- 多出的空格来自"保住你刚敲的行尾空格"这段逻辑:它靠判断新写入的这一行"是否已经带着"那个空格来决定要不要补,但标题为空时插件会在编号后面加一个不可见的标记字符,这个判断被它挡住、误以为空格没了,于是又补了一份。
- 光标错位则是因为:标题为空时,编号的插入点和你的光标恰好落在同一个坐标上,而编辑器在这种情况下的默认行为是把光标留在插入内容之前,而不是之后。
两处都已修复。现在给刚生成的空标题编号,行尾只会有一个空格,光标也会准确落在它后面,可以直接接着打标题。已有标题正文的行不受影响。