mfussenegger / nvim-jdtls

Extensions for the built-in LSP support in Neovim for eclipse.jdt.ls
GNU General Public License v3.0
1.05k stars 61 forks source link

support jump from pom or gradle #499

Closed fengwk closed 1 year ago

fengwk commented 1 year ago

Support jumping from pom.xml or gradle.build files to files associated with the jdt://protocol.

fengwk commented 1 year ago

Here is my usage:

vim.api.nvim_create_augroup("user_jdtls_setup", { clear = true })
vim.api.nvim_create_autocmd(
  { "FileType" },
  { group = "user_jdtls_setup", pattern = "java,ant", callback = setup })
vim.api.nvim_create_autocmd(
  { "FileType" },
  { group = "user_jdtls_setup", pattern = "xml", callback = function()
    local name = vim.fn.expand("%:t")
    if name == "pom.xml" then
      setup()
    end
  end})

The reason for adding the pom.xml file is that the jdt server seems to recognize the pom file. Also, it makes it easier for me to use it because it is much easier to go into the project directory and use nvim pom.xml than to open the java file after delving into the java package.

mfussenegger commented 1 year ago

I still don't understand why this requires the change.

Opening a jdt:// URI should set the filetype for that buffer to java:

https://github.com/mfussenegger/nvim-jdtls/blob/365811ecf97a08d0e2055fba210d65017344fd15/lua/jdtls.lua#L1119

And then the attach routine should run and go through the check here:

https://github.com/mfussenegger/nvim-jdtls/blob/365811ecf97a08d0e2055fba210d65017344fd15/lua/jdtls/setup.lua#L68-L70

What's the behavior you're seeing that this is supposed to fix?

fengwk commented 1 year ago

When I search for the workspace symbol from pom.xml and jump to the jdt:// file, the following code will work:

251d959c95c05f3ad04e71024bd809992805d66b#L88-L96

  local altbuf = vim.fn.bufnr("#", -1)
  if altbuf and altbuf > 0 and try_attach(altbuf) then
    return true
  end
  for _, buf in ipairs(api.nvim_list_bufs()) do
    if api.nvim_buf_is_loaded(buf) and try_attach(buf) then
      return true
    end
  end

It will get the corresponding buf of pom.xml, so it cannot satisfy the condition set by try_attach.

mfussenegger commented 1 year ago

Oh, now I get it. Thanks for explaining

wenjinnn commented 1 year ago

Here is my usage:

vim.api.nvim_create_augroup("user_jdtls_setup", { clear = true })
vim.api.nvim_create_autocmd(
  { "FileType" },
  { group = "user_jdtls_setup", pattern = "java,ant", callback = setup })
vim.api.nvim_create_autocmd(
  { "FileType" },
  { group = "user_jdtls_setup", pattern = "xml", callback = function()
    local name = vim.fn.expand("%:t")
    if name == "pom.xml" then
      setup()
    end
  end})

The reason for adding the pom.xml file is that the jdt server seems to recognize the pom file. Also, it makes it easier for me to use it because it is much easier to go into the project directory and use nvim pom.xml than to open the java file after delving into the java package.

by this usage, I can get jdtls startup with pom.xml, but can't jump to any jdt:// URI.Sorry for disturbance, am I missing something?

fengwk commented 1 year ago

This is my configuration, you can check it: https://github.com/fengwk/enjoy-neovim/blob/main/lua/fengwk/plugins/lsp/lsp-jdtls/jdtls-setup.lua