DEV Community

Cover image for I built a Neovim plugin for Java development and open sourced it — nvim-javacreator
Nirmal Ravidas
Nirmal Ravidas

Posted on

I built a Neovim plugin for Java development and open sourced it — nvim-javacreator

I built a Neovim plugin and open sourced it — nvim-javacreator.

Here is the story behind it.

The Problem

I was setting up LazyVim for Spring Boot development. Everything was working well until I needed to create a new Java file.

In IntelliJ you just right click a package, select New, choose the file type and it creates the file with the correct package name and Spring Boot annotations already filled in.

In Neovim there was nothing like that. I had to manually create the file, type the package name by looking at the folder path and add the annotations myself. Every single time. That got old quickly.

On top of that I work on multiple projects — Spring Boot, React, Python and C++. Without per project configuration Neovim does not know which language servers to load for which project. You can end up with Java LSP loading in a React project or Python LSP showing up in a Spring Boot project.

LazyVim supports this via a settings file called .neoconf.json which tells Neovim which language servers to enable or disable for a specific project. But you have to manually create and configure it for every project. I kept forgetting to do it.

So I built nvim-javacreator to solve both problems.


What it does

1. Auto project detection

When you open a project for the first time it automatically detects whether it is Spring Boot, React, Python or C++ by checking for pom.xml, package.json, requirements.txt and CMakeLists.txt.

It then creates a .neoconf.json file in your project root automatically so only the correct language servers load for that project. No manual setup required.

nvim .
-> Spring Boot Project detected
-> .neoconf.json created
-> Only jdtls and vscode-spring-boot-tools load
Enter fullscreen mode Exit fullscreen mode

2. Java file creator

Press <leader>jn to create a Java file. You get a menu to select the file type:

Template Annotations
Class plain class
Interface plain interface
Enum plain enum
Record plain record
REST Controller @RestController @RequestMapping
Service @Service
Repository @Repository JpaRepository
Entity @Entity @Table @Id
Component @Component
Configuration @Configuration

The package name is auto detected from the folder path:

src/main/java/com/myapp/service/
                 |
package com.myapp.service;
Enter fullscreen mode Exit fullscreen mode

Spring Boot annotations are already filled in. Type the class name and the file is ready.


Installation

Step 1 — Enable LazyVim extras

:LazyExtras
Enter fullscreen mode Exit fullscreen mode

Enable lang.java and lsp.neoconf.

Step 2 — Add plugin

Create ~/.config/nvim/lua/plugins/javacreator.lua:

return {
  {
    "nirmalravidas/nvim-javacreator",
    opts = {},
  },
}
Enter fullscreen mode Exit fullscreen mode

Step 3 — Sync

:Lazy sync
Enter fullscreen mode Exit fullscreen mode

That is it. Open your Spring Boot project and start coding.


Why I built this instead of using an existing plugin

I searched for existing plugins before building this. I found one that creates basic Java files but nothing that combines Spring Boot templates, auto project detection and automatic LSP isolation together.


Source code

GitHub: https://github.com/nirmalravidas/nvim-javacreator

Free and open source. Any feedback and contributions are welcome.

Top comments (0)