gbprod / yanky.nvim

Improved Yank and Put functionalities for Neovim
Do What The F*ck You Want To Public License
794 stars 22 forks source link

Delay with vim.opt.clipboard = "unnamedplus" #176

Open xzbdmw opened 1 month ago

xzbdmw commented 1 month ago
local root = vim.fn.fnamemodify("./.repro", ":p")

_G.Time = function(start, msg)
    msg = msg or ""
    local duration = 0.000001 * (vim.loop.hrtime() - start)
    -- __AUTO_GENERATED_PRINT_VAR_START__
    print(msg .. [==[duration:]==], vim.inspect(duration)) -- __AUTO_GENERATED_PRINT_VAR_END__
end
-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "--single-branch",
        "https://github.com/folke/lazy.nvim.git",
        lazypath,
    })
end
vim.opt.runtimepath:prepend(lazypath)
-- install plugins
local plugins = {
    -- do not remove the colorscheme!
    "folke/tokyonight.nvim",
    -- add any other pugins here
    {
        "gbprod/yanky.nvim",
        -- enabled = false,
        keys = {
            { "y", "<Plug>(YankyYank)", mode = { "n", "x" } },

            { "<leader>p", "<Plug>(YankyPreviousEntry)" },
            { "<leader>n", "<Plug>(YankyNextEntry)" },

            {
                "p",
                function()
                    YANK = vim.uv.hrtime()

                    return "<Plug>(YankyPutAfter)"
                end,
                expr = true,
            },
            { "P", "<Plug>(YankyPutBefore)" },

            { "gp", "<Plug>(YankyGPutAfter)", mode = { "n", "x" } },
            { "gP", "<Plug>(YankyGPutBefore)", mode = { "n", "x" } },

            -- visual mode paste
            { "p", "<Plug>(YankyPutBefore)", { desc = "Paste without copying replaced text" }, mode = { "x" } },
            { "<D-c>", "<Plug>(YankyYank)", mode = { "n", "v", "i" } },

            -- force paste the same line
            {
                "<leader>P",
                function()
                    YANK = vim.uv.hrtime()
                    return "<Plug>(YankyPutAfterCharwiseJoined)"
                end,
                mode = { "n", "x" },
                expr = true,
            },

            -- text object
            {
                "[p",
                function()
                    require("yanky.textobj").last_put()
                end,
                mode = { "x", "o" },
            },
        },
        lazy = false,
        opts = {
            ring = {
                history_length = 10,
                storage = "shada",
                storage_path = vim.fn.stdpath("data") .. "/databases/yanky.db", -- Only for sqlite storage
                sync_with_numbered_registers = true,
                cancel_event = "update",
                ignore_registers = { "_" },
                update_register_on_cycle = false,
            },
            picker = {
                select = {
                    action = nil, -- nil to use default put action
                },
                telescope = {
                    use_default_mappings = true, -- if default mappings should be used
                    mappings = nil, -- nil to use default mappings or no mappings (see `use_default_mappings`)
                },
            },
            system_clipboard = {
                sync_with_ring = false,
            },
            highlight = {
                on_put = true,
                on_yank = true,
                timer = 400,
            },
            preserve_cursor_position = {
                enabled = true,
            },
            textobj = {
                enabled = true,
            },
        },
    },
}
vim.opt.clipboard = "unnamedplus" -- Sync with system clipboard
require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

-- add anything else here
-- vim.opt.termguicolors = true
-- do not remove the colorscheme!
vim.cmd([[colorscheme tokyonight]])
vim.api.nvim_create_autocmd("TextChanged", {
    callback = function()
        Time(YANK)
    end,
})
  1. touch ~/.config/nvim/repro.lua
  2. nvim -u ~/.config/nvim/repro.lua ~/.config/nvim/repro.lua
  3. yy
  4. p
  5. see the delay is 60ms(macos 14.2 m1 pro), for (YankyPutAfterCharwiseJoined) delay is 140ms.
  6. I test the code and everytime call setreg there is a 20ms cost, if I disable this plugin ,using
    
    vim.keymap.set("n", "p", function()
    YANK = vim.uv.hrtime()
    return "p"
    end, { expr = true })

, duration is 13ms
gbprod commented 1 month ago

Intersting issue, I have to make some tests.