Skip to content

Commit

Permalink
feat: added configurable commands
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 26, 2022
1 parent c63267d commit e9ccf78
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
33 changes: 21 additions & 12 deletions lua/noice/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ local Config = require("noice.config")
local Util = require("noice.util")
local Message = require("noice.message")

---@class NoiceCommand: NoiceRouteConfig
---@field filter_opts NoiceMessageOpts

local M = {}

---@type NoiceView?
M._history_view = nil
---@param command NoiceCommand
function M.command(command)
return function()
local view = View.get_view(command.view, command.opts)
view:display(Manager.get(
command.filter,
vim.tbl_deep_extend("force", {
history = true,
sort = true,
}, command.filter_opts or {})
))
view:show()
end
end

function M.setup()
local commands = {
Expand Down Expand Up @@ -41,18 +56,12 @@ function M.setup()
message:set(vim.inspect(Config.options))
Manager.add(message)
end,
history = function()
if not M._history_view then
M._history_view = View.get_view(Config.options.history.view, Config.options.history.opts)
end
M._history_view:display(Manager.get(Config.options.history.filter, {
history = true,
sort = true,
}))
M._history_view:show()
end,
}

for name, command in pairs(Config.options.commands) do
commands[name] = M.command(command)
end

vim.api.nvim_create_user_command("Noice", function(args)
local cmd = vim.trim(args.args or "")
if commands[cmd] then
Expand Down
35 changes: 29 additions & 6 deletions lua/noice/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,29 @@ M.defaults = {
-- Icons for completion item kinds (see defaults at noice.config.icons.kinds)
kind_icons = {}, -- set to `false` to disable icons
},
---@type NoiceRouteConfig
history = {
-- options for the message history that you get with `:Noice`
view = "split",
opts = { enter = true, format = "details" },
filter = { event = { "msg_show", "notify" }, ["not"] = { kind = { "search_count", "echo" } } },
---@type table<string, NoiceCommand>
commands = {
history = {
-- options for the message history that you get with `:Noice`
view = "split",
opts = { enter = true, format = "details" },
filter = { event = { "msg_show", "notify" }, ["not"] = { kind = { "search_count", "echo" } } },
},
-- :Noice last
last = {
view = "popup",
opts = { enter = true, format = "details" },
filter = { event = { "msg_show", "notify" }, ["not"] = { kind = { "search_count", "echo" } } },
filter_opts = { count = 1 },
},
-- :Noice errors
errors = {
-- options for the message history that you get with `:Noice`
view = "popup",
opts = { enter = true, format = "details" },
filter = { error = true },
filter_opts = { reverse = true },
},
},
notify = {
-- Noice can be used as `vim.notify` so you can route any notification like other messages
Expand Down Expand Up @@ -185,6 +202,12 @@ function M.fix_legacy(opts)
if opts.lsp_progress then
opts.lsp = opts.lsp or {}
opts.lsp.progress = opts.lsp_progress
opts.lsp_progress = nil
end
if opts.history then
opts.commands = opts.commands or {}
opts.commands.history = opts.history
opts.history = nil
end
end

Expand Down
7 changes: 4 additions & 3 deletions lua/noice/config/views.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,19 @@ M.defaults = {
},
popup = {
backend = "popup",
relative = "editor",
close = {
events = { "BufLeave" },
keys = { "q" },
},
enter = true,
border = {
style = "single",
style = "rounded",
},
position = "50%",
size = {
width = "80%",
height = "60%",
width = "120",
height = "20",
},
win_options = {
winhighlight = { Normal = "NoicePopup", FloatBorder = "NoicePopupBorder" },
Expand Down

0 comments on commit e9ccf78

Please sign in to comment.