filipdutescu / renamer.nvim

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

[Bug] Error when trying to rename a class (jdtls) #100

Closed Asdas91 closed 2 years ago

Asdas91 commented 2 years ago

Describe the bug

When I rename a Java public class using renamer error appears and nothing is renamed. It's supposed to rename the class name, filename and references to the class in another files.

The error:

Error executing vim.schedule lua callback: ...l/.config/nvim/plugins/renamer.nvim/lua/renamer/init.lua:385: attempt to index field 'textDocument' (a nil value)
stack traceback:
        ...l/.config/nvim/plugins/renamer.nvim/lua/renamer/init.lua:385: in function 'handler'
        /usr/share/nvim/runtime/lua/vim/lsp.lua:957: in function 'cb'
        vim.lua:285: in function <vim.lua:285>

To reproduce

Steps to reproduce the behavior:

  1. Open a java file
  2. Move cursor to the public class name
  3. Call renamer
  4. Change text
  5. Press enter
  6. See the error

Expected behavior

The class gets renamed, the file gets renamed and references to the class also get renamed

Environment

Screenshots

image

Additional context

I can supply a dummy java project to reproduce this.

filipdutescu commented 2 years ago

Thanks for submitting this bug! I will look into it, but could you also let me know what LSP you're using?

filipdutescu commented 2 years ago

Looking a bit more at the code and the error, could you add a print(resp) immediately after the if ... then in which the error occurs? (Between line 382 and 383) just so I can see what the LSP server responded with

Asdas91 commented 2 years ago

Thank you for answering! I am using jdtls server.

When I used just the print function it only printed a hash or something so I used this nice tool https://github.com/kikito/inspect.lua. I think the problem might be the third item in documentChanges which is about renaming the file. That one has a different structure and doesn't have the "textDocument" key. resp:

{
  changes = {
    <metatable> = {
      __tostring = <function 1>
    }
  },
  documentChanges = { {
      edits = { {
          newText = 'Apps {\n    public String getGreetings() {\n        return "Hello World!";\n    }\n\n    public static void main(String[] args) {\n        System.out.println(new Apps',
          range = {
            end = {
              character = 34,
              line = 11
            },
            start = {
              character = 13,
              line = 5
            }
          }
        } },
      textDocument = {
        uri = "file:///homeFolder/Sources/gradle-project/app/src/main/java/renamer/tester/App.java"
      }
    }, {
      edits = { {
          newText = "Apps classUnderTest = new Apps",
          range = {
            end = {
              character = 36,
              line = 10
            },
            start = {
              character = 8,
              line = 10
            }
          }
        } },
      textDocument = {
        uri = "file:///homeFolder/Sources/gradle-project/app/src/test/java/renamer/tester/AppTest.java"
      }
    }, {
      kind = "rename",
      newUri = "file:///homeFolder/Sources/gradle-project/app/src/main/java/renamer/tester/Apps.java",
      oldUri = "file:///homeFolder/Sources/gradle-project/app/src/main/java/renamer/tester/App.java"
    } }
}
filipdutescu commented 2 years ago

Thanks a lot for providing the LSP response, sorry I forgot to mention you should inspect it to view it properly (you can also use the built-in vim.inspect(...) function). That seems to be the issue. This issue seems like a tough one :thinking: I need to look at the LSP docs and figure out how to properly map it.

I will work on a fix, but since it does not seem like an easy one atm, I cannot estimate an accurate ETA for it, so sorry for this. But I can assure you a fix will come ASAP. I am also sorry that I am not sure what the most efficient workaround would be. I guess, rename the file first, then the items inside it, but I am not sure how productive it would be. Will keep you updated.

Asdas91 commented 2 years ago

I see. That's a useful function. I am quite new to Lua so I didn't know about it.

Thank you for looking into it. I like the plugin it enhances the rename functionality really nicely.

filipdutescu commented 2 years ago

the latest develop should have a working fix for your issue, which properly sets qflist as well (visible with :copen)