mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-26 10:48:35 +02:00
plugins/cmp: refactor source->plugin association
Introduce the internal option `cmpSourcePlugins` where plugins can register their nvim-cmp source name association.
This commit is contained in:
parent
bd422db9ba
commit
3a8d4fee35
6 changed files with 137 additions and 100 deletions
|
@ -7,10 +7,11 @@
|
|||
}:
|
||||
with helpers.vim-plugin;
|
||||
with lib;
|
||||
rec {
|
||||
{
|
||||
mkCmpSourcePlugin =
|
||||
{
|
||||
name,
|
||||
sourceName,
|
||||
extraPlugins ? [ ],
|
||||
useDefaultPackage ? true,
|
||||
...
|
||||
|
@ -20,49 +21,8 @@ rec {
|
|||
extraPlugins = extraPlugins ++ (lists.optional useDefaultPackage pkgs.vimPlugins.${name});
|
||||
|
||||
maintainers = [ maintainers.GaetanLepage ];
|
||||
|
||||
# Register the source -> plugin name association
|
||||
imports = [ { cmpSourcePlugins.${sourceName} = name; } ];
|
||||
};
|
||||
|
||||
extractSourcesFromOptionValue = sources: if isList sources then sources else [ ];
|
||||
|
||||
autoInstallSourcePluginsModule =
|
||||
cfg:
|
||||
let
|
||||
# cfg.setup.sources
|
||||
setupSources = extractSourcesFromOptionValue cfg.settings.sources;
|
||||
# cfg.filetype.<name>.sources
|
||||
filetypeSources = mapAttrsToList (
|
||||
_: filetypeConfig: extractSourcesFromOptionValue filetypeConfig.sources
|
||||
) cfg.filetype;
|
||||
# cfg.cmdline.<name>.sources
|
||||
cmdlineSources = mapAttrsToList (
|
||||
_: cmdlineConfig: extractSourcesFromOptionValue cmdlineConfig.sources
|
||||
) cfg.cmdline;
|
||||
|
||||
# [{name = "foo";} {name = "bar"; x = 42;} ...]
|
||||
allSources = flatten (setupSources ++ filetypeSources ++ cmdlineSources);
|
||||
|
||||
# Take only the names from the sources provided by the user
|
||||
# ["foo" "bar"]
|
||||
foundSources = lists.unique (map (source: source.name) allSources);
|
||||
|
||||
# If the user has enabled the `foo` and `bar` sources, this attrs will look like:
|
||||
# {
|
||||
# cmp-foo.enable = true;
|
||||
# cmp-bar.enable = true;
|
||||
# }
|
||||
attrsEnabled = mapAttrs' (sourceName: pluginName: {
|
||||
name = pluginName;
|
||||
value.enable = mkIf (elem sourceName foundSources) true;
|
||||
}) (import ./sources.nix);
|
||||
|
||||
lspCapabilities = mkIf (elem "nvim_lsp" foundSources) {
|
||||
lsp.capabilities = ''
|
||||
capabilities = vim.tbl_deep_extend("force", capabilities, require('cmp_nvim_lsp').default_capabilities())
|
||||
'';
|
||||
};
|
||||
in
|
||||
mkMerge [
|
||||
(mkIf cfg.autoEnableSources attrsEnabled)
|
||||
lspCapabilities
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue