Fix Jumping Cursor in Neo-tree
Background
When I was using Neo-tree in nixvim, I met the bug as shown below:
I bound o and <cr> to open, and h to close_node.
In the GIF above, I firstly used o to open the folder clipboard/, and moved the cursor down to the sync/ folder. Then I use h to close the clipboard/ folder and the cursor returned to clipboard/ as expected. After that, I navigated to the events/ folder, and tried to expand the folder with o, then the bug occurred, where my cursor jumped back to clipboard/.
It seems that the position of cursor was not updated correctly when a node is opened with open operation.
Solution
So the solution will be clear and easy: just update the position of cursor after calling open to open a folder.
The Lua function is:
function(state)
local tree = state.tree
local node = assert(tree:get_node())
require("neo-tree.sources.filesystem.commands").open(state)
require("neo-tree.ui.renderer").focus_node(state, node:get_id())
endAnd Neo-tree allows us to map a function to a key. So we can simply bind the function to the key in the setup function.
I will not introduce detailed configuration in Lua, but I would like to share my configuration in nixvim.
To simplify the configuration, I created a string variable openFileWithForceFocus to store the function, and assigned it "o".__raw and "<cr>".__raw.
The minimal configuration is:
{...}: let
openFileWithForceFocus = ''
function(state)
local tree = state.tree
local node = assert(tree:get_node())
require("neo-tree.sources.filesystem.commands").open(state)
require("neo-tree.ui.renderer").focus_node(state, node:get_id())
end
'';
in {
plugins.neo-tree = {
enable = true;
settings.window.mappings = {
"<cr>".__raw = openFileWithForceFocus;
"o".__raw = openFileWithForceFocus;
};
};
}