filipdutescu / renamer.nvim

VS Code-like renaming UI for Neovim, writen in Lua.
Apache License 2.0
302 stars 11 forks source link

[Bug] offset_encoding error when renaming #117

Closed sQVe closed 2 years ago

sQVe commented 2 years ago

Describe the bug

Error when trying to rename a variable.

To reproduce

Install latest neovim and renamer, try to rename a variable.

Expected behavior

Variable should be renamed.

Environment

Error output

apply_workspace_edit must be called with valid offset encoding                                                                                                                       
Error executing vim.schedule lua callback: /usr/share/nvim/runtime/lua/vim/lsp/util.lua:366: offset_encoding: expected string, got nil                                               
stack traceback:                                                                                                                                                                     
        [C]: in function 'error'                                                                                                                                                     
        vim/shared.lua: in function 'validate'                                                                                                                                       
        /usr/share/nvim/runtime/lua/vim/lsp/util.lua:366: in function 'apply_text_edits'                                                                                             
        /usr/share/nvim/runtime/lua/vim/lsp/util.lua:778: in function 'apply_workspace_edit'                                                                                         
        ...site/pack/packer/start/renamer.nvim/lua/renamer/init.lua:486: in function '_apply_workspace_edit'                                                                         
        ...site/pack/packer/start/renamer.nvim/lua/renamer/init.lua:392: in function 'handler'                                                                                       
        /usr/share/nvim/runtime/lua/vim/lsp.lua:1025: in function ''                                                                                                                 
        vim.lua: in function <vim.lua:0>                                                                                                                                             
Press ENTER or type command to continue
filipdutescu commented 2 years ago

Thanks for reporting this issue! There is one small problem with this bug report however. There is insufficient data for me to go about. Please provide info regarding the LSP server used and the code you had when trying to rename.

sQVe commented 2 years ago

@filipdutescu Ah, of course. I was using the tsserver LSP. I cannot share the code but it is very likely reproducible on any typescript code. I'll check and see soon and give you an update.

sQVe commented 2 years ago

I can replicate it with a typescript file with the following content:

const fn = () => true

console.log(fn)

Just try to rename fn. Running :lua vim.lsp.buf.rename() works as expected.

anuvyklack commented 2 years ago

I got the same error:

apply_workspace_edit must be called with valid offset encoding
Error executing vim.schedule lua callback: /usr/share/nvim/runtime/lua/vim/lsp/util.lua:366: offset_encoding: expected string, got nil
stack traceback:
        [C]: in function 'error'
        vim/shared.lua: in function 'validate'
        /usr/share/nvim/runtime/lua/vim/lsp/util.lua:366: in function 'apply_text_edits'
        /usr/share/nvim/runtime/lua/vim/lsp/util.lua:778: in function 'apply_workspace_edit'
        ...nvim/site/pack/packer/start/renamer/lua/renamer/init.lua:486: in function '_apply_workspace_edit'
        ...nvim/site/pack/packer/start/renamer/lua/renamer/init.lua:392: in function 'handler'
        /usr/share/nvim/runtime/lua/vim/lsp.lua:1025: in function ''
        vim.lua: in function <vim.lua:0>

I experienced it with sumneko-lua 2.5.6

The minimum file to reproduce:

local M = {}
return M

Try to rename M. And for me :lua vim.lsp.buf.rename() also works as expected

filipdutescu commented 2 years ago

@anuvyklack what Neovim version are you using? I suspect this is a v0.7 issue. I use stable so I was not impacted yet at least.

filipdutescu commented 2 years ago

If it is from changes made for v0.7, it might be worth going through with GH-102 :thinking:

datwaft commented 2 years ago

I tried to make a PR to fix this, as the fix seems easy, but I could not find how to extract the offset_encoding in your codebase.

The recommended solution is extracting it via ctx.client_id, but no idea how to extract that.

If you were affected by this in your plugin, you will need to fetch the client.offset_encoding (available via ctx.client_id from most handlers). If you were using LSP functions for something not involving LSP, you should consider vendoring your own solution, although the default offset_encoding is most commonly "utf-16".

The line affected by the breaking change (at least the one I found) is:

https://github.com/filipdutescu/renamer.nvim/blob/80d627372f90660f135862c8a7f8941b28ee5373/lua/renamer/init.lua#L485-L487

As per the help file, this function now requires an offset_encoding.

apply_workspace_edit({workspace_edit}, {offset_encoding})
                Applies a `WorkspaceEdit` .

                Parameters: ~
                    {workspace_edit}   table `WorkspaceEdit`
                    {offset_encoding}  string utf-8|utf-16|utf-32 (required)

Another option is to hardcode the offset to utf-16, but that would not be recommended.

You can find the breaking change here.

Hope this helps to fix the issue.

razak17 commented 2 years ago
function renamer._apply_workspace_edit(resp)
    local params = vim.lsp.util.make_position_params()
    local results_lsp, _ = vim.lsp.buf_request_sync(0, strings.lsp_req_rename, params)
    local client_id = results_lsp and next(results_lsp) or nil
    local client = vim.lsp.get_client_by_id(client_id)

    lsp_utils.apply_workspace_edit(resp, client.offset_encoding)
end

Updating the _apply_workspace_edit function with this can be used as a temporary fix. Thanks for the comment @datwaft. It helped

ilan-schemoul commented 2 years ago

@razak17 does not seem to work for me on 0.7 I have to local renamer = require('renamer.mappings.utils') ?

fauu commented 2 years ago

@ilan-schemoul Here’s what works for me (Packer’s config function):

local r = require"renamer"
r.setup {}
r._apply_workspace_edit = function(resp)
  local params = vim.lsp.util.make_position_params()
  local results_lsp, _ = vim.lsp.buf_request_sync(0,
      require"renamer.constants".strings.lsp_req_rename, params)
  local client_id = results_lsp and next(results_lsp) or nil
  local client = vim.lsp.get_client_by_id(client_id)
  require"vim.lsp.util".apply_workspace_edit(resp, client.offset_encoding)
end
ilan-schemoul commented 2 years ago

Thank you very much. I confirm it works on now (nvim 0.7.0).

filipdutescu commented 2 years ago

Hello everyone. First, let me start by apologizing for my radio silence this past months. Due to personal reasons I had to take a break, but I have returned and came up with a proper solution for this issue, which should ensure something like this won't happen again (see GH-122).

I sincerely hope that one the fix is merged and all is working as intended you will update/return to renamer. Thank you all very much for figuring out workarounds, reporting the bug and helping others out while I was away. Once more, I want to apologize if the project seemed abandoned there for a while, but I want to assure you it is not and that I am committed to maintaining it, just the way I have done before.

I have created a milestone as well, with a due date of next Friday just to create a small buffer and to provide a tentative release date. I am sure I will put out the release before then, hopefully even today, if I finish the changes in time.