diff --git a/README.md b/README.md index 6b911b88..229c8b2d 100644 --- a/README.md +++ b/README.md @@ -300,7 +300,7 @@ return { ["<2-LeftMouse>"] = "open", [""] = "open", [""] = "cancel", -- close preview or floating neo-tree window - ["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = true } }, + ["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = true, force = nil } }, -- Read `# Preview Mode` for more information ["l"] = "focus_preview", ["S"] = "open_split", @@ -311,7 +311,6 @@ return { -- [""] = "open_drop", -- ["t"] = "open_tab_drop", ["w"] = "open_with_window_picker", - --["P"] = "toggle_preview", -- enter preview mode, which shows the current node without focusing ["C"] = "close_node", -- ['C'] = 'close_all_subnodes', ["z"] = "close_all_nodes", @@ -792,6 +791,7 @@ require("neo-tree").setup({ config = { use_float = false, -- use_image_nvim = true, + -- force = nil, -- title = 'Neo-tree Preview', }, }, @@ -804,6 +804,24 @@ Anything that causes Neo-tree to lose focus will end preview mode. When `use_float = false`, the window that was taken over by preview mode will revert back to whatever was shown in that window before preview mode began. +The `toggle_preview` command also takes a `force` option which will force the +preview to open/close if set to true/false, respectively. This can be used for +"auto-preview"-like behavior like: + +```lua + require('neo-tree').setup({ + event_handlers = { + { + event = 'after_render', + handler = function(state) + state.config = { use_float = true, force = true } + state.commands.toggle_preview(state) + end + } + } + }) +``` + You can choose a custom title for the floating window by setting the `title` option in its config. If you want to work with the floating preview mode window in autocmds or other diff --git a/doc/neo-tree.txt b/doc/neo-tree.txt index a496522f..5d7e6e4d 100644 --- a/doc/neo-tree.txt +++ b/doc/neo-tree.txt @@ -430,7 +430,7 @@ an existing split by configuring the command like this: require("neo-tree").setup({ window = { mappings = { - ["P"] = { "toggle_preview", config = { use_float = false, use_image_nvim = true } }, + ["P"] = { "toggle_preview", config = { use_float = false, use_image_nvim = true, force = nil } }, ["l"] = "focus_preview", [""] = { "scroll_preview", config = {direction = 10} }, [""] = { "scroll_preview", config = {direction = -10} }, @@ -438,10 +438,31 @@ an existing split by configuring the command like this: } }) < + Anything that causes Neo-tree to lose focus will end preview mode. When `use_float = false`, the window that was taken over by preview mode will revert back to whatever was shown in that window before preview mode began. +The `toggle_preview` command also takes a `force` option which will force the +preview to open/close if set to true/false, respectively. This can be used for +"auto-preview"-like behavior like: + +>lua + require('neo-tree').setup({ + event_handlers = { + { + event = 'after_render', + handler = function(state) + state.config = { use_float = true, force = true } + state.commands.toggle_preview(state) + end + } + } + }) +< + +You can choose a custom title for the floating window by setting the `title` option in its config. + If you want to work with the floating preview mode window in autocmds or other custom code, the window will have the `neo-tree-preview` filetype. diff --git a/lua/neo-tree/defaults.lua b/lua/neo-tree/defaults.lua index 1f030e93..605d4981 100644 --- a/lua/neo-tree/defaults.lua +++ b/lua/neo-tree/defaults.lua @@ -397,6 +397,7 @@ local config = { ["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = false, + -- force = nil, -- title = "Neo-tree Preview", -- You can define a custom title for the preview floating window. } }, [""] = { "scroll_preview", config = {direction = -10} }, diff --git a/lua/neo-tree/sources/common/preview.lua b/lua/neo-tree/sources/common/preview.lua index 7a8b5e81..d5a8d185 100644 --- a/lua/neo-tree/sources/common/preview.lua +++ b/lua/neo-tree/sources/common/preview.lua @@ -413,10 +413,10 @@ function Preview:clearHighlight() end end -local toggle_state = false +local showing = false Preview.hide = function() - toggle_state = false + showing = false if instance then instance:revert() end @@ -448,12 +448,15 @@ Preview.show = function(state) end Preview.toggle = function(state) - if toggle_state then + state.config = state.config or {} + local should_hide = state.config.force and (state.config.force == false) or showing + local should_show = state.config.force and (state.config.force == true) or not showing + if should_hide then Preview.hide() - else + elseif should_show then Preview.show(state) if instance and instance.active then - toggle_state = true + showing = true else Preview.hide() return @@ -464,7 +467,7 @@ Preview.toggle = function(state) event = events.VIM_CURSOR_MOVED, handler = function() local did_enter_preview = vim.api.nvim_get_current_win() == instance.winid - if not toggle_state or (did_enter_preview and instance.config.use_float) then + if not showing or (did_enter_preview and instance.config.use_float) then return end if vim.api.nvim_get_current_win() == winid then