A refactoring changes the structure of code without changing what it does: giving a value a name, moving lines into their own method, renaming a class. Doing that by hand is slow and easy to get wrong, because every caller must change too. , the Java language server, knows the whole project, so it can make the change everywhere at once. In you reach its refactorings through code actions and a few keymaps.

Where the refactorings live

Most refactorings are code actions, the same menu Part 3 used for quick fixes. Put the cursor on some code, or select it in visual mode, and press <leader>ca. jdtls offers only the actions that make sense at that spot, so the menu changes as you move.

LazyVim’s Java extra adds direct keys for the common ones:

  • <leader>cxv extracts a variable, <leader>cxc a constant.
  • <leader>cxm in visual mode extracts the selection into a method.
  • <leader>cr renames the symbol under the cursor.

Every example below starts from the same method in module 9 of the companion code:

@GetMapping("/catalog")
PagedModel<CatalogItem> catalog(Pageable pageable) {
    return new PagedModel<>(repository.findCatalog(pageable));
}

CatalogController.java

Extract a variable, then inline it

Select repository.findCatalog(pageable) and press <leader>cxv. jdtls adds a local variable, adds the missing import for Page, and uses the variable:

Page<CatalogItem> catalog = repository.findCatalog(pageable);
return new PagedModel<>(catalog);

jdtls picked the name catalog itself, from the method it called. It does not ask for a name first, unlike IntelliJ IDEA. If you want another name, press <leader>cr on the variable straight away.

The reverse works too. With the cursor on catalog, open <leader>ca and choose “Inline local variable”. The method is back to one line.

Extract a method or a field

Select the same expression and press <leader>cxm. jdtls creates a private method and calls it:

return new PagedModel<>(extracted(pageable));
// ...
private Page<CatalogItem> extracted(Pageable pageable) {
    return repository.findCatalog(pageable);
}

Again the name is chosen for you, here extracted. Rename it right away with <leader>cr.

“Extract to field” sits in the <leader>ca menu. It turns the expression into a private field, catalog, and assigns it inside the method. “Introduce Parameter” goes the other way: it moves the expression out to the callers by adding a new method parameter.

Extract a constant

<leader>cxc extracts a constant, a static final field. jdtls only offers it for a constant expression, such as a string or number literal. On the path "/catalog" in @GetMapping the menu lists “Extract to constant”. On a method call like repository.findCatalog(pageable) it is missing, because the result is not known at compile time. That is correct Java behaviour, not a missing feature, but it surprises people who try it on the wrong code.

Rename across the project

Put the cursor on findCatalog in the controller and press <leader>cr. Type findCatalogPage and press Enter. jdtls changes the declaration in ProductRepository.java, the call in the controller and both calls in CatalogTest.java, three files in one step. Save them all with :wa.

Renaming a class renames its file too. When we renamed the Category entity to ProductCategory, jdtls wrote ProductCategory.java, removed Category.java, and updated the repository, the Product entity and the seed code in ShopApp.

Rename changes Java code only. When we renamed the Product entity to ShopProduct, jdtls updated JpaRepository<ShopProduct, Long> but left the JPQL query select p.name, c.name from Product p ... untouched, because it is text inside a string. Check such queries yourself after renaming an entity.

Move a class

To move a class to another package, put the cursor on its name and pick “Move…” from <leader>ca. A picker lists the packages of the project. Choose one and jdtls moves the file, changes its package line and fixes the imports of every class that uses it. In our test, moving the CatalogItem record updated the controller, the repository and the test.

Change a signature

“Change signature” in <leader>ca, on a method name, opens a small form in a floating window:

Access type:
Name: catalog
Return type: PagedModel<CatalogItem>
Parameters:
- 0: Pageable pageable

Edit the values and keep the labels; lines below the form explain the format for adding and reordering parameters. Close the window with Ctrl-w q to apply. Changing Name: to listCatalog renamed the method and its callers in one go. It is plain text, not IntelliJ IDEA’s dialog with a preview, but the result is the same kind of change.

Recap

  • Refactorings are code actions: <leader>ca shows what fits at the cursor.
  • <leader>cxv, <leader>cxm and <leader>cxc extract a variable, a method and a constant; jdtls picks the names, so rename with <leader>cr afterwards.
  • Extract constant works only on constant expressions.
  • Rename, move and change signature update every file that uses the code; renaming a class renames its file.
  • Strings, such as JPQL queries, are not updated by rename.

Series: Neovim as a Java IDE. Previous: Spring Boot language support in Neovim · Next: IntelliJ IDEA features and their Neovim equivalents

Sources

Official documentation and well-known tutorials read while writing this article. The wording here is our own.