From 23f1299869c1629a2b2f6c7a31611c7de3ab6cfd Mon Sep 17 00:00:00 2001 From: Omar Zeghouani Date: Thu, 19 Aug 2021 18:55:11 +0100 Subject: [PATCH 1/2] Add toggle functionality - Ensure that preview window always closes on symbol window exit --- lua/symbols-outline.lua | 12 ++++++---- lua/symbols-outline/preview.lua | 42 ++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 74d54f7..cce3269 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -22,9 +22,7 @@ local function setup_global_autocmd() vim.cmd( "au InsertLeave,WinEnter,BufEnter,BufWinEnter,TabEnter,BufWritePost * :lua require('symbols-outline')._refresh()") vim.cmd "au BufLeave * lua require'symbols-outline'._prevent_buffer_override()" - if config.options.auto_preview then - vim.cmd "au WinEnter * lua require'symbols-outline.preview'.close_if_not_in_outline()" - end + vim.cmd("au WinEnter * lua require'symbols-outline.preview'.close()") if config.options.highlight_hovered_item then vim.cmd( "autocmd CursorHold * :lua require('symbols-outline')._highlight_current_item()") @@ -34,8 +32,12 @@ end local function setup_buffer_autocmd() if config.options.auto_preview then vim.cmd( - "au CursorHold lua require'symbols-outline.preview'.show()") + "au CursorHold lua require'symbols-outline.preview'.show(true)") + else + vim.cmd( + "au CursorMoved lua require'symbols-outline.preview'.close()") end + end local function getParams() @@ -216,7 +218,7 @@ local function setup_keymaps(bufnr) ":lua require('symbols-outline.hover').show_hover()") -- preview symbol nmap(config.options.keymaps.preview_symbol, - ":lua require('symbols-outline.preview').show()") + ":lua require('symbols-outline.preview').toggle()") -- rename symbol nmap(config.options.keymaps.rename_symbol, ":lua require('symbols-outline.rename').rename()") diff --git a/lua/symbols-outline/preview.lua b/lua/symbols-outline/preview.lua index f210050..3771edf 100644 --- a/lua/symbols-outline/preview.lua +++ b/lua/symbols-outline/preview.lua @@ -135,18 +135,6 @@ local function setup_hover_buf() update_hover() end -function M.close_if_not_in_outline() - if not is_current_win_outline() and has_code_win() then - if state.preview_win ~= nil and - vim.api.nvim_win_is_valid(state.preview_win) then - vim.api.nvim_win_close(state.preview_win, true) - end - if state.hover_win ~= nil and vim.api.nvim_win_is_valid(state.hover_win) then - vim.api.nvim_win_close(state.hover_win, true) - end - end -end - local function show_preview() if state.preview_win == nil and state.preview_buf == nil then state.preview_buf = vim.api.nvim_create_buf(false, true) @@ -198,12 +186,38 @@ local function show_hover() end end -function M.show() - if not is_current_win_outline() or #vim.api.nvim_list_wins() < 2 then +function M.show(force) + if not is_current_win_outline() or + #vim.api.nvim_list_wins() < 2 then return end + + if force ~= true and state.preview_win ~= nil then + return 1 + end + show_preview() show_hover() end +function M.close() + if has_code_win() then + if state.preview_win ~= nil and + vim.api.nvim_win_is_valid(state.preview_win) then + vim.api.nvim_win_close(state.preview_win, true) + end + if state.hover_win ~= nil and + vim.api.nvim_win_is_valid(state.hover_win) then + vim.api.nvim_win_close(state.hover_win, true) + end + end +end + +function M.toggle() + code = M.show() + if code == 1 then + M.close() + end +end + return M From 5da59b68df67bf2c5b7bc1d0eaaad646728a43f7 Mon Sep 17 00:00:00 2001 From: Omar Zeghouani Date: Fri, 20 Aug 2021 20:29:18 +0100 Subject: [PATCH 2/2] Document keybind functionality --- README.md | 2 +- doc/symbols-outline.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f143339..2424a87 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ vim.g.symbols_outline = { | Enter | Go to symbol location in code | | o | Go to symbol location in code without losing focus | | Ctrl+Space | Hover current symbol | -| K | Show current symbol preview | +| K | Toggles the current symbol preview | | r | Rename symbol | | a | Code actions | diff --git a/doc/symbols-outline.txt b/doc/symbols-outline.txt index b08ba1f..7fdc7f1 100644 --- a/doc/symbols-outline.txt +++ b/doc/symbols-outline.txt @@ -233,7 +233,7 @@ symbol_blacklist | Enter | Go to symbol location in code | | o | Go to symbol location in code without losing focus | | Ctrl+Space | Hover current symbol | -| K | Show current symbol preview | +| K | Toggles the current symbol preview | | r | Rename symbol | | a | Code actions |