[Feature] Improve `Path:normalize` to return shorter relative path using ../
还没有人认领这个 Issue。
评估
调研方向
Locate plenary.path's Path:normalize implementation and any related path tests. Compare its existing absolute and relative-down results with the proposed relative-up form, while accounting for Windows support. Done means normalize returns the shortest valid path without breaking existing behavior.
由索引模型根据 Issue 内容生成。
描述
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):
---@type LazySpec
return {
{
'nvim-lua/plenary.nvim', -- Not a plugin, just a useful library.
config = function()
local Path = require 'plenary.path'
local normalize = Path.normalize
--- Monkey-patch Path:normalize method to make it try harder looking for shortest
--- relative path by checking also path UP from cwd: '../../…'.
---@diagnostic disable-next-line: duplicate-set-field
Path.normalize = function(self, cwd)
-- Absolute (DOWN FROM / or ~) or relative (DOWN FROM cwd).
local orig = normalize(self, cwd)
-- Absolute (DOWN FROM / or ~), but we'll make it relative (UP FROM cwd).
local rel = vim.fn.fnamemodify(orig, ':p:~')
if string.match(orig, '^[/~]') then -- Absolute, thus may be shorter.
local abs = vim.fn.fnamemodify(rel, ':p')
local abs_path = Path:new(abs)
local dir = cwd .. '/'
local up = ''
repeat
up = up .. '../'
rel = abs_path:make_relative(Path:new(dir .. up):absolute())
until rel ~= abs
rel = up .. rel
end
return string.len(orig) <= string.len(rel) and orig or rel
end
end,
},
}
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).
- 主要语言
- Lua
- 星标
- 3.5k
- 派生
- 341
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
nvim-lua/plenary.nvim 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 88/100
nvim-lua/plenary.nvim#682 ·
-
uniform usage of `os.getenv`可能已有人在做 @rakotomandimby 于 124 天前认领。 未关闭
难度 1/5 1 小时以内 新手友好度 72/100
nvim-lua/plenary.nvim#680 ·
-
难度 2/5 1-3 小时 新手友好度 45/100
nvim-lua/plenary.nvim#675 ·
-
难度 4/5 3-5 天 新手友好度 28/100
nvim-lua/plenary.nvim#672 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 25/100
nvim-lua/plenary.nvim#671 · 1 条评论 ·
查看 nvim-lua/plenary.nvim 的全部 Issue
相似的 Issue
-
bug good first issue ready ticket
难度 2/5 1-3 小时 新手友好度 82/100
Domktorymysli/vclu#20 ·
-
难度 1/5 1 小时以内 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 70/100
-
documentation good first issue
难度 2/5 1-3 小时 新手友好度 62/100
InauguralSystems/EigenLua#60 ·
维护者通常 1 天内回复
-
bug
难度 1/5 1-3 小时 新手友好度 85/100
bradcypert/plum#265 ·
维护者通常 1 天内回复