├── .envrc ├── .gitignore ├── .github ├── dependabot.yml └── workflows │ ├── nix-build.yml │ ├── release-please.yml │ ├── update.yml │ ├── luarocks.yml │ └── review-checklist.yml ├── .stylua.toml ├── .luacheckrc ├── .busted ├── lua └── lze │ ├── h │ ├── init.lua │ ├── dep_of.lua │ ├── on_plugin.lua │ ├── ft.lua │ ├── colorscheme.lua │ ├── on_require.lua │ ├── cmd.lua │ ├── keys.lua │ └── event.lua │ ├── init.lua │ ├── c │ ├── parse.lua │ ├── handler.lua │ └── loader.lua │ └── meta.lua ├── shell.nix ├── default.nix ├── lze-scm-1.rockspec ├── spec ├── colorscheme_spec.lua ├── nested_event_spec.lua ├── ft_spec.lua ├── on_require_spec.lua ├── cmd_spec.lua ├── register_handler_spec.lua ├── keys_spec.lua ├── dep_of_spec.lua ├── event_spec.lua ├── on_plugin_spec.lua ├── import_spec.lua └── lze_spec.lua ├── nix ├── pkg-overlay.nix └── ci-overlay.nix ├── .editorconfig ├── CONTRIBUTING.md ├── flake.nix ├── doc └── lze.txt ├── CHANGELOG.md ├── flake.lock ├── LICENSE └── README.md /.envrc: -------------------------------------------------------------------------------- 1 | use flake . -Lv 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | result 2 | .pre-commit-config.yaml 3 | .direnv 4 | .luarc.json 5 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | updates: 4 | - package-ecosystem: "github-actions" 5 | directory: "/" 6 | schedule: 7 | interval: "weekly" 8 | -------------------------------------------------------------------------------- /.stylua.toml: -------------------------------------------------------------------------------- 1 | column_width = 120 2 | line_endings = "Unix" 3 | indent_type = "Spaces" 4 | indent_width = 4 5 | quote_style = "AutoPreferDouble" 6 | no_call_parentheses = false 7 | -------------------------------------------------------------------------------- /.luacheckrc: -------------------------------------------------------------------------------- 1 | ignore = { 2 | "631", -- max_line_length 3 | "122", -- read-only field of global variable 4 | } 5 | read_globals = { 6 | "vim", 7 | "describe", 8 | "it", 9 | "assert" 10 | } 11 | -------------------------------------------------------------------------------- /.busted: -------------------------------------------------------------------------------- 1 | return { 2 | _all = { 3 | coverage = false, 4 | lpath = "lua/?.lua;lua/?/init.lua", 5 | }, 6 | default = { 7 | verbose = true 8 | }, 9 | tests = { 10 | verbose = true 11 | }, 12 | } 13 | -------------------------------------------------------------------------------- /lua/lze/h/init.lua: -------------------------------------------------------------------------------- 1 | return { 2 | require("lze.h.colorscheme"), 3 | require("lze.h.cmd"), 4 | require("lze.h.event"), 5 | require("lze.h.ft"), 6 | require("lze.h.keys"), 7 | require("lze.h.dep_of"), 8 | require("lze.h.on_plugin"), 9 | require("lze.h.on_require"), 10 | } 11 | -------------------------------------------------------------------------------- /.github/workflows/nix-build.yml: -------------------------------------------------------------------------------- 1 | name: "Nix build" 2 | on: 3 | pull_request: 4 | push: 5 | jobs: 6 | checks: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v6 10 | - uses: DeterminateSystems/nix-installer-action@v21 11 | - run: nix flake check -L --accept-flake-config 12 | -------------------------------------------------------------------------------- /shell.nix: -------------------------------------------------------------------------------- 1 | ( 2 | import 3 | ( 4 | let 5 | lock = builtins.fromJSON (builtins.readFile ./flake.lock); 6 | in 7 | fetchTarball { 8 | url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; 9 | sha256 = lock.nodes.flake-compat.locked.narHash; 10 | } 11 | ) 12 | {src = ./.;} 13 | ) 14 | .shellNix 15 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | ( 2 | import 3 | ( 4 | let 5 | lock = builtins.fromJSON (builtins.readFile ./flake.lock); 6 | in 7 | fetchTarball { 8 | url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; 9 | sha256 = lock.nodes.flake-compat.locked.narHash; 10 | } 11 | ) 12 | {src = ./.;} 13 | ) 14 | .defaultNix 15 | -------------------------------------------------------------------------------- /.github/workflows/release-please.yml: -------------------------------------------------------------------------------- 1 | --- 2 | permissions: 3 | contents: write 4 | pull-requests: write 5 | 6 | name: Release Please 7 | 8 | on: 9 | workflow_dispatch: 10 | push: 11 | branches: 12 | - master 13 | 14 | jobs: 15 | release: 16 | name: release 17 | runs-on: ubuntu-latest 18 | steps: 19 | - uses: googleapis/release-please-action@v4 20 | with: 21 | release-type: simple 22 | token: ${{ secrets.PAT }} 23 | -------------------------------------------------------------------------------- /lze-scm-1.rockspec: -------------------------------------------------------------------------------- 1 | -- NOTE: This rockspec is used for running busted tests only, 2 | -- not for publishing to LuaRocks.org 3 | 4 | local _MODREV, _SPECREV = 'scm', '-1' 5 | rockspec_format = '3.0' 6 | package = 'lze' 7 | version = _MODREV .. _SPECREV 8 | 9 | dependencies = { 10 | 'lua >= 5.1', 11 | } 12 | 13 | test_dependencies = { 14 | 'lua >= 5.1', 15 | } 16 | 17 | source = { 18 | url = 'git://github.com/BirdeeHub/' .. package, 19 | } 20 | 21 | build = { 22 | type = 'builtin', 23 | } 24 | -------------------------------------------------------------------------------- /spec/colorscheme_spec.lua: -------------------------------------------------------------------------------- 1 | vim.g.lze = { 2 | load = function() end, 3 | } 4 | local lze = require("lze") 5 | local loader = require("lze.c.loader") 6 | local spy = require("luassert.spy") 7 | 8 | describe("handlers.colorscheme", function() 9 | it("Colorscheme only loads plugin once", function() 10 | ---@type lze.Plugin 11 | local plugin = { 12 | name = "sweetie.nvim", 13 | colorscheme = { "sweetie" }, 14 | } 15 | local spy_load = spy.on(loader, "load") 16 | lze.load(plugin) 17 | pcall(vim.cmd.colorscheme, "sweetie") 18 | pcall(vim.cmd.colorscheme, "sweetie") 19 | assert.spy(spy_load).called(1) 20 | end) 21 | end) 22 | -------------------------------------------------------------------------------- /nix/pkg-overlay.nix: -------------------------------------------------------------------------------- 1 | { 2 | name, 3 | self, 4 | }: final: prev: let 5 | packageOverrides = luaself: luaprev: { 6 | ${name} = luaself.callPackage ({buildLuarocksPackage}: 7 | buildLuarocksPackage { 8 | pname = name; 9 | version = "scm-1"; 10 | knownRockspec = "${self}/${name}-scm-1.rockspec"; 11 | src = self; 12 | }) {}; 13 | }; 14 | 15 | lua5_1 = prev.lua5_1.override { 16 | inherit packageOverrides; 17 | }; 18 | lua51Packages = final.lua5_1.pkgs; 19 | 20 | vimPlugins = 21 | prev.vimPlugins 22 | // { 23 | ${name} = final.neovimUtils.buildNeovimPlugin { 24 | pname = name; 25 | version = "dev"; 26 | src = self; 27 | }; 28 | }; 29 | in { 30 | inherit 31 | lua5_1 32 | lua51Packages 33 | vimPlugins 34 | ; 35 | } 36 | -------------------------------------------------------------------------------- /spec/nested_event_spec.lua: -------------------------------------------------------------------------------- 1 | describe("nested events", function() 2 | it("lazy-loaded colorscheme triggered by UIEnter event", function() 3 | require("lze").load({ 4 | { 5 | "sick_colorscheme_bruh.nvim", 6 | colorscheme = "sick_colorscheme_bruh", 7 | load = function() 8 | vim.g.sweetie_nvim_loaded = true 9 | end, 10 | }, 11 | { 12 | "xyzhghjggjh", 13 | event = "UIEnter", 14 | after = function() 15 | pcall(vim.cmd.colorscheme, "sick_colorscheme_bruh") 16 | end, 17 | load = function() 18 | vim.g.event_nested_works = true 19 | end, 20 | }, 21 | }) 22 | vim.api.nvim_exec_autocmds("UIEnter", {}) 23 | assert.True(vim.g.event_nested_works) 24 | assert.True(vim.g.sweetie_nvim_loaded) 25 | end) 26 | end) 27 | -------------------------------------------------------------------------------- /.github/workflows/update.yml: -------------------------------------------------------------------------------- 1 | name: update-flake-lock 2 | on: 3 | workflow_dispatch: # allows manual triggering 4 | schedule: 5 | - cron: '0 0 * * 0' # runs weekly on Sunday at 00:00 6 | 7 | jobs: 8 | lockfile: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout repository 12 | uses: actions/checkout@v6 13 | - name: Install Nix 14 | uses: DeterminateSystems/nix-installer-action@main 15 | - name: Update flake.lock 16 | uses: DeterminateSystems/update-flake-lock@main 17 | with: 18 | token: ${{ secrets.PAT }} 19 | pr-title: "chore(deps): update flake.lock" # Title of PR to be created 20 | pr-labels: | # Labels to be set on the PR 21 | dependencies 22 | automated 23 | commit-msg: "chore(deps): update flake.lock" 24 | - uses: reitermarkus/automerge@v2 25 | with: 26 | token: ${{ secrets.PAT }} 27 | merge-method: squash 28 | pull-request: ${{ github.event.inputs.pull-request }} 29 | -------------------------------------------------------------------------------- /.github/workflows/luarocks.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Push to LuaRocks 3 | 4 | on: 5 | push: 6 | tags: 7 | - '*' 8 | release: 9 | types: 10 | - created 11 | pull_request: 12 | workflow_dispatch: 13 | 14 | jobs: 15 | luarocks-release: 16 | runs-on: ubuntu-latest 17 | name: Release 18 | steps: 19 | - name: Checkout 20 | uses: actions/checkout@v6 21 | with: 22 | fetch-depth: 0 # Required to count the commits 23 | - name: Get Version 24 | run: echo "LUAROCKS_VERSION=$(git describe --abbrev=0 --tags)" >> $GITHUB_ENV 25 | - name: Luarocks Upload 26 | uses: nvim-neorocks/luarocks-tag-release@v7 27 | env: 28 | LUAROCKS_API_KEY: ${{ secrets.LUAROCKS_API_KEY }} 29 | with: 30 | version: ${{ env.LUAROCKS_VERSION }} 31 | license: GPL-2+ 32 | detailed_description: | 33 | It is intended to be used 34 | 35 | - by users of plugin managers that don't provide a convenient API for lazy-loading. 36 | - by plugin managers, to provide a convenient API for lazy-loading. 37 | -------------------------------------------------------------------------------- /nix/ci-overlay.nix: -------------------------------------------------------------------------------- 1 | # Add flake.nix test inputs as arguments here 2 | { 3 | self, 4 | plugin-name, 5 | }: final: prev: let 6 | nvim-nightly = final.neovim-nightly; 7 | 8 | mkNeorocksTest = { 9 | name, 10 | nvim ? final.neovim-unwrapped, 11 | extraPkgs ? [], 12 | }: let 13 | nvim-wrapped = final.pkgs.wrapNeovim nvim { 14 | configure = { 15 | packages.myVimPackage = { 16 | start = [ 17 | # Add plugin dependencies that aren't on LuaRocks here 18 | ]; 19 | }; 20 | }; 21 | }; 22 | in 23 | final.pkgs.neorocksTest { 24 | inherit name; 25 | pname = plugin-name; 26 | src = self; 27 | neovim = nvim-wrapped; 28 | 29 | # luaPackages = ps: with ps; []; 30 | # extraPackages = []; 31 | 32 | preCheck = '' 33 | export HOME=$(realpath .) 34 | ''; 35 | 36 | buildPhase = '' 37 | mkdir -p $out 38 | cp -r tests $out 39 | ''; 40 | }; 41 | in { 42 | nvim-stable-tests = mkNeorocksTest {name = "neovim-stable-tests";}; 43 | nvim-nightly-tests = mkNeorocksTest { 44 | name = "neovim-nightly-tests"; 45 | nvim = nvim-nightly; 46 | }; 47 | } 48 | -------------------------------------------------------------------------------- /spec/ft_spec.lua: -------------------------------------------------------------------------------- 1 | ---@diagnostic disable: invisible 2 | vim.g.lze = { 3 | injects = { 4 | load = function() end, 5 | }, 6 | } 7 | local lze = require("lze") 8 | local loader = require("lze.c.loader") 9 | local spy = require("luassert.spy") 10 | 11 | describe("handlers.ft", function() 12 | it("can parse from string", function() 13 | local f = function(inspec, ft) 14 | local res = lze.h.ft.parse(ft) 15 | assert.Same(inspec.event, res.event) 16 | assert.Same(inspec.pattern, res.pattern) 17 | assert.Same(inspec.id, res.id) 18 | end 19 | f({ 20 | event = "FileType", 21 | id = "rust", 22 | pattern = "rust", 23 | }, "rust") 24 | end) 25 | it("filetype event loads plugins", function() 26 | ---@type lze.Plugin 27 | local plugin = { 28 | name = "Foo", 29 | ft = { "rust" }, 30 | } 31 | local spy_load = spy.on(loader, "load") 32 | lze.load(plugin) 33 | vim.api.nvim_exec_autocmds("FileType", { pattern = "rust" }) 34 | vim.api.nvim_exec_autocmds("FileType", { pattern = "rust" }) 35 | assert.spy(spy_load).called(1) 36 | end) 37 | end) 38 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 4 8 | trim_trailing_whitespace = true 9 | 10 | [*.{nix,md,json}] 11 | indent_size = 2 12 | 13 | [Makefile] 14 | indent_style = tab 15 | 16 | [.github/**] 17 | charset = unset 18 | end_of_line = unset 19 | insert_final_newline = unset 20 | trim_trailing_whitespace = unset 21 | indent_style = unset 22 | indent_size = unset 23 | 24 | [*.lock] 25 | charset = unset 26 | end_of_line = unset 27 | insert_final_newline = unset 28 | trim_trailing_whitespace = unset 29 | indent_style = unset 30 | indent_size = unset 31 | 32 | [LICENSE] 33 | charset = unset 34 | end_of_line = unset 35 | insert_final_newline = unset 36 | trim_trailing_whitespace = unset 37 | indent_style = unset 38 | indent_size = unset 39 | 40 | [*.norg] 41 | charset = unset 42 | end_of_line = unset 43 | insert_final_newline = unset 44 | trim_trailing_whitespace = unset 45 | indent_style = unset 46 | indent_size = unset 47 | 48 | [doc/*] 49 | charset = unset 50 | end_of_line = unset 51 | insert_final_newline = unset 52 | trim_trailing_whitespace = unset 53 | indent_style = unset 54 | indent_size = unset 55 | 56 | [installer.lua] 57 | charset = unset 58 | end_of_line = unset 59 | insert_final_newline = unset 60 | trim_trailing_whitespace = unset 61 | indent_style = unset 62 | indent_size = unset 63 | 64 | [*/**/config/internal.lua] 65 | indent_size = unset 66 | indent_style = unset 67 | 68 | [spec/**/*.lua] 69 | indent_size = unset 70 | indent_style = unset 71 | -------------------------------------------------------------------------------- /lua/lze/h/dep_of.lua: -------------------------------------------------------------------------------- 1 | ---@type table 2 | local states = {} 3 | 4 | -- NOTE: internal handlers must use internal trigger_load 5 | -- because require('lze') requires this module. 6 | local trigger_load = require("lze.c.loader").load 7 | 8 | ---@type lze.Handler 9 | local M = { 10 | spec_field = "dep_of", 11 | } 12 | 13 | ---@param plugin lze.Plugin 14 | function M.add(plugin) 15 | local dep_of = plugin.dep_of 16 | 17 | -- I dont know why I need to tell luacheck 18 | -- that I actually loop over it in like 10 lines from now 19 | ---@type string[] 20 | --luacheck: no unused 21 | local needed_by = {} 22 | 23 | if type(dep_of) == "table" then 24 | ---@cast dep_of string[] 25 | needed_by = dep_of 26 | elseif type(dep_of) == "string" then 27 | needed_by = { dep_of } 28 | else 29 | return 30 | end 31 | for _, name in ipairs(needed_by) do 32 | if require("lze").state(name) == false then 33 | return function() 34 | trigger_load(plugin.name) 35 | end 36 | end 37 | end 38 | for _, name in ipairs(needed_by) do 39 | if states[name] == nil then 40 | states[name] = {} 41 | end 42 | vim.list_extend(states[name], { plugin.name }) 43 | end 44 | end 45 | 46 | ---@param name string 47 | function M.before(name) 48 | if states[name] then 49 | trigger_load(states[name]) 50 | states[name] = nil 51 | end 52 | end 53 | 54 | function M.cleanup() 55 | states = {} 56 | end 57 | 58 | return M 59 | -------------------------------------------------------------------------------- /lua/lze/h/on_plugin.lua: -------------------------------------------------------------------------------- 1 | -- NOTE: internal handlers must use internal trigger_load 2 | -- because require('lze') requires this module. 3 | local trigger_load = require("lze.c.loader").load 4 | 5 | ---@type table 6 | local states = {} 7 | 8 | ---@type lze.Handler 9 | local M = { 10 | spec_field = "on_plugin", 11 | } 12 | 13 | ---@param plugin lze.Plugin 14 | function M.add(plugin) 15 | local on_plugin = plugin.on_plugin 16 | 17 | -- I dont know why I need to tell luacheck 18 | -- that I actually loop over it in like 10 lines from now 19 | ---@type string[] 20 | --luacheck: no unused 21 | local loaded_on = {} 22 | 23 | if type(on_plugin) == "table" then 24 | ---@cast on_plugin string[] 25 | loaded_on = on_plugin 26 | elseif type(on_plugin) == "string" then 27 | loaded_on = { on_plugin } 28 | else 29 | return 30 | end 31 | for _, name in ipairs(loaded_on) do 32 | if require("lze").state(name) == false then 33 | return function() 34 | trigger_load(plugin.name) 35 | end 36 | end 37 | end 38 | for _, name in ipairs(loaded_on) do 39 | if states[name] == nil then 40 | states[name] = {} 41 | end 42 | vim.list_extend(states[name], { plugin.name }) 43 | end 44 | end 45 | 46 | ---@param name string 47 | function M.after(name) 48 | if states[name] then 49 | trigger_load(states[name]) 50 | states[name] = nil 51 | end 52 | end 53 | 54 | function M.cleanup() 55 | states = {} 56 | end 57 | 58 | return M 59 | -------------------------------------------------------------------------------- /spec/on_require_spec.lua: -------------------------------------------------------------------------------- 1 | local lze = require("lze") 2 | local spy = require("luassert.spy") 3 | local dummy = function(name) 4 | return name 5 | end 6 | local reqspy = spy.new(dummy) 7 | local loadspy = spy.new(dummy) 8 | local function mktestplug(name, target) 9 | return { 10 | name = name, 11 | on_require = { name }, 12 | load = function(n) 13 | loadspy(n) 14 | package.preload[n] = function() 15 | return reqspy(n) 16 | end 17 | if target then 18 | require(target) 19 | end 20 | end, 21 | } 22 | end 23 | 24 | describe("handlers.on_require", function() 25 | it("require loads plugins", function() 26 | local name = "lze_test_require_mod" 27 | ---@type lze.Plugin 28 | lze.load(mktestplug(name)) 29 | require(name) 30 | assert.spy(loadspy).called_with(name) 31 | assert.spy(reqspy).called_with(name) 32 | package.preload[name] = nil 33 | package.loaded[name] = nil 34 | end) 35 | it("handles nested plugins", function() 36 | local name = "lze_test_require_mod_1" 37 | local name2 = "lze_test_require_mod_2" 38 | local name3 = "lze_test_require_mod_3" 39 | ---@type lze.Plugin 40 | lze.load({ 41 | mktestplug(name), 42 | mktestplug(name2, name), 43 | mktestplug(name3, name2), 44 | }) 45 | assert.equal(name3, require(name3)) 46 | for _, n in ipairs({ name, name2, name3 }) do 47 | assert.spy(loadspy).called_with(n) 48 | assert.spy(reqspy).called_with(n) 49 | package.preload[n] = nil 50 | package.loaded[n] = nil 51 | end 52 | end) 53 | end) 54 | -------------------------------------------------------------------------------- /lua/lze/h/ft.lua: -------------------------------------------------------------------------------- 1 | -- NOTE: simply an alias for lze.h.event for filetype 2 | local event = require("lze.h.event") 3 | 4 | local states = {} 5 | 6 | local augroup = nil 7 | 8 | local parse = function(value) 9 | return { 10 | id = value, 11 | event = "FileType", 12 | pattern = value, 13 | augroup = augroup, 14 | } 15 | end 16 | 17 | ---@type lze.Handler 18 | local M = { 19 | spec_field = "ft", 20 | lib = { 21 | parse = parse, 22 | }, 23 | init = function() 24 | augroup = vim.api.nvim_create_augroup("lze_handler_ft", { clear = true }) 25 | end, 26 | } 27 | 28 | ---@param plugin lze.Plugin 29 | function M.add(plugin) 30 | local ft_spec = plugin.ft 31 | if not ft_spec then 32 | return 33 | end 34 | ---@type lze.Event[] 35 | plugin.event = {} 36 | if type(ft_spec) == "string" then 37 | local ft = parse(ft_spec) 38 | ---@diagnostic disable-next-line: param-type-mismatch 39 | table.insert(plugin.event, ft) 40 | elseif type(ft_spec) == "table" then 41 | for _, ft_spec_ in ipairs(ft_spec) do 42 | local ft = parse(ft_spec_) 43 | ---@diagnostic disable-next-line: param-type-mismatch 44 | table.insert(plugin.event, ft) 45 | end 46 | end 47 | states[plugin.name] = true 48 | event.add(plugin) 49 | end 50 | 51 | ---@param name string 52 | function M.before(name) 53 | if states[name] then 54 | event.before(name) 55 | states[name] = nil 56 | end 57 | end 58 | 59 | function M.cleanup() 60 | for name, _ in pairs(states) do 61 | event.before(name) 62 | end 63 | states = {} 64 | if augroup then 65 | vim.api.nvim_del_augroup_by_id(augroup) 66 | end 67 | end 68 | 69 | return M 70 | -------------------------------------------------------------------------------- /lua/lze/h/colorscheme.lua: -------------------------------------------------------------------------------- 1 | -- NOTE: internal handlers must use internal trigger_load 2 | -- because require('lze') requires this module. 3 | local loader = require("lze.c.loader") 4 | 5 | local states = {} 6 | local augroup = nil 7 | 8 | ---@type lze.Handler 9 | local M = { 10 | spec_field = "colorscheme", 11 | } 12 | 13 | ---@param name string 14 | function M.before(name) 15 | for _, plugins in pairs(states) do 16 | plugins[name] = nil 17 | end 18 | end 19 | 20 | ---@param name string 21 | local function on_colorscheme(name) 22 | local pending = states[name] or {} 23 | if vim.tbl_isempty(pending) then 24 | -- already loaded 25 | return 26 | end 27 | loader.load(vim.tbl_values(pending)) 28 | end 29 | 30 | function M.init() 31 | augroup = vim.api.nvim_create_augroup("lze_handler_colorscheme", { clear = true }) 32 | vim.api.nvim_create_autocmd("ColorSchemePre", { 33 | callback = function(event) 34 | on_colorscheme(event.match) 35 | end, 36 | nested = true, 37 | group = augroup, 38 | }) 39 | end 40 | 41 | ---@param plugin lze.Plugin 42 | function M.add(plugin) 43 | local colorscheme_spec = plugin.colorscheme 44 | if not colorscheme_spec then 45 | return 46 | end 47 | local colorscheme_def = {} 48 | if type(colorscheme_spec) == "string" then 49 | table.insert(colorscheme_def, colorscheme_spec) 50 | elseif type(colorscheme_spec) == "table" then 51 | ---@param colorscheme_spec_ string 52 | for _, colorscheme_spec_ in ipairs(colorscheme_spec) do 53 | table.insert(colorscheme_def, colorscheme_spec_) 54 | end 55 | end 56 | ---@param colorscheme string 57 | for _, colorscheme in ipairs(colorscheme_def) do 58 | states[colorscheme] = states[colorscheme] or {} 59 | states[colorscheme][plugin.name] = plugin.name 60 | end 61 | end 62 | 63 | function M.cleanup() 64 | if augroup then 65 | vim.api.nvim_del_augroup_by_id(augroup) 66 | end 67 | states = {} 68 | end 69 | 70 | return M 71 | -------------------------------------------------------------------------------- /lua/lze/h/on_require.lua: -------------------------------------------------------------------------------- 1 | -- NOTE: internal handlers must use internal trigger_load 2 | -- because require('lze') requires this module. 3 | local trigger_load = require("lze.c.loader").load 4 | 5 | ---@type table 6 | local states = {} 7 | 8 | local new_loader = function(mod_path) 9 | local plugins = {} 10 | for name, mod_paths in pairs(states) do 11 | for _, v in ipairs(mod_paths) do 12 | if mod_path and mod_path:sub(1, #v) == v then 13 | table.insert(plugins, name) 14 | break 15 | end 16 | end 17 | end 18 | if next(plugins) ~= nil then 19 | return function() 20 | package.loaded[mod_path] = nil 21 | trigger_load(plugins) 22 | return require(mod_path) 23 | end 24 | end 25 | return "\n\tlze.on_require: no plugin registered to load on require of '" .. tostring(mod_path) .. "'" 26 | end 27 | 28 | ---@type lze.Handler 29 | local M = { 30 | spec_field = "on_require", 31 | ---@param name string 32 | before = function(name) 33 | states[name] = nil 34 | end, 35 | init = function() 36 | table.insert(package.loaders or package.searchers, new_loader) 37 | end, 38 | cleanup = function() 39 | for i, v in ipairs(package.loaders or package.searchers) do 40 | if v == new_loader then 41 | table.remove(package.loaders or package.searchers, i) 42 | end 43 | end 44 | states = {} 45 | end, 46 | } 47 | 48 | ---Adds a plugin to be lazy loaded upon requiring any submodule of provided mod paths 49 | ---@param plugin lze.Plugin 50 | function M.add(plugin) 51 | local on_req = plugin.on_require 52 | 53 | -- I don't know why I need to tell luacheck 54 | -- that I actually do use it by putting it into the state table? 55 | ---@type string[] 56 | --luacheck: no unused 57 | local mod_paths = {} 58 | 59 | if type(on_req) == "table" then 60 | ---@cast on_req string[] 61 | mod_paths = on_req 62 | elseif type(on_req) == "string" then 63 | mod_paths = { on_req } 64 | else 65 | return 66 | end 67 | 68 | states[plugin.name] = mod_paths 69 | end 70 | 71 | return M 72 | -------------------------------------------------------------------------------- /lua/lze/init.lua: -------------------------------------------------------------------------------- 1 | ---@mod lze 2 | 3 | local lze = {} 4 | 5 | ---registers a handler with lze to add new spec fields 6 | ---Returns the list of spec_field values added. 7 | ---THIS SHOULD BE CALLED BEFORE ANY CALLS TO lze.load ARE MADE 8 | ---@type fun(handlers: lze.Handler[]|lze.Handler|lze.HandlerSpec[]|lze.HandlerSpec): string[] 9 | lze.register_handlers = require("lze.c.handler").register_handlers 10 | 11 | ---Returns the cleared handlers 12 | ---THIS SHOULD BE CALLED BEFORE ANY CALLS TO lze.load ARE MADE 13 | ---@type fun():lze.Handler[] 14 | lze.clear_handlers = require("lze.c.handler").clear_handlers 15 | 16 | ---Returns the cleared handlers 17 | ---THIS SHOULD BE CALLED BEFORE ANY CALLS TO lze.load ARE MADE 18 | ---@type fun(handler_names: string|string[]):lze.Handler[] 19 | lze.remove_handlers = require("lze.c.handler").remove_handlers 20 | 21 | ---Trigger loading of the lze.Plugin loading hooks. 22 | ---Used by handlers to load plugins. 23 | ---Will return the names of the plugins that were skipped, 24 | ---either because they were already loaded or because they 25 | ---were never added to the queue. 26 | ---@type fun(plugin_names: string[]|string): string[] 27 | lze.trigger_load = require("lze.c.loader").load 28 | 29 | ---May be called as many times as desired. 30 | ---Will return the duplicate lze.Plugin objects. 31 | ---Priority spec field only affects order for 32 | ---non-lazy plugins within a single load call. 33 | ---accepts a list of lze.Spec or a single lze.Spec 34 | ---may accept a module name to require instead 35 | ---@type fun(spec: string|lze.Spec): lze.Plugin[] 36 | lze.load = require("lze.c.loader").define 37 | 38 | --- `false` for already loaded (or being loaded currently), 39 | --- `nil` for never added. READ ONLY TABLE 40 | --- Function access only checks; table access returns a COPY. 41 | --- unary minus is defined as vim.deepcopy of the actual state table 42 | --- local snapshot = -require('lze').state 43 | --- whereas the following will return this object, and thus remain up to date 44 | --- local state = require('lze').state 45 | ---@alias lze.State 46 | --- | fun(name: string): boolean? # Faster, returns `boolean?`. 47 | --- | table # Access returns COPY of state for that plugin name. 48 | ---@type lze.State 49 | lze.state = require("lze.c.loader").state 50 | 51 | ---Handlers may expose things by registering them in their lib table 52 | ---require('lze').h.. 53 | ---wont populate unless used, will be removed if handler is removed 54 | ---@type table 55 | lze.h = require("lze.c.handler").libs 56 | 57 | return lze 58 | -------------------------------------------------------------------------------- /.github/workflows/review-checklist.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Review Checklist 3 | 4 | on: 5 | pull_request_target: 6 | types: [opened, review_requested] 7 | 8 | jobs: 9 | review-checklist: 10 | name: Review Checklist 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/github-script@v8 14 | with: 15 | script: | 16 | const body = context.payload.pull_request.body 17 | if (body && (body.startsWith(":robot: I have created a release *beep* *boop*") || body.startsWith("Automated changes"))) { return; } 18 | 19 | // Get a list of all issues created by the PR opener 20 | // See: https://octokit.github.io/rest.js/#pagination 21 | const creator = context.payload.sender.login 22 | const opts = github.rest.issues.listForRepo.endpoint.merge({ 23 | ...context.issue, 24 | creator, 25 | state: 'all' 26 | }) 27 | const issues = await github.paginate(opts) 28 | 29 | for (const issue of issues) { 30 | if (issue.number === context.issue.number) { 31 | continue 32 | } 33 | 34 | if (issue.pull_request) { 35 | return // Creator is already a contributor. 36 | } 37 | } 38 | 39 | const { data: comments } = await github.rest.issues.listComments({ 40 | issue_number: context.issue.number, 41 | owner: context.repo.owner, 42 | repo: context.repo.repo, 43 | }); 44 | 45 | // if comment already exists, then just return 46 | if (comments.find(comment => comment.body.includes("### Review Checklist"))) { return; } 47 | 48 | // TODO: Update and 49 | 50 | github.rest.issues.createComment({ 51 | issue_number: context.issue.number, 52 | owner: context.repo.owner, 53 | repo: context.repo.repo, 54 | body: `### Review Checklist 55 | 56 | Does this PR follow the [Contribution Guidelines](https://github.com///blob/master/CONTRIBUTING.md)? Following is a _partial_ checklist: 57 | 58 | Proper [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/) scoping: 59 | 60 | - For example, fix(context): some bugfix 61 | 62 | - [ ] Pull request title has the appropriate conventional commit prefix. 63 | 64 | If applicable: 65 | 66 | - [ ] Tested 67 | - [ ] Tests have been added. 68 | - [ ] Tested manually (Steps to reproduce in PR description). 69 | - [ ] Updated documentation. 70 | - [ ] Updated [CHANGELOG.md](https://github.com///blob/master/CHANGELOG.md) 71 | `, 72 | }) 73 | -------------------------------------------------------------------------------- /spec/cmd_spec.lua: -------------------------------------------------------------------------------- 1 | ---@diagnostic disable: invisible 2 | vim.g.lze = { 3 | load = function() end, 4 | } 5 | local lze = require("lze") 6 | local loader = require("lze.c.loader") 7 | local spy = require("luassert.spy") 8 | 9 | describe("handlers.cmd", function() 10 | it("Command only loads plugin once and executes plugin command", function() 11 | local counter = 0 12 | ---@type lze.Plugin 13 | local plugin = { 14 | name = "foos", 15 | cmd = { "Foos" }, 16 | after = function() 17 | vim.api.nvim_create_user_command("Foos", function() 18 | counter = counter + 1 19 | end, {}) 20 | end, 21 | } 22 | local spy_load = spy.on(loader, "load") 23 | lze.load({ plugin }) 24 | assert.is_not_nil(vim.cmd.Foos) 25 | vim.cmd.Foos() 26 | vim.cmd.Foos() 27 | assert.spy(spy_load).called(1) 28 | assert.same(2, counter) 29 | end) 30 | it("Multiple commands only load plugin once", function() 31 | ---@param commands string[] 32 | local function itt(commands, name) 33 | ---@type lze.Plugin 34 | local plugin = { 35 | name = name, 36 | cmd = commands, 37 | after = function() 38 | vim.api.nvim_create_user_command(commands[1], function() end, {}) 39 | vim.api.nvim_create_user_command(commands[2], function() end, {}) 40 | end, 41 | } 42 | local spy_load = spy.on(loader, "load") 43 | lze.load({ plugin }) 44 | vim.cmd[commands[1]]() 45 | vim.cmd[commands[2]]() 46 | assert.spy(spy_load).called(1) 47 | end 48 | itt({ "Foo", "Bar" }, "foo5") 49 | itt({ "Bar", "Foo" }, "foo4") 50 | end) 51 | it("Deletes command before loading the plugin when other specs load it", function() 52 | local should_be_inc = 0 53 | local plugin = { 54 | name = "my_other_plus_cmd", 55 | cmd = "MyTestCMD", 56 | load = function(_) 57 | if vim.fn.exists(":MyTestCMD") == 0 then 58 | vim.api.nvim_create_user_command("MyTestCMD", function(_) 59 | should_be_inc = should_be_inc + 1 60 | end, {}) 61 | end 62 | end, 63 | } 64 | lze.load(plugin) 65 | lze.trigger_load(plugin.name) 66 | vim.cmd[plugin.cmd]() 67 | assert.equal(1, should_be_inc) 68 | end) 69 | it("Doesn't error when deleting command if the command doesn't exist", function() 70 | local plugin = { 71 | name = "my_test_cmd", 72 | cmd = "TestCMD", 73 | } 74 | local plugin2 = { 75 | name = "my_other_cmd_dep", 76 | dep_of = { "my_test_cmd" }, 77 | cmd = "TestCMD", 78 | } 79 | lze.load({ plugin, plugin2 }) 80 | lze.trigger_load(plugin.name) 81 | end) 82 | end) 83 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing guide 2 | 3 | Contributions are more than welcome! 4 | 5 | ## Commit messages / PR title 6 | 7 | Please ensure your pull request title conforms to [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/). 8 | 9 | ## CI 10 | 11 | CI checks are run using [`nix`](https://nixos.org/download.html#download-nix). 12 | 13 | ## Development 14 | 15 | ### Dev environment 16 | 17 | We use the following tools: 18 | 19 | #### Formatting 20 | 21 | - [`.editorconfig`](https://editorconfig.org/) (with [`editorconfig-checker`](https://github.com/editorconfig-checker/editorconfig-checker)) 22 | - [`stylua`](https://github.com/JohnnyMorganz/StyLua) [Lua] 23 | - [`alejandra`](https://github.com/kamadorueda/alejandra) [Nix] 24 | 25 | #### Linting 26 | 27 | - [`luacheck`](https://github.com/mpeterv/luacheck) [Lua] 28 | - [`markdownlint`](https://github.com/DavidAnson/markdownlint) [Markdown] 29 | 30 | #### Static type checking 31 | 32 | - [`lua-language-server`](https://github.com/LuaLS/lua-language-server/wiki/Diagnosis-Report#create-a-report) 33 | 34 | ### Nix devShell 35 | 36 | - Requires [flakes](https://wiki.nixos.org/wiki/Flakes) to be enabled. 37 | 38 | This project provides a `flake.nix` that can 39 | bootstrap all of the above development tools. 40 | 41 | To enter a development shell: 42 | 43 | ```console 44 | nix develop 45 | ``` 46 | 47 | To apply formatting, while in a devShell, run 48 | 49 | ```console 50 | pre-commit run --all 51 | ``` 52 | 53 | If you use [`direnv`](https://direnv.net/), 54 | just run `direnv allow` and you will be dropped in this devShell. 55 | 56 | ### Running tests 57 | 58 | I use [`busted`](https://lunarmodules.github.io/busted/) for testing, 59 | but with Neovim as the Lua interpreter. 60 | 61 | The easiest way to run tests is with Nix (see below). 62 | 63 | If you do not use Nix, you can also run the test suite using `luarocks test`. 64 | For more information on how to set up Neovim as a Lua interpreter, see 65 | 66 | - The [neorocks tutorial](https://github.com/nvim-neorocks/neorocks#without-neolua). 67 | 68 | Or 69 | 70 | - [`nlua`](https://github.com/mfussenegger/nlua). 71 | 72 | > [!NOTE] 73 | > 74 | > The Nix devShell sets up `luarocks test` and `busted` to use Neovim as the interpreter. 75 | 76 | ### Running tests and checks with Nix 77 | 78 | If you just want to run all checks that are available, run: 79 | 80 | ```console 81 | nix flake check -L --option sandbox false 82 | ``` 83 | 84 | To run tests locally, using Nix: 85 | 86 | ```console 87 | nix build .#checks..integration-nightly -L --option sandbox false 88 | ``` 89 | 90 | For example: 91 | 92 | ```console 93 | nix build .#checks.x86_64-linux.integration-nightly -L --option sandbox false 94 | ``` 95 | 96 | For formatting and linting: 97 | 98 | ```console 99 | nix build .#checks..pre-commit-check -L 100 | ``` 101 | 102 | For static type checking: 103 | 104 | ```console 105 | nix build .#checks..type-check-nightly -L 106 | ``` 107 | -------------------------------------------------------------------------------- /spec/register_handler_spec.lua: -------------------------------------------------------------------------------- 1 | local lze = require("lze") 2 | local spy = require("luassert.spy") 3 | 4 | describe("handlers.custom", function() 5 | ---@class state_entry 6 | ---@field load_called boolean 7 | ---@field after_load_called_after? boolean 8 | 9 | ---@type table 10 | local plugin_state = {} 11 | 12 | ---@class TestHandler: lze.Handler 13 | ---@type TestHandler 14 | local hndl = { 15 | spec_field = "testfield", 16 | add = function(_) end, 17 | before = function(_) end, 18 | after = function(name) 19 | if plugin_state[name] and plugin_state[name].load_called then 20 | plugin_state[name] = vim.tbl_extend("error", plugin_state[name], { after_load_called_after = true }) 21 | return 22 | end 23 | plugin_state[name] = vim.tbl_extend("error", plugin_state[name] or {}, { after_load_called_after = false }) 24 | end, 25 | } 26 | 27 | ---@class TestHandler: lze.Handler 28 | ---@type TestHandler 29 | local hndl2 = { 30 | spec_field = "testfield2", 31 | set_lazy = false, 32 | } 33 | 34 | local test_plugin = { 35 | "testplugin", 36 | testfield = { "a", "b" }, 37 | load = function(plugin) 38 | plugin_state[plugin] = { 39 | load_called = true, 40 | } 41 | end, 42 | } 43 | local test_plugin_loaded = vim.tbl_extend("error", test_plugin, { lazy = true }) 44 | test_plugin_loaded.name = test_plugin_loaded[1] 45 | test_plugin_loaded[1] = nil 46 | 47 | local test_plugin2 = { 48 | "test_plugin2-not-lazy", 49 | testfield2 = { "a", "b" }, 50 | load = function() end, 51 | } 52 | 53 | local test_plugin3 = { 54 | "testplugin3-is-lazy", 55 | testfield = { "a", "b" }, 56 | testfield2 = { "a", "b" }, 57 | load = function() end, 58 | } 59 | local test_plugin3_loaded = vim.tbl_extend("error", test_plugin3, { lazy = true }) 60 | test_plugin3_loaded.name = test_plugin3_loaded[1] 61 | test_plugin3_loaded[1] = nil 62 | 63 | local addspy = spy.on(hndl, "add") 64 | local delspy = spy.on(hndl, "before") 65 | local afterspy = spy.on(hndl, "after") 66 | 67 | it("Duplicate handlers fail to register", function() 68 | assert.same({}, lze.register_handlers(require("lze.h.ft"))) 69 | end) 70 | it("can add plugins to the handler", function() 71 | assert.same({ hndl.spec_field }, lze.register_handlers(hndl)) 72 | assert.same({ hndl2.spec_field }, lze.register_handlers(hndl2)) 73 | lze.load(test_plugin) 74 | assert.spy(addspy).called_with(test_plugin_loaded) 75 | end) 76 | it("loading a plugin calls before", function() 77 | lze.trigger_load("testplugin") 78 | assert.spy(delspy).called_with("testplugin") 79 | end) 80 | it("handler after is called after load", function() 81 | assert.spy(afterspy).called_with("testplugin") 82 | assert.True(plugin_state["testplugin"].load_called) 83 | assert.True(plugin_state["testplugin"].after_load_called_after) 84 | end) 85 | it("can choose if it affects lazy setting", function() 86 | lze.load({ test_plugin2, test_plugin3 }) 87 | assert.False(lze.state(test_plugin2.name)) 88 | assert.same(lze.state[test_plugin3.name], test_plugin3_loaded) 89 | end) 90 | end) 91 | -------------------------------------------------------------------------------- /spec/keys_spec.lua: -------------------------------------------------------------------------------- 1 | ---@diagnostic disable: invisible 2 | vim.g.lze = { 3 | injects = { 4 | load = function() end, 5 | }, 6 | } 7 | local lze = require("lze") 8 | local loader = require("lze.c.loader") 9 | local spy = require("luassert.spy") 10 | 11 | describe("handlers.keys", function() 12 | it("parses ids correctly", function() 13 | local tests = { 14 | { "", "", true }, 15 | { "", "", true }, 16 | { "k", "K", false }, 17 | } 18 | for _, test in ipairs(tests) do 19 | if test[3] then 20 | assert.same(lze.h.keys.parse(test[1])[1].id, lze.h.keys.parse(test[2])[1].id) 21 | else 22 | assert.is_not.same(lze.h.keys.parse(test[1])[1].id, lze.h.keys.parse(test[2])[1].id) 23 | end 24 | end 25 | end) 26 | it("Key only loads plugin once", function() 27 | local lhs = "tt" 28 | ---@type lze.Plugin 29 | local plugin = { 30 | name = "food", 31 | keys = lhs, 32 | } 33 | local spy_load = spy.on(loader, "load") 34 | lze.load(plugin) 35 | local feed = vim.api.nvim_replace_termcodes("" .. lhs, true, true, true) 36 | vim.api.nvim_feedkeys(feed, "ix", false) 37 | vim.api.nvim_feedkeys(feed, "ix", false) 38 | assert.spy(spy_load).called(1) 39 | assert.False(lze.state(plugin.name)) 40 | end) 41 | it("Multiple keys only load plugin once", function() 42 | ---@param lzkeys string[]|lze.KeysSpec[] 43 | local function itt(lzkeys, name) 44 | local timesloaded = 0 45 | local parsed_keys = {} 46 | for _, key in ipairs(lzkeys) do 47 | table.insert(parsed_keys, lze.h.keys.parse(key)[1]) 48 | end 49 | ---@type lze.Plugin 50 | local plugin = { 51 | name = name, 52 | keys = lzkeys, 53 | load = function() 54 | timesloaded = timesloaded + 1 55 | end, 56 | } 57 | lze.load(plugin) 58 | local feed1 = vim.api.nvim_replace_termcodes("" .. parsed_keys[1].lhs, true, true, true) 59 | vim.api.nvim_feedkeys(feed1, "ix", false) 60 | local feed2 = vim.api.nvim_replace_termcodes("" .. parsed_keys[2].lhs, true, true, true) 61 | vim.api.nvim_feedkeys(feed2, "ix", false) 62 | assert.Equal(1, timesloaded) 63 | assert.False(lze.state(plugin.name)) 64 | end 65 | itt({ "tt", "ff" }, "foody1") 66 | itt({ "ff", "tt" }, "foody2") 67 | end) 68 | it("Plugins' keymaps are triggered", function() 69 | local lhs = "xy" 70 | local triggered = false 71 | ---@type lze.Plugin 72 | local plugin = { 73 | name = "bazzite", 74 | keys = lhs, 75 | after = function() 76 | vim.keymap.set("n", lhs, function() 77 | triggered = true 78 | end) 79 | end, 80 | } 81 | lze.load(plugin) 82 | local feed = vim.api.nvim_replace_termcodes("" .. lhs, true, true, true) 83 | vim.api.nvim_feedkeys(feed, "ix", false) 84 | vim.api.nvim_feedkeys(feed, "x", false) 85 | assert.True(triggered) 86 | assert.False(lze.state(plugin.name)) 87 | end) 88 | end) 89 | -------------------------------------------------------------------------------- /lua/lze/c/parse.lua: -------------------------------------------------------------------------------- 1 | -- needed so we can use stuff defined later in the file 2 | local lib = {} 3 | 4 | ---@param spec lze.SpecImport 5 | local function is_disabled(spec) 6 | return spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) 7 | end 8 | 9 | ---@param spec lze.SpecImport 10 | ---@param result lze.Plugin[] 11 | local function import_spec(spec, result) 12 | if is_disabled(spec) then 13 | return 14 | end 15 | if type(spec.import) == "table" then 16 | ---@diagnostic disable-next-line: param-type-mismatch 17 | lib.normalize(spec.import, result) 18 | return 19 | end 20 | if type(spec.import) ~= "string" then 21 | vim.schedule(function() 22 | vim.notify( 23 | "Invalid import spec. The 'import' field should be a module name (or more specs), but was instead: " 24 | .. vim.inspect(spec), 25 | vim.log.levels.ERROR, 26 | { title = "lze" } 27 | ) 28 | end) 29 | return 30 | end 31 | local modname = spec.import 32 | local ok, mod = pcall(require, modname) 33 | if not ok then 34 | vim.schedule(function() 35 | local err = type(mod) == "string" and "': " .. mod or "" 36 | vim.notify("Failed to load module '" .. modname .. err, vim.log.levels.ERROR, { title = "lze" }) 37 | end) 38 | return 39 | end 40 | if type(mod) ~= "table" then 41 | vim.schedule(function() 42 | vim.notify( 43 | "Invalid plugin spec module '" .. modname .. "' of type '" .. type(mod) .. "'", 44 | vim.log.levels.ERROR, 45 | { title = "lze" } 46 | ) 47 | end) 48 | return 49 | end 50 | lib.normalize(mod, result) 51 | end 52 | 53 | ---@param spec lze.Spec 54 | ---@return boolean 55 | local function is_spec_list(spec) 56 | return #spec > 1 or vim.islist(spec) and #spec > 1 or #spec == 1 and type(spec[1]) == "table" 57 | end 58 | 59 | ---@param spec lze.Spec 60 | ---@return boolean 61 | local function is_single_plugin_spec(spec) 62 | ---@diagnostic disable-next-line: undefined-field 63 | return type(spec[1]) == "string" or type(spec.name) == "string" 64 | end 65 | 66 | ---@param spec lze.Spec 67 | ---@param result lze.Plugin[] 68 | function lib.normalize(spec, result) 69 | if is_spec_list(spec) then 70 | for _, sp in ipairs(spec) do 71 | ---@cast sp lze.Spec 72 | lib.normalize(sp, result) 73 | end 74 | elseif is_single_plugin_spec(spec) then 75 | ---@diagnostic disable-next-line: inject-field 76 | spec.name = spec.name or spec[1] 77 | spec[1] = nil 78 | if type(spec) == "table" and type(spec.name) == "string" then 79 | table.insert(result, spec) 80 | else 81 | vim.schedule(function() 82 | vim.notify( 83 | "attempted to add a plugin with no name: " .. vim.inspect(spec), 84 | vim.log.levels.ERROR, 85 | { title = "lze" } 86 | ) 87 | end) 88 | end 89 | elseif spec.import then 90 | ---@cast spec lze.SpecImport 91 | import_spec(spec, result) 92 | end 93 | end 94 | 95 | ---@param spec lze.Spec 96 | ---@return lze.Plugin[] 97 | return function(spec) 98 | local result = {} 99 | lib.normalize(spec, result) 100 | return result 101 | end 102 | -------------------------------------------------------------------------------- /lua/lze/h/cmd.lua: -------------------------------------------------------------------------------- 1 | -- NOTE: internal handlers must use internal trigger_load 2 | -- because require('lze') requires this module. 3 | local loader = require("lze.c.loader") 4 | 5 | local states = {} 6 | 7 | ---@type lze.Handler 8 | local M = { 9 | spec_field = "cmd", 10 | } 11 | 12 | ---@param cmd string 13 | local function load(cmd) 14 | loader.load(vim.tbl_values(states[cmd])) 15 | end 16 | 17 | ---@param cmd string 18 | local function add_cmd(cmd) 19 | vim.api.nvim_create_user_command(cmd, function(event) 20 | ---@cast event vim.api.keyset.user_command 21 | local command = { 22 | cmd = cmd, 23 | bang = event.bang or nil, 24 | ---@diagnostic disable-next-line: undefined-field 25 | mods = event.smods, 26 | ---@diagnostic disable-next-line: undefined-field 27 | args = event.fargs, 28 | count = event.count >= 0 and event.range == 0 and event.count or nil, 29 | } 30 | 31 | if event.range == 1 then 32 | ---@diagnostic disable-next-line: undefined-field 33 | command.range = { event.line1 } 34 | elseif event.range == 2 then 35 | ---@diagnostic disable-next-line: undefined-field 36 | command.range = { event.line1, event.line2 } 37 | end 38 | 39 | load(cmd) 40 | 41 | local info = vim.api.nvim_get_commands({})[cmd] or vim.api.nvim_buf_get_commands(0, {})[cmd] 42 | if not info then 43 | vim.schedule(function() 44 | ---@type string 45 | local plugins = "`" .. table.concat(vim.tbl_values(states[cmd]), ", ") .. "`" 46 | vim.notify("Command `" .. cmd .. "` not found after loading " .. plugins, vim.log.levels.ERROR) 47 | end) 48 | return 49 | end 50 | 51 | command.nargs = info.nargs 52 | ---@diagnostic disable-next-line: undefined-field 53 | if event.args and event.args ~= "" and info.nargs and info.nargs:find("[1?]") then 54 | ---@diagnostic disable-next-line: undefined-field 55 | command.args = { event.args } 56 | end 57 | vim.cmd(command) 58 | end, { 59 | bang = true, 60 | range = true, 61 | nargs = "*", 62 | complete = function(_, line) 63 | load(cmd) 64 | return vim.fn.getcompletion(line, "cmdline") 65 | end, 66 | }) 67 | end 68 | 69 | ---@param name string 70 | function M.before(name) 71 | for cmd, plugins in pairs(states) do 72 | if plugins[name] then 73 | pcall(vim.api.nvim_del_user_command, cmd) 74 | end 75 | plugins[name] = nil 76 | end 77 | end 78 | 79 | ---@param plugin lze.Plugin 80 | function M.add(plugin) 81 | local cmd_spec = plugin.cmd 82 | if not cmd_spec then 83 | return 84 | end 85 | local cmd_def = {} 86 | if type(cmd_spec) == "string" then 87 | table.insert(cmd_def, cmd_spec) 88 | elseif type(cmd_spec) == "table" then 89 | ---@param cmd_spec_ string 90 | for _, cmd_spec_ in ipairs(cmd_spec) do 91 | table.insert(cmd_def, cmd_spec_) 92 | end 93 | end 94 | ---@param cmd string 95 | for _, cmd in ipairs(cmd_def) do 96 | states[cmd] = states[cmd] or {} 97 | states[cmd][plugin.name] = plugin.name 98 | add_cmd(cmd) 99 | end 100 | end 101 | 102 | function M.cleanup() 103 | for cmd, _ in pairs(states) do 104 | pcall(vim.api.nvim_del_user_command, cmd) 105 | end 106 | states = {} 107 | end 108 | 109 | return M 110 | -------------------------------------------------------------------------------- /spec/dep_of_spec.lua: -------------------------------------------------------------------------------- 1 | local lze = require("lze") 2 | 3 | describe("handlers.dep_of", function() 4 | ---@class state_entry_deps 5 | ---@field load_called? boolean 6 | ---@field after_called? boolean 7 | ---@field before_called? boolean 8 | ---@field loaded_after? string[] 9 | ---@field loaded_after_before? string[] 10 | ---@field loaded_after_after? string[] 11 | 12 | ---@type table 13 | local plugin_state = {} 14 | 15 | local filter_states = function(plugin, filter) 16 | local result = {} 17 | for name, state in pairs(plugin_state) do 18 | if name ~= plugin.name and filter(name, state) then 19 | table.insert(result, name) 20 | end 21 | end 22 | return result 23 | end 24 | 25 | local test_load_fun = function(plugin) 26 | if plugin_state[plugin] == nil then 27 | plugin_state[plugin] = {} 28 | end 29 | plugin_state[plugin] = vim.tbl_extend("force", plugin_state[plugin], { 30 | load_called = true, 31 | loaded_after = filter_states(plugin, function(_, v) 32 | return v.load_called 33 | end), 34 | loaded_after_after = filter_states(plugin, function(_, v) 35 | return v.after_called 36 | end), 37 | loaded_after_before = filter_states(plugin, function(_, v) 38 | return v.before_called 39 | end), 40 | }) 41 | end 42 | 43 | local test_before_fun = function(plugin) 44 | if plugin_state[plugin.name] == nil then 45 | plugin_state[plugin.name] = {} 46 | end 47 | plugin_state[plugin.name].before_called = true 48 | end 49 | local test_after_fun = function(plugin) 50 | if plugin_state[plugin.name] == nil then 51 | plugin_state[plugin.name] = {} 52 | end 53 | plugin_state[plugin.name].after_called = true 54 | end 55 | 56 | local tpl = { 57 | name = "test_plugin_3", 58 | lazy = true, 59 | load = test_load_fun, 60 | before = test_before_fun, 61 | after = test_after_fun, 62 | } 63 | local tpl_2 = { 64 | name = "test_plugin_4", 65 | dep_of = { "test_plugin_3" }, 66 | load = test_load_fun, 67 | before = test_before_fun, 68 | after = test_after_fun, 69 | } 70 | 71 | it("dep_of loads after before and before load", function() 72 | lze.load(tpl_2) 73 | lze.load(tpl) 74 | lze.trigger_load(tpl.name) 75 | 76 | assert.same(true, plugin_state[tpl.name].load_called) 77 | assert.same(true, plugin_state[tpl.name].before_called) 78 | assert.same(true, plugin_state[tpl.name].after_called) 79 | assert.same({ "test_plugin_4" }, plugin_state[tpl.name].loaded_after) 80 | assert.same({ "test_plugin_4" }, plugin_state[tpl.name].loaded_after_after) 81 | assert.True(vim.tbl_contains(plugin_state[tpl.name].loaded_after_before, "test_plugin_3")) 82 | assert.True(vim.tbl_contains(plugin_state[tpl.name].loaded_after_before, "test_plugin_4")) 83 | 84 | assert.same(true, plugin_state[tpl_2.name].load_called) 85 | assert.same(true, plugin_state[tpl_2.name].before_called) 86 | assert.same(true, plugin_state[tpl_2.name].after_called) 87 | assert.same({}, plugin_state[tpl_2.name].loaded_after) 88 | assert.same({}, plugin_state[tpl_2.name].loaded_after_after) 89 | assert.True(vim.tbl_contains(plugin_state[tpl_2.name].loaded_after_before, "test_plugin_3")) 90 | assert.True(vim.tbl_contains(plugin_state[tpl_2.name].loaded_after_before, "test_plugin_4")) 91 | end) 92 | end) 93 | -------------------------------------------------------------------------------- /spec/event_spec.lua: -------------------------------------------------------------------------------- 1 | ---@diagnostic disable: invisible 2 | vim.g.lze = { 3 | load = function() end, 4 | } 5 | local lze = require("lze") 6 | local loader = require("lze.c.loader") 7 | local spy = require("luassert.spy") 8 | 9 | describe("handlers.event", function() 10 | it("can parse from string", function() 11 | local f = function(inspec, evstr) 12 | local res = lze.h.event.parse(evstr) 13 | assert.Same(inspec.event, res.event) 14 | assert.Same(inspec.pattern, res.pattern) 15 | assert.Same(inspec.id, res.id) 16 | end 17 | f({ 18 | event = "VimEnter", 19 | id = "VimEnter", 20 | }, "VimEnter") 21 | end) 22 | it("can parse from table", function() 23 | local f = function(inspec) 24 | local res = lze.h.event.parse({ 25 | event = inspec.event, 26 | pattern = inspec.pattern, 27 | }) 28 | assert.Same(inspec.event, res.event) 29 | assert.Same(inspec.pattern, res.pattern) 30 | assert.Same(inspec.id, res.id) 31 | end 32 | f({ 33 | event = "VimEnter", 34 | id = "VimEnter", 35 | }) 36 | f({ 37 | event = { "VimEnter", "BufEnter" }, 38 | id = "VimEnter|BufEnter", 39 | }) 40 | f({ 41 | event = "BufEnter", 42 | id = "BufEnter *.lua", 43 | pattern = "*.lua", 44 | }) 45 | end) 46 | it("Event only loads plugin once", function() 47 | ---@type lze.Plugin 48 | local plugin = { 49 | name = "foo", 50 | event = { lze.h.event.parse("BufEnter") }, 51 | } 52 | local spy_load = spy.on(loader, "load") 53 | lze.load(plugin) 54 | vim.api.nvim_exec_autocmds("BufEnter", {}) 55 | vim.api.nvim_exec_autocmds("BufEnter", {}) 56 | assert.spy(spy_load).called(1) 57 | assert.False(lze.state(plugin.name)) 58 | end) 59 | it("Multiple events only load plugin once", function() 60 | ---@type table 61 | local called_number = {} 62 | ---@param events lze.Event[] 63 | local function itt(events, name) 64 | ---@type lze.Plugin 65 | local plugin = { 66 | name = name, 67 | event = events, 68 | after = function(plugin) 69 | called_number[plugin.name] = (called_number[plugin.name] or 0) + 1 70 | end, 71 | } 72 | local spy_load = spy.on(loader, "load") 73 | lze.load(plugin) 74 | vim.api.nvim_exec_autocmds(events[1].event, { 75 | pattern = ".lua", 76 | }) 77 | vim.api.nvim_exec_autocmds(events[2].event, { 78 | pattern = ".lua", 79 | }) 80 | assert.spy(spy_load).called_with({ plugin.name }) 81 | assert.Equal(1, called_number[plugin.name]) 82 | assert.False(lze.state(plugin.name)) 83 | end 84 | itt({ lze.h.event.parse("BufEnter"), lze.h.event.parse("WinEnter") }, "foo2") 85 | itt({ lze.h.event.parse("WinEnter"), lze.h.event.parse("BufEnter") }, "foo3") 86 | end) 87 | it("Plugins' event handlers are triggered", function() 88 | local triggered = false 89 | ---@type lze.Plugin 90 | local plugin = { 91 | name = "foo6", 92 | event = { lze.h.event.parse("BufEnter") }, 93 | after = function() 94 | triggered = true 95 | end, 96 | } 97 | ---@diagnostic disable-next-line: duplicate-set-field 98 | lze.load(plugin) 99 | vim.api.nvim_exec_autocmds("BufEnter", {}) 100 | assert.True(triggered) 101 | assert.False(lze.state(plugin.name)) 102 | end) 103 | it("DeferredUIEnter", function() 104 | ---@type lze.Plugin 105 | local plugin = { 106 | name = "bla", 107 | event = { lze.h.event.parse("DeferredUIEnter") }, 108 | } 109 | local spy_load = spy.on(loader, "load") 110 | lze.load(plugin) 111 | vim.api.nvim_exec_autocmds("User", { pattern = "DeferredUIEnter", modeline = false }) 112 | assert.spy(spy_load).called(1) 113 | assert.False(lze.state(plugin.name)) 114 | end) 115 | end) 116 | -------------------------------------------------------------------------------- /spec/on_plugin_spec.lua: -------------------------------------------------------------------------------- 1 | local lze = require("lze") 2 | 3 | describe("handlers.on_plugin", function() 4 | ---@class state_entry_after 5 | ---@field load_called? boolean 6 | ---@field after_called? boolean 7 | ---@field before_called? boolean 8 | ---@field loaded_after? string[] 9 | ---@field loaded_after_before? string[] 10 | ---@field loaded_after_after? string[] 11 | 12 | ---@type table 13 | local plugin_state = {} 14 | 15 | local filter_states = function(plugin, filter) 16 | local result = {} 17 | for name, state in pairs(plugin_state) do 18 | if name ~= plugin.name and filter(name, state) then 19 | table.insert(result, name) 20 | end 21 | end 22 | return result 23 | end 24 | 25 | local test_load_fun = function(plugin) 26 | if plugin_state[plugin] == nil then 27 | plugin_state[plugin] = {} 28 | end 29 | plugin_state[plugin] = vim.tbl_extend("force", plugin_state[plugin], { 30 | load_called = true, 31 | loaded_after = filter_states(plugin, function(_, v) 32 | return v.load_called 33 | end), 34 | loaded_after_after = filter_states(plugin, function(_, v) 35 | return v.after_called 36 | end), 37 | loaded_after_before = filter_states(plugin, function(_, v) 38 | return v.before_called 39 | end), 40 | }) 41 | end 42 | 43 | local test_before_fun = function(plugin) 44 | if plugin_state[plugin.name] == nil then 45 | plugin_state[plugin.name] = {} 46 | end 47 | plugin_state[plugin.name].before_called = true 48 | end 49 | local test_after_fun = function(plugin) 50 | if plugin_state[plugin.name] == nil then 51 | plugin_state[plugin.name] = {} 52 | end 53 | plugin_state[plugin.name].after_called = true 54 | end 55 | 56 | local test_plugin = { 57 | "test_plugin", 58 | lazy = true, 59 | load = test_load_fun, 60 | before = test_before_fun, 61 | after = test_after_fun, 62 | } 63 | local test_plugin_2 = { 64 | "test_plugin_2", 65 | on_plugin = { "test_plugin" }, 66 | load = test_load_fun, 67 | before = test_before_fun, 68 | after = test_after_fun, 69 | } 70 | local tpl = vim.tbl_extend("keep", test_plugin, { lazy = true }) 71 | tpl.name = tpl[1] 72 | tpl[1] = nil 73 | 74 | local tpl_2 = vim.tbl_extend("error", test_plugin_2, { lazy = true }) 75 | tpl_2.name = tpl_2[1] 76 | tpl_2[1] = nil 77 | 78 | it("dep_of loads after before and before load", function() 79 | lze.load(test_plugin) 80 | lze.load(test_plugin_2) 81 | lze.trigger_load(tpl.name) 82 | 83 | assert.same(true, plugin_state[tpl.name].load_called) 84 | assert.same(true, plugin_state[tpl.name].before_called) 85 | assert.same(true, plugin_state[tpl.name].after_called) 86 | assert.same({}, plugin_state[tpl.name].loaded_after) 87 | assert.same({}, plugin_state[tpl.name].loaded_after_after) 88 | assert.same({ "test_plugin" }, plugin_state[tpl.name].loaded_after_before) 89 | 90 | assert.same(true, plugin_state[tpl_2.name].load_called) 91 | assert.same(true, plugin_state[tpl_2.name].before_called) 92 | assert.same(true, plugin_state[tpl_2.name].after_called) 93 | assert.same({}, plugin_state[tpl_2.name].loaded_after_after) 94 | assert.same({ "test_plugin" }, plugin_state[tpl_2.name].loaded_after) 95 | assert.True(vim.tbl_contains(plugin_state[tpl_2.name].loaded_after_before, "test_plugin")) 96 | assert.True(vim.tbl_contains(plugin_state[tpl_2.name].loaded_after_before, "test_plugin_2")) 97 | end) 98 | it("dep_of loads if other was loaded already", function() 99 | local testval = false 100 | local defertest = { 101 | "defer_test_plugin", 102 | } 103 | local defertest2 = { 104 | "defer_test_plugin_2", 105 | on_plugin = { "defer_test_plugin" }, 106 | load = function(_) 107 | testval = true 108 | end, 109 | } 110 | lze.load(defertest) 111 | lze.load(defertest2) 112 | assert.True(testval) 113 | end) 114 | end) 115 | -------------------------------------------------------------------------------- /spec/import_spec.lua: -------------------------------------------------------------------------------- 1 | local lz = require("lze") 2 | vim.g.lze = { 3 | injects = { 4 | load = function() end, 5 | }, 6 | } 7 | local tempdir = vim.fn.tempname() 8 | vim.system({ "rm", "-r", tempdir }):wait() 9 | vim.system({ "mkdir", "-p", tempdir .. "/lua/plugins" }):wait() 10 | 11 | local loader = require("lze.c.loader") 12 | local spy = require("luassert.spy") 13 | 14 | describe("lze", function() 15 | describe("load", function() 16 | it("import from spec", function() 17 | lz.load({ 18 | { 19 | import = { 20 | { 21 | "TESTPLUGIN_IMPORT_FROM_SPEC", 22 | lazy = true, 23 | }, 24 | }, 25 | }, 26 | }) 27 | assert.True(lz.state("TESTPLUGIN_IMPORT_FROM_SPEC")) 28 | lz.trigger_load("TESTPLUGIN_IMPORT_FROM_SPEC") 29 | assert.False(lz.state("TESTPLUGIN_IMPORT_FROM_SPEC")) 30 | end) 31 | it("import", function() 32 | local plugin_config_content = [[ 33 | return { 34 | "baz.nvim", 35 | cmd = "Baz", 36 | } 37 | ]] 38 | local spec_file = vim.fs.joinpath(tempdir, "lua", "plugins", "init.lua") 39 | local fh = assert(io.open(spec_file, "w"), "Could not open config file for writing") 40 | fh:write(plugin_config_content) 41 | fh:close() 42 | vim.opt.runtimepath:append(tempdir) 43 | local spy_load = spy.on(loader, "load") 44 | lz.load("plugins") 45 | vim.cmd.Baz() 46 | assert.spy(spy_load).called(1) 47 | vim.system({ "rm", spec_file }):wait() 48 | package.loaded["plugins"] = nil 49 | end) 50 | it("import root file", function() 51 | local plugin_config_content = [[ 52 | return { 53 | { "cuteify.nvim" }, 54 | { "bat.nvim", cmd = "Bat" }, 55 | } 56 | ]] 57 | local plugin1_spec_file = vim.fs.joinpath(tempdir, "lua", "plugins.lua") 58 | local fh = assert(io.open(plugin1_spec_file, "w"), "Could not open config file for writing") 59 | fh:write(plugin_config_content) 60 | fh:close() 61 | vim.opt.runtimepath:append(tempdir) 62 | local spy_load = spy.on(loader, "load") 63 | lz.load("plugins") 64 | assert.spy(spy_load).called(1) 65 | vim.cmd.Bat() 66 | assert.spy(spy_load).called(2) 67 | vim.system({ "rm", plugin1_spec_file }):wait() 68 | package.loaded["plugins"] = nil 69 | end) 70 | it("import plugin specs and spec file", function() 71 | local plugins_dir = vim.fs.joinpath(tempdir, "lua", "plugins") 72 | local plugin1_config_content = [[ 73 | return { 74 | "telescope.nvim", 75 | cmd = "Telescope", 76 | after = function() 77 | vim.g.telescope_after = true 78 | end, 79 | } 80 | ]] 81 | local spec_file = vim.fs.joinpath(plugins_dir, "telescope.lua") 82 | local fh = assert(io.open(spec_file, "w"), "Could not open config file for writing") 83 | fh:write(plugin1_config_content) 84 | fh:close() 85 | local plugin2_config_content = [[ 86 | return { 87 | { 88 | "foo.nvim", 89 | cmd = "Foo", 90 | after = function() 91 | vim.g.foo_after = true 92 | end, 93 | }, 94 | { import = "plugins.telescope", }, 95 | } 96 | ]] 97 | local plugin2_dir = vim.fs.joinpath(plugins_dir, "foo") 98 | vim.system({ "mkdir", "-p", plugin2_dir }):wait() 99 | local plugin2_spec_file = vim.fs.joinpath(plugin2_dir, "init.lua") 100 | fh = assert(io.open(plugin2_spec_file, "w"), "Could not open config file for writing") 101 | fh:write(plugin2_config_content) 102 | fh:close() 103 | vim.opt.runtimepath:append(tempdir) 104 | local spy_load = spy.on(loader, "load") 105 | lz.load({ 106 | { import = "plugins.foo" }, 107 | { "sweetie.nvim" }, 108 | }) 109 | assert.spy(spy_load).called(1) 110 | vim.cmd.Telescope() 111 | assert.spy(spy_load).called(2) 112 | assert.True(vim.g.telescope_after) 113 | vim.cmd.Foo() 114 | assert.spy(spy_load).called(3) 115 | assert.True(vim.g.foo_after) 116 | -- vim.cmd.Linked() 117 | -- assert.spy(spy_load).called(4) 118 | vim.system({ "rm", plugin2_spec_file }):wait() 119 | package.loaded["plugins.foo"] = nil 120 | package.loaded["plugins.telescope"] = nil 121 | end) 122 | end) 123 | end) 124 | -------------------------------------------------------------------------------- /spec/lze_spec.lua: -------------------------------------------------------------------------------- 1 | local lz = require("lze") 2 | vim.g.lze = { 3 | injects = { 4 | load = function() end, 5 | }, 6 | } 7 | local loader = require("lze.c.loader") 8 | local spy = require("luassert.spy") 9 | 10 | describe("lze", function() 11 | describe("load", function() 12 | it("__len works", function() 13 | assert.True(#lz.state == 0) 14 | lz.load({ 15 | { 16 | "neorgblahblahblah", 17 | lazy = true, 18 | }, 19 | }) 20 | assert.True(#lz.state == 1) 21 | end) 22 | it("list of plugin specs", function() 23 | local spy_load = spy.on(loader, "load") 24 | lz.load({ 25 | { 26 | "neorg", 27 | }, 28 | { 29 | "crates.nvim", 30 | ft = { "toml", "rust" }, 31 | }, 32 | { 33 | "telescope.nvim", 34 | keys = { { "tt", mode = { "n", "v" } } }, 35 | cmd = "Telescope", 36 | }, 37 | }) 38 | assert.spy(spy_load).called(1) 39 | assert.spy(spy_load).called_with("neorg") 40 | vim.api.nvim_exec_autocmds("FileType", { pattern = "toml" }) 41 | assert.spy(spy_load).called(2) 42 | assert.spy(spy_load).called_with({ "crates.nvim" }) 43 | vim.cmd.Telescope() 44 | assert.spy(spy_load).called(3) 45 | assert.spy(spy_load).called_with({ "telescope.nvim" }) 46 | end) 47 | it("individual plugin specs", function() 48 | local spy_load = spy.on(loader, "load") 49 | lz.load({ 50 | "foo.nvim", 51 | keys = "ff", 52 | }) 53 | assert.spy(spy_load).called(0) 54 | local feed = vim.api.nvim_replace_termcodes("ff", true, true, true) 55 | vim.api.nvim_feedkeys(feed, "ix", false) 56 | assert.spy(spy_load).called(1) 57 | lz.load({ 58 | "bar.nvim", 59 | cmd = "Bar", 60 | }) 61 | vim.cmd.Bar() 62 | assert.spy(spy_load).called(2) 63 | end) 64 | it("can override load implementation via plugin spec", function() 65 | local loaded = false 66 | lz.load({ 67 | "baz.nvim", 68 | keys = "bb", 69 | load = function() 70 | loaded = true 71 | end, 72 | }) 73 | assert.False(loaded) 74 | local feed = vim.api.nvim_replace_termcodes("bb", true, true, true) 75 | vim.api.nvim_feedkeys(feed, "ix", false) 76 | assert.True(loaded) 77 | end) 78 | it("list with a single plugin spec", function() 79 | local spy_load = spy.on(loader, "load") 80 | lz.load({ 81 | { 82 | "single.nvim", 83 | cmd = "Single", 84 | }, 85 | }) 86 | assert.spy(spy_load).called(0) 87 | pcall(vim.cmd.Single) 88 | assert.spy(spy_load).called(1) 89 | assert.spy(spy_load).called_with({ "single.nvim" }) 90 | end) 91 | it("can disable plugin specs as specified", function() 92 | lz.load({ 93 | "fool.nvim", 94 | keys = "ff", 95 | enabled = false, 96 | }) 97 | assert(lz.state("fool.nvim") == nil) 98 | lz.load({ 99 | "bard.nvim", 100 | enabled = function() 101 | return false 102 | end, 103 | }) 104 | assert(lz.state("bard.nvim") == nil) 105 | local checked = 0 106 | lz.load({ 107 | "barz.nvim", 108 | lazy = true, 109 | enabled = function() 110 | if checked == 0 then 111 | checked = checked + 1 112 | return true 113 | elseif checked == 1 then 114 | checked = checked + 1 115 | return false 116 | elseif checked == 2 then 117 | checked = checked + 1 118 | ---@diagnostic disable-next-line: return-type-mismatch 119 | return nil 120 | else 121 | return true 122 | end 123 | end, 124 | }) 125 | assert(type(lz.state["barz.nvim"]) == "table") 126 | lz.trigger_load("barz.nvim") 127 | assert(type(lz.state["barz.nvim"]) == "table") 128 | lz.trigger_load("barz.nvim") 129 | assert(type(lz.state["barz.nvim"]) == "table") 130 | lz.trigger_load("barz.nvim") 131 | assert(lz.state("barz.nvim") == false) 132 | end) 133 | it("handles errors in startup plugins without stopping", function() 134 | local spy_load = spy.on(loader, "load") 135 | pcall(lz.load, { 136 | { 137 | "rustyness", 138 | load = function() 139 | error("I shouldn't break anything") 140 | end, 141 | }, 142 | { 143 | "create", 144 | }, 145 | { 146 | "noscope.nvim", 147 | }, 148 | }) 149 | assert.spy(spy_load).called(3) 150 | end) 151 | end) 152 | end) 153 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "Add laziness to your favourite plugin manager!"; 3 | nixConfig = { 4 | extra-substituters = [ 5 | "https://nix-community.cachix.org" 6 | ]; 7 | extra-trusted-public-keys = [ 8 | "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=" 9 | ]; 10 | }; 11 | 12 | inputs = { 13 | nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; 14 | 15 | flake-parts.url = "github:hercules-ci/flake-parts"; 16 | 17 | pre-commit-hooks = { 18 | url = "github:cachix/pre-commit-hooks.nix"; 19 | }; 20 | 21 | neorocks.url = "github:nvim-neorocks/neorocks"; 22 | 23 | gen-luarc.url = "github:mrcjkb/nix-gen-luarc-json"; 24 | }; 25 | 26 | outputs = inputs @ { 27 | self, 28 | nixpkgs, 29 | flake-parts, 30 | pre-commit-hooks, 31 | neorocks, 32 | gen-luarc, 33 | ... 34 | }: let 35 | name = "lze"; 36 | 37 | pkg-overlay = import ./nix/pkg-overlay.nix { 38 | inherit name self; 39 | }; 40 | in 41 | flake-parts.lib.mkFlake {inherit inputs;} { 42 | systems = [ 43 | "x86_64-linux" 44 | "aarch64-linux" 45 | "x86_64-darwin" 46 | "aarch64-darwin" 47 | ]; 48 | perSystem = { 49 | config, 50 | self', 51 | inputs', 52 | system, 53 | ... 54 | }: let 55 | ci-overlay = import ./nix/ci-overlay.nix { 56 | inherit self; 57 | plugin-name = name; 58 | }; 59 | 60 | pkgs = import nixpkgs { 61 | inherit system; 62 | overlays = [ 63 | gen-luarc.overlays.default 64 | neorocks.overlays.default 65 | ci-overlay 66 | pkg-overlay 67 | ]; 68 | }; 69 | 70 | luarc = pkgs.mk-luarc { 71 | nvim = pkgs.neovim-nightly; 72 | }; 73 | luarccurrent = pkgs.mk-luarc { 74 | nvim = pkgs.neovim; 75 | }; 76 | 77 | type-check-nightly = pre-commit-hooks.lib.${system}.run { 78 | src = self; 79 | hooks = { 80 | lua-ls = { 81 | enable = true; 82 | settings.configuration = luarc; 83 | }; 84 | }; 85 | }; 86 | 87 | pre-commit-check = pre-commit-hooks.lib.${system}.run { 88 | src = self; 89 | hooks = { 90 | alejandra.enable = true; 91 | stylua.enable = true; 92 | luacheck = { 93 | enable = true; 94 | }; 95 | lua-ls = { 96 | enable = true; 97 | settings.configuration = luarccurrent; 98 | }; 99 | editorconfig-checker.enable = true; 100 | markdownlint = { 101 | enable = true; 102 | settings.configuration = { 103 | MD028 = false; 104 | MD060 = false; 105 | }; 106 | excludes = [ 107 | "CHANGELOG.md" 108 | ]; 109 | }; 110 | lemmy-docgen = let 111 | genpath = pkgs.lib.makeBinPath (with pkgs; [ 112 | gawk 113 | git 114 | gnused 115 | coreutils 116 | lemmy-help 117 | ]); 118 | lemmyscript = pkgs.writeShellScript "lemmy-helper" '' 119 | export PATH="${genpath}:$PATH" 120 | gitroot="$(git rev-parse --show-toplevel)" 121 | if [ -z "$gitroot" ]; then 122 | echo "Error: Unable to determine Git root." 123 | exit 1 124 | fi 125 | maindoc="$(realpath "$gitroot/doc/lze.txt")" 126 | luamain="$(realpath "$gitroot/lua/lze/init.lua")" 127 | luameta="$(realpath "$gitroot/lua/lze/meta.lua")" 128 | mkdir -p "$(dirname "$maindoc")" 129 | export DOCOUT=$(mktemp) 130 | lemmy-help "$luamain" > "$DOCOUT" 131 | export BASHCACHE=$(mktemp) 132 | modeline="vim:tw=78:ts=8:noet:ft=help:norl:" 133 | sed "/$modeline/d" "$DOCOUT" > $BASHCACHE 134 | echo " *lze.types*" >> $BASHCACHE 135 | echo "" >> $BASHCACHE 136 | echo ">lua" >> $BASHCACHE 137 | awk '{print " " $0}' "$luameta" >> $BASHCACHE 138 | echo "<" >> $BASHCACHE 139 | echo "==============================================================================" >> $BASHCACHE 140 | echo "$modeline" >> $BASHCACHE 141 | cat "$BASHCACHE" > "$maindoc" 142 | rm "$BASHCACHE" 143 | rm "$DOCOUT" 144 | ''; 145 | in { 146 | enable = true; 147 | name = "lemmy-docgen"; 148 | entry = "${lemmyscript}"; 149 | }; 150 | }; 151 | }; 152 | 153 | devShell = pkgs.mkShell { 154 | name = "lze devShell"; 155 | DEVSHELL = 0; 156 | shellHook = '' 157 | ${pre-commit-check.shellHook} 158 | ln -fs ${pkgs.luarc-to-json luarc} .luarc.json 159 | ''; 160 | buildInputs = 161 | self.checks.${system}.pre-commit-check.enabledPackages 162 | ++ (with pkgs; [ 163 | lua-language-server 164 | busted-nlua 165 | ]); 166 | }; 167 | in { 168 | devShells = { 169 | default = devShell; 170 | inherit devShell; 171 | }; 172 | 173 | packages = rec { 174 | default = lze-vimPlugin; 175 | lze-luaPackage = pkgs.lua51Packages.${name}; 176 | lze-vimPlugin = pkgs.vimPlugins.${name}; 177 | }; 178 | 179 | checks = { 180 | inherit 181 | pre-commit-check 182 | type-check-nightly 183 | ; 184 | inherit 185 | (pkgs) 186 | nvim-nightly-tests 187 | ; 188 | }; 189 | }; 190 | flake = { 191 | overlays.default = pkg-overlay; 192 | }; 193 | }; 194 | } 195 | -------------------------------------------------------------------------------- /lua/lze/h/keys.lua: -------------------------------------------------------------------------------- 1 | -- NOTE: internal handlers must use internal trigger_load 2 | -- because require('lze') requires this module. 3 | local loader = require("lze.c.loader") 4 | 5 | local augroup = nil 6 | 7 | ---@param value lze.KeysSpec 8 | ---@param mode? string 9 | ---@return lze.Keys 10 | local function _parse(value, mode) 11 | local ret = vim.deepcopy(value) --[[@as lze.Keys]] 12 | ret.lhs = ret[1] or ret.lhs or "" 13 | ret.rhs = ret[2] or ret.rhs 14 | ret[1] = nil 15 | ret[2] = nil 16 | ret.mode = mode or "n" 17 | ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true) 18 | if ret.ft then 19 | local ft = type(ret.ft) == "string" and { ret.ft } or ret.ft --[[@as string[] ]] 20 | ret.id = ret.id .. " (" .. table.concat(ft, ", ") .. ")" 21 | end 22 | if ret.mode ~= "n" then 23 | ret.id = ret.id .. " (" .. ret.mode .. ")" 24 | end 25 | return ret 26 | end 27 | 28 | ---@param value string|lze.KeysSpec 29 | ---@return lze.Keys[] 30 | local function parse(value) 31 | value = type(value) == "string" and { value } or value --[[@as lze.KeysSpec]] 32 | local modes = type(value.mode) == "string" and { value.mode } or value.mode --[[ @as string[] | nil ]] 33 | if not modes then 34 | return { _parse(value) } 35 | end 36 | local ret = {} 37 | for _, mode in ipairs(modes) do 38 | table.insert(ret, _parse(value, mode)) 39 | end 40 | return ret 41 | end 42 | 43 | ---@param keys lze.Keys 44 | local function is_nop(keys) 45 | return type(keys.rhs) == "string" and (keys.rhs == "" or keys.rhs:lower() == "") 46 | end 47 | 48 | local states = {} 49 | 50 | ---@type lze.Handler 51 | local M = { 52 | spec_field = "keys", 53 | lib = { 54 | parse = parse, 55 | }, 56 | } 57 | 58 | function M.init() 59 | augroup = vim.api.nvim_create_augroup("lze_handler_keys_ft", { clear = true }) 60 | end 61 | 62 | local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true } 63 | 64 | ---@param keys lze.Keys 65 | ---@return lze.KeysBase 66 | local function get_opts(keys) 67 | ---@type lze.KeysBase 68 | local ret = {} 69 | for k, v in pairs(keys) do 70 | if type(k) ~= "number" and not skip[k] then 71 | ret[k] = v 72 | end 73 | end 74 | return ret 75 | end 76 | 77 | -- Create a mapping if it is managed by lze 78 | ---@param keys lze.Keys 79 | ---@param buf integer? 80 | local function set(keys, buf) 81 | if keys.rhs then 82 | local opts = get_opts(keys) 83 | ---@diagnostic disable-next-line: inject-field 84 | opts.buffer = buf 85 | vim.keymap.set(keys.mode, keys.lhs, keys.rhs, opts) 86 | end 87 | end 88 | 89 | ---@param keys lze.Keys 90 | local function add_keys(keys) 91 | local del = function() 92 | pcall(vim.keymap.del, keys.mode, keys.lhs, { 93 | -- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer 94 | -- So the mapping could still exist in other buffers 95 | buffer = keys.ft and true or nil, 96 | }) 97 | end 98 | local lhs = keys.lhs 99 | local opts = get_opts(keys) 100 | 101 | ---@param buf? number 102 | local function add(buf) 103 | if is_nop(keys) then 104 | return set(keys, buf) 105 | end 106 | vim.keymap.set(keys.mode, lhs, function() 107 | local plugins = states[keys.id] 108 | -- always delete the mapping immediately to prevent recursive mappings 109 | del() 110 | -- make sure to create global mappings when needed 111 | -- buffer-local mappings are managed by lze 112 | if not keys.ft then 113 | set(keys) 114 | end 115 | states[keys.id] = nil 116 | if plugins then 117 | loader.load(vim.tbl_keys(plugins)) 118 | end 119 | -- Create the real buffer-local mapping 120 | if keys.ft then 121 | set(keys, buf) 122 | end 123 | if keys.mode:sub(-1) == "a" then 124 | lhs = lhs .. "" 125 | end 126 | local feed = vim.api.nvim_replace_termcodes("" .. lhs, true, true, true) 127 | -- insert instead of append the lhs 128 | vim.api.nvim_feedkeys(feed, "i", false) 129 | end, { 130 | desc = opts.desc, 131 | nowait = opts.nowait, 132 | -- we do not return anything, but this is still needed to make operator pending mappings work 133 | expr = true, 134 | buffer = buf, 135 | }) 136 | end 137 | -- buffer-local mappings 138 | if keys.ft then 139 | vim.api.nvim_create_autocmd("FileType", { 140 | group = augroup, 141 | pattern = keys.ft, 142 | nested = true, 143 | callback = function(event) 144 | if states[keys.id] then 145 | add(event.buf) 146 | else 147 | -- Only create the mapping if its managed by lze 148 | -- otherwise the plugin is supposed to manage it 149 | set(keys, event.buf) 150 | end 151 | end, 152 | }) 153 | else 154 | add() 155 | end 156 | return del 157 | end 158 | 159 | ---@param plugin lze.Plugin 160 | function M.add(plugin) 161 | local keys_spec = plugin.keys 162 | if not keys_spec then 163 | return 164 | end 165 | local keys_def = {} 166 | if type(keys_spec) == "string" then 167 | local keys = parse(keys_spec) 168 | vim.list_extend(keys_def, keys) 169 | elseif type(keys_spec) == "table" then 170 | ---@param keys_spec_ string | lze.KeysSpec 171 | for _, keys_spec_ in ipairs(keys_spec) do 172 | local keys = parse(keys_spec_) 173 | vim.list_extend(keys_def, keys) 174 | end 175 | end 176 | ---@param key lze.Keys 177 | for _, key in ipairs(keys_def or {}) do 178 | states[key.id] = states[key.id] or {} 179 | states[key.id][plugin.name] = add_keys(key) 180 | end 181 | end 182 | 183 | ---@param name string 184 | function M.before(name) 185 | for _, plugins in pairs(states) do 186 | plugins[name] = nil 187 | end 188 | end 189 | 190 | function M.cleanup() 191 | if augroup then 192 | vim.api.nvim_del_augroup_by_id(augroup) 193 | end 194 | for _, plugins in pairs(states) do 195 | for _, del in pairs(plugins) do 196 | del() 197 | end 198 | end 199 | states = {} 200 | end 201 | 202 | return M 203 | -------------------------------------------------------------------------------- /lua/lze/meta.lua: -------------------------------------------------------------------------------- 1 | ---@meta 2 | error("Cannot import a meta module") 3 | 4 | ---@class lze.PluginBase 5 | --- 6 | ---Whether to enable this plugin. Useful to disable plugins under certain conditions. 7 | ---@field enabled? boolean|(fun():boolean) 8 | --- 9 | ---Only useful for lazy=false plugins to force loading certain plugins first. 10 | ---Default priority is 50 11 | ---@field priority? number 12 | --- 13 | ---Set this to override the `load` function for an individual plugin. 14 | ---Defaults to `vim.g.lze.load()`, see |lze.Config|. 15 | ---@field load? fun(name: string) 16 | --- 17 | ---True will allow a plugin to be added to the queue again after it has already been triggered. 18 | ---@field allow_again? boolean 19 | --- 20 | ---Will be run before loading any plugins in that require('lze').load() call 21 | ---@field beforeAll? fun(self:lze.Plugin) 22 | --- 23 | ---Will be run before loading this plugin 24 | ---@field before? fun(self:lze.Plugin) 25 | --- 26 | ---Will be executed after loading this plugin 27 | ---@field after? fun(self:lze.Plugin) 28 | --- 29 | ---Whether to lazy-load this plugin. Defaults to `false`. 30 | ---Using a handler's field sets this automatically. 31 | ---@field lazy? boolean 32 | 33 | -- NOTE: 34 | -- Builtin Handler Types: 35 | 36 | ---@class lze.KeysBase: vim.keymap.set.Opts 37 | ---@field desc? string 38 | ---@field noremap? boolean 39 | ---@field remap? boolean 40 | ---@field expr? boolean 41 | ---@field nowait? boolean 42 | ---@field ft? string|string[] 43 | 44 | ---@class lze.KeysSpec: lze.KeysBase 45 | ---@field [1]? string [1] or lhs required, [1] wins 46 | ---@field [2]? string|fun()|false will override rhs 47 | ---@field lhs? string [1] or lhs required, [1] wins 48 | ---@field rhs? string|fun()|false [2] will override rhs if set 49 | ---@field mode? string|string[] 50 | 51 | ---@class lze.Keys: lze.KeysBase 52 | ---@field lhs string lhs 53 | ---@field rhs? string|fun() rhs 54 | ---@field mode? string 55 | ---@field id string 56 | ---@field name string 57 | 58 | ---@alias lze.Event {id:string, event:string[]|string, pattern?:string[]|string, augroup:integer} 59 | ---@alias lze.EventSpec string|{event?:string|string[], pattern?:string|string[], augroup?:integer}|string[] 60 | 61 | ---@class lze.SpecHandlers 62 | --- 63 | ---Load a plugin on one or more |autocmd-events|. 64 | ---@field event? string|lze.EventSpec[] 65 | --- 66 | ---Load a plugin on one or more |user-commands|. 67 | ---@field cmd? string[]|string 68 | --- 69 | ---Load a plugin on one or more |FileType| events. 70 | ---@field ft? string[]|string 71 | --- 72 | ---Load a plugin on one or more |key-mapping|s. 73 | ---@field keys? string|string[]|lze.KeysSpec[] 74 | --- 75 | ---Load a plugin on one or more |colorscheme| events. 76 | ---@field colorscheme? string[]|string 77 | --- 78 | ---Load a plugin before load of one or more other plugins. 79 | ---@field dep_of? string[]|string 80 | --- 81 | ---Load a plugin after load of one or more other plugins. 82 | ---@field on_plugin? string[]|string 83 | --- 84 | ---Accepts a top-level lua module name or a 85 | ---list of top-level lua module names. 86 | ---Will load when any submodule of those listed is `require`d 87 | ---@field on_require? string[]|string 88 | --- 89 | ---@field [string] any? Fields for any extra handlers you might add 90 | 91 | -- NOTE: 92 | -- Defintion of lze.Plugin and lze.PluginSpec 93 | -- combining above types. 94 | 95 | ---Internal lze.Plugin type, after being parsed. 96 | ---Is the type passed to handlers in modify and add hooks. 97 | ---@class lze.Plugin: lze.PluginBase,lze.SpecHandlers 98 | ---The plugin name (not its main module), e.g. "sweetie.nvim" 99 | ---@field name string 100 | 101 | ---The lze.PluginSpec type, passed to require('lze').load() as entries in lze.Spec 102 | ---@class lze.PluginSpec: lze.PluginBase,lze.SpecHandlers 103 | ---The plugin name (not its main module), e.g. "sweetie.nvim" 104 | ---@field [1] string 105 | 106 | ---@class lze.SpecImport 107 | ---@field import string|lze.Spec spec module to import 108 | ---@field enabled? boolean|(fun():boolean) 109 | 110 | ---List of lze.PluginSpec and/or lze.SpecImport 111 | ---@alias lze.Spec lze.PluginSpec | lze.Plugin | lze.SpecImport | lze.Spec[] 112 | 113 | -- NOTE: 114 | -- lze.Handler type definition 115 | 116 | ---Listed in the order they are called by lze if the handler has been registered 117 | ---@class lze.Handler 118 | ---@field spec_field string 119 | --- 120 | ---Your handler's 1 chance to modify plugins before they are entered into state! 121 | ---Called only if your field was present. 122 | ---@field modify? fun(plugin: lze.Plugin): lze.Plugin, fun()? 123 | --- 124 | ---Called after being entered into state but before any loading has occurred 125 | ---@field add? fun(plugin: lze.Plugin): fun()? 126 | --- 127 | ---Whether using this handler's field should have an effect on the lazy setting 128 | ---True or nil is true 129 | ---Default: nil 130 | ---@field set_lazy? boolean 131 | --- 132 | ---Handlers may export functions and other values via this set, 133 | ---which then may be accessed via `require('lze').h[spec_field].your_func()` 134 | ---@field lib? table 135 | ---Called when the handler is registered 136 | ---@field init? fun() 137 | ---Allows you to clean up any global modifications your handler makes to the environment. 138 | ---@field cleanup? fun() 139 | --- 140 | ---runs at the end of require('lze').load() 141 | ---for handlers to set up extra triggers such as the 142 | ---event handler's DeferredUIEnter event 143 | ---@field post_def? fun() 144 | --- 145 | ---Plugin's before will run first 146 | ---@field before? fun(name: string) 147 | ---Plugin's load will run here 148 | ---@field after? fun(name: string) 149 | ---Plugin's after will run after 150 | 151 | ---Optional, for passing to register_handlers, 152 | ---if easily changing if a handler gets added or not is desired 153 | ---@class lze.HandlerSpec 154 | ---@field handler lze.Handler 155 | ---@field enabled? boolean|(fun():boolean) 156 | 157 | -- NOTE: 158 | -- global vim.g.lze config values table 159 | 160 | ---@class lze.Config 161 | --- 162 | ---Default callback used to load a plugin. 163 | ---Takes the plugin name (not the module name). Defaults to |packadd| if not set. 164 | ---Not provided to handler hooks 165 | ---@field load? fun(name: string) 166 | --- 167 | ---Allows you to inject a default value 168 | ---for any non-nil plugin spec field 169 | ---which will be visible to all handler hooks. 170 | ---@field injects? table 171 | --- 172 | ---If false, lze will print error messages on fewer things. 173 | ---@field verbose? boolean 174 | --- 175 | ---Default priority for startup plugins. Defaults to 50 if unset 176 | ---@field default_priority? integer 177 | --- 178 | ---If true, lze will not automatically register the default handlers. 179 | ---@field without_default_handlers? boolean 180 | 181 | ---@type lze.Config 182 | vim.g.lze = vim.g.lze 183 | -------------------------------------------------------------------------------- /lua/lze/h/event.lua: -------------------------------------------------------------------------------- 1 | -- NOTE: internal handlers must use internal trigger_load 2 | -- because require('lze') requires this module. 3 | local loader = require("lze.c.loader") 4 | 5 | ---@class lze.EventOpts 6 | ---@field event string 7 | ---@field group? string 8 | ---@field exclude? string[] augroups to exclude 9 | ---@field data? unknown 10 | ---@field buffer? number 11 | 12 | local alias_events = { 13 | DeferredUIEnter = { id = "User DeferredUIEnter", event = "User", pattern = "DeferredUIEnter" }, 14 | } 15 | 16 | local pending = {} 17 | ---@type integer 18 | local augroup = nil 19 | local deferred_enter_event_id = nil 20 | 21 | ---@type fun(spec: lze.EventSpec): lze.Event 22 | local parse = function(spec) 23 | local ret = alias_events[spec] 24 | if ret then 25 | ret.augroup = ret.augroup or augroup 26 | return ret 27 | end 28 | if type(spec) == "string" then 29 | local event, pattern = spec:match("^(%w+)%s+(.*)$") 30 | event = event or spec 31 | return { id = spec, event = event, pattern = pattern, augroup = augroup } 32 | elseif vim.islist(spec) then 33 | ret = { id = table.concat(spec, "|"), event = spec, augroup = augroup } 34 | else 35 | ret = spec --[[@as lze.Event]] 36 | if not ret.id then 37 | ---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch 38 | ret.id = type(ret.event) == "string" and ret.event 39 | or ret.event == nil and "*" 40 | ---@diagnostic disable-next-line: param-type-mismatch 41 | or table.concat(ret.event, "|") 42 | if ret.pattern then 43 | ---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch 44 | ret.id = ret.id 45 | .. " " 46 | .. ( 47 | type(ret.pattern) == "string" and ret.pattern 48 | or table.concat(ret.pattern --[[@as table]], ", ") 49 | ) 50 | end 51 | end 52 | ret.augroup = ret.augroup or augroup 53 | end 54 | return ret 55 | end 56 | 57 | ---@type lze.Handler 58 | local M = { 59 | spec_field = "event", 60 | lib = { 61 | parse = parse, 62 | ---@param name string 63 | ---@param spec lze.EventSpec 64 | set_event_alias = function(name, spec) 65 | alias_events[name] = spec and parse(spec) or nil 66 | end, 67 | }, 68 | } 69 | 70 | local deferred_ui_enter = vim.schedule_wrap(function() 71 | if vim.v.exiting ~= vim.NIL then 72 | return 73 | end 74 | vim.api.nvim_exec_autocmds("User", { pattern = "DeferredUIEnter", modeline = false }) 75 | end) 76 | 77 | function M.init() 78 | augroup = vim.api.nvim_create_augroup("lze_handler_event", { clear = true }) 79 | deferred_enter_event_id = vim.api.nvim_create_autocmd("UIEnter", { 80 | once = true, 81 | nested = true, 82 | callback = deferred_ui_enter, 83 | }) 84 | end 85 | 86 | function M.post_def() 87 | if vim.v.vim_did_enter == 1 then 88 | deferred_ui_enter() 89 | end 90 | end 91 | 92 | -- Get all augroups for an event 93 | ---@param event string 94 | ---@return string[] 95 | local function get_augroups(event) 96 | local ret = {} 97 | for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event })) do 98 | if autocmd.group_name ~= nil then 99 | table.insert(ret, autocmd.group_name) 100 | end 101 | end 102 | return ret 103 | end 104 | 105 | -- Get the current state of the event and all the events that will be fired 106 | ---@param event string 107 | ---@param buf integer 108 | ---@param data unknown 109 | ---@return lze.EventOpts 110 | local function get_state(event, buf, data) 111 | ---@type lze.EventOpts 112 | return { 113 | event = event, 114 | exclude = get_augroups(event), 115 | buffer = buf, 116 | data = data, 117 | } 118 | end 119 | 120 | -- Trigger an event 121 | ---@param opts lze.EventOpts 122 | local function _trigger(opts) 123 | xpcall( 124 | function() 125 | vim.api.nvim_exec_autocmds(opts.event, { 126 | buffer = opts.buffer, 127 | group = opts.group, 128 | modeline = false, 129 | data = opts.data, 130 | }) 131 | end, 132 | vim.schedule_wrap(function(err) 133 | vim.notify(err, vim.log.levels.ERROR) 134 | end) 135 | ) 136 | end 137 | 138 | -- Trigger an event. When a group is given, only the events in that group will be triggered. 139 | -- When exclude is set, the events in those groups will be skipped. 140 | ---@param opts lze.EventOpts 141 | local function trigger(opts) 142 | if opts.group or opts.exclude == nil then 143 | return _trigger(opts) 144 | end 145 | ---@type table 146 | local done = {} 147 | for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = opts.event })) do 148 | local id = autocmd.event .. ":" .. (autocmd.group or "") ---@type string 149 | local skip = done[id] or (opts.exclude and vim.tbl_contains(opts.exclude, autocmd.group_name)) 150 | done[id] = true 151 | if autocmd.group and not skip then 152 | ---@diagnostic disable-next-line: assign-type-mismatch 153 | opts.group = autocmd.group_name 154 | _trigger(opts) 155 | end 156 | end 157 | end 158 | 159 | ---@param event lze.Event 160 | local function add_event(event) 161 | local done = false 162 | vim.api.nvim_create_autocmd(event.event, { 163 | group = event.augroup, 164 | once = true, 165 | nested = true, 166 | pattern = event.pattern, 167 | callback = function(ev) 168 | if done or not pending[event.id] then 169 | return 170 | end 171 | -- HACK: work-around for https://github.com/neovim/neovim/issues/25526 172 | done = true 173 | local state = get_state(ev.event, ev.buf, ev.data) 174 | -- load the plugins 175 | loader.load(vim.tbl_keys(pending[event.id])) 176 | -- check if any plugin created an event handler for this event and fire the group 177 | trigger(state) 178 | end, 179 | }) 180 | end 181 | 182 | ---@param plugin lze.Plugin 183 | function M.add(plugin) 184 | local event_spec = plugin.event 185 | if not event_spec then 186 | return 187 | end 188 | local event_def = {} 189 | if type(event_spec) == "string" then 190 | local event = parse(event_spec) 191 | table.insert(event_def, event) 192 | elseif type(event_spec) == "table" then 193 | ---@param ev lze.EventSpec[] 194 | for _, ev in ipairs(event_spec) do 195 | local event = parse(ev) 196 | table.insert(event_def, event) 197 | end 198 | end 199 | ---@param event lze.Event 200 | for _, event in ipairs(event_def or {}) do 201 | pending[event.id] = pending[event.id] or {} 202 | pending[event.id][plugin.name] = event.augroup 203 | add_event(event) 204 | end 205 | end 206 | 207 | ---@param name string 208 | function M.before(name) 209 | for _, plugins in pairs(pending) do 210 | plugins[name] = nil 211 | end 212 | end 213 | 214 | function M.cleanup() 215 | for _, plugins in pairs(pending) do 216 | for k, p in pairs(plugins) do 217 | if p == augroup then 218 | plugins[k] = nil 219 | end 220 | end 221 | end 222 | if augroup then 223 | vim.api.nvim_del_augroup_by_id(augroup) 224 | end 225 | if deferred_enter_event_id then 226 | vim.api.nvim_del_autocmd(deferred_enter_event_id) 227 | end 228 | end 229 | 230 | return M 231 | -------------------------------------------------------------------------------- /lua/lze/c/handler.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | ---@type lze.Handler[] 4 | local handlers = vim.tbl_get(vim.g, "lze", "without_default_handlers") and {} or require("lze.h") 5 | for _, handler in ipairs(handlers) do 6 | if handler.init then 7 | handler.init() 8 | end 9 | end 10 | 11 | local lib_meta = { 12 | __index = function(t, k) 13 | local h_lib 14 | for _, handler in ipairs(handlers) do 15 | if handler.spec_field == k then 16 | h_lib = handler.lib 17 | break 18 | end 19 | end 20 | if type(h_lib) ~= "table" then 21 | vim.schedule(function() 22 | vim.notify( 23 | "handler '" .. k .. "' does not export any lib functions or is not registered", 24 | vim.log.levels.ERROR, 25 | { title = "lze" } 26 | ) 27 | end) 28 | return nil 29 | end 30 | rawset(t, k, h_lib) 31 | return h_lib 32 | end, 33 | __newindex = function(t, k, v) 34 | rawset(t, k, v) 35 | end, 36 | } 37 | 38 | M.libs = setmetatable({}, lib_meta) 39 | 40 | ---Removes and returns all handlers 41 | ---@return lze.Handler[] 42 | function M.clear_handlers() 43 | if vim.tbl_get(vim.g, "lze", "verbose") ~= false and #require("lze.c.loader").state ~= 0 then 44 | vim.schedule(function() 45 | vim.notify( 46 | "removing handlers while there are pending plugin specs may produce unpredictable behavior\n" 47 | .. "If you know what you are doing, set vim.g.lze.verbose = false", 48 | vim.log.levels.ERROR, 49 | { title = "lze" } 50 | ) 51 | end) 52 | end 53 | for _, handler in ipairs(handlers) do 54 | if handler.cleanup then 55 | handler.cleanup() 56 | end 57 | end 58 | local old_handlers = handlers 59 | handlers = {} 60 | M.libs = setmetatable({}, lib_meta) 61 | return old_handlers 62 | end 63 | 64 | ---Removes and returns the named handlers (named by spec_field) 65 | ---@param names string|string[] 66 | ---@return lze.Handler[] 67 | function M.remove_handlers(names) 68 | if vim.tbl_get(vim.g, "lze", "verbose") ~= false and #require("lze.c.loader").state ~= 0 then 69 | vim.schedule(function() 70 | vim.notify( 71 | "removing handlers while there are pending plugin specs may produce unpredictable behavior\n" 72 | .. "If you know what you are doing, set vim.g.lze.verbose = false", 73 | vim.log.levels.ERROR, 74 | { title = "lze" } 75 | ) 76 | end) 77 | end 78 | names = type(names) ~= "table" and { names } or names 79 | ---@cast names string[] 80 | local removed_handlers = {} 81 | for _, name in ipairs(names) do 82 | for i, handler in ipairs(handlers) do 83 | if handler.spec_field == name then 84 | if handler.cleanup then 85 | handler.cleanup() 86 | end 87 | table.remove(handlers, i) 88 | M.libs[name] = nil 89 | table.insert(removed_handlers, handler) 90 | break 91 | end 92 | end 93 | end 94 | return removed_handlers 95 | end 96 | 97 | ---@param spec lze.Plugin|lze.HandlerSpec 98 | local function is_disabled(spec) 99 | return spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) 100 | end 101 | 102 | ---Register new handlers. 103 | ---Will refuse duplicates, and return a list of added handler spec_fields 104 | ---@param handler_list lze.Handler[]|lze.Handler|lze.HandlerSpec[]|lze.HandlerSpec 105 | ---@return string[] 106 | function M.register_handlers(handler_list) 107 | assert(type(handler_list) == "table", "invalid argument to lze.register_handlers") 108 | if handler_list.spec_field or handler_list.handler then 109 | handler_list = { handler_list } 110 | end 111 | 112 | local existing_handler_fields = {} 113 | for _, hndl in ipairs(handlers) do 114 | existing_handler_fields[hndl.spec_field] = true 115 | end 116 | local added_names = {} 117 | for _, spec in ipairs(handler_list) do 118 | local handler = spec.spec_field and spec 119 | ---@cast spec lze.HandlerSpec 120 | or spec.handler and not is_disabled(spec) and spec.handler 121 | local spec_field = handler and handler.spec_field or nil 122 | if spec_field and not existing_handler_fields[spec_field] then 123 | existing_handler_fields[spec_field] = true 124 | table.insert(added_names, spec_field) 125 | table.insert(handlers, handler) 126 | if handler.init then 127 | handler.init() 128 | end 129 | end 130 | end 131 | return added_names 132 | end 133 | 134 | ---gets value for plugin.lazy by checking handler field useage 135 | ---@param spec lze.Plugin 136 | ---@return boolean 137 | function M.is_lazy(spec) 138 | for _, hndl in ipairs(handlers) do 139 | if hndl.set_lazy ~= false and spec[hndl.spec_field] ~= nil then 140 | return true 141 | end 142 | end 143 | return false 144 | end 145 | 146 | ---modify is called if the handler has a modify hook 147 | ---and then spec_field for that handler on the plugin 148 | ---is not nil 149 | ---@param plugin lze.Plugin 150 | ---@param delay fun(f: fun()) 151 | ---@return lze.Plugin 152 | function M.run_modify(plugin, delay) 153 | ---@param hndl lze.Handler 154 | ---@param p lze.Plugin 155 | ---@return any 156 | local function if_has_run(hndl, p) 157 | if not hndl.modify then 158 | return p 159 | end 160 | if p[hndl.spec_field] == nil then 161 | return p 162 | end 163 | if is_disabled(p) then 164 | return p 165 | end 166 | local ret, delayed = hndl.modify(p) 167 | if delayed then 168 | delay(delayed) 169 | end 170 | return ret 171 | end 172 | local res = plugin 173 | for _, hndl in ipairs(handlers) do 174 | res = if_has_run(hndl, res) 175 | end 176 | return res 177 | end 178 | 179 | ---handlers can set up any of their own triggers for themselves here 180 | ---such as things like the event handler's DeferredUIEnter event 181 | function M.run_post_def() 182 | for _, handler in ipairs(handlers) do 183 | if handler.post_def then 184 | handler.post_def() 185 | end 186 | end 187 | end 188 | 189 | ---called after the plugin's load hook 190 | ---but before its after hook 191 | ---@param name string 192 | function M.run_after(name) 193 | for _, handler in ipairs(handlers) do 194 | if handler.after then 195 | handler.after(name) 196 | end 197 | end 198 | end 199 | 200 | ---called before the plugin's load hook 201 | ---but after its before hook 202 | ---@param name string 203 | function M.run_before(name) 204 | for _, handler in ipairs(handlers) do 205 | if handler.before then 206 | handler.before(name) 207 | end 208 | end 209 | end 210 | 211 | -- calls add for all the handlers, each handler gets a copy, so that they 212 | -- cannot change the plugin object recieved by the other handlers 213 | -- outside of the modify step 214 | ---@param plugins lze.Plugin[] 215 | ---@param delay fun(f: fun()) 216 | function M.init(plugins, delay) 217 | for _, plugin in ipairs(plugins) do 218 | for _, handler in ipairs(handlers) do 219 | if handler.add then 220 | local delayed = handler.add(vim.deepcopy(plugin)) 221 | if delayed then 222 | delay(delayed) 223 | end 224 | end 225 | end 226 | end 227 | end 228 | 229 | return M 230 | -------------------------------------------------------------------------------- /lua/lze/c/loader.lua: -------------------------------------------------------------------------------- 1 | ---@param spec lze.Plugin 2 | local function is_enabled(spec) 3 | local disabled = spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) 4 | return not disabled 5 | end 6 | 7 | ---@param hook_key "before" | "after" | "beforeAll" | "load" 8 | ---@param plugin lze.Plugin 9 | ---@param arg? any 10 | local function hook(hook_key, plugin, arg) 11 | if plugin[hook_key] then 12 | xpcall( 13 | plugin[hook_key], 14 | vim.schedule_wrap(function(err) 15 | if hook_key ~= "load" or vim.tbl_get(vim.g, "lze", "verbose") ~= false then 16 | vim.notify( 17 | "Failed to run '" .. hook_key .. "' hook for " .. plugin.name .. ": " .. tostring(err or ""), 18 | vim.log.levels.ERROR, 19 | { title = "lze.spec." .. hook_key } 20 | ) 21 | end 22 | end), 23 | -- NOTE: Why do I need to tell lua_ls that xpcall takes varargs to pass my tests???? 24 | ---@diagnostic disable-next-line: redundant-parameter 25 | arg or plugin 26 | ) 27 | end 28 | end 29 | 30 | ---@mod lze.loader 31 | local M = {} 32 | 33 | ---@type table 34 | local state = {} 35 | 36 | -- NOTE: must be userdata rather than table 37 | -- otherwise __len never gets called 38 | -- because tables cache their length 39 | -- but this only works with luajit 40 | -- so if you change to non-luajit, 41 | -- just make it a table and accept that # will always return 0. 42 | 43 | -- also tell lua_ls to be quiet about that on 5.4 so the test suite doesnt complain 44 | ---@diagnostic disable-next-line: deprecated 45 | local proxy = newproxy and newproxy(true) or {} 46 | local proxy_mt = getmetatable(proxy) 47 | proxy_mt.__index = function(_, k) 48 | return vim.deepcopy(state[k]) 49 | end 50 | proxy_mt.__tostring = function() 51 | return vim.inspect(state) 52 | end 53 | proxy_mt.__len = function(_) 54 | local count = 0 55 | for _, v in pairs(state) do 56 | if v then 57 | count = count + 1 58 | end 59 | end 60 | return count 61 | end 62 | ---@param name string 63 | ---@return boolean? 64 | proxy_mt.__call = function(_, name) 65 | ---@diagnostic disable-next-line: return-type-mismatch 66 | return state[name] and true or state[name] 67 | end 68 | proxy_mt.__unm = function(_) 69 | return vim.deepcopy(state) 70 | end 71 | proxy_mt.__newindex = function(_, k, v) 72 | vim.schedule(function() 73 | vim.notify( 74 | "Arbitrary modification of lze's internal state is not allowed outside of an lze.Handler's modify hook.\n" 75 | .. "value: '" 76 | .. vim.inspect(v) 77 | .. "' NOT added to require('lze').state." 78 | .. tostring(k), 79 | vim.log.levels.ERROR, 80 | { title = "lze.state" } 81 | ) 82 | end) 83 | end 84 | 85 | ---@type lze.State 86 | ---@diagnostic disable-next-line: assign-type-mismatch 87 | M.state = proxy 88 | 89 | ---@type fun(plugin_names: string[]|string): string[] 90 | function M.load(plugin_names) 91 | plugin_names = (type(plugin_names) == "string") and { plugin_names } or plugin_names 92 | ---@type string[] 93 | local skipped = {} 94 | ---@cast plugin_names string[] 95 | for _, pname in ipairs(plugin_names) do 96 | local plugin = state[pname] 97 | if plugin and is_enabled(plugin) then 98 | state[pname] = false 99 | local load_impl = plugin.load or vim.tbl_get(vim.g, "lze", "load") or vim.cmd.packadd 100 | -- technically plugin spec before hooks can modify 101 | -- the plugin item provided to their own after hook 102 | -- This is not worth a deepcopy and should be considered a feature 103 | hook("before", plugin) 104 | require("lze.c.handler").run_before(pname) 105 | hook("load", { name = pname, load = load_impl }, pname) 106 | require("lze.c.handler").run_after(pname) 107 | hook("after", plugin) 108 | else 109 | if type(pname) ~= "string" then 110 | vim.notify( 111 | "Invalid plugin name recieved: " .. vim.inspect(pname), 112 | vim.log.levels.ERROR, 113 | { title = "lze.trigger_load" } 114 | ) 115 | else 116 | table.insert(skipped, pname) 117 | if plugin == nil and vim.tbl_get(vim.g, "lze", "verbose") ~= false then 118 | vim.schedule(function() 119 | vim.notify( 120 | "Plugin " .. pname .. " not found", 121 | vim.log.levels.ERROR, 122 | { title = "lze.trigger_load" } 123 | ) 124 | end) 125 | end 126 | end 127 | end 128 | end 129 | return skipped 130 | end 131 | 132 | local delayed = {} 133 | ---@param f fun() 134 | local function delay(f) 135 | table.insert(delayed, f) 136 | end 137 | local function run_delayed() 138 | local torun = delayed 139 | delayed = {} 140 | for _, f in ipairs(torun) do 141 | local ok, err = pcall(f) 142 | if not ok then 143 | vim.schedule(function() 144 | vim.notify( 145 | "Error occurred in deferred function returned by handler modify or add hook \nError text: " .. err, 146 | vim.log.levels.ERROR, 147 | { title = "lze.handler_deferred_fn" } 148 | ) 149 | end) 150 | end 151 | end 152 | end 153 | 154 | ---@param plugins lze.Plugin[] 155 | ---@param verbose boolean 156 | ---@return lze.Plugin[] final 157 | ---@return lze.Plugin[] disabled 158 | local function add(plugins, verbose) 159 | local final = {} 160 | local duplicates = {} 161 | local injects = vim.tbl_get(vim.g, "lze", "injects") or {} 162 | for _, v in ipairs(plugins) do 163 | local plugin = require("lze.c.handler").run_modify(vim.tbl_extend("keep", v, injects), delay) 164 | assert( 165 | type(plugin) == "table" and type(plugin.name) == "string", 166 | "handler modify hook must return a valid plugin" 167 | ) 168 | if plugin.lazy == nil then 169 | plugin.lazy = require("lze.c.handler").is_lazy(plugin) 170 | end 171 | local p = vim.deepcopy(plugin) 172 | if is_enabled(plugin) then 173 | -- deepcopy after all handlers use modify 174 | -- now we have a copy the handlers can't change 175 | local name = p.name 176 | if state[name] == nil then 177 | state[name] = p 178 | table.insert(final, p) 179 | elseif v.allow_again and not state[name] then 180 | state[name] = p 181 | table.insert(final, p) 182 | else 183 | table.insert(duplicates, p) 184 | if verbose then 185 | vim.schedule(function() 186 | vim.notify("attempted to add " .. p.name .. " twice", vim.log.levels.ERROR, { title = "lze" }) 187 | end) 188 | end 189 | end 190 | end 191 | end 192 | -- Return copy of result, and names of duplicates 193 | -- This prevents handlers from changing state after the handler's modify hooks 194 | return vim.deepcopy(final), duplicates 195 | end 196 | 197 | ---@param plugins lze.Plugin[] 198 | ---@param verbose boolean 199 | local function load_startup_plugins(plugins, verbose) 200 | local startups = {} 201 | local default_priority = vim.tbl_get(vim.g, "lze", "default_priority") or 50 202 | for _, plugin in ipairs(plugins) do 203 | if not plugin.lazy then 204 | table.insert(startups, { 205 | name = plugin.name, 206 | priority = plugin.priority or default_priority, 207 | }) 208 | end 209 | hook("beforeAll", plugin) 210 | end 211 | run_delayed() 212 | table.sort(startups, function(a, b) 213 | return a.priority > b.priority 214 | end) 215 | for _, plugin in ipairs(startups) do 216 | local ok, v = pcall(M.load, plugin.name) 217 | if verbose and not ok then 218 | vim.schedule(function() 219 | vim.notify( 220 | "Error occurred in '" .. plugin.name .. "'\nError text: " .. vim.inspect(v), 221 | vim.log.levels.ERROR, 222 | { title = "lze.load_startup_plugins" } 223 | ) 224 | end) 225 | end 226 | end 227 | end 228 | 229 | ---@type fun(spec: string|lze.Spec): lze.Plugin[] 230 | function M.define(spec) 231 | local verbose = vim.tbl_get(vim.g, "lze", "verbose") ~= false 232 | if type(spec) == "string" then 233 | spec = { import = spec } 234 | elseif spec == nil or type(spec) ~= "table" then 235 | if verbose then 236 | vim.schedule(function() 237 | vim.notify( 238 | "load was called with no arguments or invalid spec. Called with: " .. vim.inspect(spec), 239 | vim.log.levels.ERROR, 240 | { title = "lze" } 241 | ) 242 | end) 243 | end 244 | return {} 245 | end 246 | -- plugins that are parsed as disabled in this stage will not be included 247 | -- plugins that are parsed as enabled in this stage will remain in the queue 248 | -- until trigger_load is called AND the plugin is parsed as enabled at that time. 249 | local plugins = require("lze.c.parse")(spec) 250 | -- add non-duplicates to state. 251 | local final_plugins, duplicates = add(plugins, verbose) 252 | -- calls handler adds with copies to prevent handlers messing with each other 253 | require("lze.c.handler").init(final_plugins, delay) 254 | -- will call beforeAll of all plugin specs in the order passed in. 255 | -- will then call trigger_load on the non-lazy plugins 256 | -- in order of priority and the order passed in. 257 | load_startup_plugins(final_plugins, verbose) 258 | -- handlers can set up any of their own triggers for themselves here 259 | -- such as things like the event handler's DeferredUIEnter event 260 | require("lze.c.handler").run_post_def() 261 | return duplicates 262 | end 263 | 264 | return M 265 | -------------------------------------------------------------------------------- /doc/lze.txt: -------------------------------------------------------------------------------- 1 | ============================================================================== 2 | *lze* 3 | 4 | lze.register_handlers *lze.register_handlers* 5 | registers a handler with lze to add new spec fields 6 | Returns the list of spec_field values added. 7 | THIS SHOULD BE CALLED BEFORE ANY CALLS TO lze.load ARE MADE 8 | 9 | Type: ~ 10 | (fun(handlers:lze.Handler[]|lze.Handler|lze.HandlerSpec[]|lze.HandlerSpec):string[]) 11 | 12 | 13 | lze.clear_handlers *lze.clear_handlers* 14 | Returns the cleared handlers 15 | THIS SHOULD BE CALLED BEFORE ANY CALLS TO lze.load ARE MADE 16 | 17 | Type: ~ 18 | (fun():lze.Handler[]) 19 | 20 | 21 | lze.remove_handlers *lze.remove_handlers* 22 | Returns the cleared handlers 23 | THIS SHOULD BE CALLED BEFORE ANY CALLS TO lze.load ARE MADE 24 | 25 | Type: ~ 26 | (fun(handler_names:string|string[]):lze.Handler[]) 27 | 28 | 29 | lze.trigger_load *lze.trigger_load* 30 | Trigger loading of the lze.Plugin loading hooks. 31 | Used by handlers to load plugins. 32 | Will return the names of the plugins that were skipped, 33 | either because they were already loaded or because they 34 | were never added to the queue. 35 | 36 | Type: ~ 37 | (fun(plugin_names:string[]|string):string[]) 38 | 39 | 40 | lze.load *lze.load* 41 | May be called as many times as desired. 42 | Will return the duplicate lze.Plugin objects. 43 | Priority spec field only affects order for 44 | non-lazy plugins within a single load call. 45 | accepts a list of lze.Spec or a single lze.Spec 46 | may accept a module name to require instead 47 | 48 | Type: ~ 49 | (fun(spec:string|lze.Spec):lze.Plugin[]) 50 | 51 | 52 | lze.State *lze.State* 53 | `false` for already loaded (or being loaded currently), 54 | `nil` for never added. READ ONLY TABLE 55 | Function access only checks; table access returns a COPY. 56 | unary minus is defined as vim.deepcopy of the actual state table 57 | local snapshot = -require('lze').state 58 | whereas the following will return this object, and thus remain up to date 59 | local state = require('lze').state 60 | 61 | Variants: ~ 62 | 63 | 64 | 65 | lze.state *lze.state* 66 | | fun(name: string): boolean? # Faster, returns `boolean?`. 67 | | table # Access returns COPY of state for that plugin name. 68 | 69 | Type: ~ 70 | (lze.State) 71 | 72 | 73 | lze.h *lze.h* 74 | Handlers may expose things by registering them in their lib table 75 | require('lze').h.. 76 | wont populate unless used, will be removed if handler is removed 77 | 78 | Type: ~ 79 | (table) 80 | 81 | 82 | *lze.types* 83 | 84 | >lua 85 | ---@meta 86 | error("Cannot import a meta module") 87 | 88 | ---@class lze.PluginBase 89 | --- 90 | ---Whether to enable this plugin. Useful to disable plugins under certain conditions. 91 | ---@field enabled? boolean|(fun():boolean) 92 | --- 93 | ---Only useful for lazy=false plugins to force loading certain plugins first. 94 | ---Default priority is 50 95 | ---@field priority? number 96 | --- 97 | ---Set this to override the `load` function for an individual plugin. 98 | ---Defaults to `vim.g.lze.load()`, see |lze.Config|. 99 | ---@field load? fun(name: string) 100 | --- 101 | ---True will allow a plugin to be added to the queue again after it has already been triggered. 102 | ---@field allow_again? boolean 103 | --- 104 | ---Will be run before loading any plugins in that require('lze').load() call 105 | ---@field beforeAll? fun(self:lze.Plugin) 106 | --- 107 | ---Will be run before loading this plugin 108 | ---@field before? fun(self:lze.Plugin) 109 | --- 110 | ---Will be executed after loading this plugin 111 | ---@field after? fun(self:lze.Plugin) 112 | --- 113 | ---Whether to lazy-load this plugin. Defaults to `false`. 114 | ---Using a handler's field sets this automatically. 115 | ---@field lazy? boolean 116 | 117 | -- NOTE: 118 | -- Builtin Handler Types: 119 | 120 | ---@class lze.KeysBase: vim.keymap.set.Opts 121 | ---@field desc? string 122 | ---@field noremap? boolean 123 | ---@field remap? boolean 124 | ---@field expr? boolean 125 | ---@field nowait? boolean 126 | ---@field ft? string|string[] 127 | 128 | ---@class lze.KeysSpec: lze.KeysBase 129 | ---@field [1]? string [1] or lhs required, [1] wins 130 | ---@field [2]? string|fun()|false will override rhs 131 | ---@field lhs? string [1] or lhs required, [1] wins 132 | ---@field rhs? string|fun()|false [2] will override rhs if set 133 | ---@field mode? string|string[] 134 | 135 | ---@class lze.Keys: lze.KeysBase 136 | ---@field lhs string lhs 137 | ---@field rhs? string|fun() rhs 138 | ---@field mode? string 139 | ---@field id string 140 | ---@field name string 141 | 142 | ---@alias lze.Event {id:string, event:string[]|string, pattern?:string[]|string, augroup:integer} 143 | ---@alias lze.EventSpec string|{event?:string|string[], pattern?:string|string[], augroup?:integer}|string[] 144 | 145 | ---@class lze.SpecHandlers 146 | --- 147 | ---Load a plugin on one or more |autocmd-events|. 148 | ---@field event? string|lze.EventSpec[] 149 | --- 150 | ---Load a plugin on one or more |user-commands|. 151 | ---@field cmd? string[]|string 152 | --- 153 | ---Load a plugin on one or more |FileType| events. 154 | ---@field ft? string[]|string 155 | --- 156 | ---Load a plugin on one or more |key-mapping|s. 157 | ---@field keys? string|string[]|lze.KeysSpec[] 158 | --- 159 | ---Load a plugin on one or more |colorscheme| events. 160 | ---@field colorscheme? string[]|string 161 | --- 162 | ---Load a plugin before load of one or more other plugins. 163 | ---@field dep_of? string[]|string 164 | --- 165 | ---Load a plugin after load of one or more other plugins. 166 | ---@field on_plugin? string[]|string 167 | --- 168 | ---Accepts a top-level lua module name or a 169 | ---list of top-level lua module names. 170 | ---Will load when any submodule of those listed is `require`d 171 | ---@field on_require? string[]|string 172 | --- 173 | ---@field [string] any? Fields for any extra handlers you might add 174 | 175 | -- NOTE: 176 | -- Defintion of lze.Plugin and lze.PluginSpec 177 | -- combining above types. 178 | 179 | ---Internal lze.Plugin type, after being parsed. 180 | ---Is the type passed to handlers in modify and add hooks. 181 | ---@class lze.Plugin: lze.PluginBase,lze.SpecHandlers 182 | ---The plugin name (not its main module), e.g. "sweetie.nvim" 183 | ---@field name string 184 | 185 | ---The lze.PluginSpec type, passed to require('lze').load() as entries in lze.Spec 186 | ---@class lze.PluginSpec: lze.PluginBase,lze.SpecHandlers 187 | ---The plugin name (not its main module), e.g. "sweetie.nvim" 188 | ---@field [1] string 189 | 190 | ---@class lze.SpecImport 191 | ---@field import string|lze.Spec spec module to import 192 | ---@field enabled? boolean|(fun():boolean) 193 | 194 | ---List of lze.PluginSpec and/or lze.SpecImport 195 | ---@alias lze.Spec lze.PluginSpec | lze.Plugin | lze.SpecImport | lze.Spec[] 196 | 197 | -- NOTE: 198 | -- lze.Handler type definition 199 | 200 | ---Listed in the order they are called by lze if the handler has been registered 201 | ---@class lze.Handler 202 | ---@field spec_field string 203 | --- 204 | ---Your handler's 1 chance to modify plugins before they are entered into state! 205 | ---Called only if your field was present. 206 | ---@field modify? fun(plugin: lze.Plugin): lze.Plugin, fun()? 207 | --- 208 | ---Called after being entered into state but before any loading has occurred 209 | ---@field add? fun(plugin: lze.Plugin): fun()? 210 | --- 211 | ---Whether using this handler's field should have an effect on the lazy setting 212 | ---True or nil is true 213 | ---Default: nil 214 | ---@field set_lazy? boolean 215 | --- 216 | ---Handlers may export functions and other values via this set, 217 | ---which then may be accessed via `require('lze').h[spec_field].your_func()` 218 | ---@field lib? table 219 | ---Called when the handler is registered 220 | ---@field init? fun() 221 | ---Allows you to clean up any global modifications your handler makes to the environment. 222 | ---@field cleanup? fun() 223 | --- 224 | ---runs at the end of require('lze').load() 225 | ---for handlers to set up extra triggers such as the 226 | ---event handler's DeferredUIEnter event 227 | ---@field post_def? fun() 228 | --- 229 | ---Plugin's before will run first 230 | ---@field before? fun(name: string) 231 | ---Plugin's load will run here 232 | ---@field after? fun(name: string) 233 | ---Plugin's after will run after 234 | 235 | ---Optional, for passing to register_handlers, 236 | ---if easily changing if a handler gets added or not is desired 237 | ---@class lze.HandlerSpec 238 | ---@field handler lze.Handler 239 | ---@field enabled? boolean|(fun():boolean) 240 | 241 | -- NOTE: 242 | -- global vim.g.lze config values table 243 | 244 | ---@class lze.Config 245 | --- 246 | ---Default callback used to load a plugin. 247 | ---Takes the plugin name (not the module name). Defaults to |packadd| if not set. 248 | ---Not provided to handler hooks 249 | ---@field load? fun(name: string) 250 | --- 251 | ---Allows you to inject a default value 252 | ---for any non-nil plugin spec field 253 | ---which will be visible to all handler hooks. 254 | ---@field injects? table 255 | --- 256 | ---If false, lze will print error messages on fewer things. 257 | ---@field verbose? boolean 258 | --- 259 | ---Default priority for startup plugins. Defaults to 50 if unset 260 | ---@field default_priority? integer 261 | --- 262 | ---If true, lze will not automatically register the default handlers. 263 | ---@field without_default_handlers? boolean 264 | 265 | ---@type lze.Config 266 | vim.g.lze = vim.g.lze 267 | < 268 | ============================================================================== 269 | vim:tw=78:ts=8:noet:ft=help:norl: 270 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [0.12.0](https://github.com/BirdeeHub/lze/compare/v0.11.7...v0.12.0) (2025-12-11) 4 | 5 | 6 | ### Features 7 | 8 | * **import:** import spec now accepts more specs ([bad1300](https://github.com/BirdeeHub/lze/commit/bad1300cc6ab7e0fe356906effff77086b49c482)) 9 | * **import:** import spec now accepts more specs ([4b3189b](https://github.com/BirdeeHub/lze/commit/4b3189bce92a05b4d86975c7738813cae841100e)) 10 | 11 | ## [0.11.7](https://github.com/BirdeeHub/lze/compare/v0.11.6...v0.11.7) (2025-08-30) 12 | 13 | 14 | ### Bug Fixes 15 | 16 | * **5_second_rule:** change precedence on new options before anyone notices they exist ([5328b7d](https://github.com/BirdeeHub/lze/commit/5328b7d7a16362e24eefd29afba5cb9d17774623)) 17 | 18 | ## [0.11.6](https://github.com/BirdeeHub/lze/compare/v0.11.5...v0.11.6) (2025-08-30) 19 | 20 | 21 | ### Bug Fixes 22 | 23 | * **add_workaround:** vim.pack specs data field does not currently allow mixed tables ([a77bb9a](https://github.com/BirdeeHub/lze/commit/a77bb9acf342495ce298fb01e964d57eb974e108)) 24 | 25 | ## [0.11.5](https://github.com/BirdeeHub/lze/compare/v0.11.4...v0.11.5) (2025-08-21) 26 | 27 | 28 | ### Bug Fixes 29 | 30 | * **types:** simplified type annotation oh plugin specs ([e5e2292](https://github.com/BirdeeHub/lze/commit/e5e2292695f0226f35b05c9adda58ce422d558e5)) 31 | * **types:** simplified type definition of plugin specs ([ebc2543](https://github.com/BirdeeHub/lze/commit/ebc25432b58078d0a1e6932048412ac8a223ec49)) 32 | 33 | ## [0.11.4](https://github.com/BirdeeHub/lze/compare/v0.11.3...v0.11.4) (2025-06-19) 34 | 35 | 36 | ### Bug Fixes 37 | 38 | * **deprecate:** removed lze.x, which has done nothing but warn for 5 months ([a68ab6b](https://github.com/BirdeeHub/lze/commit/a68ab6b52756b0d1650ae79eb6442dd278cff435)) 39 | 40 | ## [0.11.3](https://github.com/BirdeeHub/lze/compare/v0.11.2...v0.11.3) (2025-05-25) 41 | 42 | 43 | ### Bug Fixes 44 | 45 | * **on_require:** improved formatting of error message ([6551328](https://github.com/BirdeeHub/lze/commit/6551328084a7ff79f61665d4e389524b2b058e56)) 46 | 47 | ## [0.11.2](https://github.com/BirdeeHub/lze/compare/v0.11.1...v0.11.2) (2025-05-25) 48 | 49 | 50 | ### Bug Fixes 51 | 52 | * **feature:** improved how on_require handler works internally ([7ff9e10](https://github.com/BirdeeHub/lze/commit/7ff9e104c41ecc585035cc3683ac92a308fefc3f)) 53 | 54 | ## [0.11.1](https://github.com/BirdeeHub/lze/compare/v0.11.0...v0.11.1) (2025-04-08) 55 | 56 | 57 | ### Bug Fixes 58 | 59 | * **on_require:** now properly propagates error messages ([efbd778](https://github.com/BirdeeHub/lze/commit/efbd778e1b5e9d98ecc31298256e3ff5bcfed0d4)) 60 | 61 | ## [0.11.0](https://github.com/BirdeeHub/lze/compare/v0.10.0...v0.11.0) (2025-03-30) 62 | 63 | 64 | ### Features 65 | 66 | * **vim.g.lze.injects:** set a default value for a field, visible to handlers ([4ef81ea](https://github.com/BirdeeHub/lze/commit/4ef81ea502884ab3b3415d5cf3f5b33dbc95206b)) 67 | 68 | ## [0.10.0](https://github.com/BirdeeHub/lze/compare/v0.9.1...v0.10.0) (2025-03-25) 69 | 70 | 71 | ### Features 72 | 73 | * **version:** remove vim.iter to support older nvim versions ([4efeb2f](https://github.com/BirdeeHub/lze/commit/4efeb2f33ab12f6f500d9392ab80be9b19026b0c)) 74 | 75 | ## [0.9.1](https://github.com/BirdeeHub/lze/compare/v0.9.0...v0.9.1) (2025-03-21) 76 | 77 | 78 | ### Bug Fixes 79 | 80 | * **beforeAll:** once again beforeAll ([da9a0c8](https://github.com/BirdeeHub/lze/commit/da9a0c8e44162fecd1b03c41dbb55e133f8bc65f)) 81 | 82 | ## [0.9.0](https://github.com/BirdeeHub/lze/compare/v0.8.1...v0.9.0) (2025-03-21) 83 | 84 | 85 | ### Features 86 | 87 | * **handler:** add and modify hooks can return a function to be deferred ([a43c98e](https://github.com/BirdeeHub/lze/commit/a43c98e67e357b9c6af1fc0592fc3b8533d1af8b)) 88 | 89 | ## [0.8.1](https://github.com/BirdeeHub/lze/compare/v0.8.0...v0.8.1) (2025-03-07) 90 | 91 | 92 | ### Bug Fixes 93 | 94 | * **event:** ft loop ([308290b](https://github.com/BirdeeHub/lze/commit/308290b31b77c9f24eb63abbd07394f1d1797ae2)) 95 | * **event:** ft loop ([5ba6ccf](https://github.com/BirdeeHub/lze/commit/5ba6ccfc854c876902a1a29bfd39cfb3b141370b)) 96 | 97 | ## [0.8.0](https://github.com/BirdeeHub/lze/compare/v0.7.11...v0.8.0) (2025-02-19) 98 | 99 | 100 | ### Features 101 | 102 | * **lze.h:** lze.h[spec_field].handler_exported_function() ([67220ae](https://github.com/BirdeeHub/lze/commit/67220aeb7dc079d4b45d875e0dbdab842718e956)) 103 | 104 | ## [0.7.11](https://github.com/BirdeeHub/lze/compare/v0.7.10...v0.7.11) (2025-02-13) 105 | 106 | 107 | ### Bug Fixes 108 | 109 | * **trigger_load:** before can no longer alter load ([ed7f60e](https://github.com/BirdeeHub/lze/commit/ed7f60e4d1f21520028c2cfd8a413572fb69a819)) 110 | 111 | ## [0.7.10](https://github.com/BirdeeHub/lze/compare/v0.7.9...v0.7.10) (2025-02-13) 112 | 113 | 114 | ### Bug Fixes 115 | 116 | * **cmd:** doesnt error when asked to delete a command that doesnt exist ([4d0ef68](https://github.com/BirdeeHub/lze/commit/4d0ef68a08cf17a4ecc41201bb5716af7020829b)) 117 | 118 | ## [0.7.9](https://github.com/BirdeeHub/lze/compare/v0.7.8...v0.7.9) (2025-02-08) 119 | 120 | 121 | ### Bug Fixes 122 | 123 | * **modify:** modify now runs before is lazy so that it can know the original setting ([870cfc3](https://github.com/BirdeeHub/lze/commit/870cfc3fb302c0929cf86cf31c19a8cdd4aacaa3)) 124 | 125 | ## [0.7.8](https://github.com/BirdeeHub/lze/compare/v0.7.7...v0.7.8) (2025-02-08) 126 | 127 | 128 | ### Bug Fixes 129 | 130 | * **modify:** modify now runs before is lazy so that it can know the original setting ([ab2282d](https://github.com/BirdeeHub/lze/commit/ab2282d3745b6e6f9b61cfa42c8165124dbd9bb4)) 131 | 132 | ## [0.7.7](https://github.com/BirdeeHub/lze/compare/v0.7.6...v0.7.7) (2025-02-08) 133 | 134 | 135 | ### Bug Fixes 136 | 137 | * **trigger_load:** further improve error handling ([97d3a29](https://github.com/BirdeeHub/lze/commit/97d3a29cc4d4d123d0e8c7892d56eccd85e2352f)) 138 | 139 | ## [0.7.6](https://github.com/BirdeeHub/lze/compare/v0.7.5...v0.7.6) (2025-02-08) 140 | 141 | 142 | ### Bug Fixes 143 | 144 | * **startup_plugins:** errors in startup_plugins are now handled more gracefully ([2cc19e6](https://github.com/BirdeeHub/lze/commit/2cc19e6c9eedc7f0a8efc15b69dd9f209bdf0d53)) 145 | 146 | ## [0.7.5](https://github.com/BirdeeHub/lze/compare/v0.7.4...v0.7.5) (2025-02-07) 147 | 148 | 149 | ### Bug Fixes 150 | 151 | * **warning:** made warning less verbose ([c76bc2c](https://github.com/BirdeeHub/lze/commit/c76bc2c885ec02ac53f0b2889b6c9d1601d163a5)) 152 | 153 | ## [0.7.4](https://github.com/BirdeeHub/lze/compare/v0.7.3...v0.7.4) (2025-02-07) 154 | 155 | 156 | ### Bug Fixes 157 | 158 | * **load_return:** lze.load returns duplicate plugins, not a list of strings ([48b335a](https://github.com/BirdeeHub/lze/commit/48b335a06b780d51d4469b31f74a27e1999d13e3)) 159 | 160 | ## [0.7.3](https://github.com/BirdeeHub/lze/compare/v0.7.2...v0.7.3) (2025-02-07) 161 | 162 | 163 | ### Bug Fixes 164 | 165 | * **internal:** performance ([c6b9067](https://github.com/BirdeeHub/lze/commit/c6b9067783b8ae96fa02b604b23ba5fcdf6dfdce)) 166 | 167 | ## [0.7.2](https://github.com/BirdeeHub/lze/compare/v0.7.1...v0.7.2) (2025-02-07) 168 | 169 | 170 | ### Bug Fixes 171 | 172 | * **internal:** prevent misuse of internal functions ([09213be](https://github.com/BirdeeHub/lze/commit/09213beb1b7ddf256e8c0227796a167f753d34ad)) 173 | 174 | ## [0.7.1](https://github.com/BirdeeHub/lze/compare/v0.7.0...v0.7.1) (2025-02-07) 175 | 176 | 177 | ### Bug Fixes 178 | 179 | * **internal:** prevent misuse of internal functions ([f5d753e](https://github.com/BirdeeHub/lze/commit/f5d753e4f7d544add3352e721f24cc05d1cf621c)) 180 | 181 | ## [0.7.0](https://github.com/BirdeeHub/lze/compare/v0.6.3...v0.7.0) (2025-02-05) 182 | 183 | 184 | ### Features 185 | 186 | * **handlers:** lze.remove_handlers(string|string[]):lze.Handler[] ([9fd946f](https://github.com/BirdeeHub/lze/commit/9fd946fc3109f685fbebff74b785c0d596cb40b5)) 187 | 188 | ## [0.6.3](https://github.com/BirdeeHub/lze/compare/v0.6.2...v0.6.3) (2025-01-26) 189 | 190 | 191 | ### Bug Fixes 192 | 193 | * **nop:** support for nop keybindings ([d0cc725](https://github.com/BirdeeHub/lze/commit/d0cc72559b81b1c7588e539a5392bfa7d4f28120)) 194 | 195 | ## [0.6.2](https://github.com/BirdeeHub/lze/compare/v0.6.1...v0.6.2) (2025-01-26) 196 | 197 | 198 | ### Bug Fixes 199 | 200 | * **handler.spec_field:** false is now an allowed value ([586f744](https://github.com/BirdeeHub/lze/commit/586f7448b11f213510318b3b8c9f594c5576919f)) 201 | 202 | ## [0.6.1](https://github.com/BirdeeHub/lze/compare/v0.6.0...v0.6.1) (2025-01-26) 203 | 204 | 205 | ### Bug Fixes 206 | 207 | * **handler.spec_field:** false is now an allowed value ([8fd70c3](https://github.com/BirdeeHub/lze/commit/8fd70c3a51523a8eece4df5995f4b3af6e5c237a)) 208 | 209 | ## [0.6.0](https://github.com/BirdeeHub/lze/compare/v0.5.0...v0.6.0) (2025-01-26) 210 | 211 | 212 | ### Features 213 | 214 | * **plz_ignore:** removed loadbearing comment ([b45c6e5](https://github.com/BirdeeHub/lze/commit/b45c6e546481a130bd6eaf0a9c3e6f6c445df451)) 215 | 216 | ## [0.5.0](https://github.com/BirdeeHub/lze/compare/v0.4.6...v0.5.0) (2025-01-26) 217 | 218 | 219 | ### Features 220 | 221 | * **lze.x by default:** lze.x now included by default. ([a49ff1f](https://github.com/BirdeeHub/lze/commit/a49ff1fe9afbea0fe4beff106baad5438ab44027)) 222 | 223 | ## [0.4.6](https://github.com/BirdeeHub/lze/compare/v0.4.5...v0.4.6) (2025-01-26) 224 | 225 | 226 | ### Bug Fixes 227 | 228 | * **beforeAll:** fixed unintended spec mutability ([01cf37f](https://github.com/BirdeeHub/lze/commit/01cf37fc70ab946045580a9d16cbd57cc33926da)) 229 | 230 | ## [0.4.5](https://github.com/BirdeeHub/lze/compare/v0.4.4...v0.4.5) (2024-12-17) 231 | 232 | 233 | ### Bug Fixes 234 | 235 | * **query_state:** depreciated query_state ([4b9e2ce](https://github.com/BirdeeHub/lze/commit/4b9e2ce774f6241ce7ec675145f9139f3395dd3f)) 236 | 237 | ## [0.4.4](https://github.com/BirdeeHub/lze/compare/v0.4.3...v0.4.4) (2024-11-04) 238 | 239 | 240 | ### Performance Improvements 241 | 242 | * **register_handlers:** improved it further ([041aa70](https://github.com/BirdeeHub/lze/commit/041aa70cb606b2a1a22b7d714656b3d7b15af706)) 243 | 244 | ## [0.4.3](https://github.com/BirdeeHub/lze/compare/v0.4.2...v0.4.3) (2024-11-04) 245 | 246 | 247 | ### Performance Improvements 248 | 249 | * **register_handlers:** improved it further ([0b25e31](https://github.com/BirdeeHub/lze/commit/0b25e317477f87a48c67c5ef1aa1426a54f2f79c)) 250 | 251 | ## [0.4.2](https://github.com/BirdeeHub/lze/compare/v0.4.1...v0.4.2) (2024-11-03) 252 | 253 | 254 | ### Performance Improvements 255 | 256 | * **register_handlers:** removed a loop from processing of spec ([cc73ce3](https://github.com/BirdeeHub/lze/commit/cc73ce303e97e59ad5c2e3d1362e0f69730f17ec)) 257 | 258 | ## [0.4.1](https://github.com/BirdeeHub/lze/compare/v0.4.0...v0.4.1) (2024-11-03) 259 | 260 | 261 | ### Performance Improvements 262 | 263 | * **register_handlers:** removed a loop processing the spec ([8f8a62a](https://github.com/BirdeeHub/lze/commit/8f8a62ae54e41f7bbb5f52a39b968b225d9e225d)) 264 | 265 | ## [0.4.0](https://github.com/BirdeeHub/lze/compare/v0.3.0...v0.4.0) (2024-11-03) 266 | 267 | 268 | ### Features 269 | 270 | * **handler.set_lazy:** ability to choose if a handler affects laziness ([58f3ff4](https://github.com/BirdeeHub/lze/commit/58f3ff4936396c556efedca33bf984169a24d1a3)) 271 | 272 | ## [0.3.0](https://github.com/BirdeeHub/lze/compare/v0.2.0...v0.3.0) (2024-11-03) 273 | 274 | 275 | ### Features 276 | 277 | * **lze.state:** added ability to snapshot state ([0474c38](https://github.com/BirdeeHub/lze/commit/0474c38e4c91020c2894adcbbb67ca46a837ba82)) 278 | 279 | ## [0.2.0](https://github.com/BirdeeHub/lze/compare/v0.1.4...v0.2.0) (2024-11-02) 280 | 281 | 282 | ### Features 283 | 284 | * **lze.State:** cheaper state query ([4b0fd7a](https://github.com/BirdeeHub/lze/commit/4b0fd7adb49835641bc6a01b3ea08066498e95a2)) 285 | 286 | ## [0.1.4](https://github.com/BirdeeHub/lze/compare/v0.1.3...v0.1.4) (2024-10-28) 287 | 288 | 289 | ### Bug Fixes 290 | 291 | * **cmd:** dummy cmd hanging around ([7a2649f](https://github.com/BirdeeHub/lze/commit/7a2649fe921d54f16910e1c062bb0c6be55c0c0a)) 292 | 293 | ## [0.1.3](https://github.com/BirdeeHub/lze/compare/v0.1.2...v0.1.3) (2024-10-24) 294 | 295 | 296 | ### Performance Improvements 297 | 298 | * **spec.parse:** slight improvement on last change ([d919d28](https://github.com/BirdeeHub/lze/commit/d919d28faab5edded746d1c2dd8bc12473a42af8)) 299 | 300 | ## [0.1.2](https://github.com/BirdeeHub/lze/compare/v0.1.1...v0.1.2) (2024-10-24) 301 | 302 | 303 | ### Bug Fixes 304 | 305 | * **lazy attribute:** spec.lazy = false is now authoritative ([e4b03d5](https://github.com/BirdeeHub/lze/commit/e4b03d557b5fae3ff563895c87143a30cba113a0)) 306 | 307 | ## [0.1.1](https://github.com/BirdeeHub/lze/compare/v0.1.0...v0.1.1) (2024-09-03) 308 | 309 | 310 | ### Bug Fixes 311 | 312 | * **nested events:** triggering events from after of event trigger ([f77d182](https://github.com/BirdeeHub/lze/commit/f77d182735f0df27c482b5894d2e73cd418cd6c2)) 313 | 314 | ## [0.1.0](https://github.com/BirdeeHub/lze/compare/v0.0.0...v0.1.0) (2024-08-31) 315 | 316 | 317 | ### Features 318 | 319 | * **release-0.0.1:** has tests and readme, release-worthy? ([c07c96d](https://github.com/BirdeeHub/lze/commit/c07c96db7fe71d4434e550d43ff89de2320297fe)) 320 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "flake-compat": { 4 | "flake": false, 5 | "locked": { 6 | "lastModified": 1696426674, 7 | "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", 8 | "owner": "edolstra", 9 | "repo": "flake-compat", 10 | "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", 11 | "type": "github" 12 | }, 13 | "original": { 14 | "owner": "edolstra", 15 | "repo": "flake-compat", 16 | "type": "github" 17 | } 18 | }, 19 | "flake-compat_2": { 20 | "flake": false, 21 | "locked": { 22 | "lastModified": 1765121682, 23 | "narHash": "sha256-4VBOP18BFeiPkyhy9o4ssBNQEvfvv1kXkasAYd0+rrA=", 24 | "owner": "edolstra", 25 | "repo": "flake-compat", 26 | "rev": "65f23138d8d09a92e30f1e5c87611b23ef451bf3", 27 | "type": "github" 28 | }, 29 | "original": { 30 | "owner": "edolstra", 31 | "repo": "flake-compat", 32 | "type": "github" 33 | } 34 | }, 35 | "flake-compat_3": { 36 | "flake": false, 37 | "locked": { 38 | "lastModified": 1761588595, 39 | "narHash": "sha256-XKUZz9zewJNUj46b4AJdiRZJAvSZ0Dqj2BNfXvFlJC4=", 40 | "owner": "edolstra", 41 | "repo": "flake-compat", 42 | "rev": "f387cd2afec9419c8ee37694406ca490c3f34ee5", 43 | "type": "github" 44 | }, 45 | "original": { 46 | "owner": "edolstra", 47 | "repo": "flake-compat", 48 | "type": "github" 49 | } 50 | }, 51 | "flake-compat_4": { 52 | "flake": false, 53 | "locked": { 54 | "lastModified": 1761588595, 55 | "narHash": "sha256-XKUZz9zewJNUj46b4AJdiRZJAvSZ0Dqj2BNfXvFlJC4=", 56 | "owner": "edolstra", 57 | "repo": "flake-compat", 58 | "rev": "f387cd2afec9419c8ee37694406ca490c3f34ee5", 59 | "type": "github" 60 | }, 61 | "original": { 62 | "owner": "edolstra", 63 | "repo": "flake-compat", 64 | "type": "github" 65 | } 66 | }, 67 | "flake-parts": { 68 | "inputs": { 69 | "nixpkgs-lib": "nixpkgs-lib" 70 | }, 71 | "locked": { 72 | "lastModified": 1765835352, 73 | "narHash": "sha256-XswHlK/Qtjasvhd1nOa1e8MgZ8GS//jBoTqWtrS1Giw=", 74 | "owner": "hercules-ci", 75 | "repo": "flake-parts", 76 | "rev": "a34fae9c08a15ad73f295041fec82323541400a9", 77 | "type": "github" 78 | }, 79 | "original": { 80 | "owner": "hercules-ci", 81 | "repo": "flake-parts", 82 | "type": "github" 83 | } 84 | }, 85 | "flake-parts_2": { 86 | "inputs": { 87 | "nixpkgs-lib": "nixpkgs-lib_2" 88 | }, 89 | "locked": { 90 | "lastModified": 1717285511, 91 | "narHash": "sha256-iKzJcpdXih14qYVcZ9QC9XuZYnPc6T8YImb6dX166kw=", 92 | "owner": "hercules-ci", 93 | "repo": "flake-parts", 94 | "rev": "2a55567fcf15b1b1c7ed712a2c6fadaec7412ea8", 95 | "type": "github" 96 | }, 97 | "original": { 98 | "owner": "hercules-ci", 99 | "repo": "flake-parts", 100 | "type": "github" 101 | } 102 | }, 103 | "flake-parts_3": { 104 | "inputs": { 105 | "nixpkgs-lib": "nixpkgs-lib_3" 106 | }, 107 | "locked": { 108 | "lastModified": 1765835352, 109 | "narHash": "sha256-XswHlK/Qtjasvhd1nOa1e8MgZ8GS//jBoTqWtrS1Giw=", 110 | "owner": "hercules-ci", 111 | "repo": "flake-parts", 112 | "rev": "a34fae9c08a15ad73f295041fec82323541400a9", 113 | "type": "github" 114 | }, 115 | "original": { 116 | "owner": "hercules-ci", 117 | "repo": "flake-parts", 118 | "type": "github" 119 | } 120 | }, 121 | "flake-parts_4": { 122 | "inputs": { 123 | "nixpkgs-lib": [ 124 | "neorocks", 125 | "neovim-nightly", 126 | "nixpkgs" 127 | ] 128 | }, 129 | "locked": { 130 | "lastModified": 1765835352, 131 | "narHash": "sha256-XswHlK/Qtjasvhd1nOa1e8MgZ8GS//jBoTqWtrS1Giw=", 132 | "owner": "hercules-ci", 133 | "repo": "flake-parts", 134 | "rev": "a34fae9c08a15ad73f295041fec82323541400a9", 135 | "type": "github" 136 | }, 137 | "original": { 138 | "owner": "hercules-ci", 139 | "repo": "flake-parts", 140 | "type": "github" 141 | } 142 | }, 143 | "gen-luarc": { 144 | "inputs": { 145 | "flake-parts": "flake-parts_2", 146 | "git-hooks": "git-hooks", 147 | "luvit-meta": "luvit-meta", 148 | "nixpkgs": "nixpkgs" 149 | }, 150 | "locked": { 151 | "lastModified": 1755304025, 152 | "narHash": "sha256-xVKfjFwc0zMbLMjLTiHz+0llggkjs93SmHkhaa9S3M4=", 153 | "owner": "mrcjkb", 154 | "repo": "nix-gen-luarc-json", 155 | "rev": "1865b0ebb753ae5324d7381b1fa8c98c04ec7509", 156 | "type": "github" 157 | }, 158 | "original": { 159 | "owner": "mrcjkb", 160 | "repo": "nix-gen-luarc-json", 161 | "type": "github" 162 | } 163 | }, 164 | "git-hooks": { 165 | "inputs": { 166 | "flake-compat": "flake-compat", 167 | "gitignore": "gitignore", 168 | "nixpkgs": [ 169 | "gen-luarc", 170 | "nixpkgs" 171 | ], 172 | "nixpkgs-stable": "nixpkgs-stable" 173 | }, 174 | "locked": { 175 | "lastModified": 1723803910, 176 | "narHash": "sha256-yezvUuFiEnCFbGuwj/bQcqg7RykIEqudOy/RBrId0pc=", 177 | "owner": "cachix", 178 | "repo": "git-hooks.nix", 179 | "rev": "bfef0ada09e2c8ac55bbcd0831bd0c9d42e651ba", 180 | "type": "github" 181 | }, 182 | "original": { 183 | "owner": "cachix", 184 | "repo": "git-hooks.nix", 185 | "type": "github" 186 | } 187 | }, 188 | "git-hooks_2": { 189 | "inputs": { 190 | "flake-compat": "flake-compat_3", 191 | "gitignore": "gitignore_2", 192 | "nixpkgs": "nixpkgs_2" 193 | }, 194 | "locked": { 195 | "lastModified": 1765911976, 196 | "narHash": "sha256-t3T/xm8zstHRLx+pIHxVpQTiySbKqcQbK+r+01XVKc0=", 197 | "owner": "cachix", 198 | "repo": "git-hooks.nix", 199 | "rev": "b68b780b69702a090c8bb1b973bab13756cc7a27", 200 | "type": "github" 201 | }, 202 | "original": { 203 | "owner": "cachix", 204 | "repo": "git-hooks.nix", 205 | "type": "github" 206 | } 207 | }, 208 | "gitignore": { 209 | "inputs": { 210 | "nixpkgs": [ 211 | "gen-luarc", 212 | "git-hooks", 213 | "nixpkgs" 214 | ] 215 | }, 216 | "locked": { 217 | "lastModified": 1709087332, 218 | "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", 219 | "owner": "hercules-ci", 220 | "repo": "gitignore.nix", 221 | "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", 222 | "type": "github" 223 | }, 224 | "original": { 225 | "owner": "hercules-ci", 226 | "repo": "gitignore.nix", 227 | "type": "github" 228 | } 229 | }, 230 | "gitignore_2": { 231 | "inputs": { 232 | "nixpkgs": [ 233 | "neorocks", 234 | "git-hooks", 235 | "nixpkgs" 236 | ] 237 | }, 238 | "locked": { 239 | "lastModified": 1709087332, 240 | "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", 241 | "owner": "hercules-ci", 242 | "repo": "gitignore.nix", 243 | "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", 244 | "type": "github" 245 | }, 246 | "original": { 247 | "owner": "hercules-ci", 248 | "repo": "gitignore.nix", 249 | "type": "github" 250 | } 251 | }, 252 | "gitignore_3": { 253 | "inputs": { 254 | "nixpkgs": [ 255 | "pre-commit-hooks", 256 | "nixpkgs" 257 | ] 258 | }, 259 | "locked": { 260 | "lastModified": 1709087332, 261 | "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", 262 | "owner": "hercules-ci", 263 | "repo": "gitignore.nix", 264 | "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", 265 | "type": "github" 266 | }, 267 | "original": { 268 | "owner": "hercules-ci", 269 | "repo": "gitignore.nix", 270 | "type": "github" 271 | } 272 | }, 273 | "luvit-meta": { 274 | "flake": false, 275 | "locked": { 276 | "lastModified": 1705776742, 277 | "narHash": "sha256-zAAptV/oLuLAAsa2zSB/6fxlElk4+jNZd/cPr9oxFig=", 278 | "owner": "Bilal2453", 279 | "repo": "luvit-meta", 280 | "rev": "ce76f6f6cdc9201523a5875a4471dcfe0186eb60", 281 | "type": "github" 282 | }, 283 | "original": { 284 | "owner": "Bilal2453", 285 | "repo": "luvit-meta", 286 | "type": "github" 287 | } 288 | }, 289 | "neorocks": { 290 | "inputs": { 291 | "flake-compat": "flake-compat_2", 292 | "flake-parts": "flake-parts_3", 293 | "git-hooks": "git-hooks_2", 294 | "neovim-nightly": "neovim-nightly", 295 | "nixpkgs": "nixpkgs_4" 296 | }, 297 | "locked": { 298 | "lastModified": 1766208982, 299 | "narHash": "sha256-NCQFFYfeR/JeSqtoGoBmROwYsZUUSjPs78Wu//+m2I8=", 300 | "owner": "nvim-neorocks", 301 | "repo": "neorocks", 302 | "rev": "d506bdb8c8013ad31e340a375f5939c60f6fa3e5", 303 | "type": "github" 304 | }, 305 | "original": { 306 | "owner": "nvim-neorocks", 307 | "repo": "neorocks", 308 | "type": "github" 309 | } 310 | }, 311 | "neovim-nightly": { 312 | "inputs": { 313 | "flake-parts": "flake-parts_4", 314 | "neovim-src": "neovim-src", 315 | "nixpkgs": "nixpkgs_3" 316 | }, 317 | "locked": { 318 | "lastModified": 1766189079, 319 | "narHash": "sha256-K+xdRPIfIE8Xr3JHKNkVNnxPnihrPtW+D1rtcjHx1u4=", 320 | "owner": "nix-community", 321 | "repo": "neovim-nightly-overlay", 322 | "rev": "7e47a8c64312e726aafc0789a5d9043501a7e3ae", 323 | "type": "github" 324 | }, 325 | "original": { 326 | "owner": "nix-community", 327 | "repo": "neovim-nightly-overlay", 328 | "type": "github" 329 | } 330 | }, 331 | "neovim-src": { 332 | "flake": false, 333 | "locked": { 334 | "lastModified": 1766187129, 335 | "narHash": "sha256-SqvXXTi+nLihMFhHUT7/1JtNqWk2cMKiCvWxYKE03r8=", 336 | "owner": "neovim", 337 | "repo": "neovim", 338 | "rev": "eac2f0443e032ba238ca6b4a9e2fd6135be454b3", 339 | "type": "github" 340 | }, 341 | "original": { 342 | "owner": "neovim", 343 | "repo": "neovim", 344 | "type": "github" 345 | } 346 | }, 347 | "nixpkgs": { 348 | "locked": { 349 | "lastModified": 1718714799, 350 | "narHash": "sha256-FUZpz9rg3gL8NVPKbqU8ei1VkPLsTIfAJ2fdAf5qjak=", 351 | "owner": "nixos", 352 | "repo": "nixpkgs", 353 | "rev": "c00d587b1a1afbf200b1d8f0b0e4ba9deb1c7f0e", 354 | "type": "github" 355 | }, 356 | "original": { 357 | "owner": "nixos", 358 | "ref": "nixos-unstable", 359 | "repo": "nixpkgs", 360 | "type": "github" 361 | } 362 | }, 363 | "nixpkgs-lib": { 364 | "locked": { 365 | "lastModified": 1765674936, 366 | "narHash": "sha256-k00uTP4JNfmejrCLJOwdObYC9jHRrr/5M/a/8L2EIdo=", 367 | "owner": "nix-community", 368 | "repo": "nixpkgs.lib", 369 | "rev": "2075416fcb47225d9b68ac469a5c4801a9c4dd85", 370 | "type": "github" 371 | }, 372 | "original": { 373 | "owner": "nix-community", 374 | "repo": "nixpkgs.lib", 375 | "type": "github" 376 | } 377 | }, 378 | "nixpkgs-lib_2": { 379 | "locked": { 380 | "lastModified": 1717284937, 381 | "narHash": "sha256-lIbdfCsf8LMFloheeE6N31+BMIeixqyQWbSr2vk79EQ=", 382 | "type": "tarball", 383 | "url": "https://github.com/NixOS/nixpkgs/archive/eb9ceca17df2ea50a250b6b27f7bf6ab0186f198.tar.gz" 384 | }, 385 | "original": { 386 | "type": "tarball", 387 | "url": "https://github.com/NixOS/nixpkgs/archive/eb9ceca17df2ea50a250b6b27f7bf6ab0186f198.tar.gz" 388 | } 389 | }, 390 | "nixpkgs-lib_3": { 391 | "locked": { 392 | "lastModified": 1765674936, 393 | "narHash": "sha256-k00uTP4JNfmejrCLJOwdObYC9jHRrr/5M/a/8L2EIdo=", 394 | "owner": "nix-community", 395 | "repo": "nixpkgs.lib", 396 | "rev": "2075416fcb47225d9b68ac469a5c4801a9c4dd85", 397 | "type": "github" 398 | }, 399 | "original": { 400 | "owner": "nix-community", 401 | "repo": "nixpkgs.lib", 402 | "type": "github" 403 | } 404 | }, 405 | "nixpkgs-stable": { 406 | "locked": { 407 | "lastModified": 1720386169, 408 | "narHash": "sha256-NGKVY4PjzwAa4upkGtAMz1npHGoRzWotlSnVlqI40mo=", 409 | "owner": "NixOS", 410 | "repo": "nixpkgs", 411 | "rev": "194846768975b7ad2c4988bdb82572c00222c0d7", 412 | "type": "github" 413 | }, 414 | "original": { 415 | "owner": "NixOS", 416 | "ref": "nixos-24.05", 417 | "repo": "nixpkgs", 418 | "type": "github" 419 | } 420 | }, 421 | "nixpkgs_2": { 422 | "locked": { 423 | "lastModified": 1764947035, 424 | "narHash": "sha256-EYHSjVM4Ox4lvCXUMiKKs2vETUSL5mx+J2FfutM7T9w=", 425 | "owner": "NixOS", 426 | "repo": "nixpkgs", 427 | "rev": "a672be65651c80d3f592a89b3945466584a22069", 428 | "type": "github" 429 | }, 430 | "original": { 431 | "owner": "NixOS", 432 | "ref": "nixpkgs-unstable", 433 | "repo": "nixpkgs", 434 | "type": "github" 435 | } 436 | }, 437 | "nixpkgs_3": { 438 | "locked": { 439 | "lastModified": 1766025857, 440 | "narHash": "sha256-Lav5jJazCW4mdg1iHcROpuXqmM94BWJvabLFWaJVJp0=", 441 | "owner": "NixOS", 442 | "repo": "nixpkgs", 443 | "rev": "def3da69945bbe338c373fddad5a1bb49cf199ce", 444 | "type": "github" 445 | }, 446 | "original": { 447 | "owner": "NixOS", 448 | "ref": "nixpkgs-unstable", 449 | "repo": "nixpkgs", 450 | "type": "github" 451 | } 452 | }, 453 | "nixpkgs_4": { 454 | "locked": { 455 | "lastModified": 1766125104, 456 | "narHash": "sha256-l/YGrEpLromL4viUo5GmFH3K5M1j0Mb9O+LiaeCPWEM=", 457 | "owner": "nixos", 458 | "repo": "nixpkgs", 459 | "rev": "7d853e518814cca2a657b72eeba67ae20ebf7059", 460 | "type": "github" 461 | }, 462 | "original": { 463 | "owner": "nixos", 464 | "ref": "nixpkgs-unstable", 465 | "repo": "nixpkgs", 466 | "type": "github" 467 | } 468 | }, 469 | "nixpkgs_5": { 470 | "locked": { 471 | "lastModified": 1766125104, 472 | "narHash": "sha256-l/YGrEpLromL4viUo5GmFH3K5M1j0Mb9O+LiaeCPWEM=", 473 | "owner": "nixos", 474 | "repo": "nixpkgs", 475 | "rev": "7d853e518814cca2a657b72eeba67ae20ebf7059", 476 | "type": "github" 477 | }, 478 | "original": { 479 | "owner": "nixos", 480 | "ref": "nixpkgs-unstable", 481 | "repo": "nixpkgs", 482 | "type": "github" 483 | } 484 | }, 485 | "nixpkgs_6": { 486 | "locked": { 487 | "lastModified": 1764947035, 488 | "narHash": "sha256-EYHSjVM4Ox4lvCXUMiKKs2vETUSL5mx+J2FfutM7T9w=", 489 | "owner": "NixOS", 490 | "repo": "nixpkgs", 491 | "rev": "a672be65651c80d3f592a89b3945466584a22069", 492 | "type": "github" 493 | }, 494 | "original": { 495 | "owner": "NixOS", 496 | "ref": "nixpkgs-unstable", 497 | "repo": "nixpkgs", 498 | "type": "github" 499 | } 500 | }, 501 | "pre-commit-hooks": { 502 | "inputs": { 503 | "flake-compat": "flake-compat_4", 504 | "gitignore": "gitignore_3", 505 | "nixpkgs": "nixpkgs_6" 506 | }, 507 | "locked": { 508 | "lastModified": 1765911976, 509 | "narHash": "sha256-t3T/xm8zstHRLx+pIHxVpQTiySbKqcQbK+r+01XVKc0=", 510 | "owner": "cachix", 511 | "repo": "pre-commit-hooks.nix", 512 | "rev": "b68b780b69702a090c8bb1b973bab13756cc7a27", 513 | "type": "github" 514 | }, 515 | "original": { 516 | "owner": "cachix", 517 | "repo": "pre-commit-hooks.nix", 518 | "type": "github" 519 | } 520 | }, 521 | "root": { 522 | "inputs": { 523 | "flake-parts": "flake-parts", 524 | "gen-luarc": "gen-luarc", 525 | "neorocks": "neorocks", 526 | "nixpkgs": "nixpkgs_5", 527 | "pre-commit-hooks": "pre-commit-hooks" 528 | } 529 | } 530 | }, 531 | "root": "root", 532 | "version": 7 533 | } 534 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2 (or later), June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | 342 | GPL LICENSE EXCEPTION FOR DERIVED OPEN SOURCE SOFTWARE 343 | 344 | The GPL version 2 license applies only to the Nix CI infrastructure provided 345 | by this template repository, including any modifications made to the infrastructure. 346 | Any software that uses or is derived from this template may be licensed under any 347 | OSI approved open source license, without being subject to the GPL version 2 license 348 | of the infrastructure. 349 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # :sloth: lze 2 | 3 | [![Neovim][neovim-shield]][neovim-url] 4 | [![Lua][lua-shield]][lua-url] 5 | [![LuaRocks][luarocks-shield]][luarocks-url] 6 | 7 | A dead simple lazy-loading Lua library for Neovim plugins. 8 | 9 | It is intended to be used 10 | 11 | - by users of plugin managers that don't provide a convenient API for lazy-loading. 12 | - by plugin managers, to provide a convenient API for lazy-loading. 13 | 14 | > What problem does it solve? 15 | If I am downloading a plugin as optional, 16 | can't I call `vim.cmd.packadd` from an autocommand myself to load it? 17 | 18 | Yes you can. However, you may wish to trigger a plugin on one of *many* triggers, 19 | rather than just one triggering condition. 20 | 21 | You will find that either gets very verbose very quickly, 22 | or results in something like this plugin. 23 | 24 | `lze` solves this problem in a simple, performant, and extensible way. 25 | 26 | ## :question: Is this [`lz.n`](https://github.com/nvim-neorocks/lz.n)? 27 | 28 | Nope. I quite like `lz.n`. I have contributed to `lz.n` many times. 29 | 30 | `lz.n` is a great plugin. 31 | I think having the handlers manage the entire 32 | state of `lz.n` is an elegant solution. 33 | 34 | But it also meant I had to be more careful with state when writing handlers, 35 | or else my plugin may not be properly trigger-able. 36 | `lz.n` to that effect has provided an API to make this easier. 37 | 38 | This is my take on `lz.n`. 39 | 40 | The core has been entirely rewritten 41 | and it handles its state entirely differently. 42 | 43 | It shares some code where some handlers parse their specs, 44 | otherwise it works entirely differently, but 45 | with a largely compatible [plugin spec](#plugin-spec) 46 | 47 | However, import specs can only import a 48 | single module rather than a whole directory. 49 | 50 | Why? 51 | 52 | `lze` actually treats your list of specs as a list. 53 | If your startup plugins have not been given a priority, 54 | they load in the order passed in. 55 | 56 | What order should the directory be imported in? I left it up to you. 57 | 58 | > Why does the readme say it is a dead simple library? 59 | 60 | The core of `lze` is simply a read-only table. You queue up 61 | a plugin, a handler loads it when you tell it to, 62 | it gets replaced with `false`. 63 | 64 | Handlers have 1 chance to prevent a plugin from entering, 65 | or modify it before it enters **if active for that spec**, 66 | (none of the default handlers need this). 67 | 68 | Once it has been entered, 69 | it will remain there until it has been loaded via 70 | a call to `require('lze').trigger_load(name)` (or a list of names). 71 | 72 | You can only add it to the queue again 73 | *after* it has been loaded, and specifically allow it to be added again. 74 | 75 | That's basically it. The handlers call 76 | `trigger_load` with some names on some sort of event, 77 | `lze` loads it if its in the table, 78 | and if not, it returns the skipped ones, 79 | by default, warning if it wasn't found at all. 80 | 81 | ## :star2: Features 82 | 83 | - API for lazy-loading plugins on: 84 | - Events (`:h autocmd-events`) 85 | - `FileType` events 86 | - Key mappings 87 | - User commands 88 | - Colorscheme events 89 | - Other plugins 90 | - whatever you can write a [custom handler](#custom-handlers) for 91 | - Works with: 92 | - Neovim's built-in `:h packpath` (`:h packadd`) 93 | - Any plugin manager that supports manually lazy-loading 94 | plugins by name 95 | - Configurable in multiple files 96 | 97 | ## :moon: Introduction 98 | 99 | `lze` provides abstractions for lazy-loading Neovim plugins, 100 | with an API that is loosely based on [`lazy.nvim`](https://github.com/folke/lazy.nvim), 101 | but reduced down to the very basics required for lazy-loading only. 102 | 103 | If attempting lazy loading via autocommands, it can get very verbose 104 | when you wish to load a plugin on multiple triggers. 105 | 106 | This greatly simplifies that process, and is easy to extend with 107 | your own custom fields via [custom handlers](#custom-handlers), 108 | the same mechanism through which the builtin handlers are created. 109 | 110 | > [!NOTE] 111 | > 112 | > **Should I lazy-load plugins?** 113 | > 114 | > It should be a plugin author's responsibility to ensure their plugin doesn't 115 | > unnecessarily impact startup time, not yours! 116 | > 117 | > See [nvim-neorocks "DO's and DONT's" guide for plugin developers](https://github.com/nvim-neorocks/nvim-best-practices?tab=readme-ov-file#sleeping_bed-lazy-loading). 118 | > 119 | > Regardless, some authors may not have the capacity 120 | > or knowledge to improve their plugins' startup impact. 121 | > 122 | > If you find a plugin that takes too long to load, 123 | > or worse, forces you to load it manually at startup with a 124 | > call to a heavy `setup` function, 125 | > consider opening an issue on the plugin's issue tracker. 126 | 127 | ### :milky_way: Philosophy 128 | 129 | `lze` is designed based on the UNIX philosophy: Do one thing well. 130 | 131 | ### :zzz: Comparison with `lazy.nvim` 132 | 133 | - `lze` is **not a plugin manager**, but focuses **on lazy-loading only**. 134 | It is intended to be used with (or by) a plugin manager. 135 | - The feature set is minimal, to [reduce code complexity](https://grugbrain.dev/) 136 | and simplify the API. 137 | For example, the following `lazy.nvim` features are **out of scope**: 138 | - Merging multiple plugin specs for a single plugin 139 | (primarily intended for use by Neovim distributions). 140 | - `lazy.vim` completely disables and takes over Neovim's 141 | built-in loading mechanisms, including 142 | adding a plugin's API (`lua`, `autoload`, ...) 143 | to the runtimepath. 144 | `lze` doesn't. 145 | Its only concern is plugin initialization, which is 146 | the bulk of the startup overhead. 147 | - Automatic lazy-loading of colorschemes. 148 | `lze` provides a `colorscheme` handler in the plugin spec. 149 | - Heuristics for determining a `main` module and automatically calling 150 | a `setup()` function. 151 | - Abstractions for plugin configuration with an `opts` table. 152 | `lze` provides simple hooks that you can use to specify 153 | when to load configurations. 154 | - Heuristics for automatically loading plugins on `require`. 155 | You can lazy-load on require with some configuration however! 156 | - Features related to plugin management. 157 | - Profiling tools. 158 | - UI. 159 | - Some configuration options are different. 160 | 161 | ## :pencil: Requirements 162 | 163 | - `Neovim >= 0.7.0` 164 | 165 | ## :books: Usage 166 | 167 | ```lua 168 | require("lze").load(plugins) 169 | ``` 170 | 171 | - **plugins**: this should be a `table` or a `string` 172 | - `table`: 173 | - A list with your [Plugin Specs](#plugin-spec) 174 | - Or a single plugin spec. 175 | - `string`: a Lua module name that contains your [Plugin Spec](#plugin-spec). 176 | See [Structuring Your Plugins](#structuring-your-plugins) 177 | 178 | > [!TIP] 179 | > You can call require('lze').load() as many times as you wish. 180 | > 181 | > - See also: [`:h lze`](./doc/lze.txt) 182 | 183 | > [!IMPORTANT] 184 | > 185 | > Since merging configs is out of scope, calling `load()` with conflicting 186 | > plugin specs is not supported. It will prevent you from doing so, 187 | > and return the list of duplicate names 188 | 189 | ### Examples 190 | 191 | ```lua 192 | require("lze").load { 193 | { 194 | "neo-tree.nvim", 195 | keys = { 196 | -- Create a key mapping and lazy-load when it is used 197 | { "ft", "Neotree toggle", desc = "NeoTree toggle" }, 198 | }, 199 | after = function() 200 | require("neo-tree").setup() 201 | end, 202 | }, 203 | { 204 | "crates.nvim", 205 | -- lazy-load when opening a toml file 206 | ft = "toml", 207 | }, 208 | { 209 | "sweetie.nvim", 210 | -- lazy-load when setting the `sweetie` colorscheme 211 | colorscheme = "sweetie", 212 | }, 213 | { 214 | "vim-startuptime", 215 | cmd = "StartupTime", 216 | before = function() 217 | -- Configuration for plugins that don't force you to call a `setup` function 218 | -- for initialization should typically go in a `before` 219 | --- or `beforeAll` function. 220 | vim.g.startuptime_tries = 10 221 | end, 222 | }, 223 | { 224 | "care.nvim", 225 | -- load care.nvim on InsertEnter 226 | event = "InsertEnter", 227 | }, 228 | { 229 | "dial.nvim", 230 | -- lazy-load on keys. -- Mode is `n` by default. 231 | keys = { "", { "", mode = "n" } }, 232 | }, 233 | } 234 | ``` 235 | 236 | 237 |
238 | 239 | vim.pack.add example 240 | 241 | 242 | There are several ways to use `lze` with `vim.pack.add`. Some of them are listed here. 243 | 244 | ```lua 245 | vim.pack.add({ 246 | "https://github.com/BirdeeHub/lze", 247 | "https://github.com/Wansmer/treesj", 248 | { src = "https://github.com/NTBBloodBatch/sweetie.nvim", name = "sweetie" } 249 | }, { 250 | -- prevent packadd! or packadd like this to allow on_require handler to load plugin spec 251 | load = function() end, 252 | -- choose your preference for install confirmation 253 | confirm = true, 254 | }) 255 | vim.cmd.packadd("lze") 256 | 257 | require("lze").load { 258 | { 259 | "sweetie", -- note the name change above 260 | colorscheme = "sweetie", 261 | }, 262 | { 263 | "treesj", 264 | cmd = { "TSJToggle" }, 265 | keys = { { "Tt", ":TSJToggle", mode = { "n" }, desc = "treesj split/join" }, }, 266 | after = function(_) 267 | require('treesj').setup({}) 268 | end, 269 | } 270 | } 271 | ``` 272 | 273 | OR 274 | 275 | ```lua 276 | vim.pack.add({ 277 | "https://github.com/BirdeeHub/lze", 278 | { src = "https://github.com/Wansmer/treesj", data = { opt = true, } }, 279 | { src = "https://github.com/NTBBloodBatch/sweetie.nvim", name = "sweetie", data = { opt = true, } } 280 | }, { 281 | load = function(p) 282 | if not (p.spec.data or {}).opt then 283 | vim.cmd.packadd(p.spec.name) 284 | end 285 | end, 286 | -- choose your preference for install confirmation 287 | confirm = true, 288 | }) 289 | 290 | require("lze").load { 291 | { 292 | "sweetie", -- note the name change above 293 | colorscheme = "sweetie", 294 | }, 295 | { 296 | "treesj", 297 | cmd = { "TSJToggle" }, 298 | keys = { { "Tt", ":TSJToggle", mode = { "n" }, desc = "treesj split/join" }, }, 299 | after = function(_) 300 | require('treesj').setup({}) 301 | end, 302 | } 303 | } 304 | ``` 305 | 306 | OR 307 | 308 | ```lua 309 | vim.pack.add({ "https://github.com/BirdeeHub/lze", }, { confirm = false --[[or true, up to you]], }) 310 | vim.pack.add({ 311 | { 312 | src = "https://github.com/NTBBloodBatch/sweetie.nvim", 313 | data = { 314 | colorscheme = "sweetie", 315 | } 316 | }, 317 | { 318 | src = "https://github.com/Wansmer/treesj", 319 | data = { 320 | cmd = { "TSJToggle" }, 321 | -- spec.data rejects mixed tables, so for keys, use lhs and rhs instead of [1] and [2] 322 | -- see: https://github.com/neovim/neovim/issues/35550 323 | keys = { { lhs = "Tt", rhs = ":TSJToggle", mode = { "n" }, desc = "treesj split/join" }, }, 324 | after = function(_) 325 | require('treesj').setup({}) 326 | end, 327 | } 328 | } 329 | }, { 330 | load = function(p) 331 | local spec = p.spec.data or {} 332 | spec.name = p.spec.name 333 | require('lze').load(spec) 334 | end, 335 | -- choose your preference for install confirmation 336 | confirm = true, 337 | }) 338 | ``` 339 | 340 |
341 | 342 |
343 | 344 | paq-nvim example 345 | 346 | 347 | ```lua 348 | require "paq" { 349 | "BirdeeHub/lze", 350 | { "nvim-telescope/telescope.nvim", opt = true }, 351 | { "NTBBloodBatch/sweetie.nvim", opt = true } 352 | } 353 | 354 | require("lze").load { 355 | { 356 | "telescope.nvim", 357 | cmd = "Telescope", 358 | }, 359 | { 360 | "sweetie.nvim", 361 | colorscheme = "sweetie", 362 | }, 363 | } 364 | ``` 365 | 366 |
367 | 368 |
369 | 370 | Nix examples 371 | 372 | 373 | - Home Manager: 374 | 375 | ```nix 376 | programs.neovim = { 377 | enable = true; 378 | plugins = with pkgs.vimPlugins [ 379 | { 380 | plugin = lze; 381 | # config = '' 382 | # -- optional, add extra handlers 383 | # -- require("lze").register_handlers(some_custom_handler_here) 384 | # ''; 385 | # type = "lua"; 386 | } 387 | { 388 | plugin = telescope-nvim; 389 | config = '' 390 | require("lze").load { 391 | "telescope.nvim", 392 | cmd = "Telescope", 393 | } 394 | ''; 395 | type = "lua"; 396 | optional = true; 397 | } 398 | { 399 | plugin = sweetie-nvim; 400 | config = '' 401 | require("lze").load { 402 | "sweetie.nvim", 403 | colorscheme = "sweetie", 404 | } 405 | ''; 406 | type = "lua"; 407 | optional = true; 408 | } 409 | ]; 410 | }; 411 | ``` 412 | 413 | - [nixCats](https://github.com/BirdeeHub/nixCats-nvim) 414 | 415 | While the home manager syntax is also accepted by `nixCats` anywhere we can add plugins, 416 | `nixCats` allows you to configure it in your normal lua files. 417 | 418 | Add it to your `startupPlugins` set as shown below, 419 | put the desired plugins in `optionalPlugins` so they don't auto-load, 420 | then configure as in [the regular examples](#examples) 421 | wherever you want in your config. 422 | 423 | ```nix 424 | # in your categoryDefinitions 425 | categoryDefinitions = { pkgs, settings, categories, name, ... }: { 426 | # :help nixCats.flake.outputs.categories 427 | startupPlugins = with pkgs.vimPlugins; { 428 | someName = [ 429 | # in startupPlugins so that it is available 430 | lze 431 | ]; 432 | }; 433 | optionalPlugins = with pkgs.vimPlugins; { 434 | someName = [ 435 | # the plugins you wish to load via lze 436 | ]; 437 | # you can name the categories whatever you want, 438 | # the important thing is, 439 | # optionalPlugins is for lazy loading via packadd 440 | }; 441 | }; 442 | # see :help nixCats.flake.outputs.packageDefinitions 443 | packageDefinitions = { 444 | nvim = {pkgs , ... }: { 445 | # see :help nixCats.flake.outputs.settings 446 | settings = {/* your settings */ }; 447 | categories = { 448 | # don't forget to enable it for the desired package! 449 | someName = true; 450 | # ... your other categories here 451 | }; 452 | }; 453 | }; 454 | # ... the rest of your nix where you call the builder and export packages 455 | ``` 456 | 457 | - Not on nixpkgs-unstable? 458 | 459 | If your neovim is not on the `nixpkgs-unstable` channel, 460 | `vimPlugins.lze` may not yet be in nixpkgs for you. 461 | You may instead get it from this flake! 462 | ```nix 463 | # in your flake inputs: 464 | inputs = { 465 | lze.url = "github:BirdeeHub/lze"; 466 | }; 467 | ``` 468 | Then, pass your config your inputs from your flake, 469 | and retrieve `lze` with: 470 | ```nix 471 | inputs.lze.packages.${pkgs.system}.default`: 472 | ``` 473 | 474 |
475 | 476 | 477 | ### :wrench: Configuration 478 | 479 | You can override the function used to load plugins. 480 | `lze` has the following defaults: 481 | 482 | ```lua 483 | vim.g.lze = { 484 | injects = {}, 485 | ---@type fun(name: string) 486 | load = vim.cmd.packadd, 487 | ---@type boolean 488 | verbose = true, 489 | ---@type integer 490 | default_priority = 50, 491 | ---@type boolean 492 | without_default_handlers = false, 493 | } 494 | ``` 495 | 496 | `vim.g.lze.without_default_handlers` must be set before you require `lze` 497 | or it will have no effect. 498 | 499 | If `vim.g.lze.verbose` is `false` it will not print a warning 500 | in cases of duplicate and missing plugins, or when passing in an empty list. 501 | 502 | `vim.g.lze.load` defines the fallback function used for the load hook for plugins. 503 | This value is not present in the plugin spec when handlers receive the plugin spec. 504 | 505 | In contrast, `vim.g.lze.injects` injects default values 506 | for ANY field BEFORE any handlers receive the plugin spec. 507 | 508 | ### Plugin spec 509 | 510 | #### Loading hooks 511 | 512 | 513 | | Property | Type | Description | `lazy.nvim` equivalent | 514 | |------------------|------|-------------|-----------------------| 515 | | **[1]** | `string` | REQUIRED. The plugin's name (not the module name, and not the url). This is the directory name of the plugin in the packpath and is usually the same as the repo name of the repo it was cloned from. | `name`[^1] | 516 | | **enabled?** | `boolean` or `fun():boolean` | When `false`, or if the `function` returns `nil` or `false`, then this plugin will not be included in the spec. | `enabled` | 517 | | **beforeAll?** | `fun(lze.Plugin)` | Always executed upon calling `require('lze').load(spec)` before any plugin specs from that call are triggered to be loaded. | `init` | 518 | | **before?** | `fun(lze.Plugin)` | Executed before a plugin is loaded. | None | 519 | | **after?** | `fun(lze.Plugin)` | Executed after a plugin is loaded. | `config` | 520 | | **priority?** | `number` | Only useful for **start** plugins (not lazy-loaded) added within **the same `require('lze').load(spec)` call** to force loading certain plugins first. Default priority is `50`, or the value of `vim.g.lze.default_priority`. | `priority` | 521 | | **load?** | `fun(string)` | Can be used to override the `vim.g.lze.load(name)` function for an individual plugin. (default is `vim.cmd.packadd(name)`)[^2] | None. | 522 | | **allow_again?** | `boolean` or `fun():boolean` | When a plugin has ALREADY BEEN LOADED, true would allow you to add it again. No idea why you would want this outside of testing. | None. | 523 | | **lazy?** | `boolean` | Using a handler's field sets this automatically, but you can set this manually as well. | `lazy` | 524 | 525 | 526 | #### Lazy-loading triggers provided by the default handlers 527 | 528 | 529 | | Property | Type | Description | `lazy.nvim` equivalent | 530 | |----------|------|-------------|----------------------| 531 | | **event?** | `string` or `{event?:string\|string[], pattern?:string\|string[]}\` or `string[]` | Lazy-load on event. Events can be specified as `BufEnter` or with a pattern like `BufEnter *.lua`. | `event` | 532 | | **cmd?** | `string` or `string[]` | Lazy-load on command. | `cmd` | 533 | | **ft?** | `string` or `string[]` | Lazy-load on filetype. | `ft` | 534 | | **keys?** | `string` or `string[]` or `lze.KeysSpec[]` | Lazy-load on key mapping. | `keys` | 535 | | **colorscheme?** | `string` or `string[]` | Lazy-load on colorscheme. | None. `lazy.nvim` lazy-loads colorschemes automatically[^3]. | 536 | | **dep_of?** | `string` or `string[]` | Lazy-load before another plugin but after its `before` hook. Accepts a plugin name or a list of plugin names. | None but is sorta the reverse of the dependencies key of the `lazy.nvim` plugin spec | 537 | | **on_plugin?** | `string` or `string[]` | Lazy-load after another plugin but before its `after` hook. Accepts a plugin name or a list of plugin names. | None. | 538 | | **on_require?** | `string` or `string[]` | Accepts a top-level **lua module** name or a list of top-level **lua module** names. Will load when any submodule of those listed is `require`d | None. `lazy.nvim` does this automatically. | 539 | 540 | 541 | [^1]: In contrast to `lazy.nvim`'s `name` field, a `lze.PluginSpec`'s `name` *is not optional*. 542 | This is because `lze` is not a plugin manager and needs to be told which 543 | plugins to load. 544 | [^2]: for example, lazy-loading cmp sources will 545 | require you to source its `after/plugin` file, 546 | as packadd does not do this automatically for you. 547 | [^3]: The reason this library doesn't lazy-load colorschemes automatically is that 548 | it would have to know where the plugin is installed in order to determine 549 | which plugin to load. 550 | 551 | ### User events 552 | 553 | - `DeferredUIEnter`: Triggered when `require('lze').load()` is done and after `UIEnter`. 554 | Can be used as an `event` to lazy-load plugins that are not immediately needed 555 | for the initial UI[^4]. 556 | 557 | But users may define more aliased events if they wish. 558 | The event handler exports a function you may call to set them. 559 | 560 | `require('lze').h.event.set_event_alias(name: string, spec: lze.EventSpec?)` 561 | 562 | [^4]: This is equivalent to `lazy.nvim`'s `VeryLazy` event. 563 | 564 | ### Plugins with after directories 565 | 566 | Relying on another plugin's `plugin` or `after/plugin` scripts is considered a bug, 567 | as Neovim's built-in loading mechanism does not guarantee initialisation order. 568 | Requiring users to manually call a `setup` function [is an anti pattern](https://github.com/nvim-neorocks/nvim-best-practices?tab=readme-ov-file#zap-initialization). 569 | Forcing users to think about the order in which they load plugins that 570 | extend or depend on each other is not great either and we 571 | suggest opening an issue or submitting 572 | a PR to fix any of these issues upstream. 573 | 574 | > [!NOTE] 575 | > 576 | > - `vim.cmd.packadd` does not work with plugins that rely 577 | > on `after` directories of plugins, such as many 578 | > nvim-cmp sources. 579 | > To source `after` directories of a plugin, 580 | > you should replace the load function for the plugin with: 581 | 582 | ```lua 583 | local function load_with_after(name) 584 | vim.cmd.packadd(name) 585 | vim.cmd.packadd(name .. "/after") 586 | end 587 | ``` 588 | 589 | For example: 590 | 591 | ```lua 592 | require("lze").load { 593 | "cmp-cmdline", 594 | on_plugin = { "nvim-cmp" }, 595 | load = load_with_after, 596 | } 597 | ``` 598 | 599 | [lzextras](https://github.com/BirdeeHub/lzextras?tab=readme-ov-file#loaders) 600 | provides this function and a few others as well! 601 | 602 | > [!NOTE] 603 | > 604 | > - You may also wish to use [`rtp.nvim`](https://github.com/nvim-neorocks/rtp.nvim) 605 | > for sourcing `ftdetect` files in plugins without loading them, 606 | > for when plugins provide their own filetypes 607 | > and you wish to trigger on that filetype. 608 | 609 | ### Structuring Your Plugins 610 | 611 | Unlike `lazy.nvim`, in `lze` you may call 612 | `require('lze').load` as many times as you would like. 613 | 614 | This means being able to import files via specs is not as useful. 615 | 616 | The `import` spec of `lze` allows for importing a single lua module, 617 | unlike `lz.n` or `lazy.nvim`, where it imports an entire directory. 618 | 619 | The `import` spec of `lze` also accepts another `lze.Spec` type. 620 | This may prove useful when generating definitions from another templating tool. 621 | 622 | That module may return a list of specs, 623 | which means it can also return a list of import specs. 624 | 625 | This way, you get to choose the order, and can 626 | have files in that directory that are not imported if you wish. 627 | 628 | ```lua 629 | require("lze").load("plugins") 630 | ``` 631 | 632 | where `lua/plugins` in your config contains an `init.lua` with something like, 633 | 634 | 635 | ```lua 636 | return { 637 | { 638 | "undotree", 639 | cmd = { 640 | "UndotreeToggle", 641 | "UndotreeHide", 642 | "UndotreeShow", 643 | "UndotreeFocus", 644 | "UndotreePersistUndo", 645 | }, 646 | keys = { { "U", "UndotreeToggle", mode = { "n" }, desc = "Undo Tree" }, }, 647 | before = function(_) 648 | vim.g.undotree_WindowLayout = 1 649 | vim.g.undotree_SplitWidth = 40 650 | end, 651 | }, 652 | { import = "plugins.afile" }, 653 | { import = "plugins.another" }, 654 | { import = "plugins.another_file" }, 655 | { import = "plugins.yet_another_file" }, 656 | } 657 | ``` 658 | 659 | 660 | where the imported files would return plugin specs as shown above. 661 | 662 | ## :electric_plug: API 663 | 664 | ### Custom handlers 665 | 666 | You may register your own handlers to lazy-load plugins via 667 | other triggers not already covered by the plugin spec. 668 | 669 | ```lua 670 | ---@param handlers lze.Handler[]|lze.Handler|lze.HandlerSpec[]|lze.HandlerSpec 671 | ---@return string[] handlers_registered 672 | require("lze").register_handlers({ 673 | require("my_handlers.module1"), 674 | require("my_handlers.module2"), 675 | { 676 | handler = require("my_handlers.module3"), 677 | enabled = true, 678 | }, 679 | }) 680 | ``` 681 | 682 | You may call this function multiple times, 683 | each call will append the new handlers (if enabled) to the end of the list. 684 | 685 | The handlers define the fields you may use for lazy loading, 686 | with the fields like `ft` and `event` that exist 687 | in the default plugin spec being defined by 688 | the [default handlers](./lua/lze/h). 689 | 690 | The order of this list of handlers is important. 691 | 692 | It is the same as the order in which their hooks are called. 693 | 694 | If you wish to redefine a default handler, or change the order 695 | in which the default handlers are called, 696 | there exists a `require('lze').clear_handlers()` 697 | and a `require('lze').remove_handlers(handler_names: string|string[])` 698 | function for this purpose. They return the removed handlers. 699 | 700 | > [!WARNING] 701 | > You must register ALL handlers before calling `require('lze').load`, 702 | > because they will not be retroactively applied to 703 | > the `load` calls that occur before they are registered. 704 | > 705 | > In addition, removing a handler after it already 706 | > has had plugins added to it is undefined behavior. 707 | > Existing plugin items will remain in state 708 | > and trigger-able via `require('lze').trigger_load` 709 | > 710 | > While the default handlers clear their state when removed, 711 | > it is not necessary to be adding and removing handlers often 712 | > for the purpose of loading plugins or various things in your config. 713 | > 714 | > So you should do ALL handler additions AND removals 715 | > BEFORE calling `require('lze').load`. 716 | 717 | #### lze.HandlerSpec 718 | 719 | You can also add them as specs instead of just directly as a list. 720 | 721 | 722 | | Property | Type | Description | 723 | | --- | --- | --- | 724 | | handler | `lze.Handler` | the `lze.Handler` you wish to add | 725 | | enabled? | `boolean?` or `fun():boolean?` | determines at time of registration if the handler should be added or not. Defaults to `true` | 726 | 727 | 728 | ### Writing Custom Handlers 729 | 730 | #### `lze.Handler` 731 | 732 | 733 | | Property | Type | Description | 734 | | --- | --- | --- | 735 | | spec_field | `string` | the `lze.PluginSpec` field used to configure the handler | 736 | | add? | `fun(plugin: lze.Plugin): fun()?` | called once for each handler before any plugin has been loaded. Tells your handler about each plugin so you can set up a trigger for it if your handler was used. | 737 | | before? | `fun(name: string)` | called after each plugin spec's before `hook`, and before its `load` hook | 738 | | after? | `fun(name: string)` | called after each plugin spec's `load` hook and before its `after` hook | 739 | | modify? | `fun(plugin: lze.Plugin): lze.Plugin, fun()?` | This function is called before a plugin is added to state. It is your one chance to modify the plugin spec, it is active only if your spec_field was used in that spec, and is called in the order the handlers have been added. | 740 | | set_lazy? | `boolean` | Whether using this handler's field should have an effect on the lazy setting. True or nil is true. Default: nil | 741 | | post_def? | `fun()` | For adding custom triggers such as the event handler's `DeferredUIEnter` event, called at the end of `require('lze').load` | 742 | | lib? | `table` | Handlers may export functions and other values via this set, which then may be accessed via `require('lze').h[spec_field].your_func()` | 743 | | init? | `fun()` | Called when the handler is registered. | 744 | | cleanup? | `fun()` | Called when the handler is removed. | 745 | 746 | 747 | All handler hooks will be called in the order in which your handlers are registered. 748 | 749 | Your handler first has a chance to modify the 750 | parsed plugin spec before it is loaded into the state of `lze`. 751 | None of the builtin handlers have this hook. 752 | 753 | The `modify` field of a handler will only be called if that handler's 754 | `spec_field` was used in that [plugin spec](#plugin-spec) (meaning, it is not nil). 755 | 756 | It is called before the plugin is added to state, 757 | and thus you will not be able to call `trigger_load` on it yet. 758 | To get around this, you may return a function 759 | as an optional second return value, which will 760 | be called after `add` and before any functions deferred by `add`. 761 | 762 | Then, your handler will have a chance to add plugins to its list to trigger 763 | via its `add` hook. The `add` hook is called 764 | before any plugins have been loaded 765 | in that `require('lze').load` call. 766 | 767 | You should also avoid calling `trigger_load` in the `add` hook, 768 | as it may not have been added to all handlers yet. 769 | You may optionally return a function in order to defer code 770 | until after all `add` hooks and all functions 771 | deferred by `modify` have been called. 772 | 773 | Your handler will then decide when to load 774 | a plugin and run its associated hooks 775 | using the `trigger_load` function. 776 | 777 | ```lua 778 | ---@overload fun(plugin_name: string | string[]): string[] 779 | require('lze').trigger_load 780 | ``` 781 | 782 | `trigger_load` will resist being called multiple times on the same plugin name. 783 | It will return the list of names it skipped. 784 | 785 | There exists a function to check if a plugin is available to be loaded. 786 | 787 | `require('lze').state(name)` will return true 788 | if the plugin is ready to be loaded, 789 | false if already loaded or currently being loaded, 790 | and nil if it was never added. 791 | 792 | Less performant, but more informative: 793 | 794 | For debugging purposes, or if necessary, 795 | you may use the table access form `require('lze').state[name]` 796 | which will return a COPY of the internal state of the plugin. 797 | 798 | You should already have a copy of the plugin 799 | via your handler's add function so you shouldn't 800 | ever NEED to get a copy. But it is nice for troubleshooting. 801 | 802 | > [!TIP] 803 | > You should delete the plugin from your handler's state 804 | > in either the `before` or `after` hooks 805 | > so that you don't have to carry around 806 | > unnecessary state and increase your chance of error and your memory usage. 807 | > However, not doing so would not cause any bugs in `lze`. 808 | > It just might let you call `trigger_load` multiple times to no effect. 809 | 810 | ## :green_heart: Contributing 811 | 812 | All contributions are welcome! 813 | See [CONTRIBUTING.md](./CONTRIBUTING.md). 814 | 815 | ## :book: License 816 | 817 | This library is [licensed](./LICENSE) according to GPL version 2 818 | or (at your option) any later version. 819 | 820 | 821 | 822 | [neovim-shield]: https://img.shields.io/badge/NeoVim-%2357A143.svg?&style=for-the-badge&logo=neovim&logoColor=white 823 | [neovim-url]: https://neovim.io/ 824 | [lua-shield]: https://img.shields.io/badge/lua-%232C2D72.svg?style=for-the-badge&logo=lua&logoColor=white 825 | [lua-url]: https://www.lua.org/ 826 | [luarocks-shield]: 827 | https://img.shields.io/luarocks/v/BirdeeHub/lze?logo=lua&color=purple&style=for-the-badge 828 | [luarocks-url]: https://luarocks.org/modules/BirdeeHub/lze 829 | 830 | --------------------------------------------------------------------------------