feat: Support command modifiers on open commands

This commit is contained in:
hedy
2023-11-15 22:32:33 +08:00
parent 81ebdc5714
commit 7c703361e9
2 changed files with 68 additions and 25 deletions

View File

@@ -378,39 +378,62 @@ A fallback is always used if the previous candidate returned a falsey value.
## Commands
- **:Outline[!]**
- **:Outline[!]**      ✅ bang ✅ mods
Toggle outline. With bang (`!`) the cursor focus stays in your
original window after opening the outline window. Set `focus_on_open = true` to
always use this behaviour.
- **:OutlineOpen[!]**
You can use command modifiers `topleft`/`aboveleft`/`botright`/`belowright`
on this command to control how the outline window split is created. Other
modifiers are ignored.
Example:
```vim
" in config: position='right'
:topleft Outline " opens with 'topleft vsplit'
:belowright Outline " opens with 'belowright vsplit'
:Outline " opens with 'botright vsplit'
```
- **:OutlineOpen[!]**      ✅ bang ✅ mods
Open outline. With bang (`!`) the cursor focus stays in your original
window after opening the outline window. Set `focus_on_open = true` to always
use this behaviour.
- **:OutlineClose**
You can use command modifiers `topleft`/`aboveleft`/`botright`/`belowright`
on this command to control how the outline window split is created. Other
modifiers are ignored.
```vim
" in config: position='left'
:aboveleft OutlineOpen " opens with 'aboveleft vsplit'
:belowright OutlineOpen " opens with 'belowright vsplit'
:OutlineOpen " opens with 'topleft vsplit'
```
- **:OutlineClose**      ❌ bang ❌ mods
Close outline
- **:OutlineFocus**
- **:OutlineFocus**      ❌ bang ❌ mods
Toggle focus between outline and code/source window
- **:OutlineFocusOutline**
- **:OutlineFocusOutline**      ❌ bang ❌ mods
Focus on outline
- **:OutlineFocusCode**
- **:OutlineFocusCode**      ❌ bang ❌ mods
Focus on source window
- **:OutlineStatus**
- **:OutlineStatus**      ❌ bang ❌ mods
Display current provider and outline window status in the messages area.
- **:OutlineFollow[!]**
- **:OutlineFollow[!]**      ✅ bang ❌ mods
Go to corresponding node in outline based on cursor position in code, and
focus on the outline window.

View File

@@ -425,7 +425,16 @@ local function handler(response, opts)
M.state.code_win = vim.api.nvim_get_current_win()
if opts and opts.on_symbols then
opts.on_symbols()
end
M.view:setup_view()
if opts and opts.on_outline_setup then
opts.on_outline_setup()
end
-- clear state when buffer is closed
vim.api.nvim_buf_attach(M.view.bufnr, false, {
on_detach = function(_, _)
@@ -449,7 +458,9 @@ local function handler(response, opts)
end
---@class outline.OutlineOpts
---@field focus_outline boolean
---@field focus_outline boolean?
---@field on_symbols function?
---@field on_outline_setup function?
---Set position of outline window to match cursor position in code, return
---whether the window is just newly opened (previously not open).
@@ -508,12 +519,29 @@ function M.toggle_outline(opts)
end
end
-- Used for Outline user command
local function _cmd_toggle_outline(opts)
local function _cmd_open_with_mods(fn)
return function(opts)
local old_sc, use_old_sc
local split = opts.smods.split
if split ~= "" then
old_sc = cfg.o.outline_window.split_command
use_old_sc = true
cfg.o.outline_window.split_command = split .. ' vsplit'
end
local function on_outline_setup()
if use_old_sc then
cfg.o.outline_window.split_command = old_sc
-- the old option should already have been resolved during set up
end
end
if opts.bang then
M.toggle_outline({ focus_outline = false })
fn({ focus_outline = false, on_outline_setup = on_outline_setup })
else
M.toggle_outline({ focus_outline = true })
fn({ focus_outline = true, on_outline_setup = on_outline_setup })
end
end
end
@@ -531,14 +559,6 @@ function M.open_outline(opts)
end
end
local function _cmd_open_outline(opts)
if opts.bang then
M.open_outline({ focus_outline = false })
else
M.open_outline({ focus_outline = true })
end
end
---Close the outline window.
function M.close_outline()
M.view:close()
@@ -620,13 +640,13 @@ local function setup_commands()
vim.api.nvim_create_user_command('Outline'..n, c, o)
end
cmd('', _cmd_toggle_outline, {
cmd('', _cmd_open_with_mods(M.toggle_outline), {
desc = "Toggle the outline window. \
With bang, keep focus on initial window after opening.",
nargs = 0,
bang = true,
})
cmd('Open', _cmd_open_outline, {
cmd('Open', _cmd_open_with_mods(M.open_outline), {
desc = "With bang, keep focus on initial window after opening.",
nargs = 0,
bang = true,