From a8ce31be834ccbc1968fb58a46f2dcd36117a601 Mon Sep 17 00:00:00 2001 From: Austin Liu Date: Wed, 18 Feb 2026 12:17:24 -0800 Subject: [PATCH 1/3] fix(indent): improve indentation --- .../modules/core/esupports/indent/module.lua | 100 ++++++++---------- 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/lua/neorg/modules/core/esupports/indent/module.lua b/lua/neorg/modules/core/esupports/indent/module.lua index b0d334f81..8faba6319 100644 --- a/lua/neorg/modules/core/esupports/indent/module.lua +++ b/lua/neorg/modules/core/esupports/indent/module.lua @@ -43,29 +43,21 @@ module.public = { return 0 end - local indent_data = module.config.public.indents[node:type()] or module.config.public.indents._ - - if not indent_data then + local config_indent_data = module.config.public.indents[node:type()] or module.config.public.indents._ + if not config_indent_data then return 0 end - local _, initial_indent = node:start() - - local indent = 0 - - for _, modifier in ipairs(indent_data.modifiers or {}) do - if module.config.public.modifiers[modifier] then - local ret = module.config.public.modifiers[modifier](buf, node, line, initial_indent) ---@diagnostic disable-line -- TODO: type error workaround - - if ret ~= 0 then - indent = ret - end - end + -- Check if the code is within a verbatim block + local parent_ranged_verbatim_tag = + module.required["core.integrations.treesitter"].find_parent(node, "ranged_verbatim_tag$") + if parent_ranged_verbatim_tag then + config_indent_data = module.config.public.indents[parent_ranged_verbatim_tag:type()] + or module.config.public.indents._ end - local line_len = (vim.api.nvim_buf_get_lines(buf, line, line + 1, true)[1] or ""):len() - -- Ensure that the cursor is within the `norg` language + local line_len = (vim.api.nvim_buf_get_lines(buf, line, line + 1, true)[1] or ""):len() local current_lang = vim.treesitter.get_parser(buf, "norg"):language_for_range({ line, line_len, @@ -75,61 +67,55 @@ module.public = { -- If it isn't then fall back to `nvim-treesitter`'s indent instead. if current_lang:lang() ~= "norg" then - -- If we're in a ranged tag then apart from providing nvim-treesitter indents also make sure - -- to account for the indentation level of the tag itself. - if node:type() == "ranged_verbatim_tag_content" then - local lnum = line - local start = node:range() - - while lnum > start do - if vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, true)[1]:match("^%s*$") then - lnum = lnum - 1 - else - return vim.fn["nvim_treesitter#indent"]() - end - end - - return module.required["core.integrations.treesitter"].get_node_range(node:parent()).column_start - + vim.fn["nvim_treesitter#indent"]() + if parent_ranged_verbatim_tag ~= nil then + return math.max( + require("nvim-treesitter").indentexpr(), + module.required["core.integrations.treesitter"].get_node_range( + ---@diagnostic disable-next-line: param-type-mismatch + parent_ranged_verbatim_tag -- ranged_verbatim_tag, e.g. `@code lua` + ).column_start + ) else - return vim.fn["nvim_treesitter#indent"]() + return require("nvim-treesitter").indentexpr() end end - -- Check if the code is within a verbatim block - local ranged_tag = - module.required["core.integrations.treesitter"].find_parent(node, "ranged_verbatim_tag_content") - indent_data = ranged_tag and module.config.public.indents[ranged_tag:type()] or indent_data - - -- Indents can be a static value, so account for that here - if type(indent_data.indent) == "number" then - -- If the indent is -1 then let Neovim indent instead of us - if indent_data.indent == -1 then - return -1 - end - - local new_indent = indent + indent_data.indent + (module.config.public.tweaks[node:type()] or 0) - - if (not module.config.public.dedent_excess) and new_indent <= initial_indent then - return initial_indent + -- Compute indentation of configured modifiers + -- - the value of the last modifier that returns a non-zero value is used + -- local _, initial_indent = node:start() + local initial_indent = vim.fn.indent(line + 1) + local modifier_indent = 0 + for _, modifier in ipairs(config_indent_data.modifiers or {}) do + if module.config.public.modifiers[modifier] then + local ret = module.config.public.modifiers[modifier](buf, node, line, initial_indent) ---@diagnostic disable-line -- TODO: type error workaround + if ret ~= 0 then + modifier_indent = ret + end end - - return new_indent end - local calculated_indent = indent_data.indent(buf, node, line, indent, initial_indent) or 0 + local config_indent = config_indent_data.indent + if type(config_indent) == "function" then + config_indent = config_indent(buf, node, line, modifier_indent, initial_indent) or 0 + end - if calculated_indent == -1 then + if config_indent == -1 then return -1 end - local new_indent = indent + calculated_indent + (module.config.public.tweaks[node:type()] or 0) + local computed_indent = config_indent + modifier_indent + (module.config.public.tweaks[node:type()] or 0) + + -- Don't dedent if dedent_excess is false + if (not module.config.public.dedent_excess) and computed_indent <= initial_indent then + return initial_indent + end - if (not module.config.public.dedent_excess) and new_indent <= initial_indent then + -- Keep manual indents in verbatim blocks + if parent_ranged_verbatim_tag and computed_indent <= initial_indent then return initial_indent end - return new_indent + return computed_indent end, ---re-evaluate the indent expression for each line in the range, and apply the new indentation From 579cc790e3f0ca15644bc69304428775e8330c6d Mon Sep 17 00:00:00 2001 From: Austin Liu Date: Wed, 18 Feb 2026 15:14:08 -0800 Subject: [PATCH 2/3] fix(indent): dedent ranged verbatim tags --- lua/neorg/modules/core/esupports/indent/module.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lua/neorg/modules/core/esupports/indent/module.lua b/lua/neorg/modules/core/esupports/indent/module.lua index 8faba6319..954cb3a94 100644 --- a/lua/neorg/modules/core/esupports/indent/module.lua +++ b/lua/neorg/modules/core/esupports/indent/module.lua @@ -110,8 +110,11 @@ module.public = { return initial_indent end - -- Keep manual indents in verbatim blocks - if parent_ranged_verbatim_tag and computed_indent <= initial_indent then + if + parent_ranged_verbatim_tag + and module.required["core.integrations.treesitter"].find_parent(node, "ranged_verbatim_tag_content$") + and computed_indent <= initial_indent + then return initial_indent end From 2d3be71dd4b16299078a1e792777df82e1a19134 Mon Sep 17 00:00:00 2001 From: Austin Liu Date: Wed, 18 Feb 2026 15:40:05 -0800 Subject: [PATCH 3/3] fix: make compatible with nvim-treesitter master _and_ main branch --- lua/neorg/modules/core/esupports/indent/module.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/neorg/modules/core/esupports/indent/module.lua b/lua/neorg/modules/core/esupports/indent/module.lua index 954cb3a94..81a7aa670 100644 --- a/lua/neorg/modules/core/esupports/indent/module.lua +++ b/lua/neorg/modules/core/esupports/indent/module.lua @@ -69,14 +69,16 @@ module.public = { if current_lang:lang() ~= "norg" then if parent_ranged_verbatim_tag ~= nil then return math.max( - require("nvim-treesitter").indentexpr(), + require("nvim-treesitter").indentexpr and require("nvim-treesitter").indentexpr() + or vim.fn["nvim_treesitter#indent"](), module.required["core.integrations.treesitter"].get_node_range( ---@diagnostic disable-next-line: param-type-mismatch parent_ranged_verbatim_tag -- ranged_verbatim_tag, e.g. `@code lua` ).column_start ) else - return require("nvim-treesitter").indentexpr() + return require("nvim-treesitter").indentexpr and require("nvim-treesitter").indentexpr() + or vim.fn["nvim_treesitter#indent"]() end end