From 07e07cf303b9a91391af550b2f8c29cc25aab07b Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 5 Feb 2022 13:40:41 +0100 Subject: fix(indents): ignore comment_parsers for indentation --- lua/nvim-treesitter/indent.lua | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'lua') diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua index 9d7a68ee4..db6832098 100644 --- a/lua/nvim-treesitter/indent.lua +++ b/lua/nvim-treesitter/indent.lua @@ -9,6 +9,12 @@ M.avoid_force_reparsing = { yaml = true, } +M.comment_parsers = { + comment = true, + jsdoc = true, + phpdoc = 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) @@ -70,8 +76,20 @@ function M.get_indent(lnum) parser:parse() end - -- get_root_for_position is 0-based. - local root, _, lang_tree = tsutils.get_root_for_position(lnum - 1, 0, parser) + -- Get language tree with smallest range around node that's not a comment parser + local root, lang_tree + parser:for_each_tree(function(tstree, tree) + if not tstree or M.comment_parsers[tree:lang()] then + return + end + local local_root = tstree:root() + if tsutils.is_in_node_range(local_root, lnum - 1, 0) then + if not root or tsutils.node_length(root) >= tsutils.node_length(local_root) then + root = local_root + lang_tree = tree + end + end + end) -- Not likely, but just in case... if not root then -- cgit v1.2.3-70-g09d2