avataralpha2phi

Summary

The web content provides a comprehensive guide on configuring and using Neovim for Java application development, including setup instructions, plugin installation, and key mappings.

Abstract

The article "Neovim for Beginners — Java" is a detailed tutorial aimed at developers who want to use Neovim as their Integrated Development Environment (IDE) for Java. It outlines the necessary steps to set up Neovim for Java development, starting with the installation of the Java Development Kit (JDK) and the language server eclipse.jdt.ls. The guide walks through configuring Neovim using the nvim-jdtls plugin and setting up file type plugins for Java. It also covers how to manage the data directory, format code according to Google's style guide, and map keys for specific Java actions. The article emphasizes the importance of testing the setup with a real Java project, such as a Spring Boot application, and demonstrates the functionality of the language server and formatting tools. Additionally, it suggests using null-ls.nvim for those who prefer Google's Java formatting style and mentions other plugins like jc.nvim for enhanced Java completion features. The article concludes with a recommendation to explore a series of Vim/Neovim articles and an AI service called ZAI.chat.

Opinions

  • The author advocates for using Neovim as a powerful and customizable alternative to traditional Java IDEs.
  • There is a clear preference for the Google Java formatting style, as it is highlighted multiple times throughout the article.
  • The article suggests that the nvim-jdtls plugin and the null-ls.nvim formatter are essential tools for Java development in Neovim.
  • The author values the community and open-source contributions, as evidenced by the mention of the jc.nvim plugin and the invitation to support the author's work on Medium.
  • Testing the setup with a practical project is considered crucial for ensuring that the language server and other tools are functioning correctly.
  • The recommendation of ZAI.chat indicates the author's endorsement of cost-effective AI services that offer performance comparable to more expensive options like ChatGPT Plus (GPT-4).

Neovim for Beginners — Java

Use Neovim for Java application development.

Neovim for Beginners — Java

We have already configured Neovim for languages like Python, JavaScript, TypeScript, Rust, Go, Lua, HTML, etc. In this article, let’s check out how we can use Neovim for Java application development.

This article is part of the Neovim for Beginners series.

The Neovim configuration files can be found in this repository.

Getting Started

Java Development Kit (JDK)

Depending on the operating system, we need to install OpenJDK or Oracle JDK.

Language Server

For the language server, we can use eclipse.jdt.ls.Installing the language server is easy by using nvim-lsp-installer.

Type the command :LspInstall jdtls to install the language server. Alternatively, type the command :LspInstallInfo, select jdtls and press i to install.

Install Language Server

The installation folder can be found under the standard path location.

local JDTLS_LOCATION = vim.fn.stdpath "data" .. "/lsp_servers/jdtls"

We will use this trick later to avoid hard-coding the location.

Configuration

Let’s get started with the configuration.

Plugin — nvim-jdtls

In the lua/plugins.lua file, add the following lines to install the nvim-jdtls plugin using packer.nvim.

use { "mfussenegger/nvim-jdtls", ft = { "java" }

File Type Plugin

Create the Java file type plugin after/ftplugin/java.lua file with the following Lua code. We will go through the code in more detail.

  • We make a protected call to the jdtls module (line 1 — line 5). If it is not available we skip the configuration.
  • The JDTLS_LOCATION is derived from the Neovim standard path location.
local JDTLS_LOCATION = vim.fn.stdpath "data" .. "/lsp_servers/jdtls"

The variable is used in the language server configuration to avoid hard coding (line 46 — line 49).

"-jar",
vim.fn.glob(JDTLS_LOCATION .. "/plugins/org.eclipse.equinox.launcher_*.jar"),
"-configuration",
JDTLS_LOCATION .. "/config_" .. SYSTEM,
  • For the data directory, we configure it to be under the HOME folder.
-- Data directory - change it to your liking
local HOME = os.getenv "HOME"
local WORKSPACE_PATH = HOME .. "/workspace/java/"
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local workspace_dir = WORKSPACE_PATH .. project_name

It is used to configure the data directory parameter (line 50 — line 51).

"-data",
 workspace_dir,

Ensure the data directory is different from the project folder to avoid the issue mentioned hereCannot detect the project, only syntax errors are reported.

  • For formatting, we prefer the Google style (line 81 — line 86).
format = {
  enabled = true,
 settings = {
   url = vim.fn.stdpath "config" .. "/lang-servers/intellij-java-google-style.xml",
    profile = "GoogleStyle",
  },
},
  • We also add the following jdtls commands (line 136).
require("jdtls.setup").add_commands()
jdtls Commands

Key Mappings

In the lua/config/whichkey.lua file, we configure the key mappings for the Java file type.

We add the key mappings to organize imports and extract variable/constant/method in both normal and visual modes.

Testing

Let’s try this out with a Spring Boot project.

Using Neovim for Java
  • The language server works now and nvim-cmp is used as the completion engine.
  • Any error or diagnostic messages are displayed.
  • The code is formatted automatically when the buffer is saved.
  • The imports are organized when we invoke the command.
Using Neovim for Java
  • From the code actions menu, we can generate the toString method, extract the method, generate the constructor, and perform other refactoring actions depending on the current cursor location.
  • We can select the code, and extract the constant, variable, or method.

Note: If you encounter any error and the language server is not attached to the buffer, make sure the JDK is compatible with jdtls.

Formatting

For anyone who prefers the Google Java formatting style, we can use the null-ls.nvim formatter.

Google Java Formatting

Check out this article on how to configure thenull-ls.nvim plugin.

In the following article, we will configure Neovim to debug a Java application.

Other Plugins

Do also check out jc.nvim which is a Java completion plugin that automates the installation of jdt.ls and provides many more features.

Check out Learn Neovim The Practical Way for all the Vim/Neovim articles!

If you are not a Medium member yet and want to become one, click here (A part of the subscription fee will be used to support alpha2phi).

Vim
Programming
Coding
Software Development
Software Engineering
Recommended from ReadMedium