feat(refactor.navigation): allow a fallback_function for goto_definition

`fallback_function` is called when nvim-treesitter can not resolve the
variable under the cursor.
This commit is contained in:
Stephan Seitz 2020-08-29 11:37:26 +02:00 committed by Thomas Vigouroux
parent 6352cdc943
commit bc36521967
3 changed files with 19 additions and 7 deletions

View file

@ -184,9 +184,10 @@ EOF
## Refactor: navigation ## Refactor: navigation
Provides "go to definition" for the symbol under the cursor, Provides "go to definition" for the symbol under the cursor,
and lists the definitions from the current file. and lists the definitions from the current file. If you use
goto_next_usage/goto_previous_usage go to the next usage of the `goto_definition_lsp_fallback` instead of `goto_definition` in the config below
identifier under the cursor. `vim.lsp.buf.definition` is used if nvim-treesitter can not resolve the variable.
`goto_next_usage`/`goto_previous_usage` go to the next usage of the identifier under the cursor.
```lua ```lua

View file

@ -244,6 +244,11 @@ Supported options:
- keymaps: - keymaps:
- goto_definition: go to the definition of the symbol under the cursor. - goto_definition: go to the definition of the symbol under the cursor.
Defaults to `gnd`. Defaults to `gnd`.
- goto_definition_lsp_fallback: go to the definition of the symbol under
the cursor or use vim.lsp.buf.definition if the symbol can not be
resolved. You can use your own fallback function if create a mapping for
`lua require'nvim-treesitter.refactor.navigation(nil, fallback_function)<cr>` .
No default mapping
- list_definitions: list all definitions from the current file. - list_definitions: list all definitions from the current file.
Defaults to `gnD`. Defaults to `gnD`.
- goto_next_usage: go to next usage of identifier under the cursor. - goto_next_usage: go to next usage of identifier under the cursor.

View file

@ -8,16 +8,23 @@ local api = vim.api
local M = {} local M = {}
function M.goto_definition(bufnr) function M.goto_definition(bufnr, fallback_function)
local bufnr = bufnr or api.nvim_get_current_buf() local bufnr = bufnr or api.nvim_get_current_buf()
local node_at_point = ts_utils.get_node_at_cursor() local node_at_point = ts_utils.get_node_at_cursor()
if not node_at_point then return end if not node_at_point then return end
local definition, _ = locals.find_definition(node_at_point, bufnr) local definition = locals.find_definition(node_at_point, bufnr)
ts_utils.goto_node(definition)
if fallback_function and definition.id == node_at_point.id then
fallback_function()
else
ts_utils.goto_node(definition)
end
end end
function M.goto_definition_lsp_fallback(bufnr) M.goto_definition(bufnr, vim.lsp.buf.definition) end
function M.list_definitions(bufnr) function M.list_definitions(bufnr)
local bufnr = bufnr or api.nvim_get_current_buf() local bufnr = bufnr or api.nvim_get_current_buf()
local definitions = locals.get_definitions(bufnr) local definitions = locals.get_definitions(bufnr)
@ -57,7 +64,6 @@ function M.goto_adjacent_usage(bufnr, delta)
local target_index = (index + delta + #usages - 1) % #usages + 1 local target_index = (index + delta + #usages - 1) % #usages + 1
ts_utils.goto_node(usages[target_index]) ts_utils.goto_node(usages[target_index])
return usages[target_index]
end end
function M.goto_next_usage(bufnr) return M.goto_adjacent_usage(bufnr, 1) end function M.goto_next_usage(bufnr) return M.goto_adjacent_usage(bufnr, 1) end