Skip to content

Commit 3f37829

Browse files
fix(formatexpr): Correctly fallback to internal formatexpr
Fixes #838
1 parent 6c39469 commit 3f37829

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lua/orgmode/files/headline.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,9 @@ function Headline:set_tags(tags)
288288
end
289289

290290
function Headline:align_tags()
291-
local current_text, tags_node = self:tags_to_string()
292-
if tags_node then
293-
self:set_tags(current_text)
291+
local own_tags, node = self:get_own_tags()
292+
if node then
293+
self:set_tags(utils.tags_to_string(own_tags))
294294
end
295295
end
296296

lua/orgmode/org/format.lua

+16-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ local function format_line(linenr)
2626
return false
2727
end
2828

29+
local formatexpr_cache = {}
30+
2931
local function format()
3032
if vim.tbl_contains({ 'i', 'R', 'ic', 'ix' }, vim.fn.mode()) then
3133
-- `formatexpr` is also called when exceeding `textwidth` in insert mode
@@ -37,16 +39,27 @@ local function format()
3739
local end_line = vim.v.lnum + vim.v.count - 1
3840
local formatted = false
3941

42+
-- If single line is being formatted and is cached in the loop below,
43+
-- Just fallback to internal formatting
44+
if start_line == end_line and formatexpr_cache[start_line] then
45+
return 1
46+
end
47+
4048
for linenr = start_line, end_line do
4149
local line_formatted = format_line(linenr)
50+
if not line_formatted then
51+
formatexpr_cache[linenr] = true
52+
end
4253
formatted = formatted or line_formatted
4354
end
4455

45-
if formatted then
46-
return 0
56+
for line in pairs(formatexpr_cache) do
57+
vim.cmd(('%dnormal! gqq'):format(line))
4758
end
4859

49-
return 1
60+
formatexpr_cache = {}
61+
62+
return formatted and 0 or 1
5063
end
5164

5265
return format

0 commit comments

Comments
 (0)