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
1 change: 1 addition & 0 deletions lua/neo-tree/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local config = {
enable_modified_markers = true, -- Show markers for files with unsaved changes.
enable_opened_markers = true, -- Enable tracking of opened files. Required for `components.name.highlight_opened_files`
enable_refresh_on_write = true, -- Refresh the tree when a file is written. Only used if `use_libuv_file_watcher` is false.
enable_cursor_hijack = false, -- If enabled neotree will keep the cursor on the first letter of the filename when moving in the tree.
git_status_async = true,
-- These options are for people with VERY large git repos
git_status_async_options = {
Expand Down
5 changes: 5 additions & 0 deletions lua/neo-tree/setup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local file_nesting = require("neo-tree.sources.common.file-nesting")
local highlights = require("neo-tree.ui.highlights")
local manager = require("neo-tree.sources.manager")
local netrw = require("neo-tree.setup.netrw")
local hijack_cursor = require("neo-tree.sources.common.hijack_cursor")

local M = {}

Expand Down Expand Up @@ -681,6 +682,10 @@ M.merge_config = function(user_config, is_auto_config)
manager.redraw(source_name)
end

if M.config.enable_cursor_hijack then
hijack_cursor.setup(all_sources)
end

if M.config.auto_clean_after_session_restore then
require("neo-tree.ui.renderer").clean_invalid_neotree_buffers(false)
events.subscribe({
Expand Down
36 changes: 36 additions & 0 deletions lua/neo-tree/sources/common/hijack_cursor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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, hijacking cursor position")
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

--Enables cursor hijack behavior for given sources
---@param sources table List of all sources to configure hijack for
M.setup = function(sources)
for _, source_name in ipairs(sources) do
manager.subscribe(source_name, {
event = events.VIM_CURSOR_MOVED,
handler = setup_for_module(source_name),
})
end
end

return M