Skip to content

Commit

Permalink
feat: added integrations for fzf-lua
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 12, 2024
1 parent c53fa88 commit 1605be3
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Highly experimental plugin that completely replaces the UI for `messages`, `cmdl
- 💻 fully customizable **cmdline** with icons
- 💅 **syntax highlighting** for `vim` and `lua` on the **cmdline**
- 🚥 **statusline** components
- 🔭 open message history in [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim)
- 🔭 open message history in [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) or [fzf-lua](https://github.com/ibhagwan/fzf-lua)

## 🔥 Status

Expand Down Expand Up @@ -524,6 +524,8 @@ Formatters are used in `format` definitions. **Noice** includes the following bu
},
telescope = ..., -- formatter used to display telescope results
telescope_preview = ..., -- formatter used to preview telescope results
fzf = ..., -- formatter used to display fzf results
fzf_preview = ..., -- formatter used to preview fzf results
lsp_progress = ..., -- formatter used by lsp progress
lsp_progress_done = ..., -- formatter used by lsp progress
}
Expand Down Expand Up @@ -634,6 +636,11 @@ require("lualine").setup({

</details>

## 🔭 Pickers

For convenience, you can do `:Noice pick`, which will open a picker with all the messages in the history,
either with `telescope` or `fzf-lua`.

## 🔭 Telescope

In order to use **Noice** in **Telescope**, you can either do `:Noice telescope`,
Expand All @@ -644,6 +651,11 @@ The results panel is formatted using `config.format.formatters.telescope`. The p
require("telescope").load_extension("noice")
```

## 🔭 Fzf Lua

In order to use **Noice** in **FzfLua**, you can do `:Noice fzf`.
The results panel is formatted using `config.format.formatters.fzf`. The preview is formatted with `config.format.formatters.telescope_fzf`

## 🚀 Usage

- `:Noice` or `:Noice history` shows the message history
Expand Down
12 changes: 12 additions & 0 deletions lua/noice/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ function M.setup()
telescope = function()
require("telescope").extensions.noice.noice({})
end,
fzf = function()
require("noice.integrations.fzf").open({})
end,
pick = function()
if pcall(_G.require, "telescope.config") then
require("telescope").extensions.noice.noice({})
elseif pcall(_G.require, "fzf-lua") then
require("noice.integrations.fzf").open({})
else
Util.error("No picker available")
end
end,
stats = function()
Manager.add(Util.stats.message())
end,
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 @@ -30,6 +30,22 @@ M.builtin = {
"\n",
"{message}",
},
fzf = {
"{level} ",
"{date} ",
"{title} ",
"{message}",
},
fzf_preview = {
"{level} ",
"{date} ",
"{event}",
{ "{kind}", before = { ".", hl_group = "NoiceFormatKind" } },
"\n",
"{title}\n",
"\n",
"{message}",
},
lsp_progress = {
{
"{progress} ",
Expand Down
110 changes: 110 additions & 0 deletions lua/noice/integrations/fzf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
local require = require("noice.util.lazy")

local fzf = require("fzf-lua")
local builtin = require("fzf-lua.previewer.builtin")
local Config = require("noice.config")
local Manager = require("noice.message.manager")
local Format = require("noice.text.format")

local M = {}

---@alias NoiceEntry {message: NoiceMessage, ordinal: string, display: string}

---@param message NoiceMessage
---@return NoiceEntry
function M.entry(message)
message = Format.format(message, "fzf")
local line = message._lines[1]
local hl = {} ---@type string[]
for _, text in ipairs(line._texts) do
---@type string?
local hl_group = text.extmark and text.extmark.hl_group
hl[#hl + 1] = hl_group and fzf.utils.ansi_from_hl(hl_group, text:content()) or text:content()
end
hl[#hl + 1] = fzf.utils.ansi_from_hl("Conceal", " [" .. message.id .. "]")
return {
message = message,
ordinal = message:content(),
display = table.concat(hl, ""),
}
end

function M.find()
local messages = Manager.get(Config.options.commands.history.filter, {
history = true,
sort = true,
reverse = true,
})
---@type table<number, NoiceEntry>
local ret = {}

for _, message in ipairs(messages) do
ret[message.id] = M.entry(message)
end

return ret
end

---@param messages table<number, NoiceEntry>
function M.previewer(messages)
local previewer = builtin.buffer_or_file:extend()

function previewer:new(o, opts, fzf_win)
previewer.super.new(self, o, opts, fzf_win)
self.title = "Noice"
setmetatable(self, previewer)
return self
end

function previewer:parse_entry(entry_str)
local id = tonumber(entry_str:match("%[(%d+)%]$"))
local entry = messages[id]
assert(entry, "No message found for entry: " .. entry_str)
return entry
end

function previewer:populate_preview_buf(entry_str)
local buf = self:get_tmp_buffer()
local entry = self:parse_entry(entry_str)
assert(entry, "No message found for entry: " .. entry_str)

---@type NoiceMessage
local m = Format.format(entry.message, "fzf_preview")
m:render(buf, Config.ns)

self:set_preview_buf(buf)
self.win:update_title(" Noice ")
self.win:update_scrollbar()
end

return previewer
end

---@param opts? table<string, any>
function M.open(opts)
local messages = M.find()
opts = vim.tbl_deep_extend("force", opts or {}, {
prompt = false,
winopts = {
title = " Noice ",
title_pos = "center",
preview = {
title = " Noice ",
title_pos = "center",
},
},
previewer = M.previewer(messages),
fzf_opts = {
["--no-multi"] = "",
},
actions = {
default = function() end,
},
})
local lines = vim.tbl_map(function(entry)
return entry.display
end, vim.tbl_values(messages))
return fzf.fzf_exec(lines, opts)
end

return M

0 comments on commit 1605be3

Please sign in to comment.