feat: can load lazy plugins on events
parent
eb11cfbe1a
commit
70484e221f
18
README.md
18
README.md
|
|
@ -44,7 +44,7 @@ A [neovim] plugin and configuration management plugin, highly inspired by [lazy]
|
||||||
- [X] Lazy load your plugins
|
- [X] Lazy load your plugins
|
||||||
- [X] on command
|
- [X] on command
|
||||||
- [X] on filetype
|
- [X] on filetype
|
||||||
- [ ] on event
|
- [X] on event
|
||||||
- [ ] on keybinding
|
- [ ] on keybinding
|
||||||
- [X] load plugins in order (via plugin `dependencies` property)
|
- [X] load plugins in order (via plugin `dependencies` property)
|
||||||
- [X] Have a `:Sloth` command to load or query your plugins
|
- [X] Have a `:Sloth` command to load or query your plugins
|
||||||
|
|
@ -164,8 +164,9 @@ The Plugin configuration object accepts the following properties:
|
||||||
| `config` | `null` | Lua code (as string of path) to call after loading the plugin |
|
| `config` | `null` | Lua code (as string of path) to call after loading the plugin |
|
||||||
| `dependencies` | `[]` | The plugin dependencies³ |
|
| `dependencies` | `[]` | The plugin dependencies³ |
|
||||||
| `lazy` | `false` | Should the plugin be loaded lazily |
|
| `lazy` | `false` | Should the plugin be loaded lazily |
|
||||||
| `cmd` | `[]` | Command to put as place_holder to lazy load the plugin⁴ |
|
| `cmd` | `[]` | Command to put as place_holder to load the lazy plugin⁴ |
|
||||||
| `ft` | `[]` | Filetype to watch to lazy load the plugin⁴ |
|
| `ft` | `[]` | Filetype to watch to load the lazy plugin⁴ |
|
||||||
|
| `events` | `[]` | Events to watch to load the lazy plugin⁴⁵ |
|
||||||
|
|
||||||
> ² The plugin can be either a nix package or an object with only `name` and
|
> ² The plugin can be either a nix package or an object with only `name` and
|
||||||
> `src` as properties. The latter will be used to create a nix package of your
|
> `src` as properties. The latter will be used to create a nix package of your
|
||||||
|
|
@ -178,6 +179,17 @@ The Plugin configuration object accepts the following properties:
|
||||||
|
|
||||||
> ⁴ Setting this property implicitly set `lazy` to `true`.
|
> ⁴ Setting this property implicitly set `lazy` to `true`.
|
||||||
|
|
||||||
|
> ⁵ Events can be a string, an Event object or a list of either. You can
|
||||||
|
> represent a simple event name and pattern with the following string format :
|
||||||
|
> "BufRead *.md".
|
||||||
|
|
||||||
|
The Event configuration object accepts the following properties:
|
||||||
|
|
||||||
|
| name | default | description |
|
||||||
|
|-----------|---------|----------------------------------------------------------------|
|
||||||
|
| `name` | N/A | The name of the event as string or list of string **REQUIRED** |
|
||||||
|
| `pattern` | `null` | Pattern (as string or list of string) associated to the event |
|
||||||
|
|
||||||
### neovim (lua)
|
### neovim (lua)
|
||||||
|
|
||||||
#### Using default `init.lua`
|
#### Using default `init.lua`
|
||||||
|
|
|
||||||
39
lib/deps.nix
39
lib/deps.nix
|
|
@ -6,14 +6,19 @@
|
||||||
types,
|
types,
|
||||||
...
|
...
|
||||||
}: let
|
}: let
|
||||||
inherit (builtins) isPath foldl';
|
inherit (builtins) foldl' isPath isList isString mapAttrs match elemAt;
|
||||||
inherit (lib.attrsets) attrNames optionalAttrs;
|
inherit (lib.attrsets) attrNames optionalAttrs;
|
||||||
inherit (lib.lists) concatMap;
|
inherit (lib.lists) concatMap;
|
||||||
inherit (lib.strings) fileContents;
|
inherit (lib.strings) fileContents splitString;
|
||||||
lua = callPackage ./lua.nix {};
|
lua = callPackage ./lua.nix {};
|
||||||
|
|
||||||
callPackage = lib.callPackageWith (pkgs // dependenciesExtraArgs);
|
callPackage = lib.callPackageWith (pkgs // dependenciesExtraArgs);
|
||||||
|
|
||||||
|
hasMatch = pattern: str: isList (match pattern str);
|
||||||
|
wrapArray = value:
|
||||||
|
if isList value
|
||||||
|
then value
|
||||||
|
else [value];
|
||||||
|
|
||||||
defaultPlugin = {
|
defaultPlugin = {
|
||||||
enabled = true;
|
enabled = true;
|
||||||
init = null;
|
init = null;
|
||||||
|
|
@ -22,6 +27,7 @@
|
||||||
lazy = false;
|
lazy = false;
|
||||||
cmd = [];
|
cmd = [];
|
||||||
ft = [];
|
ft = [];
|
||||||
|
events = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
remotePluginToNeovimPlugin = p:
|
remotePluginToNeovimPlugin = p:
|
||||||
|
|
@ -30,6 +36,26 @@
|
||||||
pname = name;
|
pname = name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
normalizeEvent = event: let
|
||||||
|
value =
|
||||||
|
if ! isString event
|
||||||
|
then event
|
||||||
|
else
|
||||||
|
if ! hasMatch ".* .*" event
|
||||||
|
then {name = event;}
|
||||||
|
else let
|
||||||
|
part = elemAt (splitString " " event);
|
||||||
|
in {
|
||||||
|
name = part 0;
|
||||||
|
pattern = part 1;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
mapAttrs (_: wrapArray) value;
|
||||||
|
normalizeEvents = events:
|
||||||
|
if isList events
|
||||||
|
then map normalizeEvent events
|
||||||
|
else [(normalizeEvent events)];
|
||||||
|
|
||||||
withPluginDefaults = dep: defaultPlugin // dep;
|
withPluginDefaults = dep: defaultPlugin // dep;
|
||||||
normalizePlugin = d: let
|
normalizePlugin = d: let
|
||||||
dep = types.dependency d;
|
dep = types.dependency d;
|
||||||
|
|
@ -48,7 +74,9 @@
|
||||||
// rec {
|
// rec {
|
||||||
hasCommands = p.cmd != [];
|
hasCommands = p.cmd != [];
|
||||||
hasFileTypes = p.ft != [];
|
hasFileTypes = p.ft != [];
|
||||||
lazy = p.lazy || hasCommands || hasFileTypes;
|
events = normalizeEvents p.events;
|
||||||
|
hasEvents = p.events != [];
|
||||||
|
lazy = p.lazy || hasCommands || hasFileTypes || hasEvents;
|
||||||
optional = lazy || p.init != null;
|
optional = lazy || p.init != null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -137,6 +165,9 @@
|
||||||
})
|
})
|
||||||
// (optionalAttrs plugin.hasFileTypes {
|
// (optionalAttrs plugin.hasFileTypes {
|
||||||
inherit (plugin) ft;
|
inherit (plugin) ft;
|
||||||
|
})
|
||||||
|
// (optionalAttrs plugin.hasEvents {
|
||||||
|
inherit (plugin) events;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
pluginsLuaDef = plugins:
|
pluginsLuaDef = plugins:
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,12 @@
|
||||||
src = any;
|
src = any;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
eventType = with yants;
|
||||||
|
struct "event" {
|
||||||
|
name = either string stringList;
|
||||||
|
pattern = either string stringList;
|
||||||
|
};
|
||||||
|
|
||||||
# The plugin type of dependencies
|
# The plugin type of dependencies
|
||||||
pluginType = with yants; let
|
pluginType = with yants; let
|
||||||
stringList = list string;
|
stringList = list string;
|
||||||
|
|
@ -49,7 +55,7 @@
|
||||||
lazy = option bool;
|
lazy = option bool;
|
||||||
|
|
||||||
# List of events on which the plugin should be loaded
|
# List of events on which the plugin should be loaded
|
||||||
# events = option stringList;
|
events = option (eitherN [string eventType (list (either string eventType))]);
|
||||||
|
|
||||||
# List of commands on which the plugin should be loaded
|
# List of commands on which the plugin should be loaded
|
||||||
cmd = option stringList;
|
cmd = option stringList;
|
||||||
|
|
|
||||||
|
|
@ -28,15 +28,29 @@ function M.new(values)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.ft then
|
if self.has_events then
|
||||||
local group_id = vim.api.nvim_create_augroup(self.augroup_name, {
|
local group_id = vim.api.nvim_create_augroup(self.augroup_name, {
|
||||||
clear = true,
|
clear = true,
|
||||||
})
|
})
|
||||||
vim.api.nvim_create_autocmd('FileType', {
|
|
||||||
group = group_id,
|
if self.ft then
|
||||||
pattern = self.ft,
|
vim.api.nvim_create_autocmd('FileType', {
|
||||||
callback = self:lazy_load_ft()
|
group = group_id,
|
||||||
})
|
pattern = self.ft,
|
||||||
|
callback = self:lazy_load_event('FileType')
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
if self.events then
|
||||||
|
for _, event in ipairs(self.events) do
|
||||||
|
-- print("register event", event.name, "for pattern", event.pattern)
|
||||||
|
vim.api.nvim_create_autocmd(event.name, {
|
||||||
|
group = group_id,
|
||||||
|
pattern = event.pattern,
|
||||||
|
callback = self:lazy_load_event(event.name)
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
[State.Inited] = function()
|
[State.Inited] = function()
|
||||||
|
|
@ -73,7 +87,7 @@ function M.new(values)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.ft then
|
if self.has_events then
|
||||||
vim.api.nvim_del_augroup_by_name(self.augroup_name)
|
vim.api.nvim_del_augroup_by_name(self.augroup_name)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
@ -134,6 +148,10 @@ function M:get_ft()
|
||||||
return self.values.ft
|
return self.values.ft
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M:get_events()
|
||||||
|
return self.values.events
|
||||||
|
end
|
||||||
|
|
||||||
function M:get_is_lazy()
|
function M:get_is_lazy()
|
||||||
return self.values.lazy or false
|
return self.values.lazy or false
|
||||||
end
|
end
|
||||||
|
|
@ -150,6 +168,10 @@ function M:get_is_loaded()
|
||||||
return self.state >= State.Loaded
|
return self.state >= State.Loaded
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M:get_has_events()
|
||||||
|
return self.ft or self.events
|
||||||
|
end
|
||||||
|
|
||||||
function M:get_augroup_name()
|
function M:get_augroup_name()
|
||||||
return "Sloth-plugin-" .. self.name
|
return "Sloth-plugin-" .. self.name
|
||||||
end
|
end
|
||||||
|
|
@ -187,10 +209,10 @@ function M:lazy_load_cmd(cmd)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M:lazy_load_ft()
|
function M:lazy_load_event(name)
|
||||||
return function(param)
|
return function(param)
|
||||||
self:load()
|
self:load()
|
||||||
vim.api.nvim_exec_autocmds('FileType', {
|
vim.api.nvim_exec_autocmds(name, {
|
||||||
pattern = param.match,
|
pattern = param.match,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue