mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-18 03:10:04 -04:00
indent fixes
This commit is contained in:
parent
3dbbc2dc7d
commit
f2c219cddc
2 changed files with 39 additions and 15 deletions
|
|
@ -4,6 +4,7 @@ local utils = require'nvim-treesitter.ts_utils'
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
-- TODO(kiyan): move this in tsutils and document it
|
||||||
local function get_node_at_line(root, lnum)
|
local function get_node_at_line(root, lnum)
|
||||||
for node in root:iter_children() do
|
for node in root:iter_children() do
|
||||||
local srow, _, erow = node:range()
|
local srow, _, erow = node:range()
|
||||||
|
|
@ -13,10 +14,11 @@ local function get_node_at_line(root, lnum)
|
||||||
return get_node_at_line(node, lnum)
|
return get_node_at_line(node, lnum)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local wrapper = root:descendant_for_range(lnum, 0, lnum, -1)
|
local function node_fmt(node)
|
||||||
local child = wrapper:child(0)
|
if not node then return nil end
|
||||||
return child or wrapper
|
return tostring(node)
|
||||||
end
|
end
|
||||||
|
|
||||||
local get_indents = utils.memoize_by_buf_tick(function(bufnr)
|
local get_indents = utils.memoize_by_buf_tick(function(bufnr)
|
||||||
|
|
@ -36,30 +38,51 @@ local get_indents = utils.memoize_by_buf_tick(function(bufnr)
|
||||||
return { indents = indents_map, branches = branches_map }
|
return { indents = indents_map, branches = branches_map }
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local function get_indent_size()
|
||||||
|
return vim.bo.softtabstop < 0 and vim.bo.shiftwidth or vim.bo.tabstop
|
||||||
|
end
|
||||||
|
|
||||||
function M.get_indent(lnum)
|
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 node = get_node_at_line(parser:parse()[1]:root(), lnum-1)
|
|
||||||
local indent_queries = get_indents(vim.api.nvim_get_current_buf())
|
local indent_queries = get_indents(vim.api.nvim_get_current_buf())
|
||||||
local indents = indent_queries.indents
|
local indents = indent_queries.indents
|
||||||
local branches = indent_queries.branches
|
local branches = indent_queries.branches
|
||||||
if not indents then return 0 end
|
local root = parser:parse()[1]:root()
|
||||||
|
local node = get_node_at_line(root, lnum-1)
|
||||||
|
|
||||||
while node and branches[tostring(node)] do
|
local indent = 0
|
||||||
node = node:parent()
|
local indent_size = get_indent_size()
|
||||||
end
|
|
||||||
|
|
||||||
local ind_size = vim.bo.softtabstop < 0 and vim.bo.shiftwidth or vim.bo.tabstop
|
-- if we are on a new line (for instance by typing `o` or `O`)
|
||||||
local ind = 0
|
-- we should get the node that wraps the line our cursor sits in
|
||||||
while node do
|
-- and if the node is an indent node, we should set the indent level as the indent_size
|
||||||
node = node:parent()
|
-- and we set the node as the first child of this wrapper node or the wrapper itself
|
||||||
if indents[tostring(node)] then
|
if not node then
|
||||||
ind = ind + ind_size
|
local wrapper = root:descendant_for_range(lnum, 0, lnum, -1)
|
||||||
|
node = wrapper:child(0) or wrapper
|
||||||
|
if indents[node_fmt(wrapper)] ~= nil and wrapper ~= root then
|
||||||
|
indent = indent_size
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return ind
|
while node and branches[node_fmt(node)] do
|
||||||
|
node = node:parent()
|
||||||
|
end
|
||||||
|
|
||||||
|
local prev_row = node:start()
|
||||||
|
|
||||||
|
while node do
|
||||||
|
node = node:parent()
|
||||||
|
local row = node and node:start() or prev_row
|
||||||
|
if indents[node_fmt(node)] and prev_row ~= row then
|
||||||
|
indent = indent + indent_size
|
||||||
|
prev_row = row
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return indent
|
||||||
end
|
end
|
||||||
|
|
||||||
local indent_funcs = {}
|
local indent_funcs = {}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
(for_statement)
|
(for_statement)
|
||||||
(for_in_statement)
|
(for_in_statement)
|
||||||
(repeat_statement)
|
(repeat_statement)
|
||||||
|
(return_statement)
|
||||||
(while_statement)
|
(while_statement)
|
||||||
(table)
|
(table)
|
||||||
(arguments)
|
(arguments)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue