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

Frequently encountering errors when accepting suggestions. #339

Open
WYJRichhhhh opened this issue Nov 14, 2024 · 1 comment
Open

Frequently encountering errors when accepting suggestions. #339

WYJRichhhhh opened this issue Nov 14, 2024 · 1 comment

Comments

@WYJRichhhhh
Copy link

WYJRichhhhh commented Nov 14, 2024

Sometimes it works well, but other times it frequently throws errors. Even within the same buffer, it’s available on some lines but unavailable on others.

The following is the output of the messages command.

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>

It seems like the issue is caused by inconsistencies between the content in the buffer and the plugin. Could you provide some suggestions to help me resolve this problem?

I added some debugging code, as shown below:

  vim.schedule_wrap(function()
    -- 调试:打印 range 和 newText 变量
    print("range:", vim.inspect(range))
    print("newText:", vim.inspect(newText))

    -- 调试:检查缓冲区中的行数是否足够
    local buf = vim.api.nvim_get_current_buf()
    local line_count = vim.api.nvim_buf_line_count(buf)
    print("Current buffer line count:", line_count)

    -- 获取目标行的字符数
    local target_line = vim.api.nvim_buf_get_lines(buf, range.start.line, range.start.line + 1, false)[1]
    print("Target line:", target_line)
    local target_line_length = #target_line
    print("Target line lenght:", target_line)

    -- 调整 range 的结束字符位置,确保不超出字符数
    if range["end"].character > target_line_length then
      print("Warning: Adjusting end character to fit within line length")
      range["end"].character = target_line_length
    end

    -- 调试:尝试应用 text edit 并捕获错误
    local success, err = pcall(function()
      vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Space><Left><Del>", true, false, true), "n", false)
      vim.lsp.util.apply_text_edits({ { range = range, newText = newText } }, buf, "utf-16")
    end)

    if not success then
      print("Error applying text edits:", err)
      return
    end
    -- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Space><Left><Del>", true, false, true), "n", false)
    -- vim.lsp.util.apply_text_edits({ { range = range, newText = newText } }, vim.api.nvim_get_current_buf(), "utf-16")
    -- Put cursor at the end of current line.
    local cursor_keys = "<End>"
    if has_nvim_0_10_x then
      cursor_keys = string.rep("<Down>", #vim.split(newText, "\n", { plain = true }) - 1) .. cursor_keys
    end
    vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(cursor_keys, true, false, true), "n", false)
  end)()
end

the messages as shown below:


range: {
  ["end"] = {
    character = 65,
    line = 163
  },
  start = {
    character = 0,
    line = 163
  }
}
newText: "-- 用户输入代码或命令(Read),即时执行(Eval), 并输出结果(Print), 然后等待用户输入下一条命令(Loop)"
Current buffer line count: 245
Target line: -- 用户输入代码或命令(Read),即时执行(Eval), 
Target line length: 65
Error applying text edits: .../neovim/0.10.2_1/share/nvim/runtime/lua/vim/lsp/util.lua:485: index out of range

@WYJRichhhhh
Copy link
Author

WYJRichhhhh commented Nov 14, 2024

When I changed the character encoding from UTF-16 to UTF-8(source code,not editor), it started working correctly. I suspect that this issue might be due to different strategies in handling character boundaries with various encodings in nvim’s LSP.

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

1 participant