Skip to content

Commit 9cfc537

Browse files
Fix formatexpr and add align tags to it
1 parent b621218 commit 9cfc537

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

lua/orgmode/org/format.lua

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
local Files = require('orgmode.parser.files')
22
local Table = require('orgmode.treesitter.table')
3+
local ts_org = require('orgmode.treesitter')
4+
5+
local function format_line(linenr)
6+
local line_text = vim.fn.getline(linenr)
7+
8+
if line_text:match('^%*+%s') then
9+
local headline = ts_org.closest_headline({ linenr, 1 })
10+
if headline then
11+
headline:align_tags()
12+
return true
13+
end
14+
end
15+
16+
if Table.format(linenr) then
17+
return true
18+
end
19+
20+
local ok, item = pcall(Files.get_closest_headline, linenr)
21+
if ok and item and item.logbook and item.logbook.range:is_in_line_range(linenr) then
22+
item.logbook:recalculate_estimate(linenr)
23+
return true
24+
end
25+
26+
return false
27+
end
328

429
local function format()
530
if vim.tbl_contains({ 'i', 'R', 'ic', 'ix' }, vim.fn.mode()) then
@@ -8,16 +33,17 @@ local function format()
833
return 1
934
end
1035

11-
local table = Table.format()
36+
local start_line = vim.v.lnum
37+
local end_line = vim.v.lnum + vim.v.count - 1
38+
local formatted = false
1239

13-
if table then
14-
return 0
40+
for linenr = start_line, end_line do
41+
local line_formatted = format_line(linenr)
42+
formatted = formatted or line_formatted
1543
end
1644

17-
local line = vim.fn.line('.')
18-
local ok, item = pcall(Files.get_closest_headline, line)
19-
if ok and item and item.logbook and item.logbook.range:is_in_line_range(line) then
20-
return item.logbook:recalculate_estimate(line)
45+
if formatted then
46+
return 0
2147
end
2248

2349
return 1

0 commit comments

Comments
 (0)