simonmclean / triptych.nvim

Directory browser plugin for Neovim, inspired by Ranger
Apache License 2.0
187 stars 3 forks source link

Opening and moving inside new windows from inside triptych still moves triptych cursor #49

Closed theKnightsOfRohan closed 5 months ago

theKnightsOfRohan commented 5 months ago

Bug description When opening a window inside triptych, the cursor position from within the window is still used to navigate inside triptych, which is evidenced by the file preview changing. You would expect that the triptych window movements would only register when the cursor is inside the triptych window.

I first became aware of this issue when trying to override the vim.ui.select() method with a nui menu, rather than the text-based input I found before. That minimal init is also included below.

I would propose to make the tritych cursor only move when the neovim cursor is inside the buffer, just as an added layer of protection as well.

Steps to reproduce Here is the minimal lazy config of the nui menu:

return {
    "MunifTanjim/nui.nvim",
    config = function()
        local Menu = require("nui.menu")

        vim.ui.select = function(items, opts, on_choice)
            vim.validate({
                items = { items, 'table', false },
                opts = { opts, 'table', true },
                on_choice = { on_choice, 'function', false },
            })
            opts = opts or {}
            local format_item = opts.format_item or tostring

            Menu({
                relative = "cursor",
                position = {
                    row = 2,
                    col = 0,
                },
                size = {
                    width = 40,
                },
                zindex = 1000,
                border = {
                    style = "rounded",
                    text = {
                        top = opts.prompt or "Select an item",
                        top_align = "center",
                    },
                },
            }, {
                lines = (function()
                    ---@type NuiTree.Node[]
                    local selections = {}

                    for _, item in ipairs(items) do
                        if type(item) == "string" then
                            table.insert(selections, Menu.item(item))
                        elseif type(item) == "table" then
                            table.insert(selections, Menu.item(format_item(item), item))
                        end
                    end

                    return selections
                end)(),

                on_close = function()
                    print("Closed")
                end,
                on_submit = function(selected)
                    on_choice(selected)
                end,
            }):mount()
        end

        vim.keymap.set("n", "<leader><leader>", function()
            vim.ui.select({ 'One', 'Two', 'Three' }, {}, function(selected)
                print("Selected: " .. selected)
            end)
        end)
    end,
}

By opening Triptych with this config added, the delete action's dialog is overridden, and that method is the easiest way to reproduce the bug because of the yes/no delete menu.

Setup

return {
    "simonmclean/triptych.nvim",
    event = "VeryLazy",
    dependencies = {
        "nvim-lua/plenary.nvim",
        "nvim-tree/nvim-web-devicons",
        "mhinz/vim-signify",
    },
    config = function()
        require("triptych").setup({
            mappings = {
                -- Everything below is buffer-local, meaning it will only apply to Triptych windows
                show_help = 'g?',
                jump_to_cwd = '.',           -- Pressing again will toggle back
                nav_left = 'h',
                nav_right = { 'l', '<CR>' }, -- If target is a file, opens the file in-place
                open_hsplit = { '-' },
                open_vsplit = { '|' },
                open_tab = { '<C-t>' },
                cd = '<leader>cd',
                delete = 'd',
                add = 'a',
                copy = 'c',
                rename = 'r',
                cut = 'x',
                paste = 'p',
                quit = 'q',
                toggle_hidden = '<leader>.',
            },
            extension_mappings = {},
            options = {
                dirs_first = true,
                show_hidden = false,
                line_numbers = {
                    enabled = true,
                    relative = false,
                },
                file_icons = {
                    enabled = true,
                    directory_icon = '',
                    fallback_file_icon = ''
                },
                responsive_column_widths = {
                    -- Keys are breakpoints, values are column widths
                    -- A breakpoint means "when vim.o.columns >= x, use these column widths"
                    -- Columns widths must add up to 1 after rounding to 2 decimal places
                    -- Parent or child windows can be hidden by setting a width of 0
                    ['0'] = { 0, 0.5, 0.5 },
                    ['120'] = { 0.2, 0.3, 0.5 },
                    ['200'] = { 0.25, 0.25, 0.5 },
                },
                highlights = { -- Highlight groups to use. See `:highlight` or `:h highlight`
                    file_names = 'NONE',
                    directory_names = 'NONE',
                },
                syntax_highlighting = { -- Applies to file previews
                    enabled = true,
                    debounce_ms = 100,
                },
                backdrop = 60 -- Backdrop opacity. 0 is fully opaque, 100 is fully transparent (disables the feature)
            },
            git_signs = {
                enabled = true,
                signs = {
                    -- The value can be either a string or a table.
                    -- If a string, will be basic text. If a table, will be passed as the {dict} argument to vim.fn.sign_define
                    -- If you want to add color, you can specify a highlight group in the table.
                    add = { text = "+", texthl = "SignifySignAdd" },
                    modify = { text = "~", texthl = "SignifySignChange" },
                    rename = { text = "r", texthl = "SignifySignChange" },
                    untracked = { text = "?", texthl = "SignifySignDelete" },
                },
            },
            diagnostic_signs = {
                enabled = true,
            }
        })

        vim.keymap.set("n", "<leader>pv", vim.cmd.Triptych, { silent = true })
    end,
}
simonmclean commented 5 months ago

@theKnightsOfRohan I've fixed the cursor issue. Is Triptych working with nui for you?