@@ -175,27 +175,34 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr)
175
175
indent = new_header_indent ,
176
176
})
177
177
178
- local content_indent_pad = 0
178
+ local content_indent_pad
179
179
-- Only include the header line and the content. Do not include the footer in the loop.
180
180
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 )
197
194
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
199
206
end
200
207
end
201
208
end
0 commit comments