Skip to content

Commit

Permalink
ref: fully refactor core.itero behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Nov 12, 2022
1 parent e213dec commit 5ce0e8b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
47 changes: 32 additions & 15 deletions lua/neorg/modules/core/itero/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ module.setup = function()
end

module.config.public = {
-- If true, will automatically continue list items and the like when `<CR>`
-- is pressed, and will stop when `<M-CR>` is pressed or `<CR>` is pressed
-- twice.
--
-- When false, will do the opposite - list items will not be continued unless
-- `<M-CR>` is pressed.
trigger_by_default = true,

iterables = {
"unordered_list%d",
"ordered_list%d",
-- "quote%d",
"heading%d",
"quote%d",
},

stop_types = {
"generic_list",
"quote",
},
}

Expand All @@ -31,32 +41,39 @@ end

module.on_event = function(event)
if event.split_type[2] == (module.name .. ".next-iteration") then
local ts = module.required["core.integrations.treesitter"]
local cursor_pos = event.cursor_position[1] - 1

local node_on_line = module.required["core.integrations.treesitter"].get_first_node_on_line(event.buffer, cursor_pos, module.config.public.stop_types)
local text_to_repeat = nil
local current = ts.get_first_node_on_line(event.buffer, cursor_pos, module.config.public.stop_types)

if not current then
log.error(
"Treesitter seems to be high and can't properly grab the node under the cursor. Perhaps try again?"
)
return
end

for _, iterable in ipairs(module.config.public.iterables) do
if node_on_line:type():match(iterable) then
text_to_repeat = module.required["core.integrations.treesitter"].get_node_text(node_on_line:named_child(0))
vim.api.nvim_buf_set_lines(event.buffer, cursor_pos + 1, cursor_pos + 1, true, { text_to_repeat })
while current:parent() do
if
neorg.lib.filter(module.config.public.iterables, function(_, iterable)
return current:type():match(table.concat({ "^", iterable, "$" })) and iterable or nil
end)
then
break
end
end

if node_on_line:has_error() then
vim.api.nvim_buf_set_lines(event.buffer, cursor_pos, cursor_pos + 1, true, { "" })
return
current = current:parent()
end

if not text_to_repeat then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", true, true, true), "n", false)
if not current or current:type() == "document" then
vim.notify("No object to continue! Make sure you're under a list item.")
return
end

local text_to_repeat = ts.get_node_text(current:named_child(0), event.buffer)

vim.api.nvim_buf_set_lines(event.buffer, cursor_pos + 1, cursor_pos + 1, true, { text_to_repeat })
vim.api.nvim_win_set_cursor(event.window, { cursor_pos + 2, text_to_repeat:len() })
elseif event.split_type[2] == (module.name .. ".stop-iteration") then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", true, true, true), "n", false)
end
end

Expand Down
4 changes: 1 addition & 3 deletions lua/neorg/modules/core/keybinds/keybinds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ module.config.public = {
},

i = {
{ "<CR>", "core.itero.next-iteration", },
{ "<C-j>", "core.itero.next-iteration", },
{ "<M-CR>", "core.itero.stop-iteration", },
{ "<M-CR>", "core.itero.next-iteration" },
},

-- v = {
Expand Down

0 comments on commit 5ce0e8b

Please sign in to comment.