nvim-treesitter / nvim-treesitter-refactor

Refactor module for nvim-treesitter
Apache License 2.0
407 stars 25 forks source link

Can goto_definition of treesitter use as the fallback of LSP? #13

Closed xaljer closed 3 years ago

xaljer commented 3 years ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] If I have the LSP environment of a language, I would like to use it to find definition due to better cross files finding. And treesitter would be a good fallback for more language support. And I think fallback of treesitter should be default gd of vim. Just providing a different view of priority of this tools.

Describe the solution you'd like A clear and concise description of what you want to happen.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

theHamsta commented 3 years ago

Yes, this is already implemented: https://github.com/theHamsta/nvim-treesitter-refactor/blob/4730889038caa05253467faed2a0d1faaef36d00/lua/nvim-treesitter-refactor/navigation.lua#L26 https://github.com/theHamsta/nvim-treesitter-refactor/blob/4730889038caa05253467faed2a0d1faaef36d00/lua/nvim-treesitter-refactor/navigation.lua#L26

This should work

  lua <<EOF
  require'nvim-treesitter.configs'.setup {
    refactor = {
      navigation = {
        enable = true,
        keymaps = {
          goto_definition_lsp_fallback = "gnd",
          list_definitions = "gnD",
          list_definitions_toc = "gO",
          goto_next_usage = "<a-*>",
          goto_previous_usage = "<a-#>",
        },
      },
    },
  }
  EOF
xaljer commented 3 years ago

@theHamsta May be I didn't state this clearly~

As explained in README:

vim.lsp.buf.definition is used if nvim-treesitter can not resolve the variable.

But not: using nvim-treesitter if LSP can not resolve the variable, which is what I want.

I think a better way to do this is that providing this feature as a command (or a function, a result need to know if it can resolve this), and combine different tools by user. For example, I don't use builtin LSP but the coc.nvim plugin, so above method can't work. And I combine LSP and gtags by myself like:

function! s:GoToDefinition()
    if CocAction('jumpDefinition')
        return v:true
    endif
    execute("Leaderf! gtags --auto-jump -d " . expand("<cword>"))
endfunction
theHamsta commented 3 years ago

But then you can simply call that Lua function instead of you tag command lua require(....).path.to.function()

xaljer commented 3 years ago

@theHamsta I see, thanks.

theHamsta commented 3 years ago

I think for built-in LSP you would have to write a custom callback