Skip to content

Commit aa35325

Browse files
authored
fix(esupports.indent): keep indentation of verbatim blocks without an injected TS parser (#1685)
* fix(esupports.indent): keep indentation of verbatim blocks without an injected TS parser * fix(promo): fix off-by-one error when re-indenting lines during range operation `indent.reindent_range()` expects 0-based, exclusive line ranges, but the lines returned by `nvim_buf_get_mark()` for the `<`/`>` marks are 1-based, inclusive line ranges * fix(esupports.indent): fix setting indentation of line to -1 not being ignored indent-expression might return -1 as the indentation of a line to indicate that neovim should handle the indentation of the line. When manually setting the indentation of a line with `buffer_set_line_indent` using the indentation returned by indent-expression however, the -1 indentation caused the indentation to be set to 0 instead of kept as is
1 parent 35da593 commit aa35325

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

lua/neorg/modules/core/esupports/indent/module.lua

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ module.public = {
9696
end
9797
end
9898

99+
-- Check if the code is within a verbatim block
100+
local ranged_tag =
101+
module.required["core.integrations.treesitter"].find_parent(node, "ranged_verbatim_tag_content")
102+
indent_data = ranged_tag and module.config.public.indents[ranged_tag:type()] or indent_data
103+
99104
-- Indents can be a static value, so account for that here
100105
if type(indent_data.indent) == "number" then
101106
-- If the indent is -1 then let Neovim indent instead of us
@@ -144,7 +149,7 @@ module.public = {
144149
---@param new_indent number
145150
buffer_set_line_indent = function(buffer, start_row, new_indent)
146151
local line = vim.api.nvim_buf_get_lines(buffer, start_row, start_row + 1, true)[1]
147-
if line:match("^%s*$") then
152+
if line:match("^%s*$") or new_indent == -1 then
148153
return
149154
end
150155

@@ -239,21 +244,27 @@ module.config.public = {
239244
indent = 0,
240245
},
241246

242-
["ranged_tag"] = {
243-
modifiers = { "under-headings" },
244-
indent = 0,
245-
},
246-
247247
-- Ranged tag contents' indentation should be calculated by Neovim itself.
248-
["ranged_tag_content"] = {
248+
["ranged_verbatim_tag_content"] = {
249249
indent = -1,
250250
},
251251

252252
-- `@end` tags should always be indented as far as the beginning `@` ranged verbatim tag.
253+
["ranged_verbatim_tag_end"] = {
254+
modifiers = { "ranged-tag-end" },
255+
indent = 0,
256+
},
257+
258+
-- `|end` tags should always be indented as far as the beginning `|` ranged tag.
253259
["ranged_tag_end"] = {
254-
indent = function(_, node)
255-
return module.required["core.integrations.treesitter"].get_node_range(node:parent()).column_start
256-
end,
260+
modifiers = { "ranged-tag-end" },
261+
indent = 0,
262+
},
263+
264+
-- `=end` tags should always be indented as far as the beginning `=` ranged tag.
265+
["macro_tag_end"] = {
266+
modifiers = { "ranged-tag-end" },
267+
indent = 0,
257268
},
258269
},
259270

@@ -309,6 +320,11 @@ module.config.public = {
309320

310321
return module.required["core.integrations.treesitter"].get_node_range(list:named_child(1)).column_start
311322
end,
323+
324+
-- For any ranged tag end that should always be indented as far as the beginning of the ranged tag
325+
["ranged-tag-end"] = function(_, node)
326+
return module.required["core.integrations.treesitter"].get_node_range(node:parent()).column_start
327+
end,
312328
},
313329

314330
-- Tweaks are user defined `node_name` => `indent_level` mappings,

lua/neorg/modules/core/promo/module.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ module.public = {
298298
for i = start_pos[1], end_pos[1] do
299299
module.private.promote_or_demote(buffer, "promote", i - 1, false, false)
300300
end
301-
indent.reindent_range(buffer, start_pos[1], end_pos[1])
301+
indent.reindent_range(buffer, start_pos[1] - 1, end_pos[1])
302302
end),
303303
demote = neorg.utils.wrap_dotrepeat(function()
304304
local buffer = vim.api.nvim_get_current_buf()
@@ -320,7 +320,7 @@ module.public = {
320320
for i = start_pos[1], end_pos[1] do
321321
module.private.promote_or_demote(buffer, "demote", i - 1, false, false)
322322
end
323-
indent.reindent_range(buffer, start_pos[1], end_pos[1])
323+
indent.reindent_range(buffer, start_pos[1] - 1, end_pos[1])
324324
end),
325325
}
326326

0 commit comments

Comments
 (0)