Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

[Feature] Improve `Path:normalize` to return shorter relative path using ../

Đang mở
#600 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
lua, neovim
Lĩnh vực
tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Lua
Star
3.5k
Fork
341
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của nvim-lua/plenary.nvim

Tất cả issue của nvim-lua/plenary.nvim

Issue tương tự

Thêm issue về Lua

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.