mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-06 13:30:01 -04:00
Expose internal api.
- add `exposed_state` to expose 'current_node' and 'cursor_pos' for a current buffer to the user. - add `get_buf_state` and `get_node_api` for users. - add documentation about api functions. - remove `node_movement` module which should be done in user side.
This commit is contained in:
parent
5a66c38b9f
commit
8be2dc64a2
7 changed files with 108 additions and 108 deletions
|
|
@ -5,6 +5,7 @@ local utils = require'nvim-treesitter.utils'
|
|||
local info = require'nvim-treesitter.info'
|
||||
local configs = require'nvim-treesitter.configs'
|
||||
local state = require'nvim-treesitter.state'
|
||||
local ts_utils = require'nvim-treesitter.ts_utils'
|
||||
|
||||
local M = {}
|
||||
|
||||
|
|
@ -55,4 +56,13 @@ function M.statusline(indicator_size)
|
|||
end
|
||||
end
|
||||
|
||||
function M.get_buf_state()
|
||||
local bufnr = api.nvim_get_current_buf()
|
||||
return state.exposed_state(bufnr)
|
||||
end
|
||||
|
||||
function M.get_node_api()
|
||||
return ts_utils
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
|
|
@ -228,19 +228,6 @@ local config = {
|
|||
is_supported = function(ft)
|
||||
return queries.get_query(ft, 'locals')
|
||||
end
|
||||
},
|
||||
node_movement = {
|
||||
enable = false,
|
||||
disable = {},
|
||||
is_supported = function(ft)
|
||||
return queries.get_query(ft, 'locals')
|
||||
end,
|
||||
keymaps = {
|
||||
parent_scope = "<a-k>",
|
||||
child_scope = "<a-j>",
|
||||
next_scope = "<a-l>",
|
||||
previous_scope = "<a-h>",
|
||||
},
|
||||
}
|
||||
},
|
||||
ensure_installed = nil
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
local api = vim.api
|
||||
|
||||
local configs = require'nvim-treesitter.configs'
|
||||
local state = require'nvim-treesitter.state'
|
||||
local ts_utils = require'nvim-treesitter.ts_utils'
|
||||
|
||||
local M = {}
|
||||
|
||||
local NodeMovementKind = {
|
||||
parent_scope = 'parent',
|
||||
child_scope = 'child',
|
||||
next_scope = 'next',
|
||||
previous_scope = 'previous',
|
||||
}
|
||||
|
||||
local get_node_fn = {
|
||||
[NodeMovementKind.parent_scope] = function(node, curpos)
|
||||
return ts_utils.parent_scope(node, curpos)
|
||||
end,
|
||||
[NodeMovementKind.child_scope] = function(node, curpos)
|
||||
return ts_utils.nested_scope(node, curpos)
|
||||
end,
|
||||
[NodeMovementKind.next_scope] = function(node)
|
||||
return ts_utils.next_scope(node)
|
||||
end,
|
||||
[NodeMovementKind.previous_scope] = function(node)
|
||||
return ts_utils.previous_scope(node)
|
||||
end,
|
||||
}
|
||||
|
||||
M.do_node_movement = function(kind)
|
||||
local buf = api.nvim_get_current_buf()
|
||||
|
||||
local buf_state = state.get_buf_state(buf)
|
||||
if not buf_state then return end
|
||||
|
||||
local current_node = buf_state.current_node
|
||||
if not current_node then return end
|
||||
|
||||
local destination_node = get_node_fn[kind](current_node, buf_state.cursor_pos)
|
||||
|
||||
if destination_node then
|
||||
local row, col = destination_node:start()
|
||||
vim.fn.setpos(".", { buf, row+1, col+1, 0 })
|
||||
end
|
||||
end
|
||||
|
||||
function M.parent_scope() M.do_node_movement(NodeMovementKind.parent_scope) end
|
||||
function M.child_scope() M.do_node_movement(NodeMovementKind.child_scope) end
|
||||
function M.next_scope() M.do_node_movement(NodeMovementKind.next_scope) end
|
||||
function M.previous_scope() M.do_node_movement(NodeMovementKind.previous_scope) end
|
||||
|
||||
function M.attach(bufnr)
|
||||
local bufnr = bufnr or api.nvim_get_current_buf()
|
||||
|
||||
local config = configs.get_module('node_movement')
|
||||
for funcname, mapping in pairs(config.keymaps) do
|
||||
local cmd = string.format(":lua require'nvim-treesitter.node_movement'.%s()<CR>", funcname)
|
||||
api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { silent = true })
|
||||
end
|
||||
end
|
||||
|
||||
function M.detach(bufnr)
|
||||
local buf = bufnr or api.nvim_get_current_buf()
|
||||
|
||||
local config = configs.get_module('node_movement')
|
||||
for _, mapping in pairs(config.keymaps) do
|
||||
api.nvim_buf_del_keymap(buf, 'n', mapping)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -111,4 +111,12 @@ function M.get_buf_state(bufnr)
|
|||
return buffers[bufnr]
|
||||
end
|
||||
|
||||
function M.exposed_state(bufnr)
|
||||
local buf_state = buffers[bufnr]
|
||||
return {
|
||||
cursor_pos = buf_state.cursor_pos,
|
||||
current_node = buf_state.current_node
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue