Skip to content

Commit 261c987

Browse files
authored
fix: block content alignment (#698)
* fix(indent): do not modify block indents if one line is correct Previously, if a single line was correctly aligned with a block, but the others were overindented it was possible for the overindented lines to be unindented. This is not correct. The overindented lines shouldn't shift at all if at least one of the lines is correctly indented. This is to preserve manual formatting. This commit ensures the above functionality. * fix(indent): ignore blank lines for block content indentation
1 parent 0872d1f commit 261c987

File tree

2 files changed

+76
-18
lines changed

2 files changed

+76
-18
lines changed

lua/orgmode/org/indent.lua

+25-18
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,34 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr)
175175
indent = new_header_indent,
176176
})
177177

178-
local content_indent_pad = 0
178+
local content_indent_pad
179179
-- Only include the header line and the content. Do not include the footer in the loop.
180180
for i = range.start.line + 1, range['end'].line - 2 do
181-
local curr_indent = vim.fn.indent(i + 1)
182-
-- Correctly align the pad to the new header position if it was underindented
183-
local new_indent_pad = new_header_indent - curr_indent
184-
-- If the current content indentaion is less than the new header indent we want to increase all of the
185-
-- content by the largest difference in indentation between a given content line and the new header indent.
186-
if curr_indent < new_header_indent then
187-
content_indent_pad = math.max(new_indent_pad, content_indent_pad)
188-
else
189-
-- If the current content indentation is more than the new header indentation, but it was the current
190-
-- content indentation was less than the current header indent then we want to add some indentation onto
191-
-- the content by the largest negative difference (meaning -1 > -2 > -3 so take -1 as the pad).
192-
--
193-
-- We do a check for 0 here as we don't want to do a max of neg number against 0. 0 will always win. As
194-
-- such if the current pad is 0 just set to the new calculated pad.
195-
if content_indent_pad == 0 then
196-
content_indent_pad = new_indent_pad
181+
local linenr = i + 1
182+
local line_content = vim.api.nvim_buf_get_lines(bufnr, linenr - 1, linenr, true)[1]
183+
-- If the line is blank, we should ignore it as `vim.fn.indent` will return a 0 indent for
184+
-- it which may be less indented than the header indentation. We shouldn't factor in blank
185+
-- lines for indentation.
186+
if not line_content:match('^$') then
187+
local curr_indent = vim.fn.indent(linenr)
188+
-- Correctly align the pad to the new header position if it was underindented
189+
local new_indent_pad = new_header_indent - curr_indent
190+
-- If the current content indentaion is less than the new header indent we want to increase all of the
191+
-- content by the largest difference in indentation between a given content line and the new header indent.
192+
if curr_indent < new_header_indent then
193+
content_indent_pad = math.max(new_indent_pad, content_indent_pad or 0)
197194
else
198-
content_indent_pad = math.max(new_indent_pad, content_indent_pad)
195+
-- If the current content indentation is more than the new header indentation, but it was the current
196+
-- content indentation was less than the current header indent then we want to add some indentation onto
197+
-- the content by the largest negative difference (meaning -1 > -2 > -3 so take -1 as the pad).
198+
--
199+
-- We do a check for 0 here as we don't want to do a max of neg number against 0. 0 will always win. As
200+
-- such if the current pad is 0 just set to the new calculated pad.
201+
if not content_indent_pad then
202+
content_indent_pad = new_indent_pad
203+
else
204+
content_indent_pad = math.max(new_indent_pad, content_indent_pad)
205+
end
199206
end
200207
end
201208
end

tests/plenary/org/indent_spec.lua

+51
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ local function test_full_reindent()
6262
' "another key": "another value"',
6363
' }',
6464
' #+END_SRC',
65+
' - Correctly maintains indentation when single line is at the same level as header and rest is overindented',
66+
' #+BEGIN_SRC json',
67+
' {',
68+
' "key": "value",',
69+
' "another key": "another value"',
70+
' }',
71+
' #+END_SRC',
72+
' - Correctly ignores blank lines for calculating indentation',
73+
' #+BEGIN_SRC json',
74+
'',
75+
' {',
76+
' "key": "value",',
77+
'',
78+
' "another key": "another value"',
79+
' }',
80+
'',
81+
' #+END_SRC',
6582
}
6683
helpers.create_file(unformatted_file)
6784
vim.cmd([[silent norm 0gg=G]])
@@ -118,6 +135,23 @@ local function test_full_reindent()
118135
' "another key": "another value"',
119136
' }',
120137
' #+END_SRC',
138+
' - Correctly maintains indentation when single line is at the same level as header and rest is overindented',
139+
' #+BEGIN_SRC json',
140+
' {',
141+
' "key": "value",',
142+
' "another key": "another value"',
143+
' }',
144+
' #+END_SRC',
145+
' - Correctly ignores blank lines for calculating indentation',
146+
' #+BEGIN_SRC json',
147+
'',
148+
' {',
149+
' "key": "value",',
150+
'',
151+
' "another key": "another value"',
152+
' }',
153+
'',
154+
' #+END_SRC',
121155
}
122156
else
123157
expected = {
@@ -171,6 +205,23 @@ local function test_full_reindent()
171205
' "another key": "another value"',
172206
' }',
173207
' #+END_SRC',
208+
'- Correctly maintains indentation when single line is at the same level as header and rest is overindented',
209+
' #+BEGIN_SRC json',
210+
' {',
211+
' "key": "value",',
212+
' "another key": "another value"',
213+
' }',
214+
' #+END_SRC',
215+
'- Correctly ignores blank lines for calculating indentation',
216+
' #+BEGIN_SRC json',
217+
'',
218+
' {',
219+
' "key": "value",',
220+
'',
221+
' "another key": "another value"',
222+
' }',
223+
'',
224+
' #+END_SRC',
174225
}
175226
end
176227
expect_whole_buffer(expected)

0 commit comments

Comments
 (0)