Skip to content

Commit d8652f6

Browse files
committed
feat: use treesitter range for virtual indent by default
When a user adds stars to a headline, the content in the region now all gets correctly virtually indented.
1 parent 975c196 commit d8652f6

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lua/orgmode/ui/virtual_indent.lua

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,23 @@ end
4545
---@param bufnr number buffer id
4646
---@param start_line number start line number to set the indentation, 0-based inclusive
4747
---@param end_line number end line number to set the indentation, 0-based inclusive
48-
function VirtualIndent:set_indent(bufnr, start_line, end_line)
49-
self:_delete_old_extmarks(bufnr, start_line, end_line)
48+
---@param ignore_ts? boolean whether or not to skip the treesitter start & end lookup
49+
function VirtualIndent:set_indent(bufnr, start_line, end_line, ignore_ts)
50+
ignore_ts = ignore_ts or false
51+
local headline = self.lib.headline.from_cursor({ start_line + 1, 1 })
52+
if headline and not ignore_ts then
53+
local parent = headline.headline:parent()
54+
if parent then
55+
for node in parent:iter_children() do
56+
if node:type() == 'body' then
57+
start_line = node:start()
58+
end_line = node:end_()
59+
break
60+
end
61+
end
62+
end
63+
end
64+
self:_delete_old_extmarks(bufnr, start_line - 1, end_line)
5065
for line = start_line, end_line do
5166
local indent = self:_get_indent_size(line)
5267

@@ -64,7 +79,7 @@ end
6479
---@param bufnr? number buffer id
6580
function VirtualIndent:attach(bufnr)
6681
bufnr = bufnr or 0
67-
self:set_indent(0, 0, vim.api.nvim_buf_line_count(bufnr) - 1)
82+
self:set_indent(0, 0, vim.api.nvim_buf_line_count(bufnr) - 1, true)
6883

6984
vim.api.nvim_buf_attach(bufnr, false, {
7085
on_lines = function(_, _, _, start_line, _, end_line)

0 commit comments

Comments
 (0)