From b1688b325a738271d62628af162fd906557e4d0e Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sat, 27 May 2023 23:50:32 +0200 Subject: [PATCH 1/9] feat: Add ability to hijack-cursor --- lua/neo-tree/defaults.lua | 3 ++ lua/neo-tree/setup/init.lua | 2 + lua/neo-tree/sources/common/hijack_cursor.lua | 38 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 lua/neo-tree/sources/common/hijack_cursor.lua diff --git a/lua/neo-tree/defaults.lua b/lua/neo-tree/defaults.lua index e42d6382..d601ad71 100644 --- a/lua/neo-tree/defaults.lua +++ b/lua/neo-tree/defaults.lua @@ -513,6 +513,9 @@ local config = { }, }, }, + hijack_cursor = { + enabled = true, + }, document_symbols = { follow_cursor = false, client_filters = "first", diff --git a/lua/neo-tree/setup/init.lua b/lua/neo-tree/setup/init.lua index 0f53fdf9..affb99c1 100644 --- a/lua/neo-tree/setup/init.lua +++ b/lua/neo-tree/setup/init.lua @@ -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) return M.config end diff --git a/lua/neo-tree/sources/common/hijack_cursor.lua b/lua/neo-tree/sources/common/hijack_cursor.lua new file mode 100644 index 00000000..0c92d7d9 --- /dev/null +++ b/lua/neo-tree/sources/common/hijack_cursor.lua @@ -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) + return function() + local state = manager.get_state(module) + 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() + 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 + manager.subscribe(module, { + event = events.VIM_CURSOR_MOVED, + handler = setup_for_module(module), + }) + end +end + +return M From 7ba0edf92779263b40c25d80dc30f486e5079295 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 28 May 2023 10:13:59 +0200 Subject: [PATCH 2/9] review: Fix incorrect initialization --- lua/neo-tree/defaults.lua | 4 +--- lua/neo-tree/setup/init.lua | 7 +++++-- lua/neo-tree/sources/common/hijack_cursor.lua | 21 ++++++++----------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/lua/neo-tree/defaults.lua b/lua/neo-tree/defaults.lua index d601ad71..4e9be17e 100644 --- a/lua/neo-tree/defaults.lua +++ b/lua/neo-tree/defaults.lua @@ -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 = { @@ -513,9 +514,6 @@ local config = { }, }, }, - hijack_cursor = { - enabled = true, - }, document_symbols = { follow_cursor = false, client_filters = "first", diff --git a/lua/neo-tree/setup/init.lua b/lua/neo-tree/setup/init.lua index affb99c1..a6859311 100644 --- a/lua/neo-tree/setup/init.lua +++ b/lua/neo-tree/setup/init.lua @@ -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 = {} @@ -679,6 +680,10 @@ M.merge_config = function(user_config, is_auto_config) end manager.setup(source_name, M.config[source_name], M.config, module) manager.redraw(source_name) + + if M.config.enable_cursor_hijack then + hijack_cursor.setup(source_name) + end end if M.config.auto_clean_after_session_restore then @@ -728,8 +733,6 @@ 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) return M.config end diff --git a/lua/neo-tree/sources/common/hijack_cursor.lua b/lua/neo-tree/sources/common/hijack_cursor.lua index 0c92d7d9..c30d82f4 100644 --- a/lua/neo-tree/sources/common/hijack_cursor.lua +++ b/lua/neo-tree/sources/common/hijack_cursor.lua @@ -10,7 +10,7 @@ local setup_for_module = function(module) 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") + 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() @@ -22,17 +22,14 @@ local setup_for_module = function(module) 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 - manager.subscribe(module, { - event = events.VIM_CURSOR_MOVED, - handler = setup_for_module(module), - }) - end +--Enables cursor hijack behavior for given source +---@param source_name string Name of the source to configure for +M.setup = function(source_name) + log.debug("Initing for " .. vim.inspect(source_name)) + manager.subscribe(source_name, { + event = events.VIM_CURSOR_MOVED, + handler = setup_for_module(source_name), + }) end return M From 5909848c744e3d220d407855413b3178eb1d901d Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 28 May 2023 10:29:54 +0200 Subject: [PATCH 3/9] Move loop into hijack module --- lua/neo-tree/setup/init.lua | 6 +++--- lua/neo-tree/sources/common/hijack_cursor.lua | 17 +++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lua/neo-tree/setup/init.lua b/lua/neo-tree/setup/init.lua index a6859311..1f16785a 100644 --- a/lua/neo-tree/setup/init.lua +++ b/lua/neo-tree/setup/init.lua @@ -680,10 +680,10 @@ M.merge_config = function(user_config, is_auto_config) end manager.setup(source_name, M.config[source_name], M.config, module) manager.redraw(source_name) + end - if M.config.enable_cursor_hijack then - hijack_cursor.setup(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 diff --git a/lua/neo-tree/sources/common/hijack_cursor.lua b/lua/neo-tree/sources/common/hijack_cursor.lua index c30d82f4..ad5c9295 100644 --- a/lua/neo-tree/sources/common/hijack_cursor.lua +++ b/lua/neo-tree/sources/common/hijack_cursor.lua @@ -22,14 +22,15 @@ local setup_for_module = function(module) end end ---Enables cursor hijack behavior for given source ----@param source_name string Name of the source to configure for -M.setup = function(source_name) - log.debug("Initing for " .. vim.inspect(source_name)) - manager.subscribe(source_name, { - event = events.VIM_CURSOR_MOVED, - handler = setup_for_module(source_name), - }) +--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 From 636b957a2b11fe551786c48edb14520bed6dafc6 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 28 May 2023 21:44:00 +0200 Subject: [PATCH 4/9] refactor: Use manager::_for_each_source --- lua/neo-tree/setup/init.lua | 8 ++++---- lua/neo-tree/sources/common/hijack_cursor.lua | 15 +++++++-------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lua/neo-tree/setup/init.lua b/lua/neo-tree/setup/init.lua index 1f16785a..d10b9a80 100644 --- a/lua/neo-tree/setup/init.lua +++ b/lua/neo-tree/setup/init.lua @@ -682,10 +682,6 @@ 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({ @@ -734,6 +730,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 diff --git a/lua/neo-tree/sources/common/hijack_cursor.lua b/lua/neo-tree/sources/common/hijack_cursor.lua index ad5c9295..160a02b7 100644 --- a/lua/neo-tree/sources/common/hijack_cursor.lua +++ b/lua/neo-tree/sources/common/hijack_cursor.lua @@ -4,7 +4,7 @@ local log = require("neo-tree.log") local M = {} -local setup_for_module = function(module) +local create_handler_for_module = function(module) return function() local state = manager.get_state(module) local winid = state.winid @@ -22,15 +22,14 @@ local setup_for_module = function(module) 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, { +--Enables cursor hijack behavior for all sources +M.setup = function() + manager._for_each_state(nil, function (state) + manager.subscribe(state.name, { event = events.VIM_CURSOR_MOVED, - handler = setup_for_module(source_name), + handler = create_handler_for_module(state.name), }) - end + end) end return M From 8b175e852c5e791b70467103a10ce3fa0f522a96 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 28 May 2023 21:50:02 +0200 Subject: [PATCH 5/9] Simplify --- lua/neo-tree/sources/common/hijack_cursor.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lua/neo-tree/sources/common/hijack_cursor.lua b/lua/neo-tree/sources/common/hijack_cursor.lua index 160a02b7..43a44bf9 100644 --- a/lua/neo-tree/sources/common/hijack_cursor.lua +++ b/lua/neo-tree/sources/common/hijack_cursor.lua @@ -4,9 +4,8 @@ local log = require("neo-tree.log") local M = {} -local create_handler_for_module = function(module) +local create_handler_for_module = function(state) return function() - local state = manager.get_state(module) local winid = state.winid if vim.api.nvim_get_current_win() == winid then local node = state.tree:get_node() @@ -27,7 +26,7 @@ M.setup = function() manager._for_each_state(nil, function (state) manager.subscribe(state.name, { event = events.VIM_CURSOR_MOVED, - handler = create_handler_for_module(state.name), + handler = create_handler_for_module(state), }) end) end From f4a29c963b21c7d241e290c0b40799061669a49e Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Thu, 13 Jul 2023 22:50:02 +0200 Subject: [PATCH 6/9] Fix hijack_cursor initialization --- lua/neo-tree/sources/common/hijack_cursor.lua | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lua/neo-tree/sources/common/hijack_cursor.lua b/lua/neo-tree/sources/common/hijack_cursor.lua index 43a44bf9..47ae1b6a 100644 --- a/lua/neo-tree/sources/common/hijack_cursor.lua +++ b/lua/neo-tree/sources/common/hijack_cursor.lua @@ -1,11 +1,17 @@ local events = require("neo-tree.events") -local manager = require("neo-tree.sources.manager") local log = require("neo-tree.log") local M = {} -local create_handler_for_module = function(state) - return function() +local hijack_cursor_handler = function() + if vim.o.filetype ~= "neo-tree" then + return + end + local source = vim.api.nvim_buf_get_var(0, "neo_tree_source") + local state = require("neo-tree.sources.manager").get_state(source) + if state == nil then + return + end local winid = state.winid if vim.api.nvim_get_current_win() == winid then local node = state.tree:get_node() @@ -18,17 +24,15 @@ local create_handler_for_module = function(state) vim.api.nvim_win_set_cursor(0, { row, startIndex - 1 }) end end - end end --Enables cursor hijack behavior for all sources M.setup = function() - manager._for_each_state(nil, function (state) - manager.subscribe(state.name, { - event = events.VIM_CURSOR_MOVED, - handler = create_handler_for_module(state), - }) - end) + events.subscribe({ + event = events.VIM_CURSOR_MOVED, + handler = hijack_cursor_handler, + id = "neo-tree-hijack-cursor", + }) end return M From 11f6042509d8e4dc3ffbddf0bf1d8b7d01ac3d94 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Thu, 13 Jul 2023 22:53:31 +0200 Subject: [PATCH 7/9] Fix formatting --- lua/neo-tree/sources/common/hijack_cursor.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/neo-tree/sources/common/hijack_cursor.lua b/lua/neo-tree/sources/common/hijack_cursor.lua index 47ae1b6a..8e03a960 100644 --- a/lua/neo-tree/sources/common/hijack_cursor.lua +++ b/lua/neo-tree/sources/common/hijack_cursor.lua @@ -4,7 +4,7 @@ local log = require("neo-tree.log") local M = {} local hijack_cursor_handler = function() - if vim.o.filetype ~= "neo-tree" then + if vim.o.filetype ~= "neo-tree" then return end local source = vim.api.nvim_buf_get_var(0, "neo_tree_source") From 1c421bce20559f4a98e204b882584394d1715d51 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Fri, 14 Jul 2023 08:50:06 +0200 Subject: [PATCH 8/9] Wrap buf_get_var with pcall --- lua/neo-tree/sources/common/hijack_cursor.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/neo-tree/sources/common/hijack_cursor.lua b/lua/neo-tree/sources/common/hijack_cursor.lua index 8e03a960..aefa1004 100644 --- a/lua/neo-tree/sources/common/hijack_cursor.lua +++ b/lua/neo-tree/sources/common/hijack_cursor.lua @@ -7,7 +7,11 @@ local hijack_cursor_handler = function() if vim.o.filetype ~= "neo-tree" then return end - local source = vim.api.nvim_buf_get_var(0, "neo_tree_source") + 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 state = require("neo-tree.sources.manager").get_state(source) if state == nil then return From a4d146de782e3b8112129d9613bec6f927e5c417 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Sun, 21 Jan 2024 16:55:26 +0100 Subject: [PATCH 9/9] Fix hijack for position=current --- lua/neo-tree/sources/common/hijack_cursor.lua | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lua/neo-tree/sources/common/hijack_cursor.lua b/lua/neo-tree/sources/common/hijack_cursor.lua index aefa1004..55d82fcd 100644 --- a/lua/neo-tree/sources/common/hijack_cursor.lua +++ b/lua/neo-tree/sources/common/hijack_cursor.lua @@ -1,5 +1,6 @@ local events = require("neo-tree.events") local log = require("neo-tree.log") +local manager = require("neo-tree.sources.manager") local M = {} @@ -12,21 +13,24 @@ local hijack_cursor_handler = function() log.debug("Cursor hijack failure: " .. vim.inspect(source)) return end - local state = require("neo-tree.sources.manager").get_state(source) + 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 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() - local startIndex, _ = string.find(current_line, node.name, nil, true) - if startIndex then - vim.api.nvim_win_set_cursor(0, { row, startIndex - 1 }) - 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