Skip to content

Commit

Permalink
feat: added help
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent ace4128 commit 68ac238
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lua/trouble/config/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ local M = {
Preview.close()
end
end,
help = function(self)
self:help()
end,
jump_only = function(self, ctx)
if ctx.item then
self:jump(ctx.item)
Expand Down
39 changes: 36 additions & 3 deletions lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local Render = require("trouble.view.render")
local Sort = require("trouble.sort")
local Source = require("trouble.source")
local Spec = require("trouble.spec")
local Text = require("trouble.view.text")
local Tree = require("trouble.tree")
local Util = require("trouble.util")
local Window = require("trouble.view.window")
Expand Down Expand Up @@ -214,16 +215,18 @@ end
---@param key string
---@param action trouble.Action|string
function M:map(key, action)
local desc ---@type string?
if type(action) == "string" then
desc = action:gsub("_", " ")
action = require("trouble.config.actions")[action]
end
---@type trouble.ActionFn, string?
local fn, desc
---@type trouble.ActionFn
local fn
if type(action) == "function" then
fn = action
else
fn = action.action
desc = action.desc
desc = action.desc or desc
end
self.win:map(key, function()
fn(self, self:at())
Expand Down Expand Up @@ -252,6 +255,35 @@ function M:refresh()
end
end

function M:help()
local text = Text.new({ padding = 1 })

text:nl():append("# Help ", "Title"):nl()
text:append("Press ", "Comment"):append("<q>", "Special"):append(" to close", "Comment"):nl():nl()
text:append("# Keymaps ", "Title"):nl():nl()
local keys = vim.tbl_keys(self.win.keys)
table.sort(keys)
for _, key in ipairs(keys) do
local desc = self.win.keys[key]
text:append(" - ", "@punctuation.special.markdown")
text:append(key, "Special"):append(" "):append(desc):nl()
end
text:trim()

local win = Window.new({
type = "float",
size = { width = text:width(), height = text:height() },
border = "rounded",
wo = { cursorline = false },
})
win:open():focus()
text:render(win.buf)
vim.bo[win.buf].modifiable = false

win:map("<esc>", win.close)
win:map("q", win.close)
end

function M:open()
self.win:open()
self:refresh()
Expand Down Expand Up @@ -298,6 +330,7 @@ function M:render()
self.renderer:section(section, nodes)
end
end
self.renderer:trim()

-- calculate initial folds
if self.renderer.foldlevel == nil then
Expand Down
16 changes: 16 additions & 0 deletions lua/trouble/view/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ function M.new(opts)
return self
end

function M:height()
return #self._lines
end

function M:width()
local width = 0
for _, line in ipairs(self._lines) do
local w = 0
for _, segment in ipairs(line) do
w = w + vim.fn.strdisplaywidth(segment.str)
end
width = math.max(width, w)
end
return width + ((self.opts.padding or 0) * 2)
end

---@param text string|TextSegment[]
---@param hl? string|Extmark
---@param opts? {next_indent?: TextSegment[]}
Expand Down
11 changes: 10 additions & 1 deletion lua/trouble/view/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

---@class trouble.Window.opts: trouble.Split,trouble.Float
---@field padding? {top?:number, left?:number}
---@field enter? boolean
---@field wo? vim.wo
---@field bo? vim.bo
---@field on_mount? fun(self: trouble.Window)
Expand All @@ -23,6 +22,7 @@
---@field win? number
---@field buf number
---@field id number
---@field keys table<string, string>
local M = {}
M.__index = M

Expand Down Expand Up @@ -148,6 +148,7 @@ function M:set_options(type)
end

function M:mount()
self.keys = {}
self.buf = vim.api.nvim_create_buf(false, true)
self:set_options("buf")
if self.opts.type == "split" then
Expand Down Expand Up @@ -212,6 +213,7 @@ function M:open()
end
self:close()
self:mount()
return self
end

function M:valid()
Expand Down Expand Up @@ -265,6 +267,12 @@ function M:mount_float(opts)
self.win = vim.api.nvim_open_win(self.buf, false, config)
end

function M:focus()
if self:valid() then
vim.api.nvim_set_current_win(self.win)
end
end

---@param events string|string[]
---@param fn fun(self:trouble.Window):boolean?
---@param opts? vim.api.keyset.create_autocmd | {buffer: false}
Expand All @@ -289,6 +297,7 @@ function M:map(key, fn, desc)
if not self:valid() then
error("Cannot create a keymap for an invalid window")
end
self.keys[key] = desc or key
vim.keymap.set("n", key, function()
fn(self)
end, {
Expand Down

0 comments on commit 68ac238

Please sign in to comment.