jackMort / ChatGPT.nvim

ChatGPT Neovim Plugin: Effortless Natural Language Generation with OpenAI's ChatGPT API
Apache License 2.0
3.68k stars 310 forks source link

Can't use ChatGPTRun, nvim_buf_set_text out of range #155

Closed gsuuon closed 1 year ago

gsuuon commented 1 year ago

ChatGPTRun with strategy: replace throws

Error executing vim.schedule lua callback: ...art\ChatGPT.nvim/lua/chatgpt/flows/actions/chat/init.lua:144: Invalid 'start_col': out of range
stack traceback:
        [C]: in function 'nvim_buf_set_text'
        ...art\ChatGPT.nvim/lua/chatgpt/flows/actions/chat/init.lua:144: in function <...art\ChatGPT.nvim/lua/chatgpt/flows/actions/chat/init.lua:78>

I guess this is caused by self:get_visual_selection() returning 1-indexed rows/cols but nvim_buf_set_text takes 0-indexed positions?

Doing something like this fixes it (edited to remove end_col - 1)

-      vim.api.nvim_buf_set_text(bufnr, start_row, start_col, end_row, end_col, lines)
+      vim.api.nvim_buf_set_text(bufnr, start_row - 1, start_col - 1, end_row - 1, end_col, lines)

Version: NVIM v0.9.0-dev-1230+gd1e0f7454 ChatGPT.nvim: b5f35e4

zanemcca commented 1 year ago

I am also experiencing this and I can confirm that the above patch works for me locally.

gsuuon commented 1 year ago

@jackMort not sure if you noticed my edit, end - 1 was cutting off the last char IIRC since it's end-exclusive