Skip to content

Commit

Permalink
feat!: keybind refactor, update core.itero to new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Jul 13, 2024
1 parent db75bb6 commit 3dd946a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 790 deletions.
45 changes: 15 additions & 30 deletions lua/neorg/modules/core/itero/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ the item under the cursor with the `<C-t>` and `<C-d>` bindings.
--]]

local neorg = require("neorg.core")
local lib, log, modules, utils = neorg.lib, neorg.log, neorg.modules, neorg.utils
local lib, log, modules = neorg.lib, neorg.log, neorg.modules

local module = modules.create("core.itero")

Expand Down Expand Up @@ -76,17 +76,18 @@ module.config.private = {
}

module.load = function()
modules.await("core.keybinds", function(keybinds)
keybinds.register_keybinds(module.name, { "next-iteration", "stop-iteration" })
end)
vim.keymap.set("", "<Plug>(neorg.itero.next-iteration.carriage-return)", module.public.next_iteration_cr)
end

module.on_event = function(event)
if event.split_type[2] == (module.name .. ".next-iteration") then
module.public = {
next_iteration_cr = function()
local cursor = vim.api.nvim_win_get_cursor(0)
local buffer = vim.api.nvim_get_current_buf()

local ts = module.required["core.integrations.treesitter"]
local cursor_pos = event.cursor_position[1] - 1
local cursor_pos = cursor[1] - 1

local current = ts.get_first_node_on_line(event.buffer, cursor_pos, module.config.private.stop_types)
local current = ts.get_first_node_on_line(buffer, cursor_pos, module.config.private.stop_types)

if not current then
log.error(
Expand All @@ -108,22 +109,7 @@ module.on_event = function(event)
end

if not current or current:type() == "document" then
local fallback = event.content[1]

if fallback then
assert(
type(fallback) == "string",
"Invalid argument provided to `next-iterable` keybind! Option should be of type `string`!"
)

vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(fallback, true, true, true), "t", false)
return
end

utils.notify(
"No object to continue! Make sure you're under an iterable item like a list or heading.",
vim.log.levels.WARN
)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", true, true, true), "t", false)
return
end

Expand All @@ -134,34 +120,33 @@ module.on_event = function(event)
end
) and current:named_child(1) and current:named_child(1):type() == "detached_modifier_extension"

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

local _, column = current:start()

local is_on_nonempty_line =
vim.api.nvim_buf_get_lines(event.buffer, cursor_pos, cursor_pos + 1, true)[1]:match("%S")
vim.api.nvim_buf_get_lines(buffer, cursor_pos, cursor_pos + 1, true)[1]:match("%S")
if is_on_nonempty_line then
cursor_pos = cursor_pos + 1
end

vim.api.nvim_buf_set_lines(
event.buffer,
buffer,
cursor_pos,
cursor_pos + (is_on_nonempty_line and 0 or 1),
true,
{ string.rep(" ", column) .. text_to_repeat .. (should_append_extension and "( ) " or "") }
)
vim.api.nvim_win_set_cursor(
event.window,
0,
{ cursor_pos + 1, column + text_to_repeat:len() + (should_append_extension and ("( ) "):len() or 0) }
)
end
end
}

module.events.subscribed = {
["core.keybinds"] = {
[module.name .. ".next-iteration"] = true,
[module.name .. ".stop-iteration"] = true,
},
}

Expand Down
213 changes: 0 additions & 213 deletions lua/neorg/modules/core/keybinds/keybinds.lua

This file was deleted.

Loading

0 comments on commit 3dd946a

Please sign in to comment.