You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently Path:normalize returns 3 possible paths:
absolute from /
absolute from ~
relative down from cwd
I propose to add a fourth one:
relative up from cwd
At a glance this changes behaviour in a compatible way and shouldn't break any uses.
The point is to get the shortest possible path (which is anyway feels like a current goal for this method) - sometimes it may be the absolute one, sometimes up from cwd.
I've found it really useful in Neovim, when file is opened using Telescope plugin and you doesn't control how file name will be formed (manually you can either do :tabnew ../file or :tabnew ~/proj/file), but you wanna see as short as possible file name in statusline/tab name. Here is my current implementation (as a Lazy plugin):
---@typeLazySpecreturn {
{
'nvim-lua/plenary.nvim', -- Not a plugin, just a useful library.config=function()
localPath=require'plenary.path'localnormalize=Path.normalize--- Monkey-patch Path:normalize method to make it try harder looking for shortest--- relative path by checking also path UP from cwd: '../../…'.---@diagnosticdisable-next-line:duplicate-set-fieldPath.normalize=function(self, cwd)
-- Absolute (DOWN FROM / or ~) or relative (DOWN FROM cwd).localorig=normalize(self, cwd)
-- Absolute (DOWN FROM / or ~), but we'll make it relative (UP FROM cwd).localrel=vim.fn.fnamemodify(orig, ':p:~')
ifstring.match(orig, '^[/~]') then-- Absolute, thus may be shorter.localabs=vim.fn.fnamemodify(rel, ':p')
localabs_path=Path:new(abs)
localdir=cwd..'/'localup=''repeatup=up..'../'rel=abs_path:make_relative(Path:new(dir..up):absolute())
untilrel~=absrel=up..relendreturnstring.len(orig) <=string.len(rel) andorigorrelendend,
},
}
This implementation is not suitable for the lib (because there is no Windows support and it depends on Neovim), but sometimes code worth thousands words 😄 and also it may be useful for other Neovim users until this change will be implemented (if it will be accepted at all).
The text was updated successfully, but these errors were encountered:
Currently
Path:normalize
returns 3 possible paths:I propose to add a fourth one:
At a glance this changes behaviour in a compatible way and shouldn't break any uses.
The point is to get the shortest possible path (which is anyway feels like a current goal for this method) - sometimes it may be the absolute one, sometimes up from cwd.
I've found it really useful in Neovim, when file is opened using Telescope plugin and you doesn't control how file name will be formed (manually you can either do
:tabnew ../file
or:tabnew ~/proj/file
), but you wanna see as short as possible file name in statusline/tab name. Here is my current implementation (as a Lazy plugin):This implementation is not suitable for the lib (because there is no Windows support and it depends on Neovim), but sometimes code worth thousands words 😄 and also it may be useful for other Neovim users until this change will be implemented (if it will be accepted at all).
The text was updated successfully, but these errors were encountered: