Skip to content

Commit

Permalink
refactor!: move rest of modules to new keybind system
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Jul 13, 2024
1 parent c39c807 commit 94b860b
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 244 deletions.
53 changes: 23 additions & 30 deletions lua/neorg/modules/core/dirman/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ module.load = function()

dirman_utils = module.required["core.dirman.utils"]

modules.await("core.keybinds", function(keybinds)
keybinds.register_keybind(module.name, "new.note")
end)
vim.keymap.set("", "<Plug>(neorg.dirman.new-note)", module.public.new_note)

-- Used to detect when we've entered a buffer with a potentially different cwd
module.required["core.autocommands"].enable_autocommand("BufEnter", true)
Expand Down Expand Up @@ -444,6 +442,28 @@ module.public = {
get_index = function()
return module.config.public.index
end,
new_note = function()
if module.config.public.use_popup then
module.required["core.ui"].create_prompt("NeorgNewNote", "New Note: ", function(text)
-- Create the file that the user has entered
module.public.create_file(text)
end, {
center_x = true,
center_y = true,
}, {
width = 25,
height = 1,
row = 10,
col = 0,
})
else
vim.ui.input({ prompt = "New Note: " }, function(text)
if text ~= nil and #text > 0 then
module.public.create_file(text)
end
end)
end
end
}

module.on_event = function(event)
Expand Down Expand Up @@ -503,30 +523,6 @@ module.on_event = function(event)
dirman_utils.edit_file(index_path:cmd_string())
return
end

-- If the user has executed a keybind to create a new note then create a prompt
if event.type == "core.keybinds.events.core.dirman.new.note" then
if module.config.public.use_popup then
module.required["core.ui"].create_prompt("NeorgNewNote", "New Note: ", function(text)
-- Create the file that the user has entered
module.public.create_file(text)
end, {
center_x = true,
center_y = true,
}, {
width = 25,
height = 1,
row = 10,
col = 0,
})
else
vim.ui.input({ prompt = "New Note: " }, function(text)
if text ~= nil and #text > 0 then
module.public.create_file(text)
end
end)
end
end
end

module.events.defined = {
Expand All @@ -547,9 +543,6 @@ module.events.subscribed = {
["dirman.workspace"] = true,
["dirman.index"] = true,
},
["core.keybinds"] = {
["core.dirman.new.note"] = true,
},
}

return module
25 changes: 5 additions & 20 deletions lua/neorg/modules/core/integrations/treesitter/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ module.load = function()

module.required["core.mode"].add_mode("traverse-heading")
module.required["core.mode"].add_mode("traverse-link")
modules.await("core.keybinds", function(keybinds)
keybinds.register_keybinds(module.name, { "next.heading", "previous.heading", "next.link", "previous.link" })
end)
vim.keymap.set("", "<Plug>(neorg.treesitter.next.heading)", lib.wrap(module.public.goto_next_query_match, module.private.heading_query))
vim.keymap.set("", "<Plug>(neorg.treesitter.next.link)", lib.wrap(module.public.goto_next_query_match, module.private.link_query))
vim.keymap.set("", "<Plug>(neorg.treesitter.previous.heading)", lib.wrap(module.public.goto_previous_query_match, module.private.heading_query))
vim.keymap.set("", "<Plug>(neorg.treesitter.previous.link)", lib.wrap(module.public.goto_previous_query_match, module.private.link_query))
end

module.config.public = {
Expand Down Expand Up @@ -940,17 +941,7 @@ local function install_norg_ts()
end

module.on_event = function(event)
if event.split_type[1] == "core.keybinds" then
if event.split_type[2] == "core.integrations.treesitter.next.heading" then
module.public.goto_next_query_match(module.private.heading_query)
elseif event.split_type[2] == "core.integrations.treesitter.previous.heading" then
module.public.goto_previous_query_match(module.private.heading_query)
elseif event.split_type[2] == "core.integrations.treesitter.next.link" then
module.public.goto_next_query_match(module.private.link_query)
elseif event.split_type[2] == "core.integrations.treesitter.previous.link" then
module.public.goto_previous_query_match(module.private.link_query)
end
elseif event.split_type[2] == "sync-parsers" then
if event.split_type[2] == "sync-parsers" then
local ok, err = pcall(install_norg_ts)

if not ok then
Expand All @@ -963,12 +954,6 @@ module.on_event = function(event)
end

module.events.subscribed = {
["core.keybinds"] = {
["core.integrations.treesitter.next.heading"] = true,
["core.integrations.treesitter.previous.heading"] = true,
["core.integrations.treesitter.next.link"] = true,
["core.integrations.treesitter.previous.link"] = true,
},
["core.neorgcmd"] = {
["sync-parsers"] = true,
},
Expand Down
34 changes: 13 additions & 21 deletions lua/neorg/modules/core/keybinds/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ module.load = function()
end
end

-- Temporary functions to act as wrappers
module.public = {
register_keybind = function (...)
end,
register_keybinds = function (...)
end
}

module.private = {
presets = {
neorg = {
Expand All @@ -42,78 +34,78 @@ module.private = {
-- ^mark Task as Undone
{
"<LocalLeader>tu",
"<Plug>(neorg.qol.todo_items.todo.task_undone)",
"<Plug>(neorg.qol.todo-items.todo.task-undone)",
opts = { desc = "[neorg] Mark as Undone" },
},

-- Marks the task under the cursor as "pending"
-- ^mark Task as Pending
{
"<LocalLeader>tp",
"<Plug>(neorg.qol.todo_items.todo.task_pending)",
"<Plug>(neorg.qol.todo-items.todo.task-pending)",
opts = { desc = "[neorg] Mark as Pending" },
},

-- Marks the task under the cursor as "done"
-- ^mark Task as Done
{
"<LocalLeader>td",
"<Plug>(neorg.qol.todo_items.todo.task_done)",
"<Plug>(neorg.qol.todo-items.todo.task-done)",
opts = { desc = "[neorg] Mark as Done" },
},

-- Marks the task under the cursor as "on_hold"
-- Marks the task under the cursor as "on-hold"
-- ^mark Task as on Hold
{
"<LocalLeader>th",
"<Plug>(neorg.qol.todo_items.todo.task_on_hold)",
"<Plug>(neorg.qol.todo-items.todo.task-on-hold)",
opts = { desc = "[neorg] Mark as On Hold" },
},

-- Marks the task under the cursor as "cancelled"
-- ^mark Task as Cancelled
{
"<LocalLeader>tc",
"<Plug>(neorg.qol.todo_items.todo.task_cancelled)",
"<Plug>(neorg.qol.todo-items.todo.task-cancelled)",
opts = { desc = "[neorg] Mark as Cancelled" },
},

-- Marks the task under the cursor as "recurring"
-- ^mark Task as Recurring
{
"<LocalLeader>tr",
"<Plug>(neorg.qol.todo_items.todo.task_recurring)",
"<Plug>(neorg.qol.todo-items.todo.task-recurring)",
opts = { desc = "[neorg] Mark as Recurring" },
},

-- Marks the task under the cursor as "important"
-- ^mark Task as Important
{
"<LocalLeader>ti",
"<Plug>(neorg.qol.todo_items.todo.task_important)",
"<Plug>(neorg.qol.todo-items.todo.task-important)",
opts = { desc = "[neorg] Mark as Important" },
},

-- Marks the task under the cursor as "ambiguous"
-- ^mark Task as ambiguous
{
"<LocalLeader>ta",
"<Plug>(neorg.qol.todo_items.todo.task_ambiguous)",
"<Plug>(neorg.qol.todo-items.todo.task-ambiguous)",
opts = { desc = "[neorg] Mark as Ambigous" },
},

-- Switches the task under the cursor between a select few states
{
"<C-Space>",
"<Plug>(neorg.qol.todo_items.todo.task_cycle)",
"<Plug>(neorg.qol.todo-items.todo.task-cycle)",
opts = { desc = "[neorg] Cycle Task" },
},

-- Creates a new .norg file to take notes in
-- ^New Note
{
"<LocalLeader>nn",
"<Plug>(neorg.dirman.new.note)",
"<Plug>(neorg.dirman.new-note)",
opts = { desc = "[neorg] Create New Note" },
},

Expand Down Expand Up @@ -163,8 +155,8 @@ module.private = {
},

v = {
{ ">", "<Plug>(neorg.promo.promote_range)", opts = { desc = "[neorg] Promote Objects in Range" } },
{ "<", "<Plug>(neorg.promo.demote_range)", opts = { desc = "[neorg] Demote Objects in Range" } },
{ ">", "<Plug>(neorg.promo.promote-range)", opts = { desc = "[neorg] Promote Objects in Range" } },
{ "<", "<Plug>(neorg.promo.demote-range)", opts = { desc = "[neorg] Demote Objects in Range" } },
},
},
},
Expand Down
28 changes: 10 additions & 18 deletions lua/neorg/modules/core/looking-glass/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ module.setup = function()
end

module.load = function()
modules.await("core.keybinds", function(keybinds)
keybinds.register_keybind(module.name, "magnify-code-block")
end)
vim.keymap.set("", "<Plug>(neorg.looking-glass.magnify-code-block)", module.public.magnify_code_block)
end

module.public = {
Expand Down Expand Up @@ -178,11 +176,11 @@ module.public = {
end,
})
end,
}

module.on_event = function(event)
if event.split_type[2] == "core.looking-glass.magnify-code-block" then
-- First we must check if the user has their cursor under a code block
magnify_code_block = function ()
local buffer = vim.api.nvim_get_current_buf()
local window = vim.api.nvim_get_current_win()

local query = utils.ts_parse_query(
"norg",
[[
Expand All @@ -192,15 +190,15 @@ module.on_event = function(event)
]]
)

local document_root = module.required["core.integrations.treesitter"].get_document_root(event.buffer)
local document_root = module.required["core.integrations.treesitter"].get_document_root(buffer)

--- Table containing information about the code block that is potentially under the cursor
local code_block_info

do
local cursor_pos = vim.api.nvim_win_get_cursor(event.window)
local cursor_pos = vim.api.nvim_win_get_cursor(window)

for id, node in query:iter_captures(document_root, event.buffer, cursor_pos[1] - 1, cursor_pos[1]) do
for id, node in query:iter_captures(document_root, buffer, cursor_pos[1] - 1, cursor_pos[1]) do
local capture = query.captures[id]

if capture == "tag" then
Expand Down Expand Up @@ -258,20 +256,14 @@ module.on_event = function(event)
local start = last_attribute and last_attribute["end"] or code_block_info.start

module.public.sync_text_segment(
event.buffer,
event.window,
buffer,
window,
start,
code_block_info["end"],
vsplit,
vim.api.nvim_get_current_win()
)
end
end

module.events.subscribed = {
["core.keybinds"] = {
["core.looking-glass.magnify-code-block"] = true,
},
}

return module
21 changes: 5 additions & 16 deletions lua/neorg/modules/core/presenter/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ module.load = function()
return
end

modules.await("core.keybinds", function(keybinds)
keybinds.register_keybinds(module.name, { "next_page", "previous_page", "close" })
end)
vim.keymap.set("", "<Plug>(neorg.presenter.next-page)", module.public.next_page)
vim.keymap.set("", "<Plug>(neorg.presenter.previous-page)", module.public.previous_page)
vim.keymap.set("", "<Plug>(neorg.presenter.close)", module.public.close)

modules.await("core.neorgcmd", function(neorgcmd)
neorgcmd.add_commands_from_table({
Expand Down Expand Up @@ -241,15 +241,9 @@ module.public = {
}

module.on_event = function(event)
if vim.tbl_contains({ "core.neorgcmd", "core.keybinds" }, event.split_type[1]) then
if vim.tbl_contains({ "presenter.start" }, event.split_type[2]) then
if event.split_type[1] == "core.neorgcmd" then
if event.split_type[2] == "presenter.start" then
module.public.present()
elseif vim.tbl_contains({ "presenter.close", "core.presenter.close" }, event.split_type[2]) then
module.public.close()
elseif vim.tbl_contains({ "core.presenter.previous_page" }, event.split_type[2]) then
module.public.previous_page()
elseif vim.tbl_contains({ "core.presenter.next_page" }, event.split_type[2]) then
module.public.next_page()
end
end
end
Expand All @@ -259,11 +253,6 @@ module.events.subscribed = {
["presenter.start"] = true,
["presenter.close"] = true,
},
["core.keybinds"] = {
["core.presenter.previous_page"] = true,
["core.presenter.next_page"] = true,
["core.presenter.close"] = true,
},
}

return module
Loading

0 comments on commit 94b860b

Please sign in to comment.