Compare commits
43 Commits
71b78bfca4
...
lazy
Author | SHA1 | Date | |
---|---|---|---|
70853bd01e
|
|||
9d4322572c
|
|||
3b33a604d8
|
|||
5aff147731
|
|||
edf32fbf06
|
|||
adec93b7f4
|
|||
1cd5f63f8e
|
|||
8e46eddecd
|
|||
542298c1fe
|
|||
cfc3f08d53
|
|||
1c2f49fcfa
|
|||
f6209048f1
|
|||
5bd30d9397
|
|||
1538046b6f
|
|||
5deffca36e
|
|||
318a86d786
|
|||
5541a656e2
|
|||
59a963a5c5
|
|||
cd43b3b853
|
|||
585f04c745
|
|||
a13d616da5
|
|||
26ef7289f6
|
|||
d406ee18f2
|
|||
e0f39fe0db
|
|||
e8276e3137
|
|||
44ec0633ce
|
|||
fa3457e463
|
|||
f4d1c4cf25
|
|||
2346a0baa5
|
|||
8b91fc9c2f
|
|||
b60e6a3934
|
|||
ac14e3d5bb
|
|||
07df092fc6
|
|||
1623276cb0
|
|||
92cf3634c9
|
|||
84ee8414f7
|
|||
84ac4aef17
|
|||
b7218c64c2
|
|||
0789260637
|
|||
561f1d8f8b
|
|||
fd1c5d6e79
|
|||
6bd6db3c02
|
|||
eba21a8021
|
11
.githooks/pre-commit
Executable file
11
.githooks/pre-commit
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
echo "Running tests before commit..."
|
||||||
|
|
||||||
|
# run tests
|
||||||
|
make test || {
|
||||||
|
echo "Tests failed. Commit aborted."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Tests passed. Proceeding with commit."
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
|
doc/tags
|
||||||
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2023 squibid
|
Copyright (c) 2023-2025 squibid
|
||||||
Copyright (c) 2021-2023 chiya.dev
|
Copyright (c) 2021-2023 chiya.dev
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
4
Makefile
Normal file
4
Makefile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
test:
|
||||||
|
nvim --headless -c "PlenaryBustedDirectory tests/ {minimal_init = './tests/minit.lua'}"
|
||||||
|
|
||||||
|
.PHONY: test
|
39
README.md
39
README.md
@ -1,7 +1,5 @@
|
|||||||
# dep
|
# dep
|
||||||
|
|
||||||
> This readme is a work in progress.
|
|
||||||
|
|
||||||
A versatile, declarative and correct [neovim][2] package manager in [Lua][3].
|
A versatile, declarative and correct [neovim][2] package manager in [Lua][3].
|
||||||
Originally written for personal use by [luaneko][4]. Adapted by [squibid][5] for
|
Originally written for personal use by [luaneko][4]. Adapted by [squibid][5] for
|
||||||
general use.
|
general use.
|
||||||
@ -11,13 +9,14 @@ What does that mean?
|
|||||||
1. `versatile` - packages can be declared in any Lua file in any order of your
|
1. `versatile` - packages can be declared in any Lua file in any order of your
|
||||||
liking.
|
liking.
|
||||||
2. `declarative` - packages are declared using simple Lua tables.
|
2. `declarative` - packages are declared using simple Lua tables.
|
||||||
3. `correct` - packages are always loaded in a correct and consistent order.
|
3. `correct` - packages are always loaded in a correct and consistent order
|
||||||
|
(barring any lazy loading).
|
||||||
|
|
||||||
In addition to the above dep has been built to be completely in control of you,
|
In addition to the above dep has been built to be completely in control of you,
|
||||||
the user. With the help of lazy loading you can choose when your plugin loads
|
the user. With the help of lazy loading you can choose when your plugin loads
|
||||||
down to the finest detail (examples may be found below).
|
down to the finest detail (examples may be found below).
|
||||||
|
|
||||||
See also squibid's [neovim-configs][10] for an example of how dep can be used in
|
See also squibid's [neovim-config][10] for an example of how dep can be used in
|
||||||
practice.
|
practice.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
@ -35,7 +34,7 @@ practice.
|
|||||||
local path = vim.fn.stdpath("data") .. "/site/pack/deps/opt/dep"
|
local path = vim.fn.stdpath("data") .. "/site/pack/deps/opt/dep"
|
||||||
|
|
||||||
if vim.fn.empty(vim.fn.glob(path)) > 0 then
|
if vim.fn.empty(vim.fn.glob(path)) > 0 then
|
||||||
vim.fn.system({ "git", "clone", "--depth=1", "https://git.squi.bid/dep", path })
|
vim.fn.system({ "git", "clone", "--depth=1", "https://git.squi.bid/squibid/dep", path })
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.cmd("packadd dep")
|
vim.cmd("packadd dep")
|
||||||
@ -57,6 +56,7 @@ require "dep" {
|
|||||||
- `:DepClean` - cleans removed packages.
|
- `:DepClean` - cleans removed packages.
|
||||||
- `:DepReload` - reloads all packages.
|
- `:DepReload` - reloads all packages.
|
||||||
- `:DepLog` - opens the log file.
|
- `:DepLog` - opens the log file.
|
||||||
|
- `:DepUi` - opens the ui.
|
||||||
|
|
||||||
## Package specification
|
## Package specification
|
||||||
|
|
||||||
@ -83,8 +83,9 @@ A package must be declared in the following format.
|
|||||||
os.execute(...)
|
os.execute(...)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
-- [function] Code used to determine when the package should be loaded.
|
-- [function|true] Code used to determine when the package should be loaded.
|
||||||
lazy = function(load)
|
lazy = function(load)
|
||||||
|
load:cmd("LoadPackage")
|
||||||
end,
|
end,
|
||||||
|
|
||||||
-- [string] Overrides the short name of the package.
|
-- [string] Overrides the short name of the package.
|
||||||
@ -93,7 +94,7 @@ A package must be declared in the following format.
|
|||||||
|
|
||||||
-- [string] Overrides the URL of the git repository to clone.
|
-- [string] Overrides the URL of the git repository to clone.
|
||||||
-- Defaults to "https://github.com/{full_name}.git".
|
-- Defaults to "https://github.com/{full_name}.git".
|
||||||
url = "https://git.chiya.dev/user/package.git",
|
url = "https://git.squi.bid/user/package.git",
|
||||||
|
|
||||||
-- [string] Overrides the source in which the package is gotten
|
-- [string] Overrides the source in which the package is gotten
|
||||||
-- from. This is not set by default.
|
-- from. This is not set by default.
|
||||||
@ -351,7 +352,7 @@ require "dep" {
|
|||||||
```
|
```
|
||||||
|
|
||||||
If you're in the need of a deeper understanding of how the utils work go check
|
If you're in the need of a deeper understanding of how the utils work go check
|
||||||
out `lua/lazy/utils.lua` for the source code.
|
out `lua/lazy/loader/init.lua` for the source code.
|
||||||
|
|
||||||
## Separating code into modules
|
## Separating code into modules
|
||||||
|
|
||||||
@ -378,6 +379,13 @@ return {
|
|||||||
Package specifications from other modules can be loaded using the `modules` option.
|
Package specifications from other modules can be loaded using the `modules` option.
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
|
require "dep" {
|
||||||
|
modules = {
|
||||||
|
prefix = "packages"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- the above is equivalent to
|
||||||
require "dep" {
|
require "dep" {
|
||||||
modules = {
|
modules = {
|
||||||
prefix = "packages.",
|
prefix = "packages.",
|
||||||
@ -386,7 +394,7 @@ require "dep" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
-- the above is equivalent to
|
-- or
|
||||||
require "dep" {
|
require "dep" {
|
||||||
modules = {
|
modules = {
|
||||||
"packages.search",
|
"packages.search",
|
||||||
@ -469,6 +477,19 @@ require "dep" {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Known Issues
|
||||||
|
|
||||||
|
- Lazy loading nvim-cmp doesn't work as the external sources don't get reconized
|
||||||
|
by nvim-cmp when it's loaded.
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
When contributing you may choose to run tests before commiting changes, if that
|
||||||
|
is so you may choose to run the following:
|
||||||
|
```sh
|
||||||
|
git config core.hooksPath .githooks
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
dep is licensed under the [MIT License](LICENSE).
|
dep is licensed under the [MIT License](LICENSE).
|
||||||
|
670
doc/dep.txt
Normal file
670
doc/dep.txt
Normal file
@ -0,0 +1,670 @@
|
|||||||
|
*dep.txt* Declarative Package Manager 02-Jul-2025
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
Table of Contents *dep-table-of-contents*
|
||||||
|
|
||||||
|
1. Introduction |dep|
|
||||||
|
2. Setup |dep-setup|
|
||||||
|
3. Specs |dep-spec|
|
||||||
|
- Package Spec |dep-package-spec|
|
||||||
|
- Module Spec |dep-module-spec|
|
||||||
|
4. Lazy Loading |dep-lazy-loading|
|
||||||
|
- Lazy Loading API |dep-lazy-loading-api|
|
||||||
|
- Lazy Loading API Shorthands |dep-lazy-loading-api-shorthands|
|
||||||
|
5. Commands |dep-commands|
|
||||||
|
6. Examples |dep-examples|
|
||||||
|
- Declaring Dependencies |dep-examples-declaring-dependencies|
|
||||||
|
- Modules |dep-examples-modules|
|
||||||
|
- Lazy Loading |dep-examples-lazy-loading|
|
||||||
|
7. Credits & License |dep-credits|
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Introduction *dep*
|
||||||
|
|
||||||
|
A versatile, declarative and correct neovim package manager in Lua. Originally
|
||||||
|
written for personal use by luaneko. Adapted by squibid for general use.
|
||||||
|
|
||||||
|
What does that mean?
|
||||||
|
|
||||||
|
1. `versatile` - packages can be declared in any Lua file in any order of your
|
||||||
|
liking.
|
||||||
|
2. `declarative` - packages are declared using simple Lua tables.
|
||||||
|
3. `correct` - packages are always loaded in a correct and consistent order
|
||||||
|
(barring any lazy loading).
|
||||||
|
|
||||||
|
In addition to the above dep has been built to be completely in control of you,
|
||||||
|
the user. With the help of lazy loading you can choose when your plugin loads
|
||||||
|
down to the finest detail (examples may be found below).
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Setup *dep-setup*
|
||||||
|
|
||||||
|
Put the following anywhere before any actual use of dep.
|
||||||
|
|
||||||
|
>lua
|
||||||
|
local path = vim.fn.stdpath("data") .. "/site/pack/deps/opt/dep"
|
||||||
|
|
||||||
|
if vim.fn.empty(vim.fn.glob(path)) > 0 then
|
||||||
|
vim.fn.system({
|
||||||
|
"git",
|
||||||
|
"clone",
|
||||||
|
"--depth=1",
|
||||||
|
"https://git.squi.bid/squibid/dep",
|
||||||
|
path,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.cmd("packadd dep")
|
||||||
|
<
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Specs *dep-spec*
|
||||||
|
|
||||||
|
dep uses a variation of specifications to ensure everything works smoothly.
|
||||||
|
This includes a basic spec used when setting up dep:
|
||||||
|
>lua
|
||||||
|
require("dep") {
|
||||||
|
-- [string] Specifies when dep should automatically synchronize.
|
||||||
|
-- "never": disable this behavior
|
||||||
|
-- "new": only install newly declared packages (default)
|
||||||
|
-- "always": synchronize all packages on startup
|
||||||
|
sync = "new",
|
||||||
|
|
||||||
|
-- [array] Specifies the modules to load package specifications from.
|
||||||
|
-- Defaults to an empty table.
|
||||||
|
-- Items can be either an array of package specifications,
|
||||||
|
-- or a string that indicates the name of the module from which the array
|
||||||
|
-- of package specifications is loaded.
|
||||||
|
--
|
||||||
|
-- "."'s are added between the prefix and module names as required. In
|
||||||
|
-- addition if there is only a prefix and no modules supplied then dep
|
||||||
|
-- automatically loads all lua files in the directory.
|
||||||
|
modules = {
|
||||||
|
-- [string] Prefix string to prepend to all module names. This is
|
||||||
|
-- optional.
|
||||||
|
prefix = "",
|
||||||
|
|
||||||
|
-- [string] module names
|
||||||
|
...
|
||||||
|
},
|
||||||
|
|
||||||
|
-- [table|string] package specification(s)
|
||||||
|
...
|
||||||
|
}
|
||||||
|
<
|
||||||
|
PACKAGE SPEC *dep-package-spec*
|
||||||
|
>lua
|
||||||
|
{
|
||||||
|
-- [string] Specifies the full name of the package.
|
||||||
|
-- This is the only required field; all other fields are optional.
|
||||||
|
"user/package",
|
||||||
|
|
||||||
|
-- [function] Code to run before the package is loaded into neovim.
|
||||||
|
setup = function()
|
||||||
|
vim.g.package_config = ...
|
||||||
|
end,
|
||||||
|
|
||||||
|
-- [function] Code to run after the package is loaded into neovim.
|
||||||
|
load = function()
|
||||||
|
require "package".setup(...)
|
||||||
|
end,
|
||||||
|
|
||||||
|
-- [function] Code to run after the package is installed or updated.
|
||||||
|
config = function()
|
||||||
|
os.execute(...)
|
||||||
|
end,
|
||||||
|
|
||||||
|
-- [function|true] Code used to determine when the package should be
|
||||||
|
-- loaded.
|
||||||
|
lazy = function(load)
|
||||||
|
load:cmd("LoadPackage")
|
||||||
|
end,
|
||||||
|
|
||||||
|
-- [string] Overrides the short name of the package.
|
||||||
|
-- Defaults to a substring of the full name after '/'.
|
||||||
|
as = "custom_package",
|
||||||
|
|
||||||
|
-- [string] Overrides the URL of the git repository to clone.
|
||||||
|
-- Defaults to "https://github.com/{full_name}.git".
|
||||||
|
url = "https://git.squi.bid/user/package.git",
|
||||||
|
|
||||||
|
-- [string] Overrides the source in which the package is gotten
|
||||||
|
-- from. This is not set by default.
|
||||||
|
path = "~/my-local-package/",
|
||||||
|
|
||||||
|
-- [string] Overrides the name of the branch to clone.
|
||||||
|
-- Defaults to whatever the remote configured as their HEAD, which is
|
||||||
|
-- usually "master".
|
||||||
|
branch = "develop",
|
||||||
|
|
||||||
|
-- [string] Overrides the commit ref to target
|
||||||
|
-- Defaults to the latest commit on the current branch
|
||||||
|
commit = "e76cb03",
|
||||||
|
|
||||||
|
-- [boolean] Prevents the package from being loaded.
|
||||||
|
disable = true,
|
||||||
|
|
||||||
|
-- [boolean] Prevents the package from being updated.
|
||||||
|
pin = true,
|
||||||
|
|
||||||
|
-- [string|array] Specifies requirements that must be loaded before the
|
||||||
|
-- package. If given a string, it is wrapped into an array.
|
||||||
|
reqs = {...},
|
||||||
|
|
||||||
|
-- [string|array] Specifies dependents that must be loaded after the
|
||||||
|
-- package. If given a string, it is wrapped into an array.
|
||||||
|
deps = {...}
|
||||||
|
}
|
||||||
|
<
|
||||||
|
MODULE SPEC *dep-module-spec*
|
||||||
|
>lua
|
||||||
|
{
|
||||||
|
-- [string] Specifies a name for the module
|
||||||
|
name = "a name",
|
||||||
|
|
||||||
|
-- [string] Specifies a description of the module
|
||||||
|
desc = "a description of the module",
|
||||||
|
|
||||||
|
-- [boolean] Prevents all packages in the module from being loaded.
|
||||||
|
disable = false,
|
||||||
|
|
||||||
|
-- [table|string] package specification(s)
|
||||||
|
...
|
||||||
|
}
|
||||||
|
<
|
||||||
|
More information on the package specifications may be found in
|
||||||
|
|dep-package-spec|.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. Lazy Loading *dep-lazy-loading*
|
||||||
|
|
||||||
|
Lazy loading is important for making sure neovim can load nice and fast unlike
|
||||||
|
a certain bloated IDE. It has a seperate section in this documentation to
|
||||||
|
ensure that you can use it to it's full extent.
|
||||||
|
|
||||||
|
If you refer to the |dep-package-spec| you'll notice the `lazy` flag which may
|
||||||
|
be used to conditionally load a package. When it is set to a function you
|
||||||
|
choose when it runs and more information on that may be found in
|
||||||
|
|dep-lazy-loading-api|. In addition to setting it to a function you may set it
|
||||||
|
to `true` in which case dep takes care of loading it for you.
|
||||||
|
|
||||||
|
When setting a colorscheme dep checks to make sure that the plugin is loaded,
|
||||||
|
therefore it's recommended that you make use of the `lazy` flags ability to be
|
||||||
|
set to `true` by setting any colorscheme that you have installed, but do not
|
||||||
|
use as your main one to lazy.
|
||||||
|
|
||||||
|
LAZY LOADING API *dep-lazy-loading-api*
|
||||||
|
|
||||||
|
Within the |dep-package-spec| the lazy flag when set to a function takes one
|
||||||
|
argument `load` which is a class containing loading functions. For the
|
||||||
|
following examples assume that `load` is set to the class which may be found
|
||||||
|
within `lua/dep/lazy/loader/init.lua`.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
LOAD:CMD *dep-lazy-loading-api-cmd*
|
||||||
|
|
||||||
|
`load:cmd` is a function which allows you to specify a command for the package
|
||||||
|
to load on. It takes the similar arguments to |nvim_create_user_command()|
|
||||||
|
with a key difference in what the command runs. The following is an example of
|
||||||
|
what arguments the function takes:
|
||||||
|
>lua
|
||||||
|
load:cmd("Command", {})
|
||||||
|
<
|
||||||
|
Notice the missing 'command' argument which is found in
|
||||||
|
|nvim_create_user_command|, this is replaced by a callback function. The above
|
||||||
|
is equivalent to the following:
|
||||||
|
>lua
|
||||||
|
load:cmd("Command", {
|
||||||
|
callback = function()
|
||||||
|
load:cleanup()
|
||||||
|
|
||||||
|
if (rerun) then
|
||||||
|
vim.cmd("Command")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
})
|
||||||
|
<
|
||||||
|
If you wish the second argument may be completely ommitted. Note the inclusion
|
||||||
|
of a `rerun` field, this is a parameter which may be passed into the options table
|
||||||
|
to re-run the binding after loading the package. You may choose to disable the
|
||||||
|
built-in logic by passing false.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
LOAD:AUTO *dep-lazy-loading-api-auto*
|
||||||
|
|
||||||
|
`load:auto` is a function which allows you to specify an auto command for the
|
||||||
|
package to load on. It takes the same arguments as |nvim_create_autocmd()|.
|
||||||
|
The following is an example of using it:
|
||||||
|
>lua
|
||||||
|
load:auto("InsertEnter", {})
|
||||||
|
<
|
||||||
|
Just like with |nvim_create_autocmd()| you may choose to pass in a 'callback'
|
||||||
|
by default the above is equivalent to the following:
|
||||||
|
>lua
|
||||||
|
load:auto("InsertEnter", {
|
||||||
|
callback = function()
|
||||||
|
load:cleanup()
|
||||||
|
end
|
||||||
|
})
|
||||||
|
<
|
||||||
|
As with `load:cmd` the second argument may be ommitted.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
LOAD:FT *dep-lazy-loading-api-ft*
|
||||||
|
|
||||||
|
`load:ft` is a function which allows you to specify a filetype for the package
|
||||||
|
to load on. It takes one argument: 'filetype' like so:
|
||||||
|
>lua
|
||||||
|
load:ft("lua")
|
||||||
|
<
|
||||||
|
Which is equivalent to the following:
|
||||||
|
>lua
|
||||||
|
load:auto("FileType", {
|
||||||
|
pattern = "lua",
|
||||||
|
callback = function()
|
||||||
|
load:cleanup()
|
||||||
|
end
|
||||||
|
})
|
||||||
|
<
|
||||||
|
Note that this is just an expansion of `load:auto` for your convenience.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
LOAD:KEYMAP *dep-lazy-loading-api-keymap*
|
||||||
|
|
||||||
|
`load:keymap` is a function which allows you to specify a keymap for the
|
||||||
|
package to load on. It takes the similar arguments to |vim.keymap.set()| with a
|
||||||
|
key difference in what the command runs. The following is an example of what
|
||||||
|
arguments the function takes:
|
||||||
|
>lua
|
||||||
|
load:keymap("n", "<leader>p", {})
|
||||||
|
<
|
||||||
|
Notice the missing 'rhs' argument which is found in |vim.keymap.set|, this is
|
||||||
|
replaced by a callback function. The above is equivalent to the following:
|
||||||
|
>lua
|
||||||
|
load:keymap("n", "<leader>p", {
|
||||||
|
callback = function()
|
||||||
|
-- register keymap unload
|
||||||
|
load:cleanup()
|
||||||
|
|
||||||
|
-- call the keymap after the user has mapped it
|
||||||
|
if type(rerun) == "function" then
|
||||||
|
rerun()
|
||||||
|
elseif rerun then
|
||||||
|
local keys = vim.api.nvim_replace_termcodes(bind, true, false, true)
|
||||||
|
vim.api.nvim_input(keys)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
})
|
||||||
|
<
|
||||||
|
Note the inclusion of a `rerun` field, this is a parameter which may be passed
|
||||||
|
into the options table to re-run the binding after loading the package. You
|
||||||
|
may choose to include your own logic by passing a function to the `rerun`
|
||||||
|
field or disable the built-in logic by passing false.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
LOAD:PLUGIN *dep-lazy-loading-api-plugin*
|
||||||
|
|
||||||
|
`load:plugin` is a function which allows you to specify another plugin for the
|
||||||
|
package to load after. It takes two arguments: `plugin` which is the name of
|
||||||
|
the plugin you want to follow like: 'user/package'. The second argument is
|
||||||
|
`opts` which is a table with one option: `callback` which is a function. The
|
||||||
|
following is an example:
|
||||||
|
>lua
|
||||||
|
load:plugin("user/package", {})
|
||||||
|
<
|
||||||
|
Which is the same as:
|
||||||
|
>lua
|
||||||
|
load:plugin("user/package", {
|
||||||
|
callback = function()
|
||||||
|
self:cleanup()
|
||||||
|
end
|
||||||
|
})
|
||||||
|
<
|
||||||
|
When 'user/package' is already loaded the `callback` is called immediately.
|
||||||
|
|
||||||
|
LAZY LOADING API SHORTHANDS *dep-lazy-loading-api-shorthands*
|
||||||
|
|
||||||
|
On occasion you may wish to only define one condition for the package to load.
|
||||||
|
When that is the case you may choose to use the built-in shorthands. By
|
||||||
|
loading them:
|
||||||
|
>lua
|
||||||
|
require("dep.lazy.loader.short")
|
||||||
|
<
|
||||||
|
The shorthands are very similar to those found in |dep-lazy-loading-api| with
|
||||||
|
a key exception: instead of running the functions within the body of a
|
||||||
|
function set as the lazy field to a package specification this is the lazy
|
||||||
|
field and may be use like so:
|
||||||
|
>lua
|
||||||
|
{ "user/package",
|
||||||
|
lazy = require("dep.lazy.loader.short").cmd("Command")
|
||||||
|
}
|
||||||
|
<
|
||||||
|
And you may of course put the shorthands in a variable to make this actually
|
||||||
|
shorter:
|
||||||
|
>lua
|
||||||
|
local short = require("dep.lazy.loader.short")
|
||||||
|
{ "user/package",
|
||||||
|
lazy = short.cmd("Command")
|
||||||
|
}
|
||||||
|
<
|
||||||
|
==============================================================================
|
||||||
|
5. Commands *dep-commands*
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
SYNC ALL PLUGINS *:DepSync*
|
||||||
|
- installs new packages, updates packages to the latest versions,
|
||||||
|
cleans removed packages and reloads packages as necessary.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
CLEAN REMOVED PLUGINS *:DepClean*
|
||||||
|
- cleans removed packages.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
RELOAD ALL PLUGINS *:DepReload*
|
||||||
|
- reloads all packages.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
OPEN THE UI *:DepUi*
|
||||||
|
- opens the ui.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
OPEN THE LOG *:DepLog*
|
||||||
|
- opens the log file.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
6. Examples *dep-examples*
|
||||||
|
|
||||||
|
When a string is given where a package specification table is expected, it is
|
||||||
|
assumed to be the package's full name.
|
||||||
|
|
||||||
|
>lua
|
||||||
|
require("dep") {
|
||||||
|
-- these two are equivalent
|
||||||
|
"user/package",
|
||||||
|
{ "user/package" },
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
|
A package can be declared multiple times. Multiple declarations of the same
|
||||||
|
package are combined into one. This is useful when declaring dependencies,
|
||||||
|
which is explored later.
|
||||||
|
|
||||||
|
>lua
|
||||||
|
require("dep") {
|
||||||
|
{ "user/package",
|
||||||
|
reqs = "user/dependency",
|
||||||
|
disabled = true,
|
||||||
|
config = function()
|
||||||
|
print("my config hook")
|
||||||
|
end
|
||||||
|
},
|
||||||
|
{ "user/package",
|
||||||
|
requires = "user/another_dependency",
|
||||||
|
deps = "user/dependent",
|
||||||
|
disabled = false,
|
||||||
|
config = function()
|
||||||
|
os.execute("make")
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- the above is equivalent to
|
||||||
|
require("dep") {
|
||||||
|
{ "user/package",
|
||||||
|
reqs = { "user/dependency", "user/another_dependency" },
|
||||||
|
deps = "user/dependent",
|
||||||
|
disabled = true,
|
||||||
|
config = function()
|
||||||
|
print("my config hook")
|
||||||
|
os.execute("make")
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<
|
||||||
|
DECLARING DEPENDENCIES *dep-examples-declaring-dependencies*
|
||||||
|
|
||||||
|
The dependencies and dependents declared in a package specification are
|
||||||
|
themselves package specifications. If a dependency or dependent is declared
|
||||||
|
multiple times, they are combined into one just like normal package
|
||||||
|
specifications.
|
||||||
|
>lua
|
||||||
|
require("dep") {
|
||||||
|
{ "user/package",
|
||||||
|
reqs = {
|
||||||
|
{ "user/dependency1",
|
||||||
|
reqs = "user/dependency2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- the above is equivalent to
|
||||||
|
require("dep") {
|
||||||
|
{ "user/dependency2",
|
||||||
|
deps = {
|
||||||
|
{ "user/dependency1",
|
||||||
|
deps = "user/package"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- which is equivalent to
|
||||||
|
require("dep") {
|
||||||
|
{ "user/dependency1",
|
||||||
|
reqs = "user/dependency2",
|
||||||
|
deps = "user/package"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- which is equivalent to
|
||||||
|
require("dep") {
|
||||||
|
{ "user/dependency1",
|
||||||
|
reqs = "user/dependency2"
|
||||||
|
},
|
||||||
|
{ "user/package",
|
||||||
|
reqs = "user/dependency1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- which is equivalent to
|
||||||
|
require("dep") {
|
||||||
|
{ "user/dependency2",
|
||||||
|
deps = "user/dependency1"
|
||||||
|
},
|
||||||
|
{ "user/dependency1",
|
||||||
|
deps = "user/package"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- all of the above are guaranteed to load in the following order:
|
||||||
|
-- dependency2, dependency1, package
|
||||||
|
<
|
||||||
|
If dep detects a circular dependency cycle, it reports the problematic packages
|
||||||
|
instead of hanging or crashing.
|
||||||
|
>lua
|
||||||
|
-- this throws an error saying package1 depends on package2 which depends on
|
||||||
|
-- package1
|
||||||
|
require("dep") {
|
||||||
|
{ "user/package1",
|
||||||
|
reqs = "user/package2"
|
||||||
|
},
|
||||||
|
{ "user/package2",
|
||||||
|
reqs = "user/package1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<
|
||||||
|
A dependency can be marked as disabled, which disables all dependents
|
||||||
|
automatically.
|
||||||
|
>lua
|
||||||
|
require("dep") {
|
||||||
|
{ "user/dependency",
|
||||||
|
disabled = true
|
||||||
|
},
|
||||||
|
{ "user/package1",
|
||||||
|
disabled = true, -- implied
|
||||||
|
reqs = "user/dependency"
|
||||||
|
},
|
||||||
|
{ "user/package2",
|
||||||
|
disabled = true, -- implied
|
||||||
|
reqs = "user/dependency"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<
|
||||||
|
If a dependency fails to load for some reason, all of its dependents are
|
||||||
|
guaranteed to not load.
|
||||||
|
>lua
|
||||||
|
require("dep") {
|
||||||
|
{ "user/problematic",
|
||||||
|
load = function()
|
||||||
|
error("bad hook")
|
||||||
|
end
|
||||||
|
},
|
||||||
|
{ "user/dependent",
|
||||||
|
requires = "user/problematic",
|
||||||
|
load = function()
|
||||||
|
print "unreachable"
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
|
MODULES *dep-examples-modules*
|
||||||
|
|
||||||
|
Suppose you split your `init.lua` into two files `packages/search.lua` and
|
||||||
|
`packages/vcs.lua`, which declare the packages telescope.nvim and vim-fugitive
|
||||||
|
respectively.
|
||||||
|
|
||||||
|
>lua
|
||||||
|
-- ~/.config/nvim/lua/packages/search.lua:
|
||||||
|
return {
|
||||||
|
{ "nvim-telescope/telescope.nvim",
|
||||||
|
reqs = "nvim-lua/plenary.nvim"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<
|
||||||
|
>lua
|
||||||
|
-- ~/.config/nvim/lua/packages/vcs.lua:
|
||||||
|
return { "tpope/vim-fugitive" }
|
||||||
|
<
|
||||||
|
Package specifications from other modules can be loaded using the `modules`
|
||||||
|
option.
|
||||||
|
>lua
|
||||||
|
require("dep") {
|
||||||
|
modules = {
|
||||||
|
prefix = "packages"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- the above is equivalent to
|
||||||
|
require("dep") {
|
||||||
|
modules = {
|
||||||
|
prefix = "packages.",
|
||||||
|
"search",
|
||||||
|
"vcs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- or
|
||||||
|
require("dep") {
|
||||||
|
modules = {
|
||||||
|
"packages.search",
|
||||||
|
"packages.vcs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- which is equivalent to
|
||||||
|
local packages = {}
|
||||||
|
|
||||||
|
for _, package in ipairs(require "packages.search") do
|
||||||
|
table.insert(packages, package)
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, package in ipairs(require "packages.vcs") do
|
||||||
|
table.insert(packages, package)
|
||||||
|
end
|
||||||
|
|
||||||
|
require("dep")(packages)
|
||||||
|
|
||||||
|
-- which is ultimately equivalent to
|
||||||
|
require("dep") {
|
||||||
|
{ "nvim-telescope/telescope.nvim",
|
||||||
|
reqs = "nvim-lua/plenary.nvim"
|
||||||
|
},
|
||||||
|
"tpope/vim-fugitive"
|
||||||
|
}
|
||||||
|
|
||||||
|
-- all of the above are guaranteed to load plenary.nvim before
|
||||||
|
-- telescope.nvim. order of telescope.nvim and vim-fugitive is consistent but
|
||||||
|
-- unspecified.
|
||||||
|
<
|
||||||
|
Entire modules can be marked as disabled, which disables all top-level packages
|
||||||
|
declared in that module.
|
||||||
|
>lua
|
||||||
|
return {
|
||||||
|
disable = true,
|
||||||
|
{ "user/package",
|
||||||
|
disabled = true, -- implied by module
|
||||||
|
reqs = {
|
||||||
|
{ "user/dependency",
|
||||||
|
-- disabled = true -- not implied
|
||||||
|
}
|
||||||
|
},
|
||||||
|
deps = {
|
||||||
|
{ "user/dependent",
|
||||||
|
disabled = true -- implied by dependency
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<
|
||||||
|
LAZY LOADING *dep-examples-lazy-loading*
|
||||||
|
|
||||||
|
Lazy loading is a very complicated topic, and therefore this part of the
|
||||||
|
documentation assumes you have experience with regular package managment.
|
||||||
|
Let's go over loading order, and how the lazy loader determines what needs to
|
||||||
|
be loaded.
|
||||||
|
|
||||||
|
Let's say we have the following spec:
|
||||||
|
>lua
|
||||||
|
{ "user/package",
|
||||||
|
lazy = true,
|
||||||
|
deps = "user/dependent"
|
||||||
|
}
|
||||||
|
<
|
||||||
|
This is the same as the following:
|
||||||
|
>lua
|
||||||
|
{ "user/package",
|
||||||
|
lazy = true
|
||||||
|
},
|
||||||
|
|
||||||
|
{ "user/dependent",
|
||||||
|
reqs = "user/package",
|
||||||
|
lazy = require("dep.lazy.loader.short").plugin("user/package")
|
||||||
|
}
|
||||||
|
<
|
||||||
|
What you're seeing is implicit lazy loading. By default dep will lazy load
|
||||||
|
dependents who are explicitly defined in the spec. Now if we we're to modify
|
||||||
|
'user/dependent' like so:
|
||||||
|
>lua
|
||||||
|
{ "user/package",
|
||||||
|
lazy = true
|
||||||
|
},
|
||||||
|
|
||||||
|
{ "user/dependent",
|
||||||
|
reqs = "user/package",
|
||||||
|
lazy = function(load)
|
||||||
|
load:plugin("user/package")
|
||||||
|
load:cmd("LoadDependent")
|
||||||
|
end
|
||||||
|
}
|
||||||
|
<
|
||||||
|
If we were to call the command `:LoadDependent` it would first load
|
||||||
|
'user/package', and then load 'user/dependent'.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
7. Credits & License *dep-credits*
|
||||||
|
|
||||||
|
dep is licensed under the MIT License. Check the LICENSE file for more info.
|
||||||
|
|
||||||
|
vim:tw=78:ts=8:noet:ft=help:norl:
|
224
lua/dep.lua
224
lua/dep.lua
@ -1,113 +1,23 @@
|
|||||||
local logger = require('dep.log')
|
local logger = require("dep.log")
|
||||||
local git = require('dep.git')
|
local git = require("dep.git")
|
||||||
local fs = require('dep.fs')
|
local fs = require("dep.fs")
|
||||||
local packager = require('dep.package')
|
local packager = require("dep.package")
|
||||||
local h = require('dep.helpers')
|
local modules = require("dep.modules")
|
||||||
local spec_man = require("dep.spec")
|
local bench = require("dep.bench")
|
||||||
|
local lazy = require("dep.lazy")
|
||||||
|
local ui = require("dep.ui")
|
||||||
|
|
||||||
-- all functions for convenience
|
-- all functions for convenience
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
-- TODO: actually use this (ideally make a view that shows startuptime and
|
|
||||||
-- which plugins are currently loaded)
|
|
||||||
-- performance logging
|
|
||||||
local perf = {}
|
|
||||||
|
|
||||||
-- TODO: maybe add the ability to get a lockfile? it's useful to make a config
|
-- TODO: maybe add the ability to get a lockfile? it's useful to make a config
|
||||||
-- rebuildable, but idk if it's actually useful for a neovim config
|
-- rebuildable, but idk if it's actually useful for a neovim config
|
||||||
-- (look into how ofter people who use lazy.nvim us it)
|
-- (look into how ofter people who use lazy.nvim us it)
|
||||||
|
|
||||||
--- get execution time of a function
|
|
||||||
---@param name string name of performance output
|
|
||||||
---@param code function function to run
|
|
||||||
---@vararg any arguments for code
|
|
||||||
function M.benchmark(name, code, ...)
|
|
||||||
local start = os.clock()
|
|
||||||
code(...)
|
|
||||||
perf[name] = os.clock() - start
|
|
||||||
end
|
|
||||||
|
|
||||||
--- recurse over all packages and register them
|
|
||||||
---@param speclist speclist table of specs
|
|
||||||
---@param overrides spec? a package spec that is used to override options
|
|
||||||
function M.registertree(speclist, overrides)
|
|
||||||
overrides = overrides or {}
|
|
||||||
|
|
||||||
-- recurse the packages
|
|
||||||
local over = overrides
|
|
||||||
for _, spec in ipairs(speclist) do
|
|
||||||
-- make sure the overrides override and take into account the packages spec
|
|
||||||
---@diagnostic disable-next-line: missing-fields
|
|
||||||
over = {
|
|
||||||
pin = overrides.pin or spec.pin,
|
|
||||||
disable = overrides.disable or spec.disable
|
|
||||||
}
|
|
||||||
|
|
||||||
-- While a package can fail to load we just don't care, it will work itself
|
|
||||||
-- out. The goal is to make sure every plugin that can load does load, not
|
|
||||||
-- keep working plugins from loading because an unrelated one doesn't load.
|
|
||||||
packager:new(spec, over)
|
|
||||||
end
|
|
||||||
|
|
||||||
if speclist.modules then
|
|
||||||
for _, module in ipairs(speclist.modules) do
|
|
||||||
local name = "<unnamed module>"
|
|
||||||
|
|
||||||
if type(module) == "string" then
|
|
||||||
if speclist.modules.prefix then
|
|
||||||
if speclist.modules.prefix:sub(#speclist.modules.prefix) ~= "." and
|
|
||||||
module:sub(1, 2) ~= "." then
|
|
||||||
module = "."..module
|
|
||||||
end
|
|
||||||
module = speclist.modules.prefix..module
|
|
||||||
end
|
|
||||||
|
|
||||||
name, module = module, require(module)
|
|
||||||
end
|
|
||||||
name = module.name or name
|
|
||||||
|
|
||||||
-- allow a module to be a spec
|
|
||||||
if spec_man.check(module, true) ~= false then
|
|
||||||
---@diagnostic disable-next-line: cast-local-type
|
|
||||||
module = { module }
|
|
||||||
end
|
|
||||||
|
|
||||||
local ok, err = pcall(M.registertree, module, overrides)
|
|
||||||
if not ok then
|
|
||||||
error(string.format("%s <- %s", err, name))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- reload all packages in package table spec
|
|
||||||
---@param force boolean? force all packages to load
|
|
||||||
function M.reload(force)
|
|
||||||
local reloaded = packager.get_root():loadtree(force)
|
|
||||||
|
|
||||||
if reloaded then
|
|
||||||
local ok, err
|
|
||||||
M.benchmark("reload", function()
|
|
||||||
ok, err = pcall(vim.cmd,
|
|
||||||
[[
|
|
||||||
silent! helptags ALL
|
|
||||||
silent! UpdateRemotePlugins
|
|
||||||
]])
|
|
||||||
end)
|
|
||||||
|
|
||||||
if ok then
|
|
||||||
logger:log("vim", "reloaded helptags and remote plugins")
|
|
||||||
else
|
|
||||||
logger:log("error",
|
|
||||||
"failed to reload helptags and remote plugins; reason: %s", err)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- sync a tree of plugins
|
--- sync a tree of plugins
|
||||||
---@param tree package[] tree of plugins
|
---@param tree package[] tree of plugins
|
||||||
---@param cb function? callback
|
---@param cb function? callback
|
||||||
function M.synctree(tree, cb)
|
local function synctree(tree, cb)
|
||||||
local progress = 0
|
local progress = 0
|
||||||
local has_errors = false
|
local has_errors = false
|
||||||
|
|
||||||
@ -123,7 +33,9 @@ function M.synctree(tree, cb)
|
|||||||
end
|
end
|
||||||
|
|
||||||
fs:clean(packager)
|
fs:clean(packager)
|
||||||
M.reload()
|
for _, package in pairs(tree) do
|
||||||
|
package:reload()
|
||||||
|
end
|
||||||
|
|
||||||
if cb then
|
if cb then
|
||||||
cb()
|
cb()
|
||||||
@ -145,36 +57,61 @@ function M.synctree(tree, cb)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- basically the main function of our program
|
--- check if a package should be synced
|
||||||
return function(opts)
|
---@param opts table options
|
||||||
logger.pipe = logger:setup()
|
---@param package package package table spec
|
||||||
|
---@return boolean sync
|
||||||
--- make comparison for table.sort
|
local function shouldsync(opts, package)
|
||||||
---@param a package package spec a
|
if opts.sync == "never" then
|
||||||
---@param b package package spec b
|
return false
|
||||||
---@return boolean
|
elseif opts.sync == "new" or opts.sync == nil then
|
||||||
local function comp(a, b)
|
return not package.exists
|
||||||
-- NOTE: this doesn't have to be in any real order, it just has to be
|
else
|
||||||
-- consistant, thus we can just check if the unicode value of one package
|
return opts.sync == "always"
|
||||||
-- id is less than the other
|
|
||||||
return a.id < b.id
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- make comparison for table.sort
|
||||||
|
---@param a package package spec a
|
||||||
|
---@param b package package spec b
|
||||||
|
---@return boolean
|
||||||
|
local function comp(a, b)
|
||||||
|
-- NOTE: this doesn't have to be in any real order, it just has to be
|
||||||
|
-- consistant, thus we can just check if the unicode value of one package
|
||||||
|
-- id is less than the other
|
||||||
|
return a.id < b.id
|
||||||
|
end
|
||||||
|
|
||||||
|
-- basically the main function of our program
|
||||||
|
---@param opts speclist
|
||||||
|
return function(opts)
|
||||||
|
M.config_path = debug.getinfo(2, "S").source:sub(2)
|
||||||
|
logger.pipe = logger:setup()
|
||||||
|
bench.setup()
|
||||||
|
lazy.setup()
|
||||||
|
|
||||||
|
-- generate doc tags
|
||||||
|
vim.cmd.helptags(vim.fn.stdpath('data')..'/site/pack/deps/opt/dep/doc')
|
||||||
|
|
||||||
local initialized, err = pcall(function()
|
local initialized, err = pcall(function()
|
||||||
packager.set_base_dir(opts.base_dir or vim.fn.stdpath("data").."/site/pack/deps/opt/")
|
packager.set_base_dir(opts.base_dir or vim.fn.stdpath("data").."/site/pack/deps/opt/")
|
||||||
M.benchmark("load", function()
|
bench.mark("load", function()
|
||||||
-- register all packages
|
-- register all packages
|
||||||
local root = packager:new({
|
local root = packager:new({
|
||||||
"squibid/dep",
|
"squibid/dep",
|
||||||
url = "https://git.squi.bid/squibid/dep.git",
|
url = "https://git.squi.bid/squibid/dep.git",
|
||||||
branch = "lazy",
|
branch = "lazy"
|
||||||
pin = true
|
|
||||||
})
|
})
|
||||||
if not root then
|
if not root then
|
||||||
logger:log("error", "couldn't register root package")
|
logger:log("error", "couldn't register root package")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
M.registertree(opts)
|
|
||||||
|
-- setup all packages and modules
|
||||||
|
if opts.modules then
|
||||||
|
modules:setup(opts, nil, M.config_path)
|
||||||
|
end
|
||||||
|
packager.register_speclist(opts)
|
||||||
|
|
||||||
-- sort package dependencies
|
-- sort package dependencies
|
||||||
for _, package in pairs(packager.get_packages()) do
|
for _, package in pairs(packager.get_packages()) do
|
||||||
@ -190,29 +127,20 @@ return function(opts)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
-- load packages
|
-- load packages
|
||||||
M.reload(false)
|
for _, package in pairs(packager.get_packages()) do
|
||||||
|
package:reload()
|
||||||
--- check if a package should be synced
|
|
||||||
---@param package table package table spec
|
|
||||||
---@return boolean sync
|
|
||||||
local function shouldsync(package)
|
|
||||||
if opts.sync == "new" or opts.sync == nil then
|
|
||||||
return not package.exists
|
|
||||||
else
|
|
||||||
return opts.sync == "always"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- get all package that need syncing
|
-- get all package that need syncing
|
||||||
local targets = {}
|
local targets = {}
|
||||||
for _, package in pairs(packager.get_packages()) do
|
for _, package in pairs(packager.get_packages()) do
|
||||||
if shouldsync(package) then
|
if shouldsync(opts, package) then
|
||||||
table.insert(targets, package)
|
table.insert(targets, package)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- install all targets
|
-- install all targets
|
||||||
M.synctree(targets)
|
synctree(targets)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if not initialized then
|
if not initialized then
|
||||||
@ -220,37 +148,29 @@ return function(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- add some user commands
|
-- add some user commands
|
||||||
vim.api.nvim_create_user_command("DepLog", function()
|
|
||||||
vim.cmd('vsp '..logger.path)
|
|
||||||
vim.opt_local.readonly = true
|
|
||||||
|
|
||||||
-- make the log auto update while it's open
|
|
||||||
local w = h.uv.new_fs_event()
|
|
||||||
local function watch_file(fname)
|
|
||||||
local fullpath = vim.api.nvim_call_function(
|
|
||||||
'fnamemodify', { fname, ':p' })
|
|
||||||
w:start(fullpath, {}, vim.schedule_wrap(function(...)
|
|
||||||
vim.cmd('checktime')
|
|
||||||
w:stop()
|
|
||||||
watch_file(fname)
|
|
||||||
end))
|
|
||||||
end
|
|
||||||
|
|
||||||
watch_file(logger.path)
|
|
||||||
end, {})
|
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("DepSync", function()
|
vim.api.nvim_create_user_command("DepSync", function()
|
||||||
M.synctree(packager.get_packages())
|
synctree(packager.get_packages())
|
||||||
end, {})
|
end, {})
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("DepReload", function()
|
vim.api.nvim_create_user_command("DepReload", function()
|
||||||
M.reload()
|
for _, package in pairs(packager.get_packages()) do
|
||||||
|
package:reload()
|
||||||
|
end
|
||||||
end, {})
|
end, {})
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("DepClean", function()
|
vim.api.nvim_create_user_command("DepClean", function()
|
||||||
-- clean AND reload to make sure that all old packages are gone
|
-- clean AND reload to make sure that all old packages are gone
|
||||||
fs:clean(packager)
|
fs:clean(packager)
|
||||||
M.reload()
|
end, {})
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command("DepUi", function()
|
||||||
|
ui.open(packager)
|
||||||
|
ui.set_page("P")
|
||||||
|
end, {})
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command("DepLog", function()
|
||||||
|
ui.open(packager)
|
||||||
|
ui.set_page("L")
|
||||||
end, {})
|
end, {})
|
||||||
|
|
||||||
logger:cleanup()
|
logger:cleanup()
|
||||||
|
32
lua/dep/bench.lua
Normal file
32
lua/dep/bench.lua
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
-- TODO: actually use this (ideally make a view that shows startuptime and
|
||||||
|
-- which plugins are currently loaded)
|
||||||
|
-- performance logging
|
||||||
|
|
||||||
|
---@class bench
|
||||||
|
---@field perf number[] list of all perfs
|
||||||
|
local bench = {}
|
||||||
|
local b
|
||||||
|
|
||||||
|
function bench.setup()
|
||||||
|
local o = {}
|
||||||
|
|
||||||
|
o.perf = {}
|
||||||
|
o.inited = true
|
||||||
|
|
||||||
|
b = o
|
||||||
|
end
|
||||||
|
|
||||||
|
--- benchmark a peice of code
|
||||||
|
---@param name string the name of the benchmark
|
||||||
|
---@param f function the code to benchmark
|
||||||
|
---@vararg any args for f
|
||||||
|
---@return any ret the result of f
|
||||||
|
function bench.mark(name, f, ...)
|
||||||
|
local start = os.clock()
|
||||||
|
local ret = f(...)
|
||||||
|
b.perf[name] = os.clock() - start
|
||||||
|
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
|
return bench
|
@ -38,17 +38,23 @@ function fs:clean(package)
|
|||||||
|
|
||||||
while handle do
|
while handle do
|
||||||
local name = h.uv.fs_scandir_next(handle)
|
local name = h.uv.fs_scandir_next(handle)
|
||||||
if name then
|
if name and name ~= package.get_root().name then
|
||||||
queue[name] = package.get_base_dir().."/"..name
|
queue[name] = package.get_base_dir().."/"..name
|
||||||
|
elseif name == package.get_root().name then
|
||||||
|
-- we need to ensure that there is no chance of nuking dep
|
||||||
|
goto continue
|
||||||
|
elseif name == nil then
|
||||||
|
break
|
||||||
else
|
else
|
||||||
-- if there's a single error bail out
|
-- if there's a single error bail out
|
||||||
logger:log("error", "failed to run clean uv.fs_scandir_next failed")
|
logger:log("error", "failed to run clean uv.fs_scandir_next failed")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
::continue::
|
||||||
end
|
end
|
||||||
|
|
||||||
-- keep packages that still exist
|
-- keep packages that still exist
|
||||||
for _, pkg in ipairs(package.get_packages()) do
|
for _, pkg in pairs(package.get_packages()) do
|
||||||
queue[pkg.name] = nil
|
queue[pkg.name] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
37
lua/dep/lazy/init.lua
Normal file
37
lua/dep/lazy/init.lua
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
local h = require("dep.helpers")
|
||||||
|
local packager = require("dep.package")
|
||||||
|
|
||||||
|
---@class lazy
|
||||||
|
local lazy = {}
|
||||||
|
|
||||||
|
-- since this is already a ridiculous "optimization" we should really be caching
|
||||||
|
-- the results of this for when the user keeps on loading the colorscheme that
|
||||||
|
-- they've lazy loaded, that way we speed up the lazy loading process
|
||||||
|
local function colorscheme()
|
||||||
|
-- if a colorscheme doesn't exist attempt load it prior to it being set
|
||||||
|
vim.api.nvim_create_autocmd("ColorschemePre", {
|
||||||
|
pattern = vim.fn.getcompletion("", "color"),
|
||||||
|
callback = function(e)
|
||||||
|
for _, p in pairs(packager.get_packages()) do
|
||||||
|
if not p.loaded then
|
||||||
|
for _, ext in ipairs({ ".lua", ".vim" }) do
|
||||||
|
local path = p.dir.."/colors/"..e.match..ext
|
||||||
|
if h.uv.fs_stat(path) then
|
||||||
|
p:ensureadded(true)
|
||||||
|
-- break out of here, we've loaded the colorscheme
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
--- setup all lazy handlers
|
||||||
|
function lazy.setup()
|
||||||
|
-- start the colorscheme watcher
|
||||||
|
colorscheme()
|
||||||
|
end
|
||||||
|
|
||||||
|
return lazy
|
174
lua/dep/lazy/loader/init.lua
Normal file
174
lua/dep/lazy/loader/init.lua
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
local logger = require('dep.log')
|
||||||
|
local packager = require('dep.package')
|
||||||
|
|
||||||
|
---@class lazy_loader
|
||||||
|
---@field load function the function to load the plugin
|
||||||
|
---@field command_ids string[] the commands that have been registered
|
||||||
|
---@field auto_ids number[] the auto commands that have been registered
|
||||||
|
---@field keybind_ids table[] the keybinds that have been registered
|
||||||
|
---@field plugin_ids table[] the plugins that have been registered
|
||||||
|
local lazy_loader = {}
|
||||||
|
|
||||||
|
--- create a new instance of lazy
|
||||||
|
---@return lazy
|
||||||
|
function lazy_loader:new()
|
||||||
|
local o = {}
|
||||||
|
|
||||||
|
setmetatable(o, self)
|
||||||
|
o.command_ids = {}
|
||||||
|
o.auto_ids = {}
|
||||||
|
o.keybind_ids = {}
|
||||||
|
o.plugin_ids = {}
|
||||||
|
|
||||||
|
self.__index = self
|
||||||
|
|
||||||
|
return o
|
||||||
|
end
|
||||||
|
|
||||||
|
--- set the loading callback
|
||||||
|
---@param load function the loader function
|
||||||
|
function lazy_loader:set_load(load)
|
||||||
|
self.load = load
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create a usercommand which will trigger the plugin to load
|
||||||
|
---@param name string the name of the command
|
||||||
|
---@param opts vim.api.keyset.user_command? options
|
||||||
|
function lazy_loader:cmd(name, opts)
|
||||||
|
opts = opts or {}
|
||||||
|
|
||||||
|
-- move the rerun arg to a seperate variable because keymap.set doesn't like
|
||||||
|
-- options it doesn't know of
|
||||||
|
local rerun = opts["rerun"] or true
|
||||||
|
opts['rerun'] = nil
|
||||||
|
|
||||||
|
-- load the plugin on completion
|
||||||
|
if not opts["complete"] then
|
||||||
|
opts["complete"] = function(_, line, _)
|
||||||
|
self:cleanup()
|
||||||
|
|
||||||
|
-- return all completions for the current input, we need this to ensure
|
||||||
|
-- that the new completions are loaded from the actual plugin, not our
|
||||||
|
-- definiton of the command
|
||||||
|
return vim.fn.getcompletion(line, "cmdline")
|
||||||
|
end
|
||||||
|
opts["nargs"] = "*"
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command(name, opts['callback'] or function(_)
|
||||||
|
self:cleanup()
|
||||||
|
|
||||||
|
-- attempt to rerun the command
|
||||||
|
if not rerun then
|
||||||
|
pcall(vim.cmd, name)
|
||||||
|
end
|
||||||
|
end, opts)
|
||||||
|
|
||||||
|
table.insert(self.command_ids, name)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create an auto command which will trigger the plugin to load
|
||||||
|
---@param event string the event to trigger on
|
||||||
|
---@param opts vim.api.keyset.create_autocmd? options
|
||||||
|
function lazy_loader:auto(event, opts)
|
||||||
|
opts = opts or {}
|
||||||
|
|
||||||
|
opts['callback'] = opts['callback'] or function()
|
||||||
|
self:cleanup()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- create the auto command and save it
|
||||||
|
table.insert(self.auto_ids, vim.api.nvim_create_autocmd(event, opts))
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create an auto command which will trigger on filetype
|
||||||
|
---@param filetype string filetype to register the auto on
|
||||||
|
function lazy_loader:ft(filetype)
|
||||||
|
self:auto("FileType", {
|
||||||
|
pattern = filetype
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
---@class lazy.Opts: vim.keymap.set.Opts
|
||||||
|
---@field rerun boolean|function weather to rerun and what to do
|
||||||
|
|
||||||
|
--- create a keybind which will trigger the plugin to load
|
||||||
|
---@param mode string the mode to trigger in
|
||||||
|
---@param bind string the binding to use
|
||||||
|
---@param opts lazy.Opts? options
|
||||||
|
function lazy_loader:keymap(mode, bind, opts)
|
||||||
|
opts = opts or {}
|
||||||
|
|
||||||
|
-- move the rerun arg to a seperate variable because keymap.set doesn't like
|
||||||
|
-- options it doesn't know of
|
||||||
|
local rerun = opts['rerun'] or true
|
||||||
|
opts['rerun'] = nil
|
||||||
|
|
||||||
|
vim.keymap.set(mode, bind, opts['callback'] or function()
|
||||||
|
-- register keymap unload
|
||||||
|
self:cleanup()
|
||||||
|
|
||||||
|
-- call the keymap after the user has mapped it
|
||||||
|
if type(rerun) == "function" then
|
||||||
|
rerun()
|
||||||
|
elseif rerun then
|
||||||
|
local keys = vim.api.nvim_replace_termcodes(bind, true, false, true)
|
||||||
|
vim.api.nvim_input(keys)
|
||||||
|
end
|
||||||
|
end, opts)
|
||||||
|
|
||||||
|
table.insert(self.keybind_ids, { ['mode'] = mode, ['bind'] = bind })
|
||||||
|
end
|
||||||
|
|
||||||
|
--- load a plugin when another plugin loads
|
||||||
|
---@param plugin string plugin name
|
||||||
|
---@param opts table? options
|
||||||
|
function lazy_loader:plugin(plugin, opts)
|
||||||
|
opts = opts or {}
|
||||||
|
opts["callback"] = opts["callback"] or function()
|
||||||
|
self:cleanup()
|
||||||
|
end
|
||||||
|
|
||||||
|
if packager.get_packages()[plugin].loaded then
|
||||||
|
opts["callback"]()
|
||||||
|
else
|
||||||
|
local on_load = packager.get_packages()[plugin].on_load
|
||||||
|
local on_load_idx = #on_load + 1
|
||||||
|
on_load[on_load_idx] = opts["callback"]
|
||||||
|
|
||||||
|
table.insert(self.plugin_ids, { plugin, on_load_idx })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- cleanup all the callbacks, and load the plugin
|
||||||
|
function lazy_loader:cleanup()
|
||||||
|
-- cleanup user commands
|
||||||
|
for _, command_id in ipairs(self.command_ids) do
|
||||||
|
local ok, err = pcall(vim.api.nvim_del_user_command, command_id)
|
||||||
|
if not ok then
|
||||||
|
logger:log("lazy", err or "failed to delete user command")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- cleanup auto commands
|
||||||
|
for _, auto_id in ipairs(self.auto_ids) do
|
||||||
|
local ok, err = pcall(vim.api.nvim_del_autocmd, auto_id)
|
||||||
|
if not ok then
|
||||||
|
logger:log("lazy", err or "failed to delete auto command")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- cleanup keymaps
|
||||||
|
for _, keybind_id in ipairs(self.keybind_ids) do
|
||||||
|
local ok, err = pcall(vim.keymap.del, keybind_id.mode, keybind_id.bind, {})
|
||||||
|
if not ok then
|
||||||
|
logger:log("lazy", err or "failed to delete keymap")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- cleanup plugins
|
||||||
|
for _, plugin_id in ipairs(self.plugin_ids) do
|
||||||
|
table.remove(packager.get_packages()[plugin_id[1]].on_load, plugin_id[2])
|
||||||
|
end
|
||||||
|
-- load the plugin
|
||||||
|
self:load()
|
||||||
|
end
|
||||||
|
|
||||||
|
return lazy_loader
|
72
lua/dep/lazy/loader/short.lua
Normal file
72
lua/dep/lazy/loader/short.lua
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
-- This file contains shorthands which rely on the loader core functions. They
|
||||||
|
-- are intended to ease lazy loading condition definitions to use them you may
|
||||||
|
-- do the following:
|
||||||
|
--
|
||||||
|
-- ```lua
|
||||||
|
-- _G.dep_short = require("dep.lazy.loader.short")
|
||||||
|
-- ```
|
||||||
|
--
|
||||||
|
-- Which will allow you to reference it anywhere in your config like so:
|
||||||
|
--
|
||||||
|
-- ```lua
|
||||||
|
-- require("dep") {
|
||||||
|
-- { "some/plugin",
|
||||||
|
-- lazy = dep_short.cmd("TheCommand")
|
||||||
|
-- }
|
||||||
|
-- }
|
||||||
|
-- ```
|
||||||
|
--
|
||||||
|
-- Happy vimming o/
|
||||||
|
local short = {}
|
||||||
|
|
||||||
|
--- create a single command
|
||||||
|
---@param name string the name of the command
|
||||||
|
---@param opts vim.api.keyset.user_command? options
|
||||||
|
---@return function callback
|
||||||
|
function short.cmd(name, opts)
|
||||||
|
return function(load)
|
||||||
|
load:cmd(name, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create a single auto command
|
||||||
|
---@param event string the event to trigger on
|
||||||
|
---@param opts vim.api.keyset.create_autocmd? options
|
||||||
|
---@return function callback
|
||||||
|
function short.auto(event, opts)
|
||||||
|
return function(load)
|
||||||
|
load:auto(event, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create a single auto command which will trigger on filetype
|
||||||
|
---@param filetype string filetype to register the auto on
|
||||||
|
---@return function callback
|
||||||
|
function short.ft(filetype)
|
||||||
|
return function(load)
|
||||||
|
load:ft(filetype)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create a single keybind
|
||||||
|
---@param mode string the mode to trigger in
|
||||||
|
---@param bind string the binding to use
|
||||||
|
---@param opts lazy.Opts? options
|
||||||
|
---@return function callback
|
||||||
|
function short.keymap(mode, bind, opts)
|
||||||
|
return function(load)
|
||||||
|
load:keymap(mode, bind, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create a single plugin load event for when another plugin loads
|
||||||
|
---@param plugin string plugin name
|
||||||
|
---@param opts table? options
|
||||||
|
---@return function callback
|
||||||
|
function short.plugin(plugin, opts)
|
||||||
|
return function(load)
|
||||||
|
load:plugin(plugin, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return short
|
@ -79,8 +79,8 @@ function logger:log(level, message, ...)
|
|||||||
|
|
||||||
-- write to the pipe if it's open
|
-- write to the pipe if it's open
|
||||||
if logger.pipe then
|
if logger.pipe then
|
||||||
logger.pipe:write(string.format("[%s] %s:%s: %s\n", os.date("%T"),
|
logger.pipe:write(string.format("[%s] %s:%s:(%s) %s\n", os.date("%T"),
|
||||||
source.short_src:gsub('.*%/', ''), source.currentline, message))
|
source.short_src:gsub('.*%/', ''), source.currentline, level, message))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
75
lua/dep/modules/init.lua
Normal file
75
lua/dep/modules/init.lua
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
local h = require('dep.helpers')
|
||||||
|
local logger = require('dep.log')
|
||||||
|
local module = require("dep.modules.module")
|
||||||
|
|
||||||
|
---@class modules
|
||||||
|
---@field modules module[] all modules in dep
|
||||||
|
local modules = {}
|
||||||
|
|
||||||
|
--- Initialize all the modules
|
||||||
|
---@param self table?
|
||||||
|
---@param speclist table
|
||||||
|
---@param overrides spec? overrides
|
||||||
|
---@return modules modules manager
|
||||||
|
---@nodisacard
|
||||||
|
function modules:setup(speclist, overrides, config_path)
|
||||||
|
overrides = overrides or {}
|
||||||
|
|
||||||
|
local o = {}
|
||||||
|
self = {}
|
||||||
|
self.__index = self
|
||||||
|
setmetatable(o, self)
|
||||||
|
|
||||||
|
-- create a list of modules
|
||||||
|
o.modules = {}
|
||||||
|
|
||||||
|
if (speclist.modules[1] == "*" or #speclist.modules == 0)
|
||||||
|
and speclist.modules.prefix then
|
||||||
|
|
||||||
|
local path = vim.fs.joinpath(config_path:gsub("[^/]*$", ""),
|
||||||
|
"lua", (speclist.modules.prefix:gsub("%.", "/"))
|
||||||
|
)
|
||||||
|
|
||||||
|
local handle = h.uv.fs_scandir(path)
|
||||||
|
while handle do
|
||||||
|
local name = h.uv.fs_scandir_next(handle)
|
||||||
|
if name then
|
||||||
|
-- skip non-lua files
|
||||||
|
if name:sub(#name - 3) ~= ".lua" then
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
|
||||||
|
-- remove the file extension from the name so that lua doesn't fail
|
||||||
|
-- when attempting to load it
|
||||||
|
name = name:sub(0, #name - 4)
|
||||||
|
|
||||||
|
-- put the module into the list of modules
|
||||||
|
table.insert(speclist.modules, name)
|
||||||
|
|
||||||
|
::continue::
|
||||||
|
elseif name == nil then
|
||||||
|
-- no more entries
|
||||||
|
break
|
||||||
|
else
|
||||||
|
-- if there's a single error bail out
|
||||||
|
logger:log("error", "failed to run clean uv.fs_scandir_next failed")
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- loop through all modules and initialize them
|
||||||
|
for _, modpath in ipairs(speclist.modules) do
|
||||||
|
local mod = module.new(nil, modpath, speclist.modules.prefix, overrides)
|
||||||
|
if not mod then
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
|
||||||
|
table.insert(o.modules, mod)
|
||||||
|
::continue::
|
||||||
|
end
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
return modules
|
73
lua/dep/modules/module.lua
Normal file
73
lua/dep/modules/module.lua
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
local logger = require('dep.log')
|
||||||
|
local spec_man = require("dep.spec")
|
||||||
|
local packager = require("dep.package")
|
||||||
|
|
||||||
|
---@class module
|
||||||
|
---@field name string name of the module
|
||||||
|
---@field desc string description of the module
|
||||||
|
---@field disable boolean weather to disable all the packages inside the module
|
||||||
|
---@field path string path to the module
|
||||||
|
---@field mod table the module
|
||||||
|
---@field packages package[] all packages registed from the module
|
||||||
|
local module = {}
|
||||||
|
|
||||||
|
--- Initialize a module
|
||||||
|
---@param self table?
|
||||||
|
---@param modpath string path to the module
|
||||||
|
---@param prefix string? the prefix to all modules
|
||||||
|
---@param overrides spec? a module override
|
||||||
|
---@return module|false module false on failure to load module
|
||||||
|
---@nodiscard
|
||||||
|
function module:new(modpath, prefix, overrides)
|
||||||
|
overrides = overrides or {}
|
||||||
|
|
||||||
|
local ok, err
|
||||||
|
local o = {}
|
||||||
|
self = {}
|
||||||
|
self.__index = self
|
||||||
|
setmetatable(o, self)
|
||||||
|
|
||||||
|
o.name = "<unnamed module>"
|
||||||
|
o.desc = "<undescribed module>"
|
||||||
|
if type(modpath) == "string" then
|
||||||
|
if prefix ~= nil then
|
||||||
|
if prefix:sub(#prefix) ~= "." and modpath:sub(1, 2) ~= "." then
|
||||||
|
modpath = "."..modpath
|
||||||
|
end
|
||||||
|
o.path = prefix..modpath
|
||||||
|
else
|
||||||
|
o.path = modpath
|
||||||
|
end
|
||||||
|
o.name = modpath
|
||||||
|
ok, o.mod = pcall(require, o.path)
|
||||||
|
if not ok then
|
||||||
|
logger:log("error", "failed to load module: %s", vim.inspect(o.mod))
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
o.name = o.mod.name or o.name
|
||||||
|
o.desc = o.mod.desc or o.desc
|
||||||
|
|
||||||
|
-- ensure the overrides are properly set
|
||||||
|
overrides = vim.tbl_extend("force", overrides, {
|
||||||
|
disable = o.mod.disable or overrides.disable
|
||||||
|
})
|
||||||
|
|
||||||
|
-- allow a module to be a spec
|
||||||
|
if spec_man.check(o.mod, true) ~= false then
|
||||||
|
o.mod = { o.mod }
|
||||||
|
end
|
||||||
|
|
||||||
|
ok, err = pcall(packager.register_speclist, o.mod, overrides)
|
||||||
|
if not ok then
|
||||||
|
logger:log("error", "%s <- %s", err, o.name)
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ensure that the module contains the packages that it's created
|
||||||
|
self.packages = err
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
return module
|
@ -1,5 +1,6 @@
|
|||||||
local logger = require('dep.log')
|
local logger = require('dep.log')
|
||||||
local spec_man = require("dep.spec")
|
local spec_man = require("dep.spec")
|
||||||
|
local bench = require("dep.bench")
|
||||||
|
|
||||||
---@class package
|
---@class package
|
||||||
---@field id string id of the package
|
---@field id string id of the package
|
||||||
@ -8,13 +9,12 @@ local spec_man = require("dep.spec")
|
|||||||
---@field lazy boolean if the package is lazy loaded in any way
|
---@field lazy boolean if the package is lazy loaded in any way
|
||||||
---@field added boolean if the package has been added in vim
|
---@field added boolean if the package has been added in vim
|
||||||
---@field configured boolean if the package has been configured
|
---@field configured boolean if the package has been configured
|
||||||
---@field lazied boolean if the packages lazy loading has been set
|
|
||||||
---@field loaded boolean if a package has been loaded
|
---@field loaded boolean if a package has been loaded
|
||||||
---@field subtree_loaded boolean is the subtree has been loaded
|
---@field subtree_loaded boolean is the subtree has been loaded
|
||||||
---@field on_config function[] table of functions to run on config
|
---@field on_config function[] table of functions to run on config
|
||||||
---@field on_setup function[] table of function to run on setup
|
---@field on_setup function[] table of function to run on setup
|
||||||
---@field on_load function[] table of functions to run on load
|
---@field on_load function[] table of functions to run on load
|
||||||
---@field lazy_load function[] table of functions to run which will tell the package when to load
|
---@field lazy_load table table of functions and booleans to run which will tell the package when to load
|
||||||
---@field requirements package[] this package's requirements
|
---@field requirements package[] this package's requirements
|
||||||
---@field dependents package[] packages that require this package
|
---@field dependents package[] packages that require this package
|
||||||
---@field perf table performance metrics for the package
|
---@field perf table performance metrics for the package
|
||||||
@ -88,7 +88,6 @@ function package:new(spec, overrides)
|
|||||||
o.exists = false
|
o.exists = false
|
||||||
o.lazy = false
|
o.lazy = false
|
||||||
o.added = false
|
o.added = false
|
||||||
o.lazied = false
|
|
||||||
o.configured = false
|
o.configured = false
|
||||||
o.loaded = false
|
o.loaded = false
|
||||||
o.subtree_loaded = false
|
o.subtree_loaded = false
|
||||||
@ -166,18 +165,10 @@ function package:new(spec, overrides)
|
|||||||
-- if the child package is lazy loaded make sure the child package
|
-- if the child package is lazy loaded make sure the child package
|
||||||
-- is only loaded when the parent package has finished loading
|
-- is only loaded when the parent package has finished loading
|
||||||
if o.lazy then
|
if o.lazy then
|
||||||
table.insert(o.on_load, function()
|
|
||||||
local ok = o:loadtree(true)
|
|
||||||
if not ok then
|
|
||||||
logger:log("lazy",
|
|
||||||
"failed to run loadtree for %s, some packages may not be loaded",
|
|
||||||
o.id)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
-- tell the dep that it's gonna be lazy
|
-- tell the dep that it's gonna be lazy
|
||||||
pkg.lazy = true
|
pkg.lazy = true
|
||||||
table.insert(pkg.lazy_load, function(_) end)
|
table.insert(pkg.lazy_load,
|
||||||
|
require("dep.lazy.loader.short").plugin(id))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -253,6 +244,10 @@ function package:ensureadded(force)
|
|||||||
--- load a package
|
--- load a package
|
||||||
---@param pkg package
|
---@param pkg package
|
||||||
local function loadpkg(pkg)
|
local function loadpkg(pkg)
|
||||||
|
if not self.enabled then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
-- make sure to load the dependencies first
|
-- make sure to load the dependencies first
|
||||||
for _, p in pairs(pkg.requirements) do
|
for _, p in pairs(pkg.requirements) do
|
||||||
if not p.loaded then
|
if not p.loaded then
|
||||||
@ -295,22 +290,32 @@ function package:ensureadded(force)
|
|||||||
elseif not self.added and self.lazy then
|
elseif not self.added and self.lazy then
|
||||||
logger:log("lazy", "registering %d lazy hooks for %s", #self.lazy_load,
|
logger:log("lazy", "registering %d lazy hooks for %s", #self.lazy_load,
|
||||||
self.id)
|
self.id)
|
||||||
self.lazied = true
|
|
||||||
for _, load_cond in pairs(self.lazy_load) do
|
for _, load_cond in pairs(self.lazy_load) do
|
||||||
-- configure the lazy loader for the user
|
-- configure the lazy loader for the user
|
||||||
local l = require('lazy.utils'):new()
|
local l = require('dep.lazy.loader'):new()
|
||||||
if l == true then
|
if l == true then
|
||||||
logger:log("lazy", "failed to get lazy utils")
|
logger:log("lazy", "failed to get lazy utils")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
l:set_load(function()
|
l:set_load(function()
|
||||||
|
-- ensure that we can't attempt to load a plugin twice via lazy loading
|
||||||
|
if self.loaded then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
logger:log("lazy", "triggered %d lazy hooks for %s", #self.lazy_load,
|
logger:log("lazy", "triggered %d lazy hooks for %s", #self.lazy_load,
|
||||||
self.id)
|
self.id)
|
||||||
loadpkg(self)
|
loadpkg(self)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- run it
|
-- run it if it's not just a stopper to keep the plugin lazy
|
||||||
load_cond(l)
|
if load_cond ~= true then
|
||||||
|
local ok, err = pcall(load_cond, l)
|
||||||
|
if not ok then
|
||||||
|
logger:log("error", "failed to register lazy load conditions for '%s': %s",
|
||||||
|
self.name, err)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
@ -443,4 +448,64 @@ function package.findcycle(pkgs)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- recurse over all packages and register them
|
||||||
|
---@param speclist speclist table of specs
|
||||||
|
---@param overrides spec? a package spec that is used to override options
|
||||||
|
---@return package[] packages
|
||||||
|
function package.register_speclist(speclist, overrides)
|
||||||
|
overrides = overrides or {}
|
||||||
|
local packages_from_speclist = {}
|
||||||
|
|
||||||
|
-- recurse the packages
|
||||||
|
local over = overrides
|
||||||
|
for _, spec in ipairs(speclist) do
|
||||||
|
-- make sure the overrides override and take into account the packages spec
|
||||||
|
---@diagnostic disable-next-line: missing-fields
|
||||||
|
over = {
|
||||||
|
pin = overrides.pin or spec.pin,
|
||||||
|
disable = overrides.disable or spec.disable
|
||||||
|
}
|
||||||
|
|
||||||
|
-- While a package can fail to load we just don't care, it will work itself
|
||||||
|
-- out. The goal is to make sure every plugin that can load does load, not
|
||||||
|
-- keep working plugins from loading because an unrelated one doesn't load.
|
||||||
|
local pkg = package:new(spec, over)
|
||||||
|
if not pkg then
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
|
||||||
|
-- we store all the packages in a table so that the caller may keep track of
|
||||||
|
-- their packages, this is not required and therefore the return value may
|
||||||
|
-- be discarded
|
||||||
|
table.insert(packages_from_speclist, pkg)
|
||||||
|
::continue::
|
||||||
|
end
|
||||||
|
|
||||||
|
return packages_from_speclist
|
||||||
|
end
|
||||||
|
|
||||||
|
--- reload the package
|
||||||
|
---@param self package the package to reload
|
||||||
|
---@param force boolean? force all packages to load
|
||||||
|
function package:reload(force)
|
||||||
|
local reloaded = self:loadtree(force)
|
||||||
|
|
||||||
|
if reloaded then
|
||||||
|
local ok, err
|
||||||
|
-- TODO: make a benchmark function
|
||||||
|
bench.mark("reload", function()
|
||||||
|
ok, err = pcall(vim.cmd,
|
||||||
|
[[
|
||||||
|
silent! helptags ALL
|
||||||
|
silent! UpdateRemotePlugins
|
||||||
|
]])
|
||||||
|
end)
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
logger:log("error",
|
||||||
|
"failed to reload helptags and remote plugins; reason: %s", err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return package
|
return package
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
local logger = require("dep.log")
|
local logger = require("dep.log")
|
||||||
|
|
||||||
---@class modules
|
---@class specmodules
|
||||||
---@field prefix string prefix to prepend to the modules
|
---@field prefix string prefix to prepend to the modules
|
||||||
---@field [integer] string list of all modules to load
|
---@field [integer] string list of all modules to load
|
||||||
|
|
||||||
---@class speclist
|
---@class speclist
|
||||||
---@field modules modules a list of modules
|
---@field modules specmodules? a list of modules
|
||||||
|
---@field base_dir string? the base directory for all plugins
|
||||||
|
---@field sync ("new"|"always")? when to sync (defaults to new)
|
||||||
---@field [integer] spec a spec
|
---@field [integer] spec a spec
|
||||||
|
|
||||||
---@class spec
|
---@class spec
|
||||||
@ -13,7 +15,7 @@ local logger = require("dep.log")
|
|||||||
---@field setup function? code to run before the package is loaded
|
---@field setup function? code to run before the package is loaded
|
||||||
---@field load function? code to run after the package is loaded
|
---@field load function? code to run after the package is loaded
|
||||||
---@field config function? code to run after the package is installed/updated
|
---@field config function? code to run after the package is installed/updated
|
||||||
---@field lazy function? code to run which determines when the package is loaded
|
---@field lazy function|true? code to run which determines when the package is loaded
|
||||||
---@field as string? overrides the short name of the package which is usually set
|
---@field as string? overrides the short name of the package which is usually set
|
||||||
--- to a substring of all the chars after "/" in spec[1]
|
--- to a substring of all the chars after "/" in spec[1]
|
||||||
---@field url string? the url to the git repository hosting the package
|
---@field url string? the url to the git repository hosting the package
|
||||||
@ -53,6 +55,8 @@ function spec:correct_spec()
|
|||||||
repeat
|
repeat
|
||||||
if type(self[1]) ~= "string" then
|
if type(self[1]) ~= "string" then
|
||||||
self = self[1]
|
self = self[1]
|
||||||
|
elseif self[1] == nil then
|
||||||
|
break
|
||||||
end
|
end
|
||||||
until type(self[1]) == "string"
|
until type(self[1]) == "string"
|
||||||
end
|
end
|
||||||
@ -110,8 +114,9 @@ function spec:check(silent)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if self.lazy ~= nil then -- spec.lazy
|
if self.lazy ~= nil then -- spec.lazy
|
||||||
if type(self.lazy) ~= "function" then
|
if type(self.lazy) ~= "function" and self.lazy ~= true then
|
||||||
logger:log("spec", "spec.lazy must be a function in %s", self[1])
|
logger:log("spec", "spec.lazy must be a function or boolean in %s",
|
||||||
|
self[1])
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
59
lua/dep/ui/format.lua
Normal file
59
lua/dep/ui/format.lua
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
local logger = require("dep.log")
|
||||||
|
|
||||||
|
local format = {}
|
||||||
|
|
||||||
|
--- format a boolean to a chunk with highlights
|
||||||
|
---@param b boolean
|
||||||
|
---@return chunk chunk
|
||||||
|
function format.bool(b)
|
||||||
|
return { vim.inspect(b), b and "DiffAdd" or "DiffDelete" }
|
||||||
|
end
|
||||||
|
|
||||||
|
--- format a number to a chunk with highlights
|
||||||
|
---@param n number
|
||||||
|
---@return chunk chunk
|
||||||
|
function format.number(n)
|
||||||
|
return { vim.inspect(n), "Number" }
|
||||||
|
end
|
||||||
|
|
||||||
|
--- format a log line with highlights
|
||||||
|
---@param log_line string log line
|
||||||
|
---@return chunk[] chunks
|
||||||
|
function format.log_line(log_line)
|
||||||
|
-- make sure we don't do operations on nil values
|
||||||
|
if not log_line or log_line == "" then
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
-- error on any nil values, this should prevent us from parsing an incorrectly
|
||||||
|
-- formatted log line
|
||||||
|
local log_time, colon, log_path, log_path_ln, level, rest
|
||||||
|
local ok = pcall(function()
|
||||||
|
log_time = string.sub(log_line, string.find(log_line, "%[") + 1,
|
||||||
|
string.find(log_line, "%]") - 1)
|
||||||
|
colon = string.find(log_line, ":", 11)
|
||||||
|
log_path = string.sub(log_line, string.find(log_line, "%]") + 2,
|
||||||
|
colon - 1)
|
||||||
|
log_path_ln = string.sub(log_line, colon + 1,
|
||||||
|
string.find(log_line, ":", colon + 1) - 1)
|
||||||
|
level = string.sub(log_line, string.find(log_line, "%(") + 1,
|
||||||
|
string.find(log_line, "%)") - 1)
|
||||||
|
rest = string.sub(log_line, string.find(log_line, "%)") + 2)
|
||||||
|
end)
|
||||||
|
if not ok then
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
{ "[", "" },
|
||||||
|
{ log_time, "Boolean" },
|
||||||
|
{ "] ", "" },
|
||||||
|
{ log_path, "String" },
|
||||||
|
{ ":", "" },
|
||||||
|
{ log_path_ln, "Number" },
|
||||||
|
{ ": ", "" },
|
||||||
|
{ rest, logger.stage_colors[level] or "" }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
return format
|
207
lua/dep/ui/init.lua
Normal file
207
lua/dep/ui/init.lua
Normal file
@ -0,0 +1,207 @@
|
|||||||
|
local h = require("dep.helpers")
|
||||||
|
local page = require("dep.ui.page")
|
||||||
|
local logger = require("dep.log")
|
||||||
|
local format = require("dep.ui.format")
|
||||||
|
|
||||||
|
---@class ui
|
||||||
|
---@field bufnr number
|
||||||
|
---@field winnr number
|
||||||
|
---@field header_bufnr number
|
||||||
|
---@field header_winnr number
|
||||||
|
---@field pages page[]
|
||||||
|
local ui = {}
|
||||||
|
|
||||||
|
-- the active page being displayed
|
||||||
|
local active_page
|
||||||
|
|
||||||
|
-- all the pages
|
||||||
|
local pages = {}
|
||||||
|
|
||||||
|
-- the header ext mark
|
||||||
|
local header_ext_id
|
||||||
|
|
||||||
|
local function page_packages(packager)
|
||||||
|
local p = page:new("Packages", "P")
|
||||||
|
for _, pkg in pairs(packager.get_packages()) do
|
||||||
|
p:new_line({
|
||||||
|
{ pkg.id, "@conditional" },
|
||||||
|
{ " loaded: ", "" },
|
||||||
|
format.bool(pkg.loaded),
|
||||||
|
{ " lazy: ", "" },
|
||||||
|
format.bool(pkg.lazy)
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
return p
|
||||||
|
end
|
||||||
|
|
||||||
|
local function page_log()
|
||||||
|
local p = page:new("Log", "L")
|
||||||
|
local f = io.open(logger.path, "r")
|
||||||
|
if not f then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- put the cursor at the bottom of the page after drawing
|
||||||
|
p.post_draw = function()
|
||||||
|
if ui.winnr then
|
||||||
|
vim.api.nvim_win_set_cursor(ui.winnr, { #p.content, 0 })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- read in the contents of the file, and keep watching for updates
|
||||||
|
local function update_contents()
|
||||||
|
repeat
|
||||||
|
local line = f:read("*l")
|
||||||
|
|
||||||
|
-- if the line isn't empty we shouldn't draw it
|
||||||
|
if line then
|
||||||
|
p:new_line(format.log_line(line))
|
||||||
|
end
|
||||||
|
until not line
|
||||||
|
end
|
||||||
|
|
||||||
|
update_contents()
|
||||||
|
|
||||||
|
local fullpath = vim.api.nvim_call_function(
|
||||||
|
"fnamemodify", { logger.path, ":p" })
|
||||||
|
h.uv.new_fs_event():start(fullpath, {}, vim.schedule_wrap(function()
|
||||||
|
update_contents()
|
||||||
|
|
||||||
|
-- if the log is currently being displayed then make sure to draw it when
|
||||||
|
-- it updates
|
||||||
|
if active_page == p then
|
||||||
|
p:draw(ui.bufnr)
|
||||||
|
end
|
||||||
|
end))
|
||||||
|
|
||||||
|
return p
|
||||||
|
end
|
||||||
|
|
||||||
|
--- set the current page
|
||||||
|
---@param p string|page page to set
|
||||||
|
function ui.set_page(p)
|
||||||
|
if type(p) == "string" then
|
||||||
|
for _, v in ipairs(pages) do
|
||||||
|
if p == v.kb then
|
||||||
|
v:draw(ui.bufnr)
|
||||||
|
active_page = v
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif type(p) == "table" then
|
||||||
|
p:draw(ui.bufnr)
|
||||||
|
active_page = p
|
||||||
|
end
|
||||||
|
|
||||||
|
-- color the header text
|
||||||
|
local txt = vim.api.nvim_buf_get_text(ui.header_bufnr, 0, 0, -1, -1, {})[1]
|
||||||
|
local start_range = (string.find(txt, active_page.name)) - 2
|
||||||
|
local end_range = #active_page.name + start_range + 2
|
||||||
|
|
||||||
|
if header_ext_id then
|
||||||
|
vim.api.nvim_buf_del_extmark(ui.header_bufnr, active_page.hlns, header_ext_id)
|
||||||
|
end
|
||||||
|
header_ext_id = vim.api.nvim_buf_set_extmark(ui.header_bufnr,
|
||||||
|
active_page.hlns, 0, start_range, {
|
||||||
|
hl_mode = "replace",
|
||||||
|
hl_group = "CurSearch",
|
||||||
|
end_col = end_range,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
local setup
|
||||||
|
--- setup all the pages
|
||||||
|
---@param packager package the packager
|
||||||
|
local function setup_pages(packager)
|
||||||
|
if setup then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local header_text = ""
|
||||||
|
|
||||||
|
table.insert(pages, page_packages(packager))
|
||||||
|
table.insert(pages, page_log())
|
||||||
|
|
||||||
|
for _, v in ipairs(pages) do
|
||||||
|
header_text = header_text.." "..v.name.." "
|
||||||
|
|
||||||
|
vim.keymap.set("n", v.kb, function()
|
||||||
|
ui.set_page(v)
|
||||||
|
end, { buffer = ui.bufnr })
|
||||||
|
end
|
||||||
|
|
||||||
|
-- set the header text
|
||||||
|
vim.api.nvim_buf_set_lines(ui.header_bufnr, 0, -1, false, { header_text })
|
||||||
|
|
||||||
|
-- add keymaps
|
||||||
|
vim.keymap.set("n", "q", function()
|
||||||
|
vim.api.nvim_win_close(ui.winnr, false)
|
||||||
|
ui.winnr = nil
|
||||||
|
end, { buffer = ui.bufnr })
|
||||||
|
|
||||||
|
setup = true
|
||||||
|
end
|
||||||
|
|
||||||
|
--- setup the ui
|
||||||
|
---@param packager package
|
||||||
|
function ui.open(packager)
|
||||||
|
if not ui.bufnr then
|
||||||
|
ui.bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
|
end
|
||||||
|
if not ui.header_bufnr then
|
||||||
|
ui.header_bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
local header_height = 1
|
||||||
|
local width = math.floor(vim.o.columns * 0.8)
|
||||||
|
local height = math.floor(vim.o.lines * 0.8) - header_height
|
||||||
|
|
||||||
|
if not ui.winnr then
|
||||||
|
ui.winnr = vim.api.nvim_open_win(ui.bufnr, true, {
|
||||||
|
relative = "editor",
|
||||||
|
row = (vim.o.lines - height) / 2,
|
||||||
|
col = (vim.o.columns - width) / 2,
|
||||||
|
width = width,
|
||||||
|
height = height,
|
||||||
|
border = "solid",
|
||||||
|
zindex = 998,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
if not ui.header_winnr then
|
||||||
|
ui.header_winnr = vim.api.nvim_open_win(ui.header_bufnr, false, {
|
||||||
|
relative = "editor",
|
||||||
|
row = ((vim.o.lines - height) / 2) - (header_height * 2),
|
||||||
|
col = (vim.o.columns - width) / 2,
|
||||||
|
width = width,
|
||||||
|
height = header_height,
|
||||||
|
border = "solid",
|
||||||
|
zindex = 999,
|
||||||
|
focusable = false
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_win_set_buf(ui.winnr, ui.bufnr)
|
||||||
|
vim.api.nvim_win_set_buf(ui.header_winnr, ui.header_bufnr)
|
||||||
|
|
||||||
|
-- make sure the header closes when the body does and vice versa
|
||||||
|
local function cb()
|
||||||
|
vim.api.nvim_win_close(ui.header_winnr, false)
|
||||||
|
ui.header_winnr = nil
|
||||||
|
vim.api.nvim_win_close(ui.winnr, false)
|
||||||
|
ui.winnr = nil
|
||||||
|
end
|
||||||
|
vim.api.nvim_create_autocmd("WinClosed", {
|
||||||
|
pattern = ui.winnr.."",
|
||||||
|
callback = cb
|
||||||
|
})
|
||||||
|
vim.api.nvim_create_autocmd("WinClosed", {
|
||||||
|
pattern = ui.header_winnr.."",
|
||||||
|
callback = cb
|
||||||
|
})
|
||||||
|
|
||||||
|
setup_pages(packager)
|
||||||
|
end
|
||||||
|
|
||||||
|
return ui
|
84
lua/dep/ui/page.lua
Normal file
84
lua/dep/ui/page.lua
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
---@class chunk: table
|
||||||
|
---@field [1] string text to be displayed
|
||||||
|
---@field [2] string neovim highlight group to use
|
||||||
|
|
||||||
|
---@class page
|
||||||
|
---@field name string name of the ui page
|
||||||
|
---@field kb string keybind of the page
|
||||||
|
---@field content chunk[]|chunk[][] all the chunks
|
||||||
|
---@field hlns number highlight namespace
|
||||||
|
---@field pre_draw function things to do prior to drawing to the buffer
|
||||||
|
---@field post_draw function things to do post drawing to the buffer
|
||||||
|
local page = {}
|
||||||
|
|
||||||
|
--- create a new page
|
||||||
|
---@param name string the name of the page
|
||||||
|
---@param kb string keybind to change to the page
|
||||||
|
---@return page page
|
||||||
|
function page:new(name, kb)
|
||||||
|
local o = {}
|
||||||
|
self.__index = self
|
||||||
|
setmetatable(o, self)
|
||||||
|
|
||||||
|
o.hlns = vim.api.nvim_create_namespace("DepUi")
|
||||||
|
o.name = name
|
||||||
|
o.kb = kb
|
||||||
|
o.content = {}
|
||||||
|
|
||||||
|
return o
|
||||||
|
end
|
||||||
|
|
||||||
|
--- add a new line to the page
|
||||||
|
---@param line chunk|chunk[] new line
|
||||||
|
function page:new_line(line)
|
||||||
|
table.insert(self.content, line)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- draw the page to the given buffer
|
||||||
|
---@param bufnr number buffer number
|
||||||
|
function page:draw(bufnr)
|
||||||
|
-- try to run pre_draw steps
|
||||||
|
if self.pre_draw then
|
||||||
|
self.pre_draw()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ready all information for rendering
|
||||||
|
for i, chunk in ipairs(self.content) do
|
||||||
|
local linenr = i - 1
|
||||||
|
local text = ""
|
||||||
|
local hls = {}
|
||||||
|
|
||||||
|
if type(chunk[1]) == "table" then
|
||||||
|
local j = 0
|
||||||
|
for _, ch in ipairs(chunk) do
|
||||||
|
text = text..ch[1]
|
||||||
|
table.insert(hls, { ch[2], j, j + #ch[1] })
|
||||||
|
j = j + #ch[1]
|
||||||
|
end
|
||||||
|
elseif type(chunk[1]) == "string" then
|
||||||
|
text = chunk[1]
|
||||||
|
table.insert(hls, { chunk[2], 0, #text })
|
||||||
|
end
|
||||||
|
|
||||||
|
-- draw the text to the buffer
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, linenr, -1, false, { text })
|
||||||
|
|
||||||
|
-- highlight the buffer
|
||||||
|
for _, hl in ipairs(hls) do
|
||||||
|
vim.api.nvim_buf_set_extmark(bufnr, self.hlns, linenr, hl[2], {
|
||||||
|
hl_mode = "replace",
|
||||||
|
hl_group = hl[1],
|
||||||
|
end_col = hl[3],
|
||||||
|
end_row = linenr
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
-- try to run post_draw steps
|
||||||
|
if self.post_draw then
|
||||||
|
self.post_draw()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return page
|
@ -1,116 +0,0 @@
|
|||||||
local logger = require('dep.log')
|
|
||||||
|
|
||||||
---@class lazy
|
|
||||||
---@field load function the function to load the plugin
|
|
||||||
---@field command_ids table the commands that have been registered
|
|
||||||
---@field auto_ids table the auto commands that have been registered
|
|
||||||
---@field keybind_ids table the keybinds that have been registered
|
|
||||||
local lazy = {}
|
|
||||||
|
|
||||||
--- create a new instance of lazy
|
|
||||||
---@return lazy
|
|
||||||
function lazy:new()
|
|
||||||
local o = {}
|
|
||||||
|
|
||||||
setmetatable(o, self)
|
|
||||||
o.command_ids = {}
|
|
||||||
o.auto_ids = {}
|
|
||||||
o.keybind_ids = {}
|
|
||||||
|
|
||||||
self.__index = self
|
|
||||||
|
|
||||||
return o
|
|
||||||
end
|
|
||||||
|
|
||||||
--- set the loading function
|
|
||||||
---@param load function the loading function
|
|
||||||
function lazy:set_load(load)
|
|
||||||
self.load = load
|
|
||||||
end
|
|
||||||
|
|
||||||
--- get the configured load function
|
|
||||||
---@return function load function
|
|
||||||
function lazy:get_load()
|
|
||||||
return self.load
|
|
||||||
end
|
|
||||||
|
|
||||||
--- create a usercommand which will trigger the plugin to load
|
|
||||||
---@param name string the name of the command
|
|
||||||
---@param opts vim.api.keyset.user_command? options
|
|
||||||
function lazy:cmd(name, opts)
|
|
||||||
opts = opts or {}
|
|
||||||
vim.api.nvim_create_user_command(name, opts['callback'] or function()
|
|
||||||
self:cleanup()
|
|
||||||
end, opts)
|
|
||||||
|
|
||||||
table.insert(self.command_ids, name)
|
|
||||||
end
|
|
||||||
|
|
||||||
--- user an auto command which will trigger the plugin to load
|
|
||||||
---@param event string the event to trigger on
|
|
||||||
---@param opts vim.api.keyset.create_autocmd? options
|
|
||||||
function lazy:auto(event, opts)
|
|
||||||
opts = opts or {}
|
|
||||||
|
|
||||||
opts['callback'] = opts['callback'] or function()
|
|
||||||
self:cleanup()
|
|
||||||
end
|
|
||||||
|
|
||||||
-- create the auto command and save it
|
|
||||||
table.insert(self.auto_ids, vim.api.nvim_create_autocmd(event, opts))
|
|
||||||
end
|
|
||||||
|
|
||||||
--- create a keybind which will trigger the plugin to load
|
|
||||||
---@param mode string the mode to trigger in
|
|
||||||
---@param bind string the binding to use
|
|
||||||
---@param opts vim.keymap.set.Opts? options
|
|
||||||
function lazy:keymap(mode, bind, opts)
|
|
||||||
opts = opts or {}
|
|
||||||
|
|
||||||
-- move the rerun arg to a seperate variable because keymap.set doesn't like
|
|
||||||
-- options it doesn't know of
|
|
||||||
local rerun = opts['rerun'] or true
|
|
||||||
opts['rerun'] = nil
|
|
||||||
|
|
||||||
vim.keymap.set(mode, bind, opts['callback'] or function()
|
|
||||||
-- register keymap unload
|
|
||||||
self:cleanup()
|
|
||||||
|
|
||||||
-- call the keymap after the user has mapped it
|
|
||||||
if rerun then
|
|
||||||
local keys = vim.api.nvim_replace_termcodes(bind, true, false, true)
|
|
||||||
vim.api.nvim_input(keys)
|
|
||||||
end
|
|
||||||
end, opts)
|
|
||||||
|
|
||||||
table.insert(self.keybind_ids, { ['mode'] = mode, ['bind'] = bind })
|
|
||||||
end
|
|
||||||
|
|
||||||
--- cleanup all the callbacks, and load the plugin
|
|
||||||
function lazy:cleanup()
|
|
||||||
-- cleanup user commands
|
|
||||||
for _, command_id in pairs(self.command_ids) do
|
|
||||||
local ok, err = pcall(vim.api.nvim_del_user_command, command_id)
|
|
||||||
if not ok then
|
|
||||||
logger:log("lazy", err or "failed to delete user command")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- cleanup auto commands
|
|
||||||
for _, auto_id in pairs(self.auto_ids) do
|
|
||||||
local ok, err = pcall(vim.api.nvim_del_autocmd, auto_id)
|
|
||||||
if not ok then
|
|
||||||
logger:log("lazy", err or "failed to delete auto command")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- cleanup keymaps
|
|
||||||
for _, keybind_id in pairs(self.keybind_ids) do
|
|
||||||
local ok, err = pcall(vim.keymap.del, keybind_id.mode, keybind_id.bind, {})
|
|
||||||
if not ok then
|
|
||||||
logger:log("lazy", err or "failed to delete keymap")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- load the plugin
|
|
||||||
self:load()
|
|
||||||
end
|
|
||||||
|
|
||||||
return lazy
|
|
63
tests/dep_spec.lua
Normal file
63
tests/dep_spec.lua
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
---@diagnostic disable: undefined-global, undefined-field
|
||||||
|
local dep_ui_format = require("dep.ui.format")
|
||||||
|
local dep_spec_man = require("dep.spec")
|
||||||
|
|
||||||
|
describe("ui log formatting", function()
|
||||||
|
it("returns the proper chunks to print a formatted line", function()
|
||||||
|
assert.same(
|
||||||
|
{
|
||||||
|
{ "[", "" },
|
||||||
|
{ "11:22:33", "Boolean" },
|
||||||
|
{ "] ", "" },
|
||||||
|
{ "file.lua", "String" },
|
||||||
|
{ ":", "" },
|
||||||
|
{ "1", "Number" },
|
||||||
|
{ ": ", "" },
|
||||||
|
{ "some fancy message", "" }
|
||||||
|
},
|
||||||
|
dep_ui_format.log_line("[11:22:33] file.lua:1:(vim) some fancy message")
|
||||||
|
)
|
||||||
|
|
||||||
|
-- malformed log line
|
||||||
|
assert.same({},
|
||||||
|
dep_ui_format.log_line("11:22:33] file.lua:1:(vim) some fancy message"))
|
||||||
|
|
||||||
|
-- test nil values
|
||||||
|
assert.same({}, dep_ui_format.log_line(""))
|
||||||
|
assert.same({}, dep_ui_format.log_line(nil))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("package specification", function()
|
||||||
|
it("gets the package's name", function()
|
||||||
|
assert.equal(dep_spec_man.get_name({ "user/package" }), "package")
|
||||||
|
assert.equal(dep_spec_man.get_name({ "user/package.git" }), "package.git")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("ensurses specs are in the proper format", function()
|
||||||
|
local correct = { "user/package" }
|
||||||
|
assert.same(dep_spec_man.correct_spec("user/package"), correct)
|
||||||
|
assert.same(dep_spec_man.correct_spec({ "user/package" }), correct)
|
||||||
|
assert.same(dep_spec_man.correct_spec({ { "user/package" } }), correct)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("checks a spec for correctness", function()
|
||||||
|
assert.same(
|
||||||
|
dep_spec_man.check({ "user/package" }, true),
|
||||||
|
{ "user/package" }
|
||||||
|
)
|
||||||
|
|
||||||
|
assert.same(
|
||||||
|
dep_spec_man.check({
|
||||||
|
"user/package",
|
||||||
|
deps = "user/dependency"
|
||||||
|
}, true),
|
||||||
|
{
|
||||||
|
"user/package",
|
||||||
|
deps = {
|
||||||
|
"user/dependency"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
end)
|
1
tests/minit.lua
Normal file
1
tests/minit.lua
Normal file
@ -0,0 +1 @@
|
|||||||
|
vim.opt.rtp:prepend(".")
|
Reference in New Issue
Block a user