feat(config): Allow configuring keymaps

Closes #1
This commit is contained in:
simrat39
2021-04-28 15:03:40 -07:00
parent abe479fe69
commit 067fd91f16
3 changed files with 58 additions and 28 deletions

View File

@@ -27,22 +27,40 @@ vim.g.symbols_outline = {
highlight_hovered_item = true, highlight_hovered_item = true,
show_guides = true, show_guides = true,
position = 'right', position = 'right',
keymaps = {
close = "<Esc>",
goto_location = "<Cr>",
focus_location = "o",
hover_symbol = "<C-space>",
rename_symbol = "r",
code_actions = "a",
},
} }
``` ```
```vim ```vim
" init.vim " init.vim
let g:symbols_outline = {} let g:symbols_outline = {
let g:symbols_outline.highlight_hovered_item = v:true \ "highlight_hovered_item": v:true,
let g:symbols_outline.show_guides = v:true \ "show_guides": v:true,
let g:symbols_outline.position = 'right' \ "position": 'right',
\ "keymaps": {
\ "close": "<Esc>",
\ "goto_location": "<Cr>",
\ "focus_location": "o",
\ "hover_symbol": "<C-space>",
\ "rename_symbol": "r",
\ "code_actions": "a",
\ },
\ }
``` ```
| Property | Description | Type | Default | | Property | Description | Type | Default |
| --- | -- | -- | -- | | ---------------------- | ------------------------------------------------------------------ | ----------------- | ------------------------ |
| highlight_hovered_item | Whether to highlight the currently hovered symbol (high cpu usage) | boolean | true | | highlight_hovered_item | Whether to highlight the currently hovered symbol (high cpu usage) | boolean | true |
| show_guides | Wether to show outline guides | boolean | true | | show_guides | Wether to show outline guides | boolean | true |
| position | Where to open the split window | 'right' or 'left' | 'right' | | position | Where to open the split window | 'right' or 'left' | 'right' |
| keymaps | Which keys do what | table | [here](#default-keymaps) |
### Commands ### Commands
@@ -52,14 +70,14 @@ let g:symbols_outline.position = 'right'
| `:SymbolsOutlineOpen` | Open symbols outline | | `:SymbolsOutlineOpen` | Open symbols outline |
| `:SymbolsOutlineClose` | Close symbols outline | | `:SymbolsOutlineClose` | Close symbols outline |
### Keymaps ### Default keymaps
| Key | Action | | Key | Action |
| -- | -- | | ---------- | -------------------------------------------------- |
| Escape | Close outline | | Escape | Close outline |
| Enter | Go to symbol location in code | | Enter | Go to symbol location in code |
| o | Go to symbol location in code without losing focus | | o | Go to symbol location in code without losing focus |
| Ctrl+Space | Hover current symbol | | Ctrl+Space | Hover current symbol |
| r | Rename symbol | | r | Rename symbol |
| a | Code actions | | a | Code actions |

View File

@@ -67,8 +67,8 @@ end
function M._goto_location(change_focus) function M._goto_location(change_focus)
local current_line = vim.api.nvim_win_get_cursor(M.state.outline_win)[1] local current_line = vim.api.nvim_win_get_cursor(M.state.outline_win)[1]
local node = M.state.flattened_outline_items[current_line] local node = M.state.flattened_outline_items[current_line]
vim.api.nvim_win_set_cursor(M.state.code_win, {node.line + 1, vim.api.nvim_win_set_cursor(M.state.code_win,
node.character}) {node.line + 1, node.character})
if change_focus then vim.fn.win_gotoid(M.state.code_win) end if change_focus then vim.fn.win_gotoid(M.state.code_win) end
end end
@@ -129,27 +129,31 @@ end
local function setup_keymaps(bufnr) local function setup_keymaps(bufnr)
-- goto_location of symbol and focus that window -- goto_location of symbol and focus that window
vim.api.nvim_buf_set_keymap(bufnr, "n", "<Cr>", vim.api.nvim_buf_set_keymap(bufnr, "n",
config.options.keymaps.goto_location,
":lua require('symbols-outline')._goto_location(true)<Cr>", ":lua require('symbols-outline')._goto_location(true)<Cr>",
{}) {})
-- goto_location of symbol but stay in outline -- goto_location of symbol but stay in outline
vim.api.nvim_buf_set_keymap(bufnr, "n", "o", vim.api.nvim_buf_set_keymap(bufnr, "n",
config.options.keymaps.focus_location,
":lua require('symbols-outline')._goto_location(false)<Cr>", ":lua require('symbols-outline')._goto_location(false)<Cr>",
{}) {})
-- hover symbol -- hover symbol
vim.api.nvim_buf_set_keymap(bufnr, "n", "<C-space>", vim.api.nvim_buf_set_keymap(bufnr, "n", config.options.keymaps.hover_symbol,
":lua require('symbols-outline.hover').show_hover()<Cr>", ":lua require('symbols-outline.hover').show_hover()<Cr>",
{}) {})
-- rename symbol -- rename symbol
vim.api.nvim_buf_set_keymap(bufnr, "n", "r", vim.api.nvim_buf_set_keymap(bufnr, "n",
config.options.keymaps.rename_symbol,
":lua require('symbols-outline.rename').rename()<Cr>", ":lua require('symbols-outline.rename').rename()<Cr>",
{}) {})
-- code actions -- code actions
vim.api.nvim_buf_set_keymap(bufnr, "n", "a", vim.api.nvim_buf_set_keymap(bufnr, "n", config.options.keymaps.code_actions,
":lua require('symbols-outline.code_action').show_code_actions()<Cr>", ":lua require('symbols-outline.code_action').show_code_actions()<Cr>",
{}) {})
-- close outline when escape is pressed -- close outline
vim.api.nvim_buf_set_keymap(bufnr, "n", "<Esc>", ":bw!<Cr>",{}) vim.api.nvim_buf_set_keymap(bufnr, "n", config.options.keymaps.close,
":bw!<Cr>", {})
end end
---------------------------- ----------------------------

View File

@@ -5,7 +5,15 @@ local M = {}
local defaults = { local defaults = {
highlight_hovered_item = true, highlight_hovered_item = true,
show_guides = true, show_guides = true,
position = 'right' position = 'right',
keymaps = {
close = "<Esc>",
goto_location = "<Cr>",
focus_location = "o",
hover_symbol = "<C-space>",
rename_symbol = "r",
code_actions = "a",
}
} }
M.options = {} M.options = {}