From f4d9b673709b7bb43a774df3fc6da2f9430e997e Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Mon, 24 Jun 2024 20:07:44 +0100 Subject: [PATCH] feat: implement a setup function Summary: Now when using ivy.nvim you will need to call the `setup` function. This will need to register any backends you want to use. This is an example config, this can be put into a plugin in `~/.config/nvim/plugin/ivy.lua` for example. ```lua require('ivy').setup { backends = { "ivy.backends.buffers", "ivy.backends.files", }, } ``` If you are using Lazy you can use the `config` directly to call the setup function. ```lua return { "AdeAttwood/ivy.nvim", build = "cargo build --release", config = { backends = { "ivy.backends.buffers", "ivy.backends.files", } } } ``` The `setup` function can only be called once, if its called a second time any backends or config will not be used. The module does expose the `register_backend` function, this can be used to load backends before or after the setup function is called. ```lua require('ivy').register_backend("ivy.backends.files") ``` As well as the `register_backend` the core `run`function is exposed. With this exposed we should be able to build anything we want. ```lua vim.ivy.run( "Title", function(input) return { { content = "One" }, { content = "Two" }, { content = "Three" }, } end, function(result) vim.cmd("edit " .. result) end ) ``` Test Plan: Not much to test in this one, it has been tested locally on my config that does not use any plugin managers, also a sandbox Lazy env using `NVIM_APPNAME` NVIM_APPNAME --- lua/ivy/init.lua | 28 ++++++++++++++++++++++++++++ plugin/ivy.lua | 12 ------------ 2 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 lua/ivy/init.lua diff --git a/lua/ivy/init.lua b/lua/ivy/init.lua new file mode 100644 index 0000000..f62f6fc --- /dev/null +++ b/lua/ivy/init.lua @@ -0,0 +1,28 @@ +local controller = require "ivy.controller" +local register_backend = require "ivy.register_backend" + +-- Local variable to check if ivy has been setup, this is to prevent multiple +-- setups of ivy +local has_setup = false + +local ivy = {} +ivy.run = controller.run +ivy.register_backend = register_backend + +---@class IvySetupOptions +---@field backends (IvyBackend | { ["1"]: string, ["2"]: IvyBackendOptions} | string)[] + +---@param config IvySetupOptions +function ivy.setup(config) + if has_setup then + return + end + + for _, backend in ipairs(config.backends) do + register_backend(backend) + end + + has_setup = true +end + +return ivy diff --git a/plugin/ivy.lua b/plugin/ivy.lua index 81169f9..cab4b9a 100644 --- a/plugin/ivy.lua +++ b/plugin/ivy.lua @@ -1,5 +1,4 @@ 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`. @@ -17,15 +16,4 @@ vim.paste = (function(overridden) end end)(vim.paste) -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 "ivy.backends.rg" -elseif vim.fn.executable "ag" then - register_backend "ivy.backends.ag" -end - vim.cmd "highlight IvyMatch cterm=bold gui=bold"