Skip to content

Commit

Permalink
fix(view): added support for multiple backends. Fixes #986
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 8, 2024
1 parent 8101388 commit eac7e84
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
15 changes: 1 addition & 14 deletions lua/noice/config/views.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,12 @@ M.defaults = {
hl_group = "NoiceVirtualText",
},
notify = {
view = "snacks",
format = "notify",
replace = false,
merge = false,
},
nvim_notify = {
backend = "notify",
backend = { "snacks", "notify" },
fallback = "mini",
format = "notify",
replace = false,
merge = false,
},
snacks = {
backend = "snacks",
fallback = "nvim_notify",
format = "notify",
replace = false,
merge = false,
},
split = {
backend = "split",
enter = false,
Expand Down
49 changes: 28 additions & 21 deletions lua/noice/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local Util = require("noice.util")

---@class NoiceViewBaseOptions
---@field buf_options? table<string,any>
---@field backend string
---@field backend string|string[]
---@field fallback string Fallback view in case the backend could not be loaded
---@field format? NoiceFormat|string
---@field align? NoiceAlign
Expand Down Expand Up @@ -43,32 +43,39 @@ function View.get_view(view, opts)
---@diagnostic disable-next-line: undefined-field
opts.backend = opts.backend or opts.render or view

-- check if we already loaded this backend
for _, v in ipairs(View._views) do
if v.opts.view == opts.view then
if v.view._instance == "opts" and vim.deep_equal(opts, v.opts) then
return v.view
---@type string[]
local backends = type(opts.backend) == "table" and opts.backend or { opts.backend }

for _, backend in ipairs(backends) do
opts.backend = backend
-- check if we already loaded this backend
for _, v in ipairs(View._views) do
if v.opts.view == opts.view then
if v.view._instance == "opts" and vim.deep_equal(opts, v.opts) then
return v.view
end
if v.view._instance == "view" then
return v.view
end
end
if v.view._instance == "view" then
return v.view
if v.opts.backend == backend then
if v.view._instance == "backend" then
return v.view
end
end
end
if v.opts.backend == opts.backend then
if v.view._instance == "backend" then
return v.view
end

---@type NoiceView
local mod = require("noice.view.backend." .. backend)
local init_opts = vim.deepcopy(opts)
local ret = mod(opts)
if ret:is_available() then
table.insert(View._views, { view = ret, opts = init_opts })
return ret
end
end

---@type NoiceView
local mod = require("noice.view.backend." .. opts.backend)
local init_opts = vim.deepcopy(opts)
local ret = mod(opts)
if not ret:is_available() and opts.fallback then
return View.get_view(opts.fallback, opts_orig)
end
table.insert(View._views, { view = ret, opts = init_opts })
return ret
return opts.fallback and View.get_view(opts.fallback, opts_orig) or nil
end

local _id = 0
Expand Down

0 comments on commit eac7e84

Please sign in to comment.