Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: update to new keybinding logic #68

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,19 @@ This module accepts the following configuration with the shown defaults:
You can define keybindings like this:

```lua
local neorg_callbacks = require("neorg.core.callbacks")

neorg_callbacks.on_event("core.keybinds.events.enable_keybinds", function(_, keybinds)
-- Map all the below keybinds only when the "norg" mode is active
keybinds.map_event_to_mode("norg", {
n = { -- Bind keys in normal mode
{ "<C-s>", "core.integrations.telescope.find_linkable" },
},

i = { -- Bind in insert mode
{ "<C-l>", "core.integrations.telescope.insert_link" },
},
}, {
silent = true,
noremap = true,
})
end)
vim.keymap.set("n", "<lhs>", "<Plug>(neorg.telescope.search_headings)")
```

List of all available plug mappings:
- `neorg.telescope.backlinks.file_backlinks`
- `neorg.telescope.backlinks.header_backlinks`
- `neorg.telescope.find_linkable`
- `neorg.telescope.find_norg_files`
- `neorg.telescope.insert_file_link`
- `neorg.telescope.insert_link`
- `neorg.telescope.search_headings`
- `neorg.telescope.switch_workspace`

# Support Welcome
If it's not clear by the code already, I'm a solid noob at telescope related things :)

Expand Down
100 changes: 28 additions & 72 deletions lua/neorg/modules/core/integrations/telescope/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,40 @@ local neorg = require("neorg.core")
local module = neorg.modules.create("core.integrations.telescope")

module.setup = function()
return { success = true, requires = { "core.keybinds", "core.dirman" } }
return { success = true, requires = { "core.dirman" } }
end

-- To add a new picker:
-- - Choose a name to add to this list (eg. <picker>)
-- - Create a file in `lua/telescope/_extensions/neorg/<picker>.lua`
-- - Add the picker to `lua/telescope/_extensions/neorg.lua`
-- - Add it to the list in the README

local pickers = {
"find_linkable",
"find_norg_files",
"insert_link",
"insert_file_link",
"search_headings",
"find_project_tasks",
"find_aof_project_tasks",
"find_aof_tasks",
"find_context_tasks",
"switch_workspace",
"backlinks.file_backlinks",
"backlinks.header_backlinks",
}

module.load = function()
local telescope_loaded, telescope = pcall(require, "telescope")

assert(telescope_loaded, telescope)

telescope.load_extension("neorg")

module.required["core.keybinds"].register_keybinds(module.name, {
"find_linkable",
"find_norg_files",
"insert_link",
"insert_file_link",
"search_headings",
"find_project_tasks",
"find_aof_project_tasks",
"find_aof_tasks",
"find_context_tasks",
"switch_workspace",
"find_backlinks",
"find_header_backlinks",
})
for _, picker in ipairs(pickers) do
vim.keymap.set("", ("<Plug>(neorg.telescope.%s)"):format(picker), module.public[picker])
end
end

module.config.public = {
Expand All @@ -39,64 +49,10 @@ module.config.public = {
},
}

module.public = {
find_linkable = require("telescope._extensions.neorg.find_linkable"),
find_norg_files = require("telescope._extensions.neorg.find_norg_files"),
insert_link = require("telescope._extensions.neorg.insert_link"),
insert_file_link = require("telescope._extensions.neorg.insert_file_link"),
search_headings = require("telescope._extensions.neorg.search_headings"),
find_project_tasks = require("telescope._extensions.neorg.find_project_tasks"),
find_context_tasks = require("telescope._extensions.neorg.find_context_tasks"),
find_aof_tasks = require("telescope._extensions.neorg.find_aof_tasks"),
find_aof_project_tasks = require("telescope._extensions.neorg.find_aof_project_tasks"),
switch_workspace = require("telescope._extensions.neorg.switch_workspace"),
find_backlinks = require("telescope._extensions.neorg.backlinks.file_backlinks"),
find_header_backlinks = require("telescope._extensions.neorg.backlinks.header_backlinks"),
}
module.public = {}

module.on_event = function(event)
if event.split_type[2] == "core.integrations.telescope.find_linkable" then
module.public.find_linkable()
elseif event.split_type[2] == "core.integrations.telescope.find_norg_files" then
module.public.find_norg_files()
elseif event.split_type[2] == "core.integrations.telescope.insert_link" then
module.public.insert_link()
elseif event.split_type[2] == "core.integrations.telescope.insert_file_link" then
module.public.insert_file_link()
elseif event.split_type[2] == "core.integrations.telescope.search_headings" then
module.public.search_headings()
elseif event.split_type[2] == "core.integrations.telescope.find_project_tasks" then
module.public.find_project_tasks()
elseif event.split_type[2] == "core.integrations.telescope.find_aof_tasks" then
module.public.find_aof_tasks()
elseif event.split_type[2] == "core.integrations.telescope.find_aof_project_tasks" then
module.public.find_aof_project_tasks()
elseif event.split_type[2] == "core.integrations.telescope.find_context_tasks" then
module.public.find_context_tasks()
elseif event.split_type[2] == "core.integrations.telescope.switch_workspace" then
module.public.switch_workspace()
elseif event.split_type[2] == "core.integrations.telescope.find_backlinks" then
module.public.find_backlinks()
elseif event.split_type[2] == "core.integrations.telescope.find_header_backlinks" then
module.public.find_header_backlinks()
end
for _, picker in ipairs(pickers) do
module.public[picker] = require(("telescope._extensions.neorg.%s"):format(picker))
end

module.events.subscribed = {
["core.keybinds"] = {
["core.integrations.telescope.find_linkable"] = true,
["core.integrations.telescope.find_norg_files"] = true,
["core.integrations.telescope.insert_link"] = true,
["core.integrations.telescope.insert_file_link"] = true,
["core.integrations.telescope.search_headings"] = true,
["core.integrations.telescope.find_project_tasks"] = true,
["core.integrations.telescope.find_context_tasks"] = true,
["core.integrations.telescope.find_aof_tasks"] = true,
["core.integrations.telescope.find_aof_project_tasks"] = true,
["core.integrations.telescope.switch_workspace"] = true,
["core.integrations.telescope.find_backlinks"] = true,
["core.integrations.telescope.find_header_backlinks"] = true,
},
}

return module
4 changes: 2 additions & 2 deletions lua/telescope/_extensions/neorg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ return require("telescope").register_extension({
find_aof_tasks = require("neorg.modules.core.integrations.telescope.module").public.find_aof_tasks,
find_aof_project_tasks = require("neorg.modules.core.integrations.telescope.module").public.find_aof_project_tasks,
switch_workspace = require("neorg.modules.core.integrations.telescope.module").public.switch_workspace,
find_backlinks = require("neorg.modules.core.integrations.telescope.module").public.find_backlinks,
find_header_backlinks = require("neorg.modules.core.integrations.telescope.module").public.find_header_backlinks,
find_backlinks = require("neorg.modules.core.integrations.telescope.module").public["backlinks.file_backlinks"],
find_header_backlinks = require("neorg.modules.core.integrations.telescope.module").public["backlinks.header_backlinks"],
},
})
3 changes: 1 addition & 2 deletions lua/telescope/_extensions/neorg/insert_file_link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ local function get_file_title(file)
return nil
end

local bufnr = dirman.get_file_bufnr(tostring(file))
local metadata = ts.get_document_metadata(bufnr)
local metadata = ts.get_document_metadata(file)
if not metadata or not metadata.title then
return nil
end
Expand Down