|
| 1 | +local events = require("neo-tree.events") |
| 2 | +local log = require("neo-tree.log") |
| 3 | +local manager = require("neo-tree.sources.manager") |
| 4 | + |
| 5 | +local M = {} |
| 6 | + |
| 7 | +local hijack_cursor_handler = function() |
| 8 | + if vim.o.filetype ~= "neo-tree" then |
| 9 | + return |
| 10 | + end |
| 11 | + local success, source = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_source") |
| 12 | + if not success then |
| 13 | + log.debug("Cursor hijack failure: " .. vim.inspect(source)) |
| 14 | + return |
| 15 | + end |
| 16 | + local winid = nil |
| 17 | + local _, position = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_position") |
| 18 | + if position == "current" then |
| 19 | + winid = vim.api.nvim_get_current_win() |
| 20 | + end |
| 21 | + |
| 22 | + local state = manager.get_state(source, nil, winid) |
| 23 | + if state == nil then |
| 24 | + return |
| 25 | + end |
| 26 | + local node = state.tree:get_node() |
| 27 | + log.debug("Cursor moved in tree window, hijacking cursor position") |
| 28 | + local cursor = vim.api.nvim_win_get_cursor(0) |
| 29 | + local row = cursor[1] |
| 30 | + local current_line = vim.api.nvim_get_current_line() |
| 31 | + local startIndex, _ = string.find(current_line, node.name, nil, true) |
| 32 | + if startIndex then |
| 33 | + vim.api.nvim_win_set_cursor(0, { row, startIndex - 1 }) |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +--Enables cursor hijack behavior for all sources |
| 38 | +M.setup = function() |
| 39 | + events.subscribe({ |
| 40 | + event = events.VIM_CURSOR_MOVED, |
| 41 | + handler = hijack_cursor_handler, |
| 42 | + id = "neo-tree-hijack-cursor", |
| 43 | + }) |
| 44 | +end |
| 45 | + |
| 46 | +return M |
0 commit comments