Closed c3n21 closed 2 years ago
Apparently it was a problem because in packer.nvim I had ft set
use {
'mfussenegger/nvim-jdtls',
disable = false,
ft = "java"
}
but still I don't get why it would give me such an error
Maybe an ordering issue? I assume packer loads the package lazy if you set ft = 'java'
and the ftplugin/java.lua
file is maybe executed before packer can lazy load it.
If you want to get lazy loading, you could try with disable = true
and then add a vim.cmd('packadd nvim-jdtls')
or something like that to your ftplugin/java.lua
. (I'm not using packer, but I think it still uses the vim packages mechanism)
local jdtls = require "jdtls"
if jdtls ~= nil then
This will afaik always fail if jdtls
is not available. If you want to check if it is available without failing, you'd have to use something like this:
local ok, jdtls = require('jdtls')
if ok then
...
end
Maybe an ordering issue? I assume packer loads the package lazy if you set
ft = 'java'
and theftplugin/java.lua
file is maybe executed before packer can lazy load it.If you want to get lazy loading, you could try with
disable = true
and then add avim.cmd('packadd nvim-jdtls')
or something like that to yourftplugin/java.lua
. (I'm not using packer, but I think it still uses the vim packages mechanism)
local jdtls = require "jdtls" if jdtls ~= nil then
This will afaik always fail if
jdtls
is not available. If you want to check if it is available without failing, you'd have to use something like this:local ok, jdtls = require('jdtls') if ok then ... end
What I would like to achieve is using packer.nvim as a centralized plugin manager so that I can able and disable plugins without so much hassle.
Ig that using after/ftplugin/java.lua could give me some errors if in the future I want to temporarily disable it
@c3n21 how do you use the packer use
specification ? I guess you didn't set the module
key to jdtls
.
I have tested your approach with below configuration
-- packer plugin declaration
use({
"mfussenegger/nvim-jdtls",
config = function()
-- https://github.com/fitrh/init.nvim/blob/main/lua/plugin/jdtls/config.lua
require("plugin.jdtls.config").attach()
end,
module = "jdtls",
})
-- after/ftplugin/java.lua
require("jdtls")
@c3n21 how do you use the packer
use
specification ? I guess you didn't set themodule
key tojdtls
.I have tested your approach with below configuration
-- packer plugin declaration use({ "mfussenegger/nvim-jdtls", config = function() -- https://github.com/fitrh/init.nvim/blob/main/lua/plugin/jdtls/config.lua require("plugin.jdtls.config").attach() end, module = "jdtls", })
-- after/ftplugin/java.lua require("jdtls")
That's interesting, I missed out that you could set "module" in the config.
But a problem still persists: if I want to remove nvim-jdtls
, I would also need to comment out after/ftplugin/java.lua
In the end I've managed to work it around with this
-- inside lua/config/nvim-jdtls/init.lua
if packer_plugins["nvim-jdtls"] then
local coq = require "coq" -- add this (my completion plugin)
local jdtls = require "jdtls"
local workspace_root = os.getenv('HOME') .. '/.config/nvim-jdtls/workspace/' .. vim.fn.fnamemodify(vim.fn.getcwd(),':p:h:t')
local config = coq.lsp_ensure_capabilities({
cmd = {
'jdtls.sh', workspace_root,
},
root_dir = jdtls.setup.find_root({'gradle.build', 'pom.xml', ".git"})
})
jdtls.start_or_attach(config)
else
print("nvim-jdtls not installed")
end
so that if the plugin is not installed it will just print a message and keep after/ftplugin/java.lua always there even when I have nvim-jdtls uninstalled.
LSP client configuration
Eclipse.jdt.ls version
1.5.0
Steps to Reproduce
EDIT:
Expected Result
There should be no error?
Actual Result
` Error detected while processing /home/zhifan/.config/nvim/after/ftplugin/java.lua:
E5113: Error while calling lua chunk: /home/zhifan/.config/nvim/after/ftplugin/java.lua:2: module 'jdtls' not found:
stack traceback:
`
This happens only the first time I open a java file, after that subsequent java file that I open doesn't have any issue.
Also, the jdtls attaches properly to each buffer.