Skip to content

Commit

Permalink
feat: show breadcrumb and help on command line
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Apr 28, 2021
1 parent d255b71 commit c27535c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lua/which-key/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ local M = {}

local links = {
[""] = "Function",
Seperator = "DiffAdded",
Separator = "DiffAdded",
Group = "Keyword",
Desc = "Identifier",
WhichKeyFloating = "NormalFloat",
Value = "Comment",
}

if vim.fn.hlexists("WhichKeySeperator") then links["Separator"] = "WhichKeySeperator" end

function M.setup()
for k, v in pairs(links) do vim.api.nvim_command("hi def link WhichKey" .. k .. " " .. v) end
end
Expand Down
7 changes: 3 additions & 4 deletions lua/which-key/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ M.namespace = vim.api.nvim_create_namespace("WhichKey")
---@class Options
local defaults = {
builtin = true, -- register a list of builtin key mappings
seperator = "->",
group = "+",
plugins = { marks = true, registers = true, text_objects = true },
icons = { breadcrumb = "»", separator = "", group = "+" },
plugins = { marks = true, registers = true, ["text-objects"] = true },
window = {
border = "none", -- none, single, double, shadow
position = "bottom", -- bottom, top
margin = { 1, 0, 1, 0 }, -- extra window margin [top, right, bottom, left]
padding = { 2, 0, 2, 0 }, -- extra window padding [top, right, bottom, left]
padding = { 2, 2, 2, 2 }, -- extra window padding [top, right, bottom, left]
},
layout = { height = { min = 4, max = 25 }, width = { min = 20, max = 50 }, spacing = 3 },
}
Expand Down
3 changes: 2 additions & 1 deletion lua/which-key/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function M.get_mappings(mode, prefix, buf)
if value.group then
value.label = value.label or "+prefix"
value.label = value.label:gsub("^%+", "")
value.label = Config.options.group .. value.label
value.label = Config.options.icons.group .. value.label
else
value.label = value.label or value.cmd
end
Expand Down Expand Up @@ -75,6 +75,7 @@ function M.parse_mappings(mappings, value, prefix)
if k ~= "name" then M.parse_mappings(mappings, v, prefix .. k) end
end
if prefix ~= "" then
if value.name then value.name = value.name:gsub("^%+", "") end
table.insert(mappings, { prefix = prefix, label = value.name, group = true })
end
else
Expand Down
50 changes: 47 additions & 3 deletions lua/which-key/layout.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
local Config = require("which-key.config")
local Text = require("which-key.text")
local Keys = require("which-key.keys")

---@class Layout
---@field mapping Mapping
---@field items VisualMapping[]
---@field options Options
---@field text Text
---@field results MappingGroup
local Layout = {}
Layout.__index = Layout

Expand All @@ -14,6 +16,7 @@ Layout.__index = Layout
function Layout:new(mappings, options)
options = options or Config.options
local this = {
results = mappings,
mapping = mappings.mapping,
items = mappings.mappings,
options = options,
Expand All @@ -29,6 +32,44 @@ function Layout:max_width(key)
return max
end

function Layout:trail()
local prefix = self.results.prefix
local cmd_line = { { " " } }
for i = 1, #self.mapping.keys.nvim, 1 do
local offset = #self.mapping.keys.nvim - i
local node = Keys.get_tree(self.results.mode, self.results.buf).tree:get(prefix, offset)
if not (node and node.mapping and node.mapping.label) then
node = Keys.get_tree(self.results.mode).tree:get(prefix, offset)
end
local step = self.mapping.keys.nvim[i]
if node and node.mapping and node.mapping.label then
step = self.options.icons.group .. node.mapping.label
end
table.insert(cmd_line, { step, "WhichKeyGroup" })
if i ~= #self.mapping.keys.nvim then
table.insert(cmd_line, { " " .. self.options.icons.breadcrumb .. " ", "WhichKeySeparator" })
end
end
local width = 0
for _, line in pairs(cmd_line) do width = width + Text.len(line[1]) end
local help = { --
["<bs>"] = "go up one level",
["<c-d>"] = "scroll down",
["<c-u>"] = "scroll up",
["<esc>"] = "close",
}
local help_line = {}
local help_width = 0
for key, label in pairs(help) do
help_width = help_width + Text.len(key) + Text.len(label) + 2
table.insert(help_line, { key .. " ", "WhichKey" })
table.insert(help_line, { label .. " ", "WhichKeySeperator" })
end
table.insert(cmd_line, { string.rep(" ", math.floor(vim.o.columns / 2 - help_width / 2) - width) })
for _, l in pairs(help_line) do table.insert(cmd_line, l) end
vim.api.nvim_echo(cmd_line, false, {})
end

function Layout:layout(win)
local window_width = vim.api.nvim_win_get_width(win)
local width = window_width
Expand All @@ -38,7 +79,8 @@ function Layout:layout(win)
local max_label_width = self:max_width("label")
local max_value_width = self:max_width("value")

local intro_width = max_key_width + 2 + #self.options.seperator + self.options.layout.spacing
local intro_width = max_key_width + 2 + #self.options.icons.separator +
self.options.layout.spacing
local max_width = max_label_width + intro_width + max_value_width
if max_width > width then max_width = width end

Expand Down Expand Up @@ -68,6 +110,8 @@ function Layout:layout(win)
local pad_top = self.options.window.padding[3]
local pad_left = self.options.window.padding[4]

self:trail()

for _, item in pairs(self.items) do
local start = (col - 1) * column_width + self.options.layout.spacing
if col == 1 then start = start + pad_left end
Expand All @@ -77,8 +121,8 @@ function Layout:layout(win)
self.text:set(row + pad_top, start, key, "")
start = start + #key + 1

self.text:set(row + pad_top, start, self.options.seperator, "Seperator")
start = start + #self.options.seperator + 1
self.text:set(row + pad_top, start, self.options.icons.separator, "Separator")
start = start + #self.options.icons.separator + 1

if item.value then
local value = item.value
Expand Down
4 changes: 2 additions & 2 deletions lua/which-key/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ local PluginItem

---@class Plugin
---@field name string
---@field triggers string[] | string[][]
---@field handler fun(trigger:string, mode:string, buf:number):PluginItem[]
---@field actions string[] | string[][]
---@field run fun(trigger:string, mode:string, buf:number):PluginItem[]
---@field setup fun()
local Plugin
7 changes: 3 additions & 4 deletions lua/which-key/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function M.show()
end
M.buf = vim.api.nvim_create_buf(false, true)
M.win = vim.api.nvim_open_win(M.buf, false, opts)
-- vim.api.nvim_win_hide(M.win)
vim.api.nvim_win_set_option(M.win, "winhighlight", "NormalFloat:WhichKeyFloating")
vim.cmd [[autocmd! WinClosed <buffer> lua require("which-key.view").on_close()]]
end
Expand Down Expand Up @@ -82,12 +83,10 @@ function M.scroll(up)
vim.api.nvim_win_set_cursor(M.win, cursor)
end

function M.on_close()
print(M.keys)
M.hide()
end
function M.on_close() M.hide() end

function M.hide()
vim.api.nvim_echo({ { "" } }, false, {})
M.hide_cursor()
if M.buf and vim.api.nvim_buf_is_valid(M.buf) then
vim.api.nvim_buf_delete(M.buf, { force = true })
Expand Down

0 comments on commit c27535c

Please sign in to comment.