A diagnostic is a message a language server attaches to a place in your code: an error that stops compilation, a warning about something suspicious, or a hint. In a Java project, compiles your code in the background and sends diagnostics back. Neovim’s built-in vim.diagnostic module draws them, and chooses how they look and which keys move between them.
Where diagnostics come from
Two servers report diagnostics in a Spring Boot project:
- jdtls reports Java compiler problems: unknown methods, type mismatches, missing imports, unused code.
- The Spring Boot language server (Part 4) reports Spring-specific problems, such as an unknown key in
application.properties, and advice about your Spring Boot version inpom.xml.
Neovim treats both the same way. Each diagnostic has a severity (error, warning, info or hint), a position, a message and the name of the server that sent it.
How an error looks in the editor
Open CatalogController.java from module 9 of the companion code and make a typo on purpose: change findCatalog to findCatalogs.
return new PagedModel<>(repository.findCatalogs(pageable));
CatalogController.java (shown with the typo; the real file uses findCatalog)
Leave insert mode, and within a moment three things appear on that line. An error icon sits in the sign column on the left. The method name is underlined. And at the end of the line, after a small dot, the message itself: The method findCatalogs(Pageable) is undefined for the type ProductRepository.
LazyVim deliberately waits until you leave insert mode before updating diagnostics, so half-typed code doesn’t flash red. Saving with :w triggers the same check.
Read the full message
End-of-line text is cut off when the window is narrow. Put the cursor on the line and press <leader>cd (space, then c, then d) for “line diagnostics”. A floating window shows every diagnostic on that line in full, with the compiler’s problem number in brackets. Press Escape or move the cursor to close it.
Jump between problems
You rarely want to scroll looking for red marks. LazyVim maps bracket keys that jump straight to the next or previous one:
]dand[dmove to the next or previous diagnostic of any severity.]eand[emove between errors only.]wand[wmove between warnings only.
See every problem in the project
The bracket keys work inside one file. For the whole project, press <leader>xx. The Trouble plugin opens a list at the bottom, grouped by folder and file. In our test it showed the typo in CatalogController.java, and under pom.xml two Spring Boot notices: a newer patch version, 4.1.3, and a newer minor version, 4.2.0, are available. Those come from the Spring Boot language server, not from the compiler.
Move through the list with j and k, and press Enter to jump to a problem. <leader>xX shows the same list for the current file only, and <leader>sd searches diagnostics with the fuzzy picker instead. Press <leader>xx again to close Trouble.
In a repository with many modules, the project list can be long. The companion code keeps every module of the course in one Maven build, so the list includes warnings from all of them; filter to the file you are working on when that is too much.
Fix it with a code action
Many diagnostics come with a ready-made fix. jdtls calls them quick fixes; LSP calls them code actions. With the cursor on findCatalogs, press <leader>ca. A menu opens, and the first entries are the fixes for this error:
- Change to ‘findCatalog(..)’
- Create method ‘findCatalogs(Pageable)’ in type ‘ProductRepository’
- Add cast to ‘repository’
Press 1 and Enter. The call becomes findCatalog(pageable) again and the error disappears. Below the fixes, the same menu offers refactorings and source actions, which Part 5 covers.
Missing and unused imports are the most common fix of all. Instead of fixing them one by one, run “Organize imports”: <leader>co in a Java file, or pick it from <leader>ca. When we removed the PagedModel import and added an unused java.util.List, one organize-imports call put the file back exactly as it was.
Turn diagnostics off for a moment
During a demo or while reading code, the markers can distract. <leader>ud toggles diagnostics on and off for the session. The servers keep running, so the markers come back as soon as you toggle again.
Recap
- jdtls reports Java compiler problems; the Spring Boot language server reports Spring-specific ones. Both show up the same way.
- LazyVim shows a sign, an underline and end-of-line text, and updates them when you leave insert mode or save.
<leader>cdshows the full message;]d/[d,]e/[eand]w/[wjump between problems.<leader>xxlists problems for the project in Trouble,<leader>xXfor the current file.<leader>caoffers quick fixes and other code actions;<leader>coorganizes imports.
Series: Neovim as a Java IDE. Previous: Java language support in Neovim with jdtls · Next: Spring Boot language support in Neovim
Sources
Official documentation and well-known tutorials read while writing this article. The wording here is our own.
- Neovim: vim.diagnostic documentation
- LazyVim: keymaps (diagnostics, LSP, Trouble)
- LazyVim: LSP plugin defaults (diagnostics options)
- Trouble.nvim README
- nvim-jdtls README (organize imports and code action extensions)
- Lorenzo Bettini: Neovim and Java with LazyVim, part 2: IDE mechanisms
- Nirmal Ravidas: Setting Up a Full Spring Boot Dev Environment on Linux with Neovim and LazyVim
- Dhruva Sagar: Neovim as a Java IDE
Comments