Skip to content
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

feat: Add ability to hijack-cursor #955

Merged
merged 11 commits into from
Jan 21, 2024
3 changes: 3 additions & 0 deletions lua/neo-tree/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ local config = {
},
},
},
hijack_cursor = {
enabled = true,
},
document_symbols = {
follow_cursor = false,
client_filters = "first",
Expand Down
2 changes: 2 additions & 0 deletions lua/neo-tree/setup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,8 @@ M.merge_config = function(user_config, is_auto_config)

local rt = utils.get_value(M.config, "resize_timer_interval", 50, true)
require("neo-tree.ui.renderer").resize_timer_interval = rt
--TODO how to initialize properly
require("neo-tree.sources.common.hijack_cursor").setup(M.config, nil)
cseickel marked this conversation as resolved.
Show resolved Hide resolved

return M.config
end
Expand Down
38 changes: 38 additions & 0 deletions lua/neo-tree/sources/common/hijack_cursor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local events = require("neo-tree.events")
local manager = require("neo-tree.sources.manager")
local log = require("neo-tree.log")

local M = {}

local setup_for_module = function(module)
cseickel marked this conversation as resolved.
Show resolved Hide resolved
return function()
local state = manager.get_state(module)
ghostbuster91 marked this conversation as resolved.
Show resolved Hide resolved
local winid = state.winid
if vim.api.nvim_get_current_win() == winid then
local node = state.tree:get_node()
log.debug("Cursor moved in tree window, updating cursor pos")
local cursor = vim.api.nvim_win_get_cursor(0)
local row = cursor[1]
local current_line = vim.api.nvim_get_current_line()
cseickel marked this conversation as resolved.
Show resolved Hide resolved
local startIndex, _ = string.find(current_line, node.name, nil, true)
if startIndex then
vim.api.nvim_win_set_cursor(0, { row, startIndex - 1 })
end
end
end
end

---Configures the plugin, should be called before the plugin is used.
---@param config table Configuration table containing any keys that the user
---wants to change from the defaults. May be empty to accept default values.
M.setup = function(config, global_config)
local modules = { "filesystem", "git_status", "buffers" }
for _, module in ipairs(modules) do
cseickel marked this conversation as resolved.
Show resolved Hide resolved
manager.subscribe(module, {
event = events.VIM_CURSOR_MOVED,
handler = setup_for_module(module),
})
end
end

return M