Skip to content

fix: autocenter: avoid use of feedkeys #1632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,11 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufEnter", {
pattern = "NvimTree_*",
callback = function()
local bufnr = api.nvim_get_current_buf()
vim.schedule(function()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you try this instead

  if opts.view.centralize_selection then
    create_nvim_tree_autocmd("BufEnter", {
      pattern = "NvimTree_*",
      callback = function()
        local bufnr = api.nvim_get_current_buf()
        vim.schedule(function()
          local keys = api.nvim_replace_termcodes("zz", true, false, true)
          api.nvim_buf_call(bufnr, function()
            api.nvim_feedkeys(keys, "n", true)
          end)
        end)
      end,
    })
  end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but how would that be better than norm! zz? isn't the norm solution strictly simpler?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emmanueltouzery, what was missing from my implementation is ensuring that feedkeys() only fires in the specified buffer, so nvim_buf_call() should be called regardless.

I wasn't able to reproduce the issue, but using the feedkeys() vs vim.cmd could be debatable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i'll try your solution asap, but most likely i won't manage today 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kylo252

Copy link
Contributor Author

@emmanueltouzery emmanueltouzery Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I tried @kylo252 's solution and.. it didn't work for me.

this is the test startup file for @kylo252 's solution:

vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvt-min/site]]
local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
  require("packer").startup {
    {
      "wbthomason/packer.nvim",
      {"emmanueltouzery/nvim-tree.lua", branch = 'zz_kylo'},
      "nvim-tree/nvim-web-devicons",
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    config = {
      package_root = package_root,
      compile_path = install_path .. "/plugin/packer_compiled.lua",
      display = { non_interactive = true },
    },
  }
end
if vim.fn.isdirectory(install_path) == 0 then
  print "Installing nvim-tree and dependencies."
  vim.fn.system { "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path }
end
load_plugins()
require("packer").sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]]
vim.opt.termguicolors = true
vim.opt.cursorline = true

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
  require("nvim-tree").setup {
    view = {
      centralize_selection = true,
    }
  }
  vim.cmd("NvimTreeToggle")
end

function _G.repro()
  vim.cmd("wincmd l")
  vim.cmd("NvimTreeFocus")
  vim.fn.input("what say you?")
end

and this is for my solution:

vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvt-min/site]]
local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
  require("packer").startup {
    {
      "wbthomason/packer.nvim",
      {"emmanueltouzery/nvim-tree.lua", branch = 'no_feed_keys'},
      "nvim-tree/nvim-web-devicons",
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    config = {
      package_root = package_root,
      compile_path = install_path .. "/plugin/packer_compiled.lua",
      display = { non_interactive = true },
    },
  }
end
if vim.fn.isdirectory(install_path) == 0 then
  print "Installing nvim-tree and dependencies."
  vim.fn.system { "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path }
end
load_plugins()
require("packer").sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]]
vim.opt.termguicolors = true
vim.opt.cursorline = true

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
  require("nvim-tree").setup {
    view = {
      centralize_selection = true,
    }
  }
  vim.cmd("NvimTreeToggle")
end

function _G.repro()
  vim.cmd("wincmd l")
  vim.cmd("NvimTreeFocus")
  vim.fn.input("what say you?")
end

I'm not sure how to proceed, @kylo252, maybe you can test this and prepare a patch that fixes that issue? Note that this also fails in my real-world scenario, not only in this "artificial" reproduction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again. run the test files with nvim -nu filename.lua.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again, the instructions to reproduce are:

run it with nvim -nu repro.lua
let it launch, set up, then once nvim-tree is opened, run: :lua repro().

Without the fix, at the "What say you?" prompt, zz will be inserted.
With the fix, there'll be no zz.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I didn't account for the use of input() 😅

we can go with this version then

    callback = function()
      local bufnr = api.nvim_get_current_buf()
      vim.schedule(function()
        api.nvim_buf_call(bufnr, function()
          vim.cmd [[norm! zz]]
        end)
      end)
    end,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that works! force-pushed so that's now what the MR does.

btw it wasn't only a problem with input, in fact my original issue was with the "lightspeed" plugin, which waits for user input, and was "fed" that zz, therefore leading to unpredictable behavior.

local keys = api.nvim_replace_termcodes("zz", true, false, true)
api.nvim_feedkeys(keys, "n", true)
api.nvim_buf_call(bufnr, function()
vim.cmd [[norm! zz]]
end)
end)
end,
})
Expand Down