[Feature] Improve `Path:normalize` to return shorter relative path using ../
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 35/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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).
- Langage dominant
- Lua
- Étoiles
- 3.5k
- Forks
- 340
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de nvim-lua/plenary.nvim
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
nvim-lua/plenary.nvim#682 ·
-
uniform usage of `os.getenv`Peut-être pris @rakotomandimby l’a pris il y a 128 jours. Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 72/100
nvim-lua/plenary.nvim#680 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 45/100
nvim-lua/plenary.nvim#675 ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 28/100
nvim-lua/plenary.nvim#672 · 1 commentaire ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 25/100
nvim-lua/plenary.nvim#671 · 1 commentaire ·
Toutes les issues de nvim-lua/plenary.nvim
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
windwp/nvim-ts-autotag#266 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
pterodactyl/game-eggs#657 ·
-
Adding `:wtall` commandOuverteenhancement good first issue
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
stalker57241/tabview#10 ·
-
feature-request mini.icons
Difficulté 1/5 Moins d'une heure Accessibilité débutants 82/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
mfussenegger/nvim-dap#1613 · 2 commentaires ·