From b06961a519c7494d0d0509a9eadd6bbf72b9bbf5 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 22 Jan 2022 19:10:34 +0100 Subject: fix(indents): re-parse before each indent --- lua/nvim-treesitter/indent.lua | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'lua') diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua index 4ec0a3e23..6230d11bd 100644 --- a/lua/nvim-treesitter/indent.lua +++ b/lua/nvim-treesitter/indent.lua @@ -1,7 +1,6 @@ local parsers = require "nvim-treesitter.parsers" local queries = require "nvim-treesitter.query" local tsutils = require "nvim-treesitter.ts_utils" -local highlighter = require "vim.treesitter.highlighter" local function get_first_node_at_line(root, lnum) local col = vim.fn.indent(lnum) @@ -18,7 +17,7 @@ local function find_delimiter(bufnr, node, delimiter) if child:type() == delimiter then local linenr = child:start() local line = vim.api.nvim_buf_get_lines(bufnr, linenr, linenr + 1, false)[1] - local end_char = {child:end_()} + local end_char = { child:end_() } return child, #line == end_char[2] end end @@ -56,6 +55,10 @@ function M.get_indent(lnum) if not parser or not lnum then return -1 end + + -- Reparse in case we got triggered by ":h indentkeys" + parser:parse() + local bufnr = vim.api.nvim_get_current_buf() -- get_root_for_position is 0-based. @@ -81,7 +84,8 @@ function M.get_indent(lnum) local indent_size = vim.fn.shiftwidth() local indent = 0 - if root:start() ~= 0 then + local _, _, root_start = root:start() + if root_start ~= 0 then -- injected tree indent = vim.fn.indent(root:start() + 1) end @@ -119,7 +123,13 @@ function M.get_indent(lnum) end -- do not indent for nodes that starts-and-ends on same line and starts on target line (lnum) - if not is_processed_by_row[srow] and (q.indent[node:id()] and srow ~= erow and ((srow ~= lnum - 1) or q.indent[node:id()].start_at_same_line)) then + if + not is_processed_by_row[srow] + -- Dear stylua, please don't change the semantics of this statement! + -- stylua: ignore start + and (q.indent[node:id()] and srow ~= erow and ((srow ~= lnum - 1) or q.indent[node:id()].start_at_same_line)) + -- stylua: ignore end + then indent = indent + indent_size is_processed = true end -- cgit v1.2.3-70-g09d2