Coder Social home page Coder Social logo

nvim-java / nvim-java Goto Github PK

View Code? Open in Web Editor NEW
546.0 10.0 21.0 161 KB

Painless Java in Neovim

License: MIT License

Makefile 0.47% Lua 99.53%
autocompletion debugging diagnostics language-server-client language-server-protocol neovim neovim-plugin neovim-plugins running spring

nvim-java's Introduction

β˜• 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-refactor',
    '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

Runner

  • JavaRunnerRunMain - Runs the application or selected main class (if there are multiple main classes)
:JavaRunnerRunMain
:JavaRunnerRunMain <arguments> <to> <pass>
  • JavaRunnerStopMain - Stops the running application
  • JavaRunnerToggleLogs - Toggle between show & hide runner log window

DAP

  • JavaDapConfig - DAP is autoconfigured on start up, but in case you want to force configure it again, you can use this API

Test

  • 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

Profiles

  • JavaProfile - Opens the profiles UI

Refactor

  • JavaRefactorExtractVariable - Create a variable from returned value at cursor

πŸ’» APIs

πŸ”Έdetails

Runner

  • built_in.run_app - Runs the application or selected main class (if there are multiple main classes)
require('java').runner.built_in.run_app({})
require('java').runner.built_in.run_app({'arguments', 'to', 'pass', 'to', 'main'})
  • built_in.stop_app - Stops the running application
require('java').runner.built_in.stop_app()
  • built_in.toggle_logs - Toggle between show & hide runner log window
require('java').runner.built_in.toggle_logs()

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()

Profiles

require('java').profile.ui()

Refactor

  • extract_variable - Create a variable from returned value at cursor
require('java').refactor.extract_variable()

πŸ—œοΈ 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,
          }
        }
      }
    }
  }
})

πŸ”§ Configuration

πŸ”Έdetails

For most users changing the default configuration is not necessary. But if you want, following options are available

{
 --  list of file that exists in root of the project
 root_markers = {
  'settings.gradle',
  'settings.gradle.kts',
  'pom.xml',
  'build.gradle',
  'mvnw',
  'gradlew',
  'build.gradle',
  'build.gradle.kts',
  '.git',
 },

 -- load java test plugins
 java_test = {
  enable = true,
 },

 -- load java debugger plugins
 java_debug_adapter = {
  enable = true,
 },

 jdk = {
  -- install jdk using mason.nvim
  auto_install = true,
 },

 notifications = {
  -- enable 'Configuring DAP' & 'DAP configured' messages on start up
  dap = 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!

nvim-java's People

Contributors

atm1020 avatar ellisonleao avatar github-actions[bot] avatar omnisudo avatar s1n7ax avatar vinibispo avatar zeioth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nvim-java's Issues

bug: error is thrown when the go to definition to a .class file

We have to make sure the buffer is modifiable before the de-compiled content is set

[ERROR Wed Dec 13 18:29:38 2023] ...al/share/nvim/lazy/nvim-java/lua/java/handlers/error.lua:26:
decompilation failed for jdt://contents/spring-boot-3.2.0.jar/org.springframework.boot/SpringApplication.class?=demo/%5C/root%5C/.gradle%5C/caches%5C/modules-2%5C/files-2.1%5C/org.springframework.boot%5C/spring-boot%5C/3.2.0%5C/4088ad23df91481bc7415cbfa068c423ccb7e54e%5C/spring-boot-3.2.0.jar=/gradle_used_by_scope=/main,test=/%3Corg.springframework.boot(SpringApplication.class
...im/lazy/nvim-java/lua/java/startup/decompile-watcher.lua:32: Buffer is not 'modifiable'
stack traceback:
        ...al/share/nvim/lazy/nvim-java/lua/java/handlers/error.lua:18: in function 'error_handler'
        ...e/nvim/lazy/nvim-java-core/lua/java-core/utils/async.lua:32: in function 'callback'
        ...nvim-java-core/lua/java-core/ls/clients/jdtls-client.lua:47: in function 'handler'
        /usr/local/share/nvim/runtime/lua/vim/lsp.lua:1393: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>
        [C]: in function 'wait'
        ...im/lazy/nvim-java/lua/java/startup/decompile-watcher.lua:47: in function <...im/lazy/nvim-java/lua/java/startup/decompile-watcher.lua:21>
        [C]: in function 'nvim_win_set_buf'
        /usr/local/share/nvim/runtime/lua/vim/lsp/util.lua:1146: in function 'jump_to_location'
        ...nvim/lazy/telescope.nvim/lua/telescope/builtin/__lsp.lua:212: in function 'handler'
        /usr/local/share/nvim/runtime/lua/vim/lsp.lua:1393: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

bug: server module location was changed in java-core

Did you check docs and existing issues?

  • I have read all the plugin docs
  • I have searched the existing issues
  • I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

Operating system/version

Describe the bug

Steps To Reproduce

Expected Behavior

feature: code structure change

Did you check the docs?

  • I have read all the docs

Is your feature request related to a problem? Please describe.

changes should be made to reflect the changes made by nvim-java/nvim-java-core#18 and improve the current project structure.

Describe the solution you'd like

Describe alternatives you've considered

Additional context

bug: DAP debug configuration didn't automatically effective

Did you check docs and existing issues?

  • I have read all the plugin docs
  • I have searched the existing issues
  • I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

0.9.4

Operating system/version

MacOS 14.2

Describe the bug

When I wanto start the springboot project, DAP shows that:
No configuration found for java. You need to add configs to dap.configurations.java (See :h dap-configuration)

My neovim dots is here: https://github.com/buptlibo/nvimdots

Steps To Reproduce

Use neovim to open a springboot project, then start debug the application.

Expected Behavior

The project can start normally.

bug: lazyvim config incorrect

Did you check docs and existing issues?

  • I have read all the plugin docs
  • I have searched the existing issues
  • I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

0.9.4

Operating system/version

MacOS 14.2.1

Describe the bug

Configuration example for LazyVim is not working in its current state.
I suspect there is a difference between OS or some of the dependencies as the working version was removed in the latest commit.

opts = {},

is required for mason to pick up the dependencies and install anything.

Steps To Reproduce

Follow the installation tutorial step by step, after adding configuration to plugins/java/init.lua mason is not picking up the dependencies.

Expected Behavior

The configuration file is working. If there is a difference in how the config should look like between versions or OS it should be called out and given to end user.

bug: Fail to install java-test using mason

Did you check docs and existing issues?

  • I have read all the plugin docs
  • I have searched the existing issues
  • I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

0.9.4

Operating system/version

macOS sonoma(14.1)

Describe the bug

I got error message from installation of java-testusing Mson like below.

Downloading file "https://github.com/microsoft/vscode-java-test/releases/download/0.40.1/vscjava.vscode-java-test-0.40.1.vsix"…
spawn: wget failed with exit code 8 and signal 0.
Failed to download file "https://github.com/microsoft/vscode-java-test/releases/download/0.40.1/vscjava.vscode-java-test-0.40.1.vsix".

Steps To Reproduce

Just put configuration as described in https://github.com/nvim-java/nvim-java/wiki/Lazyvim

Expected Behavior

java-test Successfully installed

bug: ci fails to generate the vimdoc

Did you check docs and existing issues?

  • I have read all the plugin docs
  • I have searched the existing issues
  • I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

Operating system/version

Describe the bug

Steps To Reproduce

Expected Behavior

bug: errors are not logged due to error in error handler

Did you check docs and existing issues?

  • I have read all the plugin docs
  • I have searched the existing issues
  • I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

Operating system/version

Describe the bug

At this point in software engineering you gotta test the tests to see if they are working correctly πŸ˜†

Steps To Reproduce

Expected Behavior

R&D: Check if the error stack traces are helpful or not

Did you check the docs?

  • I have read all the docs

Is your feature request related to a problem? Please describe.

At the moment, the stacktrace is commented when logging errors. It seems mostly it's just promise stuff in the trace

local function get_error_handler(msg, ...)
msg = string.format(msg, ...)
return function(err)
local trace = debug.traceback()
local log_obj = { msg }
table.insert(log_obj, err)
table.insert(log_obj, trace)
local log_str = table_tostring(log_obj)
log.error(log_str)
notify.error(log_str)
error(log_str)
end
end

Describe the solution you'd like

Describe alternatives you've considered

Additional context

feature: dependency view

Did you check the docs?

  • I have read all the docs

Is your feature request related to a problem? Please describe.

A feature that's always lacked from vim LSPs is a dependency view being able to see the libraries/dependencies that my projects relies on.

More info: https://github.com/orgs/nvim-java/discussions/78

Describe the solution you'd like

A way to see and look at the dependancies provided by maven/gradle/ant+ivy

Describe alternatives you've considered

keeping an instance of IntelliJ open.

Additional context

No response

feature: add java.action.applyRefactoringCommand

Did you check the docs?

  • I have read all the docs

Is your feature request related to a problem? Please describe.

First of all, thank you for this awesome plugin! It made it super simple for me to get a Java config up and running.

Second, Is there any reason why it doesn't wrap nvim-jdtls? According to this comment by its author it would be pretty simple. nvim-jdtls supports code-actions like extracting methods and variables for instance. What would be your response to this? πŸ™‚

Describe the solution you'd like

nvim-java sets up nvim-jdtls

Describe alternatives you've considered

Not using nvim-java and instead manually settings up nvim-jdtls (which seems is really tedious)

Additional context

Here's the post that I made in nvim-jdtls:

LSP client configuration

No response

Eclipse.jdt.ls version

v1.30.1

Steps to Reproduce

Hi! Thanks for making this awesome plugin! I just installed it, and when I try and perform the code action "Extract to method" I get the message

Language server `jdtls` does not support command `java.action.applyRefactoringCommand`. This command may require a client extension.

Is this expected behaviour? The README says that extract_method is supported.

This is the config that I'm using:

Click to expand
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',
        },
      },
    },
  },
  config = function()
    require('java').setup()
    require('lspconfig').jdtls.setup({})
  end
}

To reproduce:

  1. Open a .java file
  2. Select text in visual mode
  3. Do :=vim.lsp.buf.code_action()
  4. Select Extract to method
  5. Message pops up

Expected Result

Code action lets me extract code to a new method

Actual Result

I get the message

Language server `jdtls` does not support command `java.action.applyRefactoringCommand`. This command may require a client extension.

feature: restructuring the code

Did you check the docs?

  • I have read all the docs

Is your feature request related to a problem? Please describe.

This is all about double checking the structure of the project to suite better for implementing features and upcoming changes.

Describe the solution you'd like

Describe alternatives you've considered

Additional context

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.