feat(refactor.navigation): add navigation.goto_{next,previous}_usage

This commit is contained in:
Stephan Seitz 2020-08-22 20:46:40 +02:00 committed by Thomas Vigouroux
parent ffe7d96dfd
commit 5948aba886
7 changed files with 72 additions and 20 deletions

View file

@ -185,6 +185,9 @@ EOF
Provides "go to definition" for the symbol under the cursor,
and lists the definitions from the current file.
goto_next_usage/goto_previous_usage go to the next usage of the
identifier under the cursor.
```lua
lua <<EOF
@ -195,6 +198,8 @@ require'nvim-treesitter.configs'.setup {
keymaps = {
goto_definition = "gnd",
list_definitions = "gnD",
goto_next_usage = "<a-*>",
goto_previous_usage = "<a-#>",
},
},
},

View file

@ -246,6 +246,10 @@ Supported options:
Defaults to `gnd`.
- list_definitions: list all definitions from the current file.
Defaults to `gnD`.
- goto_next_usage: go to next usage of identifier under the cursor.
Defaults to `<a-*>`.
- goto_previous_usage: go to previous usage of identifier.
Defaults to `<a-#>`.
>
lua <<EOF
@ -256,6 +260,8 @@ Supported options:
keymaps = {
goto_definition = "gnd",
list_definitions = "gnD",
goto_next_usage = "<a-*>",
goto_previous_usage = "<a-#>",
},
},
},
@ -513,6 +519,14 @@ Returns the previous node within the same parent.
`allow_switch_parent` and `allow_prev_parent` follow the same rule
as |ts_utils.get_next_node| but if the node is the first node.
*ts_utils.goto_node*
goto_node(node, goto_end, avoid_set_jump)~
Sets cursor to the position of `node` in the current windows.
If `goto_end` is truthy, the cursor is set to the end the node range.
Setting `avoid_set_jump` to `true`, avoids setting the current cursor position
to the jump list.
==============================================================================
FUNCTIONS *nvim-treesitter-functions*

View file

@ -75,7 +75,9 @@ local builtin_modules = {
is_supported = queries.has_locals,
keymaps = {
goto_definition = "gnd",
list_definitions = "gnD"
list_definitions = "gnD",
goto_next_usage = "<a-*>",
goto_previous_usage = "<a-#>",
}
}
},

View file

@ -12,14 +12,10 @@ function M.goto_definition(bufnr)
local bufnr = bufnr or api.nvim_get_current_buf()
local node_at_point = ts_utils.get_node_at_cursor()
utils.set_jump()
if not node_at_point then return end
local definition, _ = locals.find_definition(node_at_point, bufnr)
local start_row, start_col, _ = definition:start()
api.nvim_win_set_cursor(0, { start_row + 1, start_col })
ts_utils.goto_node(definition)
end
function M.list_definitions(bufnr)
@ -48,6 +44,25 @@ function M.list_definitions(bufnr)
api.nvim_command('copen')
end
function M.goto_adjacent_usage(bufnr, delta)
local bufnr = bufnr or api.nvim_get_current_buf()
local node_at_point = ts_utils.get_node_at_cursor()
if not node_at_point then return end
local def_node, scope = locals.find_definition(node_at_point, bufnr)
local usages = locals.find_usages(def_node, scope, bufnr)
local index = utils.index_of(usages, node_at_point)
if not index then return end
local target_index = (index + delta + #usages - 1) % #usages + 1
ts_utils.goto_node(usages[target_index])
return usages[target_index]
end
function M.goto_next_usage(bufnr) return M.goto_adjacent_usage(bufnr, 1) end
function M.goto_previous_usage(bufnr) return M.goto_adjacent_usage(bufnr, -1) end
function M.attach(bufnr)
local config = configs.get_module('refactor.navigation')

View file

@ -1,7 +1,6 @@
local utils = require'nvim-treesitter.utils'
local ts_utils = require'nvim-treesitter.ts_utils'
local shared = require'nvim-treesitter.textobjects.shared'
local attach = require'nvim-treesitter.textobjects.attach'
local api = vim.api
local M = {}
@ -9,18 +8,7 @@ function M.goto_adjacent(query_string, forward, start, same_parent, overlapping_
local bufnr, _, node = shared.textobject_at_point(query_string)
local adjacent = shared.get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr)
if adjacent then
utils.set_jump()
local adjacent_textobject_range = {adjacent:range()}
local position
if start then
position = { adjacent_textobject_range[1] + 1, adjacent_textobject_range[2] }
else
position = { adjacent_textobject_range[3] + 1, adjacent_textobject_range[4] }
end
api.nvim_win_set_cursor(api.nvim_get_current_win(), position)
end
ts_utils.goto_node(adjacent, not start)
end
-- luacheck: push ignore 631

View file

@ -276,4 +276,24 @@ function M.swap_nodes(node_or_range1, node_or_range2, bufnr, cursor_to_second)
end
end
function M.goto_node(node, goto_end, avoid_set_jump)
if not node then return end
if not avoid_set_jump then
utils.set_jump()
end
local range = {node:range()}
local position
if not goto_end then
position = { range[1], range[2] }
else
-- ranges are exclusive: -1 character!
if range[4] == 0 then
position = { range[3] - 1, -1 }
else
position = { range[3], range[4] - 1 }
end
end
api.nvim_win_set_cursor(0, { position[1] + 1, position[2] })
end
return M

View file

@ -67,4 +67,12 @@ function M.set_jump()
vim.cmd "normal! m'"
end
function M.index_of(tbl, obj)
for i, o in ipairs(tbl) do
if o == obj then
return i
end
end
end
return M