[vim/vim] helptoc: FuzzySearch() highlights matched characters at wrong column for multibyte text (Issue #21056)

VimWei (Vim Github Repository) <[email protected]>
Newsgroups gmane.editors.vim.devel
Message-ID <vim/vim/issues/[email protected]>
VimWei created an issue (vim/vim#21056)

### Steps to reproduce

1. Create a markdown file with a multibyte heading, e.g. a single line # 中文标题
2. :set filetype=markdown
3. :colorscheme default — the fuzzy-match highlight uses the IncSearch group; some colorschemes make it indistinguishable from the surrounding text, so a default colorscheme makes the effect visible
4. :packadd helptoc | HelpToc
5. Press / and type 标题

**Actual behaviour**

The highlight lands on the wrong column — it overlaps a neighboring character and splits a multibyte character (only the first byte of the 3-byte UTF-8 char is highlighted).

### Expected behaviour

Each matched character 标 and 题 is highlighted (via the help-fuzzy-toc text property, IncSearch).

**Cause**

matchfuzzypos() returns match positions as character indices (:h matchfuzzypos(): "list of character positions ... You can use byteidx() to convert a character position to a byte position"). But text property col/length are in bytes (:h prop_add(), :h popup-props: "counted in bytes").

In runtime/pack/dist/opt/helptoc/autoload/helptoc.vim, FuzzySearch():

```viml
props: pos[i]->copy()->map((_, col: number) => ({
    col: col + 1,
    length: 1,
    type: 'help-fuzzy-toc',
}))
```
col + 1 treats a character index as a byte column, and length: 1 is one byte instead of the byte length of the matched character. This is only correct for pure-ASCII text (where character index equals byte index), which is why it goes unnoticed in English help files.

**Suggested fix**

Convert character indices to byte offsets with byteidx() and use each matched character's byte length:

```viml
props: pos[i]->copy()->map((_, cp: number) => {
    const col = byteidx(match.text, cp) + 1
    const len = byteidx(match.text, cp + 1) - byteidx(match.text, cp)
    return {col: col, length: len, type: 'help-fuzzy-toc'}
})
```

### Version of Vim

9.2.0907

### Environment

- Windows 10 
- gvim 9.2.0907  (also present in 9.1)

### Logs and stack traces

```shell

```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/vim/vim/issues/21056
You are receiving this because you are subscribed to this thread.

Message ID: <vim/vim/issues/[email protected]>

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/21056%40github.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.