Skip to content

Commit 749c0b1

Browse files
authored
fix(pathlib): work with Pathlib objects from neorg >= v8.3.0 (#58)
* fix(pathlib): work with Pathlib objects from neorg >= v8.3.0 * fix(pathlib): use `remove_suffix` instead ref: nvim-neorg/neorg#1361 * fix(pathlib): ensure compatibility with neorg < v8
1 parent a4fc4eb commit 749c0b1

File tree

5 files changed

+41
-36
lines changed

5 files changed

+41
-36
lines changed

lua/telescope/_extensions/neorg/backlinks/common.lua

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ local M = {}
22

33
---produce the regular expression used to find workspace relative paths to the given file.
44
---Optionally takes a header that should exist in the current file
5-
---@param workspace_path string "/abs/path/to/workspace"
6-
---@param current_file string "test.norg"
5+
---@param workspace_path string|PathlibPath "/abs/path/to/workspace"
6+
---@param current_file string|PathlibPath "test.norg"
77
---@param heading string? "** heading"
88
---@return string
99
M.build_backlink_regex = function(workspace_path, current_file, heading)
10-
current_file = vim.api.nvim_buf_get_name(0)
11-
current_file = current_file:gsub("%.norg$", "")
12-
current_file = current_file:gsub("^" .. workspace_path .. "/", "")
10+
local Path = require("pathlib")
11+
12+
current_file = Path(vim.api.nvim_buf_get_name(0))
13+
current_file:remove_suffix(".norg")
14+
current_file = current_file:relative_to(Path(workspace_path))
1315

1416
if not heading then
17+
-- TODO: file sep may be `\` on Windows (currently in discussion)
18+
-- use `current_file:regex_string("[/\\]", Path.const.regex_charset.rust)` instead
19+
-- https://pysan3.github.io/pathlib.nvim/doc/PathlibPath.html#PathlibPath.regex_string
1520
return ([[\{:\$/%s:.*\}]]):format(current_file) -- {:$/workspace_path:}
1621
end
1722

@@ -21,6 +26,8 @@ M.build_backlink_regex = function(workspace_path, current_file, heading)
2126
end
2227
local heading_text = heading:gsub("^%** ", "")
2328
heading_text = heading_text:gsub("^%(.%)%s?", "")
29+
-- TODO: file sep may be `\` on Windows (currently in discussion)
30+
-- use `current_file:regex_string("[/\\]", Path.const.regex_charset.rust)` instead
2431
return ([[\{:\$/%s:(#|%s) %s\}]]):format(current_file, heading_prefix, heading_text) -- {:$/workspace_path:(# heading or ** heading)}
2532
end
2633

lua/telescope/_extensions/neorg/find_norg_files.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ local function get_norg_files()
1919

2020
local norg_files = dirman.get_norg_files(current_workspace[1])
2121

22-
return { current_workspace[2], norg_files }
22+
return { current_workspace[2]:tostring(), vim.tbl_map(tostring, norg_files) }
2323
end
2424

2525
return function(opts)

lua/telescope/_extensions/neorg/insert_file_link.lua

+12-20
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ local function generate_links()
4040
if not files[2] then
4141
return
4242
end
43+
if not (pcall(require, "pathlib")) then
44+
error("neorg-telescope Dependency Error: pysan3/pathlib.nvim is a required dependency.")
45+
end
4346

4447
local ts = neorg.modules.get_module("core.integrations.treesitter")
4548

46-
local workspace_offset = #tostring(files[1]) + 1
47-
49+
local Path = require("pathlib")
4850
for _, file in pairs(files[2]) do
49-
local bufnr = dirman.get_file_bufnr(file)
51+
local bufnr = dirman.get_file_bufnr(tostring(file))
5052

5153
local title = nil
5254
local title_display = ""
@@ -59,10 +61,12 @@ local function generate_links()
5961
end
6062

6163
if vim.api.nvim_get_current_buf() ~= bufnr then
64+
file = Path(file)
65+
local relative = file:relative_to(Path(files[1]))
6266
local links = {
6367
file = file,
64-
display = "$" .. file:sub(workspace_offset, -1) .. title_display,
65-
relative = file:sub(workspace_offset, -1):sub(0, -6),
68+
display = "$/" .. relative .. title_display,
69+
relative = relative:remove_suffix(".norg"),
6670
title = title,
6771
}
6872
table.insert(res, links)
@@ -99,24 +103,12 @@ return function(opts)
99103
actions_set.select:replace(function()
100104
local entry = state.get_selected_entry()
101105

102-
local path_no_extension
103-
104-
if entry then
105-
path_no_extension, _ = entry.value.file:gsub("%.norg$", "")
106-
else
107-
path_no_extension = state.get_current_line()
108-
end
109-
110106
actions.close(prompt_bufnr)
111107

112-
local file_name, _ = path_no_extension:gsub(".*%/", "")
113-
114108
vim.api.nvim_put({
115-
"{" .. ":$" .. entry.relative .. ":" .. "}" .. "[" .. (entry.title or file_name) .. "]",
116-
}, "c", true, true)
117-
if mode == "i" then
118-
vim.api.nvim_feedkeys("a", "n", false)
119-
end
109+
"{" .. ":$/" .. entry.relative .. ":" .. "}" .. "[" .. (entry.title or entry.relative) .. "]",
110+
}, "c", false, true)
111+
vim.api.nvim_feedkeys("hf]a", "t", false)
120112
end)
121113
return true
122114
end,

lua/telescope/_extensions/neorg/insert_link.lua

+14-8
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ end
2828

2929
--- Creates links for a `file` specified by `bufnr`
3030
--- @param bufnr number
31-
--- @param file string
31+
--- @param file PathlibPath|nil
32+
--- @param workspace PathlibPath
3233
--- @return table
3334
local function get_linkables(bufnr, file, workspace)
3435
local ret = {}
3536

3637
local lines
3738
if file then
38-
lines = vim.fn.readfile(tostring(file))
39-
file = file:gsub(".norg", "")
40-
file = "$" .. file:sub(#workspace + 1, -1)
39+
lines = vim.fn.readfile(file:tostring("/"))
40+
file = file:remove_suffix(".norg")
41+
file = "$/" .. file:relative_to(workspace)
4142
else
4243
lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
4344
end
@@ -66,12 +67,17 @@ local function generate_links()
6667
if not dirman then
6768
return nil
6869
end
70+
if not (pcall(require, "pathlib")) then
71+
error("neorg-telescope Dependency Error: pysan3/pathlib.nvim is a required dependency.")
72+
end
73+
6974
local current_workspace = dirman.get_current_workspace()
7075

7176
local files = get_norg_files()
7277

78+
local Path = require("pathlib")
7379
for _, file in pairs(files[2]) do
74-
local bufnr = dirman.get_file_bufnr(file)
80+
local bufnr = dirman.get_file_bufnr(tostring(file))
7581
if not bufnr then
7682
end
7783

@@ -80,11 +86,11 @@ local function generate_links()
8086
if vim.api.nvim_get_current_buf() == bufnr then
8187
return nil
8288
else
83-
return file
89+
return Path(file)
8490
end
8591
end)()
8692

87-
local links = get_linkables(bufnr, file_inserted, current_workspace[2])
93+
local links = get_linkables(bufnr, file_inserted, Path(current_workspace[2]))
8894

8995
vim.list_extend(res, links)
9096
end
@@ -131,7 +137,7 @@ return function(opts)
131137
display = make_display,
132138
ordinal = entry.display,
133139
lnum = entry.line,
134-
file = entry.file,
140+
file = entry.file and tostring(entry.file) or nil,
135141
linkable = entry.linkable,
136142
}
137143
end,

lua/telescope/_extensions/neorg/switch_workspace.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ return function(options)
5656
local workspace = entry.value
5757
local lines = {}
5858
table.insert(lines, "Path:")
59-
table.insert(lines, workspace.path)
59+
table.insert(lines, tostring(workspace.path))
6060
table.insert(lines, "Files:")
6161
local files = neorg.modules.get_module("core.dirman").get_norg_files(workspace.name)
6262
for _, file in ipairs(files) do
63-
table.insert(lines, file)
63+
table.insert(lines, tostring(file))
6464
end
6565
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, true, lines)
6666
vim.api.nvim_buf_add_highlight(self.state.bufnr, ns, "Special", 0, 0, -1)

0 commit comments

Comments
 (0)