Skip to content

Commit

Permalink
feat: Add ability to hijack-cursor (#955)
Browse files Browse the repository at this point in the history
Co-authored-by: ghostbuster91 <ghostbuster91@users.noreply.github.com>
  • Loading branch information
ghostbuster91 and ghostbuster91 committed Jan 21, 2024
1 parent 2f2d088 commit e578fe7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions lua/neo-tree/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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.
enable_normal_mode_for_inputs = false, -- Enable normal mode for input dialogs.
git_status_async = true,
-- These options are for people with VERY large git repos
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 @@ -734,6 +735,10 @@ 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

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

return M.config
end

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

local M = {}

local hijack_cursor_handler = function()
if vim.o.filetype ~= "neo-tree" then
return
end
local success, source = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_source")
if not success then
log.debug("Cursor hijack failure: " .. vim.inspect(source))
return
end
local winid = nil
local _, position = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_position")
if position == "current" then
winid = vim.api.nvim_get_current_win()
end

local state = manager.get_state(source, nil, winid)
if state == nil then
return
end
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()
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

--Enables cursor hijack behavior for all sources
M.setup = function()
events.subscribe({
event = events.VIM_CURSOR_MOVED,
handler = hijack_cursor_handler,
id = "neo-tree-hijack-cursor",
})
end

return M

0 comments on commit e578fe7

Please sign in to comment.