mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-20 04:10:06 -04:00
indent: introduce @return to further tune indentation on o/<cr>
This commit is contained in:
parent
32cc79b7c9
commit
5123231424
1 changed files with 20 additions and 19 deletions
|
|
@ -22,20 +22,20 @@ local function node_fmt(node)
|
||||||
end
|
end
|
||||||
|
|
||||||
local get_indents = utils.memoize_by_buf_tick(function(bufnr)
|
local get_indents = utils.memoize_by_buf_tick(function(bufnr)
|
||||||
local indents = queries.get_capture_matches(bufnr, '@indent.node', 'indents') or {}
|
local get_map = function(capture)
|
||||||
local branches = queries.get_capture_matches(bufnr, '@branch.node', 'indents') or {}
|
local matches = queries.get_capture_matches(bufnr, capture, 'indents') or {}
|
||||||
|
local map = {}
|
||||||
local indents_map = {}
|
for _, node in ipairs(matches) do
|
||||||
for _, node in ipairs(indents) do
|
map[tostring(node)] = true
|
||||||
indents_map[tostring(node)] = true
|
end
|
||||||
|
return map
|
||||||
end
|
end
|
||||||
|
|
||||||
local branches_map = {}
|
return {
|
||||||
for _, node in ipairs(branches) do
|
indents = get_map('@indent.node'),
|
||||||
branches_map[tostring(node)] = true
|
branches = get_map('@branch.node'),
|
||||||
end
|
returns = get_map('@return.node'),
|
||||||
|
}
|
||||||
return { indents = indents_map, branches = branches_map }
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local function get_indent_size()
|
local function get_indent_size()
|
||||||
|
|
@ -46,9 +46,7 @@ function M.get_indent(lnum)
|
||||||
local parser = parsers.get_parser()
|
local parser = parsers.get_parser()
|
||||||
if not parser or not lnum then return -1 end
|
if not parser or not lnum then return -1 end
|
||||||
|
|
||||||
local indent_queries = get_indents(vim.api.nvim_get_current_buf())
|
local q = get_indents(vim.api.nvim_get_current_buf())
|
||||||
local indents = indent_queries.indents
|
|
||||||
local branches = indent_queries.branches
|
|
||||||
local root = parser:parse()[1]:root()
|
local root = parser:parse()[1]:root()
|
||||||
local node = get_node_at_line(root, lnum-1)
|
local node = get_node_at_line(root, lnum-1)
|
||||||
|
|
||||||
|
|
@ -64,7 +62,10 @@ function M.get_indent(lnum)
|
||||||
local prev_node = get_node_at_line(root, prevnonblank-1)
|
local prev_node = get_node_at_line(root, prevnonblank-1)
|
||||||
-- we take that node only if ends before lnum, or else we would get incorrect indent
|
-- we take that node only if ends before lnum, or else we would get incorrect indent
|
||||||
-- on <cr> in positions like e.g. `{|}` in C (| denotes cursor position)
|
-- on <cr> in positions like e.g. `{|}` in C (| denotes cursor position)
|
||||||
if prev_node and (prev_node:end_() < lnum-1) then
|
local use_prev = prev_node and (prev_node:end_() < lnum-1)
|
||||||
|
-- nodes can be marked @return to prevent using them
|
||||||
|
use_prev = use_prev and not q.returns[node_fmt(prev_node)]
|
||||||
|
if use_prev then
|
||||||
node = prev_node
|
node = prev_node
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -75,12 +76,12 @@ function M.get_indent(lnum)
|
||||||
if not node then
|
if not node then
|
||||||
local wrapper = root:descendant_for_range(lnum-1, 0, lnum-1, -1)
|
local wrapper = root:descendant_for_range(lnum-1, 0, lnum-1, -1)
|
||||||
node = wrapper:child(0) or wrapper
|
node = wrapper:child(0) or wrapper
|
||||||
if indents[node_fmt(wrapper)] ~= nil and wrapper ~= root then
|
if q.indents[node_fmt(wrapper)] ~= nil and wrapper ~= root then
|
||||||
indent = indent_size
|
indent = indent_size
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
while node and branches[node_fmt(node)] do
|
while node and q.branches[node_fmt(node)] do
|
||||||
node = node:parent()
|
node = node:parent()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -89,7 +90,7 @@ function M.get_indent(lnum)
|
||||||
while node do
|
while node do
|
||||||
node = node:parent()
|
node = node:parent()
|
||||||
local row = node and node:start() or prev_row
|
local row = node and node:start() or prev_row
|
||||||
if indents[node_fmt(node)] and prev_row ~= row then
|
if q.indents[node_fmt(node)] and prev_row ~= row then
|
||||||
indent = indent + indent_size
|
indent = indent + indent_size
|
||||||
prev_row = row
|
prev_row = row
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue