Skip to content

Commit a08bd94

Browse files
feat: support using count for promote and demote
1 parent bab2fca commit a08bd94

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

lua/orgmode/org/mappings.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ function OrgMappings:do_promote(whole_subtree)
468468
local headline = ts_org.closest_headline()
469469
local old_level = headline:level()
470470
local foldclosed = vim.fn.foldclosed('.')
471-
headline:promote(1, whole_subtree)
471+
headline:promote(vim.v.count1, whole_subtree)
472472
if foldclosed > -1 and vim.fn.foldclosed('.') == -1 then
473473
vim.cmd([[norm!zc]])
474474
end
@@ -479,7 +479,7 @@ function OrgMappings:do_demote(whole_subtree)
479479
local headline = ts_org.closest_headline()
480480
local old_level = headline:level()
481481
local foldclosed = vim.fn.foldclosed('.')
482-
headline:demote(1, whole_subtree)
482+
headline:demote(vim.v.count1, whole_subtree)
483483
if foldclosed > -1 and vim.fn.foldclosed('.') == -1 then
484484
vim.cmd([[norm!zc]])
485485
end

lua/orgmode/treesitter/headline.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ end
5656
---@param amount number
5757
---@param recursive? boolean
5858
function Headline:promote(amount, recursive)
59-
amount = amount or 1
59+
amount = math.min(amount or 1, self:level() - 1)
6060
recursive = recursive or false
6161
if self:level() == 1 then
6262
return utils.echo_warning('Cannot demote top level heading.')
@@ -74,7 +74,6 @@ function Headline:promote(amount, recursive)
7474
end)
7575
end
7676

77-
7877
---@param amount number
7978
---@param recursive? boolean
8079
function Headline:demote(amount, recursive)
@@ -84,7 +83,7 @@ function Headline:demote(amount, recursive)
8483
return self:_handle_promote_demote(recursive, function(lines)
8584
for i, line in ipairs(lines) do
8685
if line:sub(1, 1) == '*' then
87-
lines[i] = '*' .. line
86+
lines[i] = string.rep('*', amount) .. line
8887
else
8988
lines[i] = config:apply_indent(line, amount)
9089
end

tests/plenary/ui/mappings_spec.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,17 @@ describe('Mappings', function()
336336
'**** NEXT [#1] Level 3',
337337
' Content Level 3',
338338
}, vim.api.nvim_buf_get_lines(0, 2, 8, false))
339+
340+
-- Support count
341+
vim.cmd([[norm 4>s]])
342+
assert.are.same({
343+
'****** TODO Test orgmode',
344+
' DEADLINE: <2021-07-21 Wed 22:02>',
345+
'******* TODO [#A] Test orgmode level 2 :PRIVATE:',
346+
' Some content for level 2',
347+
'******** NEXT [#1] Level 3',
348+
' Content Level 3',
349+
}, vim.api.nvim_buf_get_lines(0, 2, 8, false))
339350
end)
340351

341352
it('should promote the heading (org_do_promote)', function()
@@ -391,6 +402,38 @@ describe('Mappings', function()
391402
'** NEXT [#1] Level 3',
392403
'Content Level 3',
393404
}, vim.api.nvim_buf_get_lines(0, 2, 8, false))
405+
406+
helpers.load_file_content({
407+
'***** TODO Test orgmode',
408+
' DEADLINE: <2021-07-21 Wed 22:02>',
409+
'****** TODO [#A] Test orgmode level 2 :PRIVATE:',
410+
' Some content for level 2',
411+
'******* NEXT [#1] Level 3',
412+
' Content Level 3',
413+
}, vim.api.nvim_buf_get_lines(0, 0, 6, false))
414+
vim.fn.cursor(1, 1)
415+
416+
-- Support count
417+
vim.cmd([[norm 2<s]])
418+
assert.are.same({
419+
'*** TODO Test orgmode',
420+
' DEADLINE: <2021-07-21 Wed 22:02>',
421+
'**** TODO [#A] Test orgmode level 2 :PRIVATE:',
422+
' Some content for level 2',
423+
'***** NEXT [#1] Level 3',
424+
' Content Level 3',
425+
}, vim.api.nvim_buf_get_lines(0, 0, 6, false))
426+
427+
-- Handle overflow
428+
vim.cmd([[norm 5<s]])
429+
assert.are.same({
430+
'* TODO Test orgmode',
431+
' DEADLINE: <2021-07-21 Wed 22:02>',
432+
'** TODO [#A] Test orgmode level 2 :PRIVATE:',
433+
' Some content for level 2',
434+
'*** NEXT [#1] Level 3',
435+
' Content Level 3',
436+
}, vim.api.nvim_buf_get_lines(0, 0, 6, false))
394437
end)
395438

396439
it('should add list item with Enter (org_meta_return)', function()

0 commit comments

Comments
 (0)