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
84 changes: 52 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,38 +264,58 @@ want, following options are available

```lua
{
-- 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,
},
-- 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,
},

-- We do multiple verifications to make sure things are in place to run this
-- plugin
verification = {
-- nvim-java checks for the order of execution of following
-- * require('java').setup()
-- * require('lspconfig').jdtls.setup()
-- IF they are not executed in the correct order, you will see a error
-- notification.
-- Set following to false to disable the notification if you know what you
-- are doing
invalid_order = true,

-- nvim-java checks if the require('java').setup() is called multiple
-- times.
-- IF there are multiple setup calls are executed, an error will be shown
-- Set following property value to false to disable the notification if
-- you know what you are doing
duplicate_setup_calls = true,
},
}
```

Expand Down
7 changes: 5 additions & 2 deletions lua/java.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local decomple_watch = require('java.startup.decompile-watcher')
local mason_dep = require('java.startup.mason-dep')
local nvim_dep = require('java.startup.nvim-dep')
local setup_wrap = require('java.startup.lspconfig-setup-wrap')
local startup_check = require('java.startup.startup-check')

local test = require('java.api.test')
local dap = require('java.api.dap')
Expand All @@ -16,9 +16,12 @@ local M = {}
function M.setup(custom_config)
local config =
vim.tbl_deep_extend('force', global_config, custom_config or {})

vim.g.nvim_java_config = config

nvim_dep.check()
if not startup_check() then
return
end

local is_installing = mason_dep.install(config)

Expand Down
21 changes: 21 additions & 0 deletions lua/java/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
---@field java_debug_adapter { enable: boolean }
---@field jdk { auto_install: boolean }
---@field notifications { dap: boolean }
---@field verification { invalid_order: boolean, duplicate_setup_calls: boolean }
local config = {
-- list of file that exists in root of the project
root_markers = {
Expand Down Expand Up @@ -37,6 +38,26 @@ local config = {
-- enable 'Configuring DAP' & 'DAP configured' messages on start up
dap = true,
},

-- We do multiple verifications to make sure things are in place to run this
-- plugin
verification = {
-- nvim-java checks for the order of execution of following
-- * require('java').setup()
-- * require('lspconfig').jdtls.setup()
-- IF they are not executed in the correct order, you will see a error
-- notification.
-- Set following to false to disable the notification if you know what you
-- are doing
invalid_order = true,

-- nvim-java checks if the require('java').setup() is called multiple
-- times.
-- IF there are multiple setup calls are executed, an error will be shown
-- Set following property value to false to disable the notification if
-- you know what you are doing
duplicate_setup_calls = true,
},
}

return config
27 changes: 27 additions & 0 deletions lua/java/startup/duplicate-setup-check.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local M = {}

local message = 'require("java").setup() is called more than once'
.. '\nnvim-java will continue to setup but nvim-java configurations might not work as expected'
.. '\nThis might be due to old installation instructions.'
.. '\nPlease check the latest guide at https://github.com/nvim-java/nvim-java#hammer-how-to-install'
.. '\nIf you know what you are doing, you can disable the check from the config'
.. '\nhttps://github.com/nvim-java/nvim-java#wrench-configuration'

function M.is_valid()
if vim.g.nvim_java_setup_is_called then
return {
success = false,
continue = true,
message = message,
}
end

vim.g.nvim_java_setup_is_called = true

return {
success = true,
continue = true,
}
end

return M
46 changes: 46 additions & 0 deletions lua/java/startup/exec-order-check.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local lspconfig = require('lspconfig')

local M = {}

lspconfig.util.on_setup = lspconfig.util.add_hook_before(
lspconfig.util.on_setup,
function(config)
if config.name == 'jdtls' then
vim.g.nvim_java_jdtls_setup_is_called = true
end
end
)

local message = 'Looks like require("lspconfig").jdtls.setup() is called before require("java").setup().'
.. '\nnvim-java will continue to setup but most features may not work as expected'
.. '\nThis might be due to old installation instructions.'
.. '\nPlease check the latest guide at https://github.com/nvim-java/nvim-java#hammer-how-to-install'
.. '\nIf you know what you are doing, you can disable the check from the config'
.. '\nhttps://github.com/nvim-java/nvim-java#wrench-configuration'

function M.is_valid()
if vim.g.nvim_java_jdtls_setup_is_called then
return {
success = false,
continue = true,
message = message,
}
end

local clients = vim.lsp.get_clients({ name = 'jdtls' })

if #clients > 0 then
return {
success = false,
continue = true,
message = message,
}
end

return {
success = true,
continue = true,
}
end

return M
26 changes: 16 additions & 10 deletions lua/java/startup/nvim-dep.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local notify = require('java-core.utils.notify')
local log = require('java.utils.log')

local pkgs = {
Expand Down Expand Up @@ -36,26 +35,33 @@ Please follow the install guide in https://github.com/nvim-java/nvim-java to ins

local M = {}

function M.check()
function M.is_valid()
log.info('check neovim plugin dependencies')
M.neovim_plugin_check()
end

---@private
function M.neovim_plugin_check()
for _, pkg in ipairs(pkgs) do
local ok, _ = pcall(require, pkg.name)

if not ok then
if pkg.warn then
log.warn(pkg.warn)
notify.warn(pkg.warn)
return {
success = false,
continue = true,
message = pkg.warn,
}
else
log.error(pkg.err)
error(pkg.err)
return {
success = false,
continue = false,
message = pkg.err,
}
end
end
end

return {
success = true,
continue = true,
}
end

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

local function get_checkers()
local config = vim.g.nvim_java_config
local checks = {}

if config.verification.invalid_order then
table.insert(checks, select(1, require('java.startup.exec-order-check')))
end

if config.verification.duplicate_setup_calls then
table.insert(
checks,
select(1, require('java.startup.duplicate-setup-check'))
)
end

table.insert(checks, select(1, require('java.startup.nvim-dep')))

return checks
end

return function()
local checkers = get_checkers()

for _, check in ipairs(checkers) do
local check_res = check.is_valid()

if check_res.message then
if not check_res.success then
log.error(check_res.message)
notify.error(check_res.message)
else
log.warn(check_res.message)
notify.warn(check_res.message)
end
end

if not check_res.continue then
return false
end
end

return true
end