-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ws relative file/header backlink pickers (#50)
- Loading branch information
Showing
9 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[*] | ||
indent_style = space | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
local M = {} | ||
|
||
---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 | ||
M.build_backlink_regex = 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 | ||
|
||
return M |
19 changes: 19 additions & 0 deletions
19
lua/telescope/_extensions/neorg/backlinks/file_backlinks.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
local common = require("telescope._extensions.neorg.backlinks.common") | ||
local utils = require("neorg.telescope_utils") | ||
|
||
return function() | ||
local current_workspace = utils.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 = common.build_backlink_regex(current_workspace, current_file), | ||
use_regex = true, | ||
search_dirs = { current_workspace }, | ||
prompt_title = "File Backlinks", | ||
}) | ||
end |
38 changes: 38 additions & 0 deletions
38
lua/telescope/_extensions/neorg/backlinks/header_backlinks.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
local common = require("telescope._extensions.neorg.backlinks.common") | ||
local utils = require("neorg.telescope_utils") | ||
|
||
return function() | ||
local current_workspace = utils.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 = common.build_backlink_regex(current_workspace, current_file, heading), | ||
use_regex = true, | ||
search_dirs = { current_workspace }, | ||
prompt_title = "Header Backlinks (" .. heading .. ")", | ||
}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters