Lua: normalize paths in ui.select feature#282
Lua: normalize paths in ui.select feature#282trev-dev wants to merge 2 commits intolervag:masterfrom trev-dev:fix/nvim-root-paths
Conversation
This commit fixes paths that are made from ~/Home and/or have a trailing slash
Filter's now doing too much
|
Do you use Linux? Just curious how safe it is to get the latest |
Yep, I do. Though, this feature was never Windows compatible to begin with. |
|
@bybor I'm pretty sure I can fix windows interop for this too, I just have to tweak the slashes. I'm out of time but will do it later. |
lervag
left a comment
There was a problem hiding this comment.
Beside the bug, I believe my second comment should be further discussed here, and I would also be happy if @saccarosium could participate since he was responsible for the initial lua code in #281.
I believe we can resolve this without using the filters at all. Or, at least for the get_pages function. Notice that wiki#page#get_all returns a list of pairs (path, path_from_wiki_root), where path_from_wiki_root is the result of the filter itself. So, the question is how to make vim.ui.select use the second while displaying the list of pages and the first when opening it. Perhaps this is where we'll use the format option? Something like this:
local pages = vim.fn["wiki#page#get_all"]()
vim.ui.select(pages, {
prompt = "WikiPages> ",
format_item = function(pair) return pair[2] end
}, function(pair)
vim.cmd("edit " .. pair[1])
end)I think this may also work as expected on Windows, since wiki#page#get_all already does that work.
|
I've found the problem. I wasn't using the function -- Returns shorten path
local function filter(path)
local root = vim.fn["wiki#get_root"]().. "/"
return path:gsub(root, "")
end
-- Returns full path
local function unfilter(path)
local root = vim.fn["wiki#get_root"]() .. "/"
return root .. path
end |
No, the point was that the vimscript function already returns the path relative to the wiki root. The return value of Thus, there is no reason to use the filter!
Yes, |
|
I've pushed an update that I believe both 1) fixes the present issue, and 2) improves the code slightly by using the data already created by the vimscript apis. Let me know what you think and if it works as expected on your ends as well. |
Damn, too slow! Nice one :) |
Yes; I was not sure what would be the most appropriate solution here. It should be easy to remove the preceding slash. But do we want it? I guess one could claim it sort of indicates that the path is relative to wiki root? |
|
I think it looks better without the slash, so I pushed an update that removes it. |

The new lua functions weren't working for me because I declared my root as
~/Wiki/, both the home relative path and trailing slash were tripping up the UI select and file picker.Here's a fix for that.