Skip to content

Commit

Permalink
fix(formatexpr): Correctly fallback to internal formatexpr
Browse files Browse the repository at this point in the history
Fixes #838
  • Loading branch information
kristijanhusak committed Dec 10, 2024
1 parent 6c39469 commit 3f37829
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lua/orgmode/files/headline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ function Headline:set_tags(tags)
end

function Headline:align_tags()
local current_text, tags_node = self:tags_to_string()
if tags_node then
self:set_tags(current_text)
local own_tags, node = self:get_own_tags()
if node then
self:set_tags(utils.tags_to_string(own_tags))
end
end

Expand Down
19 changes: 16 additions & 3 deletions lua/orgmode/org/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ local function format_line(linenr)
return false
end

local formatexpr_cache = {}

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

-- If single line is being formatted and is cached in the loop below,
-- Just fallback to internal formatting
if start_line == end_line and formatexpr_cache[start_line] then
return 1
end

for linenr = start_line, end_line do
local line_formatted = format_line(linenr)
if not line_formatted then
formatexpr_cache[linenr] = true
end
formatted = formatted or line_formatted
end

if formatted then
return 0
for line in pairs(formatexpr_cache) do
vim.cmd(('%dnormal! gqq'):format(line))
end

return 1
formatexpr_cache = {}

return formatted and 0 or 1
end

return format

0 comments on commit 3f37829

Please sign in to comment.