From aa035cdf2ae60eeedee76c59490d5bcce569af2a Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 23 Jan 2022 15:52:03 +0100 Subject: workaround(indents): avoid reparsing for indent sensitive yaml/python --- lua/nvim-treesitter/indent.lua | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'lua') diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua index 6230d11bd..6430a603c 100644 --- a/lua/nvim-treesitter/indent.lua +++ b/lua/nvim-treesitter/indent.lua @@ -2,6 +2,13 @@ local parsers = require "nvim-treesitter.parsers" local queries = require "nvim-treesitter.query" local tsutils = require "nvim-treesitter.ts_utils" +local M = {} + +M.avoid_force_reparsing = { + python = true, + yaml = true, +} + local function get_first_node_at_line(root, lnum) local col = vim.fn.indent(lnum) return root:descendant_for_range(lnum - 1, col, lnum - 1, col) @@ -23,8 +30,6 @@ local function find_delimiter(bufnr, node, delimiter) end end -local M = {} - local get_indents = tsutils.memoize_by_buf_tick(function(bufnr, root, lang) local map = { auto = {}, @@ -51,15 +56,19 @@ end, { ---@param lnum number (1-indexed) function M.get_indent(lnum) - local parser = parsers.get_parser() + local bufnr = vim.api.nvim_get_current_buf() + local parser = parsers.get_parser(bufnr) if not parser or not lnum then return -1 end - -- Reparse in case we got triggered by ":h indentkeys" - parser:parse() + local root_lang = parsers.get_buf_lang(bufnr) - local bufnr = vim.api.nvim_get_current_buf() + -- some languages like Python will actually have worse results when re-parsing at opened new line + if not M.avoid_force_reparsing[root_lang] then + -- Reparse in case we got triggered by ":h indentkeys" + parser:parse() + end -- get_root_for_position is 0-based. local root, _, lang_tree = tsutils.get_root_for_position(lnum - 1, 0, parser) -- cgit v1.2.3-70-g09d2