aduros / ai.vim

Generate and edit text in Neovim using OpenAI and GPT.
ISC License
293 stars 24 forks source link

Fix a bug that occurs when a multi-byte character under the cursor. #14

Closed zzhirong closed 1 year ago

zzhirong commented 1 year ago

The bug can be reproduced by pressing <CTRL-A> when the cursor is positioned on a multi-byte character. For instance, if you input "你好" and then press <CTRL-A>, the request will inevitably fail.

The reason for this issue is that the vim.api.nvim_win_get_cursor(0) or vim.api.nvim_buf_get_mark(1, "<") functions return the column number of the cursor without including the character under it. To include it, one needs to add one to the column number. This approach works well for single-byte characters but not for multi-byte ones, in which case adding one to the column number in order to include an additional byte results in an invalid encoding byte sequence which causes requests to fail. However, this bug has been resolved now.

aduros commented 1 year ago

Looks great, thanks!