mirror of
				https://github.com/NotAShelf/nvf.git
				synced 2025-11-04 12:42:21 +00:00 
			
		
		
		
	feat(snippets/luasnip): allow custom snippets
This commit is contained in:
		
					parent
					
						
							
								787678dd73
							
						
					
				
			
			
				commit
				
					
						8c7fdc0f5d
					
				
			
		
					 3 changed files with 101 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -1,11 +1,48 @@
 | 
			
		|||
{
 | 
			
		||||
  config,
 | 
			
		||||
  lib,
 | 
			
		||||
  pkgs,
 | 
			
		||||
  ...
 | 
			
		||||
}: let
 | 
			
		||||
  inherit (lib.modules) mkIf;
 | 
			
		||||
 | 
			
		||||
  inherit (lib.lists) replicate;
 | 
			
		||||
  inherit
 | 
			
		||||
    (lib.strings)
 | 
			
		||||
    optionalString
 | 
			
		||||
    removeSuffix
 | 
			
		||||
    concatStrings
 | 
			
		||||
    stringAsChars
 | 
			
		||||
    concatMapStringsSep
 | 
			
		||||
    ;
 | 
			
		||||
  inherit (lib.attrsets) mapAttrsToList;
 | 
			
		||||
  inherit (pkgs) writeTextFile;
 | 
			
		||||
  cfg = config.vim.snippets.luasnip;
 | 
			
		||||
  # LuaSnip freaks out if the indentation is wrong in snippets
 | 
			
		||||
  indent = n: s: let
 | 
			
		||||
    indentString = concatStrings (replicate n " ");
 | 
			
		||||
    sep = "\n" + indentString;
 | 
			
		||||
  in
 | 
			
		||||
    indentString
 | 
			
		||||
    + stringAsChars (c:
 | 
			
		||||
      if c == "\n"
 | 
			
		||||
      then sep
 | 
			
		||||
      else c) (removeSuffix "\n" s);
 | 
			
		||||
  customSnipmateSnippetFiles =
 | 
			
		||||
    mapAttrsToList (
 | 
			
		||||
      name: value:
 | 
			
		||||
        writeTextFile {
 | 
			
		||||
          name = "${name}.snippets";
 | 
			
		||||
          text =
 | 
			
		||||
            concatMapStringsSep "\n" (x: ''
 | 
			
		||||
              snippet ${x.trigger} ${x.description}
 | 
			
		||||
              ${indent 2 x.body}
 | 
			
		||||
            '')
 | 
			
		||||
            value;
 | 
			
		||||
          destination = "/snippets/${name}.snippets";
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    cfg.customSnippets.snipmate;
 | 
			
		||||
in {
 | 
			
		||||
  config = mkIf cfg.enable {
 | 
			
		||||
    vim = {
 | 
			
		||||
| 
						 | 
				
			
			@ -19,11 +56,16 @@ in {
 | 
			
		|||
 | 
			
		||||
        after = cfg.loaders;
 | 
			
		||||
      };
 | 
			
		||||
      startPlugins = cfg.providers;
 | 
			
		||||
      startPlugins = cfg.providers ++ customSnipmateSnippetFiles;
 | 
			
		||||
      autocomplete.nvim-cmp = mkIf config.vim.autocomplete.nvim-cmp.enable {
 | 
			
		||||
        sources = {luasnip = "[LuaSnip]";};
 | 
			
		||||
        sourcePlugins = ["cmp-luasnip"];
 | 
			
		||||
      };
 | 
			
		||||
      snippets.luasnip.loaders = ''
 | 
			
		||||
        ${optionalString (
 | 
			
		||||
          cfg.customSnippets.snipmate != {}
 | 
			
		||||
        ) "require('luasnip.loaders.from_snipmate').lazy_load()"}
 | 
			
		||||
      '';
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue