From e3d622445e412c195fc52e329191f789615c5f24 Mon Sep 17 00:00:00 2001 From: hedy Date: Thu, 2 Nov 2023 21:37:40 +0800 Subject: [PATCH] feat: Move and goto_location simultaneously, and highlight See readme changes --- README.md | 12 +++++++++-- lua/symbols-outline.lua | 33 ++++++++++++++++++++++-------- lua/symbols-outline/config.lua | 2 ++ lua/symbols-outline/utils/init.lua | 13 ++++++++++++ 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 177d68a..ecd6f9d 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ keep this list up to date. Features/Changes: - Toggling folds (and added default keymaps for it) -(simrat39/symbols-outline.nvim#194) +(simrat39/symbols-outline.nvim#194). - Control focus between outline and code window. - New commands: SymbolsOutline`Focus,FocusOutline,FocusCode` (see @@ -71,11 +71,15 @@ Features/Changes: - simrat39/symbols-outline.nvim#174 - simrat39/symbols-outline.nvim#207 -- Cursorline option for the outline window +- Cursorline option for the outline window. - Added function and command to show provider and outline window status, somewhat like `:LspInfo`. +- Move down/up by one line and peek_location immediately. + +- Flash highlight when using goto/peek location. + ## Fixes - Symbol preview (simrat39/symbols-outline.nvim#176) @@ -378,6 +382,10 @@ local opts = { fold_all = "W", unfold_all = "E", fold_reset = "R", + -- Only in this fork: + -- Move down/up by one line and peek_location immediately. + down_and_goto = '', + up_and_goto = '', }, -- Lsp clients to ignore diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 1adccc5..928c387 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -116,20 +116,33 @@ function M._current_node() return M.state.flattened_outline_items[current_line] end -function M._goto_location(change_focus) +function M.__goto_location(change_focus) local node = M._current_node() vim.api.nvim_win_set_cursor( M.state.code_win, { node.line + 1, node.character } ) + utils.flash_highlight(M.state.code_win, node.line + 1, true) if change_focus then vim.fn.win_gotoid(M.state.code_win) - if config.options.auto_close then - M.close_outline() - end end end +function M._goto_location(change_focus) + M.__goto_location(change_focus) + if change_focus and config.options.auto_close then + M.close_outline() + end +end + +function M._move_and_goto(direction) + local move = direction == 'down' and 1 or -1 + local cur = vim.api.nvim_win_get_cursor(0) + cur[1] = cur[1] + move + pcall(vim.api.nvim_win_set_cursor, 0, cur) + M.__goto_location(false) +end + function M._toggle_fold(move_cursor, node_index) local node = M.state.flattened_outline_items[node_index] or M._current_node() local is_folded = folding.is_folded(node) @@ -262,10 +275,14 @@ local function setup_keymaps(bufnr) map(config.options.keymaps.peek_location, function() M._goto_location(false) end) - -- -- goto_location of symbol but stay in outline - -- map(config.options.keymaps.down_and_goto, function() - -- M._move_and_goto(false) - -- end) + -- Move down/up in outline and peek that location in code + map(config.options.keymaps.down_and_goto, function() + M._move_and_goto('down') + end) + -- Move down/up in outline and peek that location in code + map(config.options.keymaps.up_and_goto, function() + M._move_and_goto('up') + end) -- hover symbol map( config.options.keymaps.hover_symbol, diff --git a/lua/symbols-outline/config.lua b/lua/symbols-outline/config.lua index 8c9ae57..9cb007d 100644 --- a/lua/symbols-outline/config.lua +++ b/lua/symbols-outline/config.lua @@ -47,6 +47,8 @@ M.defaults = { fold_all = 'W', unfold_all = 'E', fold_reset = 'R', + down_and_goto = '', + up_and_goto = '', }, lsp_blacklist = {}, symbol_blacklist = {}, diff --git a/lua/symbols-outline/utils/init.lua b/lua/symbols-outline/utils/init.lua index 1b00738..03c520c 100644 --- a/lua/symbols-outline/utils/init.lua +++ b/lua/symbols-outline/utils/init.lua @@ -110,4 +110,17 @@ M.merge_items_rec = function(new_node, old_node, index, parent) end end +M.flash_highlight = function(winnr, lnum, durationMs, hl_group) + hl_group = hl_group or "Visual" + if durationMs == true or durationMs == 1 then + durationMs = 300 + end + local bufnr = vim.api.nvim_win_get_buf(winnr) + local ns = vim.api.nvim_buf_add_highlight(bufnr, 0, hl_group, lnum - 1, 0, -1) + local remove_highlight = function() + pcall(vim.api.nvim_buf_clear_namespace, bufnr, ns, 0, -1) + end + vim.defer_fn(remove_highlight, durationMs) +end + return M