Skip to content

Commit 936494e

Browse files
Do not throw error when adding new list item via leader+cr mapping
1 parent 675e58f commit 936494e

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

lua/orgmode/org/mappings.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ local constants = require('orgmode.utils.constants')
1111
local ts_utils = require('nvim-treesitter.ts_utils')
1212
local utils = require('orgmode.utils')
1313
local tree_utils = require('orgmode.utils.treesitter')
14-
local Headline = require('orgmode.treesitter.headline')
14+
local ts_org = require('orgmode.treesitter')
1515
local Listitem = require('orgmode.treesitter.listitem')
1616

1717
---@class OrgMappings
@@ -64,7 +64,7 @@ function OrgMappings:archive()
6464
end
6565

6666
function OrgMappings:set_tags()
67-
local headline = Headline:new(tree_utils.closest_headline())
67+
local headline = ts_org.closest_headline()
6868
local _, current_tags = headline:tags()
6969

7070
local tags = vim.fn.OrgmodeInput('Tags: ', current_tags, Files.autocomplete_tags)
@@ -73,7 +73,7 @@ function OrgMappings:set_tags()
7373
end
7474

7575
function OrgMappings:toggle_archive_tag()
76-
local headline = Headline:new(tree_utils.closest_headline())
76+
local headline = ts_org.closest_headline()
7777
local _, current_tags = headline:tags()
7878

7979
local parsed = utils.parse_tags_string(current_tags)
@@ -159,10 +159,9 @@ function OrgMappings:toggle_checkbox()
159159
-- move to the first non-blank character so the current treesitter node is the listitem
160160
vim.cmd([[normal! _]])
161161

162-
vim.treesitter.get_parser(0, 'org'):parse()
163-
local listitem = tree_utils.find_parent_type(tree_utils.current_node(), 'listitem')
162+
local listitem = ts_org.listitem()
164163
if listitem then
165-
Listitem:new(listitem):update_checkbox('toggle')
164+
listitem:update_checkbox('toggle')
166165
end
167166

168167
vim.fn.winrestview(win_view)
@@ -313,7 +312,7 @@ function OrgMappings:priority_down()
313312
end
314313

315314
function OrgMappings:set_priority(direction)
316-
local headline = Headline:new(tree_utils.closest_headline())
315+
local headline = ts_org.closest_headline()
317316
local _, current_priority = headline:priority()
318317
local priority_state = PriorityState:new(current_priority)
319318

@@ -368,7 +367,7 @@ function OrgMappings:toggle_heading()
368367
end
369368

370369
function OrgMappings:_todo_change_state(direction)
371-
local headline = Headline:new(tree_utils.closest_headline())
370+
local headline = ts_org.closest_headline()
372371
local _, old_state, was_done = headline:todo()
373372
local changed = self:_change_todo_state(direction, true)
374373
if not changed then
@@ -530,8 +529,10 @@ function OrgMappings:handle_return(suffix)
530529

531530
-- update all parents when we insert a new checkbox
532531
if checkbox then
533-
local new_listitem = tree_utils.find_parent_type(tree_utils.current_node(), 'listitem')
534-
Listitem:new(new_listitem):update_checkbox('off')
532+
local new_listitem = ts_org.listitem()
533+
if new_listitem then
534+
new_listitem:update_checkbox('off')
535+
end
535536
end
536537

537538
vim.cmd([[startinsert!]])
@@ -764,7 +765,7 @@ end
764765
---@param use_fast_access? boolean
765766
---@return string
766767
function OrgMappings:_change_todo_state(direction, use_fast_access)
767-
local headline = Headline:new(tree_utils.closest_headline())
768+
local headline = ts_org.closest_headline()
768769
local todo, current_keyword = headline:todo()
769770
local todo_state = TodoState:new({ current_state = current_keyword })
770771
local next_state = nil

lua/orgmode/treesitter/init.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
local tree_utils = require('orgmode.utils.treesitter')
2+
local Headline = require('orgmode.treesitter.headline')
3+
local Listitem = require('orgmode.treesitter.listitem')
4+
5+
local function closest_headline()
6+
local ts_headline = tree_utils.closest_headline()
7+
if not ts_headline then
8+
error('Unable to locate closest headline')
9+
end
10+
return Headline:new(ts_headline)
11+
end
12+
13+
local function listitem()
14+
vim.treesitter.get_parser(0, 'org', {}):parse()
15+
local list_item = tree_utils.find_parent_type(tree_utils.current_node(), 'listitem')
16+
if list_item then
17+
return Listitem:new(list_item)
18+
end
19+
return nil
20+
end
21+
22+
return {
23+
closest_headline = closest_headline,
24+
listitem = listitem,
25+
}

lua/orgmode/utils/treesitter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222

2323
-- returns the nearest headline
2424
function M.closest_headline()
25-
vim.treesitter.get_parser(0, 'org'):parse()
25+
vim.treesitter.get_parser(0, 'org', {}):parse()
2626
return M.find_headline(M.current_node())
2727
end
2828

0 commit comments

Comments
 (0)