Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Index out of range when trying to accept suggestions inside comments #344

Open
cgimenes opened this issue Nov 25, 2024 · 6 comments

Comments

@cgimenes
Copy link

Copilot.lua version: f8d8d87
Neovim: 0.10.2

Error:

Error  19:46:32 msg_show.lua_error Error executing vim.schedule lua callback: .../neovim/0.10.2_1/share/nvim/runtime/lua/vim/lsp/util.lua:485: index out of range
stack traceback:
	[C]: in function 'get_line_byte_from_position'
	.../neovim/0.10.2_1/share/nvim/runtime/lua/vim/lsp/util.lua:485: in function 'apply_text_edits'
	...l/share/nvim/lazy/copilot.lua/lua/copilot/suggestion.lua:489: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>
@xebecnan
Copy link

xebecnan commented Dec 2, 2024

I encountered the same error, which in my case appears randomly. It seems that the occurrence of this error is not related to whether I attempt to accept suggestions within or outside of comments.

from example, when I am editing a python file on this line:

df = pd.read_excel(FILE_NAME, sheet_name='中文字符').d

and try to accept the suggestion of "dropna(how='all')" using <M-l> key, and this error occured. However, if I accept by word, things goes well.

update: it seems related to the Chinese character I use in the line. When replace them to English characters, the error disappeared.

btw, I found the offset_encoding argument passed to vim.lsp.util.apply_text_edits() is 'utf-16', but my actual encoding is 'utf-8', not sure is this related to the problem.

@xebecnan
Copy link

xebecnan commented Dec 2, 2024

I changed the encoding from 'utf-16' to 'utf-8' at line 489 in lua/copilot/suggestion.lua, and it seems to work well now. This change likely resolves the issue. I'm not sure if there is a way to get the actual encoding used in the buffer, I will try to investigate this further.

@xebecnan
Copy link

xebecnan commented Dec 2, 2024

This works for me. I'll try to send a pull requset :)

change line 489 of lua/copilot/suggestion.lua from

    vim.lsp.util.apply_text_edits({ { range = range, newText = newText } }, vim.api.nvim_get_current_buf(), "utf-16")

to:

    local encoding = vim.api.nvim_buf_get_option(0, "fileencoding") or 'utf-8'
    vim.lsp.util.apply_text_edits({ { range = range, newText = newText } }, vim.api.nvim_get_current_buf(), encoding)

@xebecnan
Copy link

xebecnan commented Dec 2, 2024

the doc says nvim_buf_get_option is deprecated, so I changed to nvim_get_option_value instead:

    local encoding = vim.api.nvim_get_option_value("fileencoding", {}) or 'utf-8'
    vim.lsp.util.apply_text_edits({ { range = range, newText = newText } }, vim.api.nvim_get_current_buf(), encoding)

@xebecnan
Copy link

xebecnan commented Dec 2, 2024

just found this problem is already solved in #336 >_<

@Syu-fu
Copy link
Contributor

Syu-fu commented Dec 15, 2024

I have encountered a similar problem where an error occurs when the `fileencoding' buffer is empty. This typically happens when opening a new, unsaved file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants