Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/plenary.nvim
.test_plugins
9 changes: 9 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tasks": [
{
"label": "Run Tests",
"type": "shell",
"command": "make test"
}
]
}
97 changes: 56 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
# A Neovim Plugin Template

![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/ellisonleao/nvim-plugin-template/lint-test.yml?branch=main&style=for-the-badge)
![Lua](https://img.shields.io/badge/Made%20with%20Lua-blueviolet.svg?style=for-the-badge&logo=lua)

A template repository for Neovim plugins.

## Using it

Via `gh`:

```
$ gh repo create my-plugin -p ellisonleao/nvim-plugin-template
# nvim-java

![Neovim](https://img.shields.io/badge/NeoVim-%2357A143.svg?&style=for-the-badge&logo=neovim&logoColor=white)
![Lua](https://img.shields.io/badge/lua-%232C2D72.svg?style=for-the-badge&logo=lua&logoColor=white)
![Java](https://img.shields.io/badge/java-%23ED8B00.svg?style=for-the-badge&logo=openjdk&logoColor=white)
![Gradle](https://img.shields.io/badge/Gradle-02303A.svg?style=for-the-badge&logo=Gradle&logoColor=white)
![Apache Maven](https://img.shields.io/badge/Apache%20Maven-C71A36?style=for-the-badge&logo=Apache%20Maven&logoColor=white)

No need to put up with [jdtls](https://github.com/eclipse-jdtls/eclipse.jdt.ls) nonsense anymore.
Just install and start writing `public static void main(String[] args)`.

## Features

- :white_check_mark: Diagnostics & Auto Completion
- :white_check_mark: Automatic [DAP](https://github.com/mfussenegger/nvim-dap) debug configuration
- :x: Running tests

## Why

- Uses [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) to setup `jdtls`
- Realtime server settings updates is possible using [neoconf](https://github.com/folke/neoconf.nvim)
- Everything necessary will be installed automatically (except JDKs)
- Uses `jdtls` and auto loads `jdtls` plugins from [mason.nvim](https://github.com/williamboman/mason.nvim)
- Supported plugins are,
- `lombok`
- `java-test`
- `java-debug-adapter`
- Typed & documented APIs
- No callback hells I [promise](https://github.com/pyericz/promise-lua)

## How to Use

### Install the plugin

Using [lazy.nvim](https://github.com/folke/lazy.nvim)

```lua
return {
'nvim-java/nvim-java',
dependencies = {
'nvim-java/nvim-java-core',
'neovim/nvim-lspconfig',
'williamboman/mason.nvim',
'mfussenegger/nvim-dap',
},
event = 'VeryLazy',
opts = {},
}
```

Via github web page:
### Setup JDTLS like you would usually do

Click on `Use this template`

![](https://docs.github.com/assets/cb-36544/images/help/repository/use-this-template-button.png)

## Features and structure
```lua
require('lspconfig').jdtls.setup({})
```

- 100% Lua
- Github actions for:
- running tests using [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) and [busted](https://olivinelabs.com/busted/)
- check for formatting errors (Stylua)
- vimdocs autogeneration from README.md file
- luarocks release (LUAROCKS_API_KEY secret configuration required)
Yep! That's all :)

### Plugin structure
## Projects Acknowledgement

```
.
├── lua
│   ├── plugin_name
│   │   └── module.lua
│   └── plugin_name.lua
├── Makefile
├── plugin
│   └── plugin_name.lua
├── README.md
├── tests
│   ├── minimal_init.lua
│   └── plugin_name
│   └── plugin_name_spec.lua
```
[nvim-jdtls](https://github.com/mfussenegger/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.
4 changes: 2 additions & 2 deletions dev/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ vim.opt.runtimepath:prepend(cwd)
--[[
-- plugin name will be used to reload the loaded modules
--]]
local package_name = 'plugin_name'
local package_name = 'java'

-- add the escape character to special characters
local escape_pattern = function(text)
Expand All @@ -26,7 +26,7 @@ end
local run_action = function()
vim.cmd.messages('clear')

require(package_name).hello()
require(package_name).__run()
end

-- unload and run the function from the package
Expand Down
70 changes: 0 additions & 70 deletions doc/my-template-docs.txt

This file was deleted.

38 changes: 38 additions & 0 deletions lua/java.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local deps = require('java.dependencies')
local java_mason = require('java.mason')
local java_dap = require('java.dap')
local java_lspconfig = require('java.lspconfig')
local ts = require('java.treesitter')

local M = {}

function M.setup()
deps.check()
java_lspconfig.wrap_lspconfig_setup()
java_mason.install_dependencies()
java_dap.setup_dap()
end

----------------------------------------------------------------------
-- DAP APIs --
----------------------------------------------------------------------
M.dap = {}
M.dap.config_dap = java_dap.config_dap

----------------------------------------------------------------------
-- Test APIs --
----------------------------------------------------------------------
M.test = {}
M.test.run_current_test_class = java_dap.run_current_test_class

----------------------------------------------------------------------
-- Manipulate --
----------------------------------------------------------------------
M.manipulate = {}
-- M.manipulate.organize_imports = {}

function M.__run()
ts.find_main_method()
end

return M
82 changes: 82 additions & 0 deletions lua/java/dap/dapp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
local log = require('java-core.utils.log')

local Promise = require('java-core.utils.promise')
local JavaCoreDap = require('java-core.dap')
local JavaCoreTestHelper = require('java-core.ls.helpers.test-helper')

---@class JavaDap
---@field client LSPClient
---@field test_helper JavaCoreTestHelper
---@field dap_helper JavaCoreDap
local M = {}

---@param args { client: LSPClient }
---@return JavaDap
function M:new(args)
local o = {
client = args.client,
}

o.test_helper = JavaCoreTestHelper:new({
client = args.client,
})

o.dap_helper = JavaCoreDap:new({
client = args.client,
})

setmetatable(o, self)
self.__index = self
return o
end

function M:run_current_test_class()
log.info('running the current class')

local buffer = vim.api.nvim_get_current_buf()

self.test_helper
:get_test_class_by_buffer(buffer)
:thenCall(function(classes)
self.test_helper:run_test(classes)
end)
:catch(function(err)
local msg = 'failed to run the current class'

log.error(msg, err)
error(msg .. err)
end)
end

function M:config_dap()
return Promise.resolve()
:thenCall(function()
log.debug('set dap adapter callback function')

-- setting java adapter
require('dap').adapters.java = function(callback)
self.dap_helper:get_dap_adapter():thenCall(callback):catch(function(err)
local msg = 'faild to set DAP adapter'

error(msg, err)
log.error(msg, err)
end)
end

-- setting java config
return self.dap_helper:get_dap_config()
end)
:thenCall(function(dap_config)
log.debug('set dap config: ', dap_config)

require('dap').configurations.java = dap_config
end)
:catch(function(err)
local msg = 'faild to set DAP configuration'

log.error(msg, err)
error(msg .. err)
end)
end

return M
49 changes: 49 additions & 0 deletions lua/java/dap/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local JavaDap = require('java.dap.dapp')

local log = require('java-core.utils.log')
local state = require('java.state')
local notify = require('java-core.utils.notify')

local M = {}

---Setup dap config & adapter on jdtls attach event
function M.setup_dap()
log.info('add LspAttach event handlers to setup dap adapter & config')

vim.api.nvim_create_autocmd('LspAttach', {
pattern = '*',
callback = M.on_jdtls_attach,
once = true,
group = vim.api.nvim_create_augroup('nvim-java-dap-config', {}),
})
end

---Runs the current test class
function M.run_current_test_class()
state.java_dap:run_current_test_class()
end

---Configures the dap
function M.config_dap()
state.java_dap:config_dap():thenCall(function()
notify.info('DAP configured')
end)
end

---@private
---@param ev any
function M.on_jdtls_attach(ev)
local client = vim.lsp.get_client_by_id(ev.data.client_id)

if client.name == 'jdtls' then
state.java_dap = JavaDap:new({
client = client,
})

log.info('setup java dap config & adapter')

state.java_dap:config_dap()
end
end

return M
Loading