Skip to content

Commit

Permalink
feat: added snacks integration
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Feb 8, 2025
1 parent cf758e9 commit df72b6d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lua/noice/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ function M.setup()
fzf = function()
require("noice.integrations.fzf").open({})
end,
snacks = function()
require("noice.integrations.snacks").open({})
end,
pick = function()
if pcall(_G.require, "telescope.config") then
if Snacks and Snacks.config.picker and Snacks.config.picker.enabled then
Snacks.picker("noice")
elseif pcall(_G.require, "telescope.config") then
require("telescope").extensions.noice.noice({})
elseif pcall(_G.require, "fzf-lua") then
require("noice.integrations.fzf").open({})
Expand Down
16 changes: 16 additions & 0 deletions lua/noice/config/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ M.builtin = {
"\n",
"{message}",
},
snacks = {
"{level} ",
"{date} ",
"{title} ",
"{message}",
},
snacks_preview = {
"{level} ",
"{date} ",
"{event}",
{ "{kind}", before = { ".", hl_group = "NoiceFormatKind" } },
"\n",
"{title}\n",
"\n",
"{message}",
},
lsp_progress = {
{
"{progress} ",
Expand Down
5 changes: 5 additions & 0 deletions lua/noice/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ function M.setup(options)
})

require("noice.lsp").setup()

if Snacks and pcall(require, "snacks.picker") then
Snacks.picker.sources.noice = require("noice.integrations.snacks").source
end

M._running = true
end

Expand Down
59 changes: 59 additions & 0 deletions lua/noice/integrations/snacks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local require = require("noice.util.lazy")

---@module 'snacks'

local Config = require("noice.config")
local Format = require("noice.text.format")
local Manager = require("noice.message.manager")

local M = {}

function M.find()
local messages = Manager.get(Config.options.commands.history.filter, {
history = true,
sort = true,
reverse = true,
})

---@param message NoiceMessage
return vim.tbl_map(function(message)
return {
message = message,
text = message._lines[1]:content(),
}
end, messages)
end

---@param item snacks.picker.Item
function M.format(item)
local message = item.message --[[@as NoiceMessage]]
message = Format.format(message, "snacks")
return vim.tbl_map(function(text)
return { text:content(), text.extmark and text.extmark.hl_group }
end, message._lines[1]._texts)
end

---@type snacks.picker.preview
function M.preview(ctx)
local message = ctx.item.message --[[@as NoiceMessage]]
message = Format.format(message, "snacks_preview")
ctx.preview:reset()
vim.bo[ctx.buf].modifiable = true
message:render(ctx.buf, Config.ns)
vim.bo[ctx.buf].modifiable = false
end

---@type snacks.picker.Config
M.source = {
source = "noice",
finder = M.find,
format = M.format,
preview = M.preview,
}

---@param opts? snacks.picker.Config|{}
function M.open(opts)
return Snacks.picker("noice", opts)
end

return M

0 comments on commit df72b6d

Please sign in to comment.