Skip to content

Commit

Permalink
fix(pathlib): work with Pathlib objects from neorg >= v8.3.0 (#58)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pysan3 authored Apr 19, 2024
1 parent a4fc4eb commit 749c0b1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
17 changes: 12 additions & 5 deletions lua/telescope/_extensions/neorg/backlinks/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ 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 workspace_path string|PathlibPath "/abs/path/to/workspace"
---@param current_file string|PathlibPath "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 .. "/", "")
local Path = require("pathlib")

current_file = Path(vim.api.nvim_buf_get_name(0))
current_file:remove_suffix(".norg")
current_file = current_file:relative_to(Path(workspace_path))

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

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

Expand Down
2 changes: 1 addition & 1 deletion lua/telescope/_extensions/neorg/find_norg_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local function get_norg_files()

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

return { current_workspace[2], norg_files }
return { current_workspace[2]:tostring(), vim.tbl_map(tostring, norg_files) }
end

return function(opts)
Expand Down
32 changes: 12 additions & 20 deletions lua/telescope/_extensions/neorg/insert_file_link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ local function generate_links()
if not files[2] then
return
end
if not (pcall(require, "pathlib")) then
error("neorg-telescope Dependency Error: pysan3/pathlib.nvim is a required dependency.")
end

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

local workspace_offset = #tostring(files[1]) + 1

local Path = require("pathlib")
for _, file in pairs(files[2]) do
local bufnr = dirman.get_file_bufnr(file)
local bufnr = dirman.get_file_bufnr(tostring(file))

local title = nil
local title_display = ""
Expand All @@ -59,10 +61,12 @@ local function generate_links()
end

if vim.api.nvim_get_current_buf() ~= bufnr then
file = Path(file)
local relative = file:relative_to(Path(files[1]))
local links = {
file = file,
display = "$" .. file:sub(workspace_offset, -1) .. title_display,
relative = file:sub(workspace_offset, -1):sub(0, -6),
display = "$/" .. relative .. title_display,
relative = relative:remove_suffix(".norg"),
title = title,
}
table.insert(res, links)
Expand Down Expand Up @@ -99,24 +103,12 @@ return function(opts)
actions_set.select:replace(function()
local entry = state.get_selected_entry()

local path_no_extension

if entry then
path_no_extension, _ = entry.value.file:gsub("%.norg$", "")
else
path_no_extension = state.get_current_line()
end

actions.close(prompt_bufnr)

local file_name, _ = path_no_extension:gsub(".*%/", "")

vim.api.nvim_put({
"{" .. ":$" .. entry.relative .. ":" .. "}" .. "[" .. (entry.title or file_name) .. "]",
}, "c", true, true)
if mode == "i" then
vim.api.nvim_feedkeys("a", "n", false)
end
"{" .. ":$/" .. entry.relative .. ":" .. "}" .. "[" .. (entry.title or entry.relative) .. "]",
}, "c", false, true)
vim.api.nvim_feedkeys("hf]a", "t", false)
end)
return true
end,
Expand Down
22 changes: 14 additions & 8 deletions lua/telescope/_extensions/neorg/insert_link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ end

--- Creates links for a `file` specified by `bufnr`
--- @param bufnr number
--- @param file string
--- @param file PathlibPath|nil
--- @param workspace PathlibPath
--- @return table
local function get_linkables(bufnr, file, workspace)
local ret = {}

local lines
if file then
lines = vim.fn.readfile(tostring(file))
file = file:gsub(".norg", "")
file = "$" .. file:sub(#workspace + 1, -1)
lines = vim.fn.readfile(file:tostring("/"))
file = file:remove_suffix(".norg")
file = "$/" .. file:relative_to(workspace)
else
lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
end
Expand Down Expand Up @@ -66,12 +67,17 @@ local function generate_links()
if not dirman then
return nil
end
if not (pcall(require, "pathlib")) then
error("neorg-telescope Dependency Error: pysan3/pathlib.nvim is a required dependency.")
end

local current_workspace = dirman.get_current_workspace()

local files = get_norg_files()

local Path = require("pathlib")
for _, file in pairs(files[2]) do
local bufnr = dirman.get_file_bufnr(file)
local bufnr = dirman.get_file_bufnr(tostring(file))
if not bufnr then
end

Expand All @@ -80,11 +86,11 @@ local function generate_links()
if vim.api.nvim_get_current_buf() == bufnr then
return nil
else
return file
return Path(file)
end
end)()

local links = get_linkables(bufnr, file_inserted, current_workspace[2])
local links = get_linkables(bufnr, file_inserted, Path(current_workspace[2]))

vim.list_extend(res, links)
end
Expand Down Expand Up @@ -131,7 +137,7 @@ return function(opts)
display = make_display,
ordinal = entry.display,
lnum = entry.line,
file = entry.file,
file = entry.file and tostring(entry.file) or nil,
linkable = entry.linkable,
}
end,
Expand Down
4 changes: 2 additions & 2 deletions lua/telescope/_extensions/neorg/switch_workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ return function(options)
local workspace = entry.value
local lines = {}
table.insert(lines, "Path:")
table.insert(lines, workspace.path)
table.insert(lines, tostring(workspace.path))
table.insert(lines, "Files:")
local files = neorg.modules.get_module("core.dirman").get_norg_files(workspace.name)
for _, file in ipairs(files) do
table.insert(lines, file)
table.insert(lines, tostring(file))
end
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, true, lines)
vim.api.nvim_buf_add_highlight(self.state.bufnr, ns, "Special", 0, 0, -1)
Expand Down

0 comments on commit 749c0b1

Please sign in to comment.