Skip to content

Lua: normalize paths in ui.select feature#282

Closed
trev-dev wants to merge 2 commits intolervag:masterfrom
trev-dev:fix/nvim-root-paths
Closed

Lua: normalize paths in ui.select feature#282
trev-dev wants to merge 2 commits intolervag:masterfrom
trev-dev:fix/nvim-root-paths

Conversation

@trev-dev
Copy link
Copy Markdown
Contributor

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.

This commit fixes paths that are made from ~/Home and/or have a
trailing slash
@bybor
Copy link
Copy Markdown

bybor commented Mar 25, 2023

Do you use Linux? Just curious how safe it is to get the latest wiki.vim code on Windows.

@trev-dev
Copy link
Copy Markdown
Contributor Author

Do you use Linux? Just curious how safe it is to get the latest wiki.vim code on Windows.

Yep, I do. Though, this feature was never Windows compatible to begin with.

@trev-dev
Copy link
Copy Markdown
Contributor Author

@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.

@trev-dev trev-dev marked this pull request as draft March 25, 2023 07:30
@trev-dev trev-dev changed the title Lua: guard against relative paths and trailing slashes Lua: normalize paths in ui.select feature Mar 25, 2023
Copy link
Copy Markdown
Owner

@lervag lervag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@saccarosium
Copy link
Copy Markdown
Contributor

I've found the problem. I wasn't using the function wiki#get_root() that does all the normalizing all for you. To fix the bug we need to only apply this change.

-- 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

@lervag
Copy link
Copy Markdown
Owner

lervag commented Mar 25, 2023

Also, I'm not sure if any of this is really needed. Notice how we already take care of this stuff here. I believe we can remove the filter() function and instead just use the output from `p:

The filter function is used for simply making the full path relative to the wiki_path root. I don't really see why remove it.

No, the point was that the vimscript function already returns the path relative to the wiki root. The return value of wiki#page#get_all() is a list of path pairs where the first element is the absolute path and the second element is the path relative to the wiki root.

Thus, there is no reason to use the filter!

I've found the problem. I wasn't using the function wiki#get_root() that does all the normalizing all for you. To fix the bug we need to only apply this change.

-- 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

Yes, wiki#get_root() seems a better thing to use here, but again, I think my previous suggestion is better. I'll just push it myself to show what I mean.

lervag added a commit that referenced this pull request Mar 25, 2023
@lervag
Copy link
Copy Markdown
Owner

lervag commented Mar 25, 2023

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.

@lervag lervag closed this Mar 25, 2023
@trev-dev
Copy link
Copy Markdown
Contributor Author

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 :)

@saccarosium
Copy link
Copy Markdown
Contributor

saccarosium commented Mar 25, 2023

Let me know what you think and if it works as expected on your ends as well.

Work good on my end. The only little weird thing is that files are prececced by a / (its only a visual thing).

Screenshot 2023-03-25 at 22 11 44

@lervag
Copy link
Copy Markdown
Owner

lervag commented Mar 26, 2023

Work good on my end. The only little weird thing is that files are prececced by a / (its only a visual thing).

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?

@lervag
Copy link
Copy Markdown
Owner

lervag commented Mar 26, 2023

I think it looks better without the slash, so I pushed an update that removes it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants