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: ws relative file/header backlink pickers #50

Merged
merged 5 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

benlubas marked this conversation as resolved.
Show resolved Hide resolved
[*]
indent_style = space
indent_size = 4
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ With `Telescope neorg search_headings` you can search through all the headings i
<img alt="search_headings" src="https://user-images.githubusercontent.com/81827001/153647155-80f5579f-acc9-489e-9e05-acf31a646bba.png">
</details>

### Search File and Heading Backlinks
benlubas marked this conversation as resolved.
Show resolved Hide resolved
These are limited to workspace relative links (ie.
`{:$/worspace/relative/path:}`) for the sake of simplicity. Both exact
(`{:$/path:** lvl 2 heading}`) and fuzzy (`{:$/path:# heading}`) links are
found.

<details>
<summary>Demo</summary>

![search backlink](https://github.com/benlubas/neorg-telescope/assets/56943754/1332f319-cf43-4a5b-902a-f2bbe4ea14e3)
benlubas marked this conversation as resolved.
Show resolved Hide resolved

</details>

benlubas marked this conversation as resolved.
Show resolved Hide resolved


## Gtd Pickers
### Those pickers are all broken since gtd was removed in core
<details>
Expand Down
10 changes: 10 additions & 0 deletions lua/neorg/modules/core/integrations/telescope/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module.load = function()
"find_aof_tasks",
"find_context_tasks",
"switch_workspace",
"find_backlinks",
"find_header_backlinks",
})
end

Expand All @@ -42,6 +44,8 @@ module.public = {
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.on_event = function(event)
Expand All @@ -65,6 +69,10 @@ module.on_event = function(event)
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
end

Expand All @@ -80,6 +88,8 @@ module.events.subscribed = {
["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,
},
}

Expand Down
34 changes: 34 additions & 0 deletions lua/telescope/_extensions/neorg/backlinks/file_backlinks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local neorg_loaded, neorg = pcall(require, "neorg.core")

assert(neorg_loaded, "Neorg is not loaded - please make sure to load Neorg first")

local file_backlink_regex = require("telescope._extensions.neorg.backlinks.utils")
benlubas marked this conversation as resolved.
Show resolved Hide resolved

local function get_current_workspace()
benlubas marked this conversation as resolved.
Show resolved Hide resolved
local dirman = neorg.modules.get_module("core.dirman")

if dirman then
local current_workspace = dirman.get_current_workspace()[2]

return current_workspace
end

return nil
end

return function()
local current_workspace = get_current_workspace()

if not current_workspace then
return
end

local current_file = vim.api.nvim_buf_get_name(0)

require("telescope.builtin").grep_string({
search = file_backlink_regex(current_workspace, current_file),
use_regex = true,
search_dirs = { current_workspace },
prompt_title = "File Backlinks",
})
end
53 changes: 53 additions & 0 deletions lua/telescope/_extensions/neorg/backlinks/header_backlinks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local neorg_loaded, neorg = pcall(require, "neorg.core")

assert(neorg_loaded, "Neorg is not loaded - please make sure to load Neorg first")

local file_backlink_regex = require("telescope._extensions.neorg.backlinks.utils")

local function get_current_workspace()
local dirman = neorg.modules.get_module("core.dirman")

if dirman then
local current_workspace = dirman.get_current_workspace()[2]

return current_workspace
end

return nil
end

return function()
local current_workspace = get_current_workspace()

if not current_workspace then
return
end

local current_file = vim.api.nvim_buf_get_name(0)
local linenr = vim.api.nvim_win_get_cursor(0)[1]
local lines = vim.api.nvim_buf_get_lines(0, 0, linenr, false)
local heading = nil

-- HACK: iterate backward (up) over lines, and use the first heading we find. We should be using
-- TS instead, but I'm not super familiar with how to do things like that.
for i = #lines, 1, -1 do
local line = lines[i]
local potential_heading = line:match("^%s*%*+ .*$")
if potential_heading then
heading = potential_heading
break
end
end

if not heading then
vim.notify("[Neorg Telescope] Couldn't find current heading", vim.log.levels.ERROR)
return
end

require("telescope.builtin").grep_string({
search = file_backlink_regex(current_workspace, current_file, heading),
use_regex = true,
search_dirs = { current_workspace },
prompt_title = "Header Backlinks (" .. heading .. ")",
})
end
23 changes: 23 additions & 0 deletions lua/telescope/_extensions/neorg/backlinks/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---produce the regular expression used to find workspace relative paths to the given file.
---Optionally takes a header that should exist in the current file
---@param workspace_path string "/abs/path/to/workspace"
---@param current_file string "test.norg"
---@param heading string? "** heading"
---@return string
return function(workspace_path, current_file, heading)
current_file = vim.api.nvim_buf_get_name(0)
current_file = current_file:gsub("%.norg$", "")
current_file = current_file:gsub("^" .. workspace_path .. "/", "")

if not heading then
return ([[\{:\$/%s:.*\}]]):format(current_file) -- {:$/workspace_path:}
end

local heading_prefix = heading:match("^%**")
if heading_prefix then
heading_prefix = heading_prefix:gsub("%*", "\\*")
end
local heading_text = heading:gsub("^%** ", "")
heading_text = heading_text:gsub("^%(.%)%s?", "")
return ([[\{:\$/%s:(#|%s) %s\}]]):format(current_file, heading_prefix, heading_text) -- {:$/workspace_path:(# heading or ** heading)}
end