/nvim-java

Painless Java in Neovim

Primary LanguageLuaMIT LicenseMIT

β˜• nvim-java

Java Gradle Apache Maven Neovim Lua

Just install and start writing public static void main(String[] args).

πŸ“’ Demo

Demo.mp4

πŸ’« Features

  • βœ… Diagnostics & Auto Completion
  • βœ… Automatic DAP debug configuration
  • βœ… Running tests

πŸ’‘ Why

  • Everything necessary will be installed automatically
  • Uses nvim-lspconfig to setup jdtls
  • Realtime server settings updates is possible using neoconf
  • Auto loads necessary jdtls plugins
    • Supported plugins are,
      • lombok
      • java-test
      • java-debug-adapter

πŸ”¨ How to Install

πŸ”Έdetails

Distributions:

Custom:

  • Install the plugin

Using lazy.nvim

return {
  'nvim-java/nvim-java',
  dependencies = {
    'nvim-java/lua-async-await',
    'nvim-java/nvim-java-core',
    'nvim-java/nvim-java-test',
    'nvim-java/nvim-java-dap',
    'MunifTanjim/nui.nvim',
    'neovim/nvim-lspconfig',
    'mfussenegger/nvim-dap',
    {
      'williamboman/mason.nvim',
      opts = {
        registries = {
          'github:nvim-java/mason-registry',
          'github:mason-org/mason-registry',
        },
      },
    }
  },
}
  • Setup nvim-java before lspconfig
require('java').setup()
  • Setup jdtls like you would usually do
require('lspconfig').jdtls.setup({})

Yep! That's all :)

⌨️ Commands

πŸ”Έdetails
  • JavaDapConfig - DAP is autoconfigured on start up, but in case you want to force configure it again, you can use this API
  • JavaTestRunCurrentClass - Run the test class in the active buffer
  • JavaTestDebugCurrentClass - Debug the test class in the active buffer
  • JavaTestRunCurrentMethod - Run the test method on the cursor
  • JavaTestDebugCurrentMethod - Debug the test method on the cursor
  • JavaTestViewLastReport - Open the last test report in a popup window

πŸ’» APIs

πŸ”Έdetails

DAP

  • config_dap - DAP is autoconfigured on start up, but in case you want to force configure it again, you can use this API
require('java').dap.config_dap()

Test

  • run_current_class - Run the test class in the active buffer
require('java').test.run_current_class()
  • debug_current_class - Debug the test class in the active buffer
require('java').test.debug_current_class()
  • run_current_method - Run the test method on the cursor
require('java').test.run_current_method()
  • debug_current_method - Debug the test method on the cursor
require('java').test.debug_current_method()
  • view_report - Open the last test report in a popup window
require('java').test.view_last_report()

πŸ—œοΈ How to Use JDK X.X Version?

πŸ”Έdetails

Method 1:

Neoconf can be used to manage LSP setting including jdtls. Neoconf allows global configuration as well as project vice configurations. Here is how you can set Jdtls setting on neoconf.json

{
  "lspconfig": {
    "jdtls": {
      "java.configuration.runtimes": [
        {
          "name": "JavaSE-21",
          "path": "/opt/jdk-21",
          "default": true
        }
      ]
    }
  }
}

Method 2:

Pass the settings to Jdtls setup.

require('lspconfig').jdtls.setup({
  settings = {
    java = {
      configuration = {
        runtimes = {
          {
            name = "JavaSE-21",
            path = "/opt/jdk-21",
            default = true,
          }
        }
      }
    }
  }
})

β›³ Architecture

πŸ”Έdetails

Following is the high level idea. Jdtls is the language server nvim-java communicates with. However, we don't have all the features we need just in Jdtls. So, we are loading java-test & java-debug-adapter extensions when we launch Jdtls. Once the language server is started, we communicate with the language server to do stuff.

For instance, to run the current test,

  • Request Jdtls for test classes
  • Request Jdtls for class paths, module paths, java executable
  • Request Jdtls to start a debug session and send the port of the session back
  • Prepare TCP connections to listen to the test results
  • Start nvim-dap and let user interactions to be handled by nvim-dap
  • Parse the test results as they come in
  • Once the execution is done, open a window show the test results
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚            β”‚                         β”‚            β”‚
  β”‚   Neovim   β”‚                         β”‚   VSCode   β”‚
  β”‚            β”‚                         β”‚            β”‚
  β””β”€β”€β”€β”€β”€β–²β”€β”€β”€β”€β”€β”€β”˜                         β””β”€β”€β”€β”€β”€β”€β–²β”€β”€β”€β”€β”€β”˜
        β”‚                                       β”‚
        β”‚                                       β”‚
        β”‚                                       β”‚
        β”‚                                       β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚               β”‚                β”‚                             β”‚
β”‚   nvim-java   β”‚                β”‚   Extension Pack for Java   β”‚
β”‚               β”‚                β”‚                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β–²β”€β”€β”€β”€β”€β”€β”€β”˜                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–²β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                                       β”‚
        β”‚                                       β”‚
        β”‚                                       β”‚
        β”‚                                       β”‚
        β”‚                                       β”‚
        β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”‚
        β”‚              β”‚           β”‚            β”‚
        └──────────────►   JDTLS   β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚           β”‚
                       β””β”€β”€β”€β–²β”€β”€β”€β–²β”€β”€β”€β”˜
                           β”‚   β”‚
                           β”‚   β”‚
                           β”‚   β”‚
                           β”‚   β”‚
                           β”‚   β”‚
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚   β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚               β”‚        β”‚   β”‚         β”‚                        β”‚
  β”‚   java-test   β—„β”€β”€β”€β”€β”€β”€β”€β”€β”˜   └─────────►   java-debug-adapter   β”‚
  β”‚               β”‚                      β”‚                        β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‘ Projects Acknowledgement

nvim-jdtls is a plugin that follows "Keep it simple, stupid!" approach. If you love customizing things by yourself, then give nvim-jdtls a try. I may or may not have copied some code πŸ˜‰ Beauty of Open source!