Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F132248
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lua/ivy/register_backend.lua b/lua/ivy/register_backend.lua
new file mode 100644
index 0000000..f314588
--- /dev/null
+++ b/lua/ivy/register_backend.lua
@@ -0,0 +1,54 @@
+---@class IvyBackend
+---@field command string The command this backend will have
+---@field items fun(input: string): { content: string }[] | string The callback function to get the items to select from
+---@field callback fun(result: string, action: string) The callback function to run when a item is selected
+---@field description string? The description of the backend, this will be used in the keymaps
+---@field name string? The name of the backend, this will fallback to the command if its not set
+---@field keymap string? The keymap to trigger this backend
+
+---@class IvyBackendOptions
+---@field command string The command this backend will have
+---@field keymap string? The keymap to trigger this backend
+
+---Register a new backend
+---
+---This will create all the commands and set all the keymaps for the backend
+---@param backend IvyBackend
+local register_backend_class = function(backend)
+ local user_command_options = { bang = true }
+ if backend.description ~= nil then
+ user_command_options.desc = backend.description
+ end
+
+ local name = backend.name or backend.command
+ vim.api.nvim_create_user_command(backend.command, function()
+ vim.ivy.run(name, backend.items, backend.callback)
+ end, user_command_options)
+
+ if backend.keymap ~= nil then
+ vim.api.nvim_set_keymap("n", backend.keymap, "<cmd>" .. backend.command .. "<CR>", { nowait = true, silent = true })
+ end
+end
+
+---@param backend IvyBackend | { ["1"]: string, ["2"]: IvyBackendOptions} | string The backend or backend module
+---@param options IvyBackendOptions? The options for the backend, that will be merged with the backend
+local register_backend = function(backend, options)
+ if type(backend[1]) == "string" then
+ options = backend[2]
+ backend = require(backend[1])
+ end
+
+ if type(backend) == "string" then
+ backend = require(backend)
+ end
+
+ if options then
+ for key, value in pairs(options) do
+ backend[key] = value
+ end
+ end
+
+ register_backend_class(backend)
+end
+
+return register_backend
diff --git a/lua/ivy/register_backend_spec.lua b/lua/ivy/register_backend_spec.lua
new file mode 100644
index 0000000..ab51e4e
--- /dev/null
+++ b/lua/ivy/register_backend_spec.lua
@@ -0,0 +1,71 @@
+local register_backend = require "ivy.register_backend"
+
+local function get_command(name)
+ local command_iter = vim.api.nvim_get_commands {}
+
+ for _, cmd in pairs(command_iter) do
+ if cmd.name == name then
+ return cmd
+ end
+ end
+
+ return nil
+end
+
+local function get_keymap(mode, rhs)
+ local keymap_iter = vim.api.nvim_get_keymap(mode)
+ for _, keymap in pairs(keymap_iter) do
+ if keymap.rhs == rhs then
+ return keymap
+ end
+ end
+
+ return nil
+end
+
+describe("register_backend", function()
+ after_each(function()
+ vim.api.nvim_del_user_command "IvyFd"
+
+ local keymap = get_keymap("n", "<Cmd>IvyFd<CR>")
+ if keymap then
+ vim.api.nvim_del_keymap("n", keymap.lhs)
+ end
+ end)
+
+ it("registers a backend from a string with the default options", function()
+ register_backend "ivy.backends.files"
+
+ local command = get_command "IvyFd"
+ assert.is_not_nil(command)
+
+ local keymap = get_keymap("n", "<Cmd>IvyFd<CR>")
+ assert.is_not_nil(keymap)
+ end)
+
+ it("allows you to override the keymap", function()
+ register_backend("ivy.backends.files", { keymap = "<C-p>" })
+
+ local keymap = get_keymap("n", "<Cmd>IvyFd<CR>")
+ assert(keymap ~= nil)
+ assert.are.equal("<C-P>", keymap.lhs)
+ end)
+
+ it("allows you to pass in a hole backend module", function()
+ register_backend(require "ivy.backends.files")
+
+ local command = get_command "IvyFd"
+ assert.is_not_nil(command)
+
+ local keymap = get_keymap("n", "<Cmd>IvyFd<CR>")
+ assert.is_not_nil(keymap)
+ end)
+
+ it("allows you to pass in a hole backend module", function()
+ register_backend { "ivy.backends.files", { keymap = "<C-p>" } }
+
+ local keymap = get_keymap("n", "<Cmd>IvyFd<CR>")
+ assert(keymap ~= nil)
+ assert.are.equal("<C-P>", keymap.lhs)
+ end)
+end)
diff --git a/plugin/ivy.lua b/plugin/ivy.lua
index 383e866..81169f9 100644
--- a/plugin/ivy.lua
+++ b/plugin/ivy.lua
@@ -1,50 +1,31 @@
local controller = require "ivy.controller"
+local register_backend = require "ivy.register_backend"
-- Put the controller in to the vim global so we can access it in mappings
-- better without requires. You can call controller commands like `vim.ivy.xxx`.
-- luacheck: ignore
vim.ivy = controller
-local register_backend = function(backend)
- assert(backend.command, "The backend must have a command")
- assert(backend.items, "The backend must have a items function")
- assert(backend.callback, "The backend must have a callback function")
-
- local user_command_options = { bang = true }
- if backend.description ~= nil then
- user_command_options.desc = backend.description
- end
-
- local name = backend.name or backend.command
- vim.api.nvim_create_user_command(backend.command, function()
- vim.ivy.run(name, backend.items, backend.callback)
- end, user_command_options)
-
- if backend.keymap ~= nil then
- vim.api.nvim_set_keymap("n", backend.keymap, "<cmd>" .. backend.command .. "<CR>", { nowait = true, silent = true })
- end
-end
-
vim.paste = (function(overridden)
return function(lines, phase)
local file_type = vim.api.nvim_buf_get_option(0, "filetype")
if file_type == "ivy" then
vim.ivy.paste()
else
overridden(lines, phase)
end
end
end)(vim.paste)
-register_backend(require "ivy.backends.buffers")
-register_backend(require "ivy.backends.files")
-register_backend(require "ivy.backends.lines")
-register_backend(require "ivy.backends.lsp-workspace-symbols")
+register_backend "ivy.backends.buffers"
+register_backend "ivy.backends.files"
+register_backend "ivy.backends.lines"
+register_backend "ivy.backends.lsp-workspace-symbols"
if vim.fn.executable "rg" then
- register_backend(require "ivy.backends.rg")
+ register_backend "ivy.backends.rg"
elseif vim.fn.executable "ag" then
- register_backend(require "ivy.backends.ag")
+ register_backend "ivy.backends.ag"
end
vim.cmd "highlight IvyMatch cterm=bold gui=bold"
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, May 8, 4:58 PM (1 w, 13 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
8942
Default Alt Text
(6 KB)
Attached To
Mode
R1 ivy.nvim
Attached
Detach File
Event Timeline
Log In to Comment