feat: Focus and unfocus outline window

This commit is contained in:
hedy
2023-11-01 16:50:44 +08:00
parent 0769cfe5c3
commit 2df9662290
3 changed files with 59 additions and 5 deletions

View File

@@ -34,12 +34,23 @@ Below is a list of features I've included in this fork which, at the time of
writing, has not been included upstream (in the original repo). I try my best to writing, has not been included upstream (in the original repo). I try my best to
keep this list up to date. keep this list up to date.
Features:
- Feat: Toggling folds (and added default keymaps for it) - Feat: Toggling folds (and added default keymaps for it)
(simrat39/symbols-outline.nvim#194) (simrat39/symbols-outline.nvim#194)
- Feat: Control focus between outline and code window.
- New commands: SymbolsOutline`Focus,FocusOutline,FocusCode` (see
[commands](#commands))
- Fixed issues:
- simrat39/symbols-outline.nvim#143
- simrat39/symbols-outline.nvim#174
- simrat39/symbols-outline.nvim#207
- Feat: when `auto_close=true` only auto close if `goto_location` is used (where - Feat: when `auto_close=true` only auto close if `goto_location` is used (where
focus changed), and not for `focus_location` (simrat39/symbols-outline.nvim#119) focus changed), and not for `focus_location` (simrat39/symbols-outline.nvim#119)
- Feat: Cursorline option for the outline window - Feat: Cursorline option for the outline window
Fixes:
- Fix symbol preview (simrat39/symbols-outline.nvim#176) - Fix symbol preview (simrat39/symbols-outline.nvim#176)
- Fix `SymbolsOutlineClose` crashing when already closed: simrat39/symbols-outline.nvim#163 - Fix `SymbolsOutlineClose` crashing when already closed: simrat39/symbols-outline.nvim#163
- Support Nerd fonts v3.0: simrat39/symbols-outline.nvim#225 - Support Nerd fonts v3.0: simrat39/symbols-outline.nvim#225
@@ -239,6 +250,11 @@ local opts = {
auto_unfold_hover = true, auto_unfold_hover = true,
fold_markers = { '', '' }, fold_markers = { '', '' },
wrap = false, wrap = false,
-- Only in this fork:
-- Whether to focus on the outline window when it is opened.
-- Set to false to remain focus on your previous buffer when opening
-- symbols-outline.
focus_on_open = true,
keymaps = { -- These keymaps can be a string or a table for multiple keys keymaps = { -- These keymaps can be a string or a table for multiple keys
close = {"<Esc>", "q"}, close = {"<Esc>", "q"},
goto_location = "<Cr>", goto_location = "<Cr>",
@@ -318,14 +334,18 @@ local opts = {
| auto_unfold_hover | Automatically unfold hovered symbol | boolean | true | | auto_unfold_hover | Automatically unfold hovered symbol | boolean | true |
| fold_markers | Markers to denote foldable symbol's status | table (array) | { '', '' } | | fold_markers | Markers to denote foldable symbol's status | table (array) | { '', '' } |
| wrap | Whether to wrap long lines, or let them flow off the window | boolean | false | | wrap | Whether to wrap long lines, or let them flow off the window | boolean | false |
| focus_on_open | Whether to focus cursor on the outline window when opening | boolean | true |
## Commands ## Commands
| Command | Description | | Command | Description |
| ---------------------- | ---------------------- | | ----------------------------- | -------------------------------- |
| `:SymbolsOutline` | Toggle symbols outline | | `:SymbolsOutline` | Toggle symbols outline |
| `:SymbolsOutlineOpen` | Open symbols outline | | `:SymbolsOutlineOpen` | Open symbols outline |
| `:SymbolsOutlineClose` | Close symbols outline | | `:SymbolsOutlineClose` | Close symbols outline |
| `:SymbolsOutlineFocus` | Toggle focus on symbols outline |
| `:SymbolsOutlineFocusOutline` | Focus on symbols outline |
| `:SymbolsOutlineFocusCode` | Focus on source window |
### Lua ### Lua
@@ -333,6 +353,9 @@ local opts = {
require'symbols-outline'.toggle_outline() require'symbols-outline'.toggle_outline()
require'symbols-outline'.open_outline() require'symbols-outline'.open_outline()
require'symbols-outline'.close_outline() require'symbols-outline'.close_outline()
require'symbols-outline'.focus_toggle()
require'symbols-outline'.focus_outline()
require'symbols-outline'.focus_code()
``` ```
## Default keymaps ## Default keymaps

View File

@@ -57,6 +57,9 @@ local function setup_commands()
vim.api.nvim_create_user_command('SymbolsOutline', M.toggle_outline, { nargs = 0 }) vim.api.nvim_create_user_command('SymbolsOutline', M.toggle_outline, { nargs = 0 })
vim.api.nvim_create_user_command('SymbolsOutlineOpen', M.open_outline, { nargs = 0 }) vim.api.nvim_create_user_command('SymbolsOutlineOpen', M.open_outline, { nargs = 0 })
vim.api.nvim_create_user_command('SymbolsOutlineClose', M.close_outline, { nargs = 0 }) vim.api.nvim_create_user_command('SymbolsOutlineClose', M.close_outline, { nargs = 0 })
vim.api.nvim_create_user_command('SymbolsOutlineFocusOutline', M.focus_outline, { nargs = 0 })
vim.api.nvim_create_user_command('SymbolsOutlineFocusCode', M.focus_code, { nargs = 0 })
vim.api.nvim_create_user_command('SymbolsOutlineFocus', M.focus_toggle, { nargs = 0 })
end end
------------------------- -------------------------
@@ -337,6 +340,10 @@ local function handler(response)
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items) writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
M._highlight_current_item(M.state.code_win) M._highlight_current_item(M.state.code_win)
if not config.options.focus_on_open then
vim.fn.win_gotoid(M.state.code_win)
end
end end
function M.toggle_outline() function M.toggle_outline()
@@ -357,6 +364,29 @@ function M.close_outline()
M.view:close() M.view:close()
end end
function M.focus_outline()
if M.view:is_open() then
vim.fn.win_gotoid(M.view.winnr)
end
end
function M.focus_code()
if M.state.code_win then
vim.fn.win_gotoid(M.state.code_win)
end
end
function M.focus_toggle()
if M.view:is_open() and M.state.code_win then
local winid = vim.fn.win_getid()
if winid == M.state.code_win then
vim.fn.win_gotoid(M.view.winnr)
else
vim.fn.win_gotoid(M.state.code_win)
end
end
end
function M.setup(opts) function M.setup(opts)
config.setup(opts) config.setup(opts)
ui.setup_highlights() ui.setup_highlights()

View File

@@ -21,6 +21,7 @@ M.defaults = {
auto_unfold_hover = true, auto_unfold_hover = true,
fold_markers = { '', '' }, fold_markers = { '', '' },
wrap = false, wrap = false,
focus_on_open = true,
keymaps = { -- These keymaps can be a string or a table for multiple keys keymaps = { -- These keymaps can be a string or a table for multiple keys
close = { '<Esc>', 'q' }, close = { '<Esc>', 'q' },
goto_location = '<Cr>', goto_location = '<Cr>',