Skip to content

Commit

Permalink
fix: dont do feedkeys when user uses WhichKey command with non existi…
Browse files Browse the repository at this point in the history
…ng prefix
  • Loading branch information
folke committed Apr 28, 2021
1 parent df615d4 commit f9537ce
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 11 deletions.
179 changes: 179 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,179 @@
# 🧐 Which Key

**WhichKey** is a lua plugin for Neovim 0.5 that displays a popup with possible keybindings of the command you started typing.

## ✨ Features

* opens a poup

## ⚡️ Requirements

* Neovim >= 0.5.0
* Properly configured Neovim LSP client
* [nvim-web-devicons](https://github.com/kyazdani42/nvim-web-devicons) is optional to enable file icons
* a theme with properly configured highlight groups for Neovim LSP Diagnostics
* or install 🌈 [lsp-colors](https://github.com/folke/lsp-colors.nvim) to automatically create the missing highlight groups
* a [patched font](https://www.nerdfonts.com/) for the default severity and fold icons

## 📦 Installation

Install the plugin with your preferred package manager:

### [vim-plug](https://github.com/junegunn/vim-plug)

```vim
" Vim Script
Plug 'kyazdani42/nvim-web-devicons'
Plug 'folke/lsp-trouble.nvim'
lua << EOF
require("trouble").setup {
-- your configuration comes here
-- or leave it empty to use the default settings
-- refer to the configuration section below
}
EOF
```

### [packer](https://github.com/wbthomason/packer.nvim)

```lua
-- Lua
use {
"folke/lsp-trouble.nvim",
requires = "kyazdani42/nvim-web-devicons",
config = function()
require("trouble").setup {
-- your configuration comes here
-- or leave it empty to use the default settings
-- refer to the configuration section below
}
end
}
```

## ⚙️ Configuration

### Setup

Trouble comes with the following defaults:

```lua
{
height = 10, -- height of the trouble list
icons = true, -- use dev-icons for filenames
mode = "workspace", -- "workspace" or "document"
fold_open = "", -- icon used for open folds
fold_closed = "", -- icon used for closed folds
action_keys = { -- key mappings for actions in the trouble list
close = "q", -- close the list
cancel = "<esc>", -- cancel the preview and get back to your last window / buffer / cursor
refresh = "r", -- manually refresh
jump = {"<cr>", "<tab>"}, -- jump to the diagnostic or open / close folds
toggle_mode = "m", -- toggle between "workspace" and "document" mode
toggle_preview = "P", -- toggle auto_preview
preview = "p", -- preview the diagnostic location
close_folds = {"zM", "zm"}, -- close all folds
open_folds = {"zR", "zr"}, -- open all folds
toggle_fold = {"zA", "za"}, -- toggle fold of current file
previous = "k", -- preview item
next = "j" -- next item
},
indent_lines = true, -- add an indent guide below the fold icons
auto_open = false, -- automatically open the list when you have diagnostics
auto_close = false, -- automatically close the list when you have no diagnostics
auto_preview = true, -- automatically preview the location of the diagnostic. <esc> to close preview and go back
auto_fold = false, -- automatically fold a file trouble list at creation
signs = {
-- icons / text used for a diagnostic
error = "",
warning = "",
hint = "",
information = ""
},
use_lsp_diagnostic_signs = false -- enabling this will use the signs defined in your lsp client
}
```

> 💡 if you don't want to use icons or a patched font, you can use the settings below
```lua
-- settings without a patched font or icons
{
fold_open = "v", -- icon used for open folds
fold_closed = ">", -- icon used for closed folds
indent_lines = false, -- add an indent guide below the fold icons
signs = {
-- icons / text used for a diagnostic
error = "error",
warning = "warn",
hint = "hint",
information = "info"
},
use_lsp_diagnostic_signs = false -- enabling this will use the signs defined in your lsp client
}
```

## 🚀 Usage

### Commands

Trouble comes with the following commands:

* **LspTroubleOpen**: open the list
* **LspTroubleWorkspaceOpen**: set mode to "workspace" and open the list
* **LspTroubleDocumentOpen**: set mode to "document" and open the list
* **LspTroubleClose**: close the list
* **LspTroubleToggle**: toggle the list
* **LspTroubleWorkspaceToggle**: set mode to "workspace" and toggle the list (remains open if mode changes)
* **LspTroubleDocumentToggle**: set mode to "document" and toggle the list (remains open if mode changes)
* **LspTroubleRefresh**: manually refresh

Example keybinding of `<leader>xx` that toggles the trouble list:

```vim
-- Vim Script
nnoremap <leader>xx <cmd>LspTroubleToggle<cr>
```

```lua
-- Lua
vim.api.nvim_set_keymap("n", "<leader>xx", "<cmd>LspTroubleToggle<cr>",
{silent = true, noremap = true}
)
```

## 🎨 Colors

The table below shows all the highlight groups defined for LSP Trouble with their default link.

| Highlight Group | Defaults to |
| --------------------------- | -------------------------------- |
| *LspTroubleCount* | TabLineSel |
| *LspTroubleError* | LspDiagnosticsDefaultError |
| *LspTroubleNormal* | Normal |
| *LspTroubleTextInformation* | LspTroubleText |
| *LspTroubleSignWarning* | LspDiagnosticsSignWarning |
| *LspTroubleLocation* | LineNr |
| *LspTroubleWarning* | LspDiagnosticsDefaultWarning |
| *LspTroublePreview* | Search |
| *LspTroubleTextError* | LspTroubleText |
| *LspTroubleSignInformation* | LspDiagnosticsSignInformation |
| *LspTroubleIndent* | LineNr |
| *LspTroubleSource* | Comment |
| *LspTroubleSignHint* | LspDiagnosticsSignHint |
| *LspTroubleFoldIcon* | CursorLineNr |
| *LspTroubleTextWarning* | LspTroubleText |
| *LspTroubleCode* | Comment |
| *LspTroubleInformation* | LspDiagnosticsDefaultInformation |
| *LspTroubleSignError* | LspDiagnosticsSignError |
| *LspTroubleFile* | Directory |
| *LspTroubleHint* | LspDiagnosticsDefaultHint |
| *LspTroubleTextHint* | LspTroubleText |
| *LspTroubleText* | Normal |



# Todo

* [x] hook into all groups
Expand All @@ -15,3 +191,6 @@
* [x] cleanup text objects text
* [x] buf local mappings seems to interfere with global mappings (push K in help)
* [x] fix help in visual mode
* [ ] Plug>whichkey nop
* [ ] preset plugin
* [ ] command should auto stuff
13 changes: 8 additions & 5 deletions lua/which-key/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ end

function M.register_plugin(plugin) Plugin.register(plugin) end

function M.show(keys, mode)
function M.show(keys, opts)
opts = opts or {}
if type(opts) == "string" then opts = { mode = opts } end

keys = keys or ""
mode = mode or vim.api.nvim_get_mode().mode
opts.mode = opts.mode or vim.api.nvim_get_mode().mode
local buf = vim.api.nvim_get_current_buf()
-- make sure the trees exist for update
Keys.get_tree(mode)
Keys.get_tree(mode, buf)
Keys.get_tree(opts.mode)
Keys.get_tree(opts.mode, buf)
-- update only trees related to buf
Keys.update(buf)
-- trigger which key
View.on_keys(keys, mode)
View.on_keys(keys, opts)
end

M.register = Keys.register
Expand Down
2 changes: 1 addition & 1 deletion lua/which-key/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function M.update(buf)
local id = tree.mode .. (tree.buf or "") .. node.prefix
-- hook up if needed
if not M.hooked[id] then
local cmd = [[<cmd>lua require("which-key").show(%q, %q)<cr>]]
local cmd = [[<cmd>lua require("which-key").show(%q, {mode = %q, auto = true})<cr>]]
cmd = string.format(cmd, node.prefix, tree.mode)
if tree.buf then
vim.api.nvim_buf_set_keymap(tree.buf, tree.mode, node.prefix, cmd, opts)
Expand Down
11 changes: 6 additions & 5 deletions lua/which-key/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local M = {}

M.keys = ""
M.mode = "n"
M.back = false
M.auto = false
M.buf = nil
M.win = nil

Expand Down Expand Up @@ -66,7 +66,7 @@ function M.get_input(wait)
end

if wait then
vim.defer_fn(function() M.on_keys(M.keys) end, 0)
vim.defer_fn(function() M.on_keys(M.keys, { auto = true }) end, 0)
return
end
end
Expand Down Expand Up @@ -117,9 +117,10 @@ function M.back()
if node then M.keys = node.prefix end
end

function M.on_keys(keys, mode)
function M.on_keys(keys, opts)
opts = opts or {}
M.keys = keys or ""
M.mode = mode or vim.api.nvim_get_mode().mode
M.mode = opts.mode or vim.api.nvim_get_mode().mode
M.show_cursor()
-- eat queued characters
M.get_input(false)
Expand All @@ -140,7 +141,7 @@ function M.on_keys(keys, mode)
-- Check for no mappings found. Feedkeys without remap
if #mappings.mappings == 0 then
M.hide()
vim.api.nvim_feedkeys(M.keys, "n", true)
if opts.auto then vim.api.nvim_feedkeys(M.keys, "n", true) end
return
end

Expand Down

0 comments on commit f9537ce

Please sign in to comment.