cajus-nvim icon indicating copy to clipboard operation
cajus-nvim copied to clipboard

How to use community snippets add and add personal snippets

Open practicalli-johnny opened this issue 2 years ago • 6 comments

Would it be possible to add documentation on how to use snippets with the configuration?

Specifically I would like to add personal snippets that support markdown content and mkdocs extensions.

I would also like to evaluate community snippets such as rafamadriz/friendly-snippets but it is unclear how these can be added. Simply adding friendly snippets as a plugin in plugin.fnl does not include these snippets.

I have tried adding a snippets directory in the .config/nvim/ directory and copied markdown snippets from vim-snippets which seem to appear in the TAB completion list, although they do not expand.

In fnl/config/plugion/cmp.fnl several sources for autocompletion are defined, although it is unclear what these source specifically refer to for buffer, vsnip and luasnip

(def- cmp-src-menu-items
  {:buffer "buff"
   :conjure "conj"
   :nvim_lsp "lsp"
   :vsnip "vsnp"
   :luasnip "lsnp"})

In Luasnip it discusses loaders, so curious to understand if these loaders are used or relevant to the fennel configuration

What are the supported approaches for snippets in the current configuration, or how could it be extended to support custom snippets?

Thank you

practicalli-johnny avatar Dec 11 '22 15:12 practicalli-johnny

Hey John how's it going?

I was reading the docs you send and as I understood this friendly-snippets will show as a vscode type of snippets, so in the menu items if should fall as :vsnip as your example, but I'm my tests here it is appearing as :luasnip as shown in my test config: image

So what I did to configure this was the following: First I changed the LuaSnip lines in nvim/fnl/config/plugin.fnl

  ; snippets
  :L3MON4D3/LuaSnip {:requires [:rafamadriz/friendly-snippets
                                :saadparwaiz1/cmp_luasnip]
                     :mod :lua-snip}

And created the following file nvim/fnl/config/plugin/lua-snip.fnl with the following contents:

(module config.plugin.lua-snip
  {autoload {vscode luasnip.loaders.from_vscode}})

(vscode.lazy_load)

rafaeldelboni avatar Dec 11 '22 19:12 rafaeldelboni

I did some tests and they are expanding as intended in my tests, let me know if adding the lazy_load fix this for you as well.

rafaeldelboni avatar Dec 11 '22 19:12 rafaeldelboni

Yes, the vscode json friendly snippets are working with the configuration describe above. Thank you.

I would also like to have a ~/.config/nvim/snippets/ directory with language specific snippets that I have created for myself, e.g ~/.config/nvim/snippets/markdown.json

I updated the fnl/config/plugin/lua-snip.fnl file to pass a path using :paths however this doent seem to load

(module config.plugin.lua-snip
  {autoload {vscode luasnip.loaders.from_vscode}})

;; (vscode.lazy_load)
(vscode.lazy_load 
  {:paths ["./snippets"]})

I have tried a few variations around the original lua code but without success

-- load snippets from path/of/your/nvim/config/my-cool-snippets
require("luasnip.loaders.from_vscode").lazy_load({ paths = { "./my-cool-snippets" } })

Update: I noted if I do specify a path, then the friendly snippets are no longer being loaded (this is to be expected from the paths paramter). If I mistakenly use path then the parameter is ignored and friendly snippets are loaded.

So passing paths does seem to have an effect but I seem to be passing a value for the path that is not found. I also tried a full path, but it still fails to find my own snippets.

(vscode.lazy_load {:paths ["/home/practicalli/.config/nvim/snippets"]})

Looking at the Luasnip code, lazy_load (and other load functions) calls get_snippet_files(opts) and looks for a paths key and breaks down the path strings to indivu https://github.com/L3MON4D3/LuaSnip/blob/master/lua/luasnip/loaders/from_vscode.lua#L197-L221

practicalli-johnny avatar Dec 12 '22 16:12 practicalli-johnny

Looking at the .local/state/nvim/luasnip.log it seems I need a package.json file.

ERROR | vscode-loader: Tried reading package /home/practicalli/projects/practicalli/neovim-config-redux/snippets/package.json, but it does not exist

So for local snippets I need to create a package.json file that lists each file in that directory that represents a file of snippets, similar to that of the friendly snippets package

Maybe I should create a practicalli/neovim-snippets project and add that to my plugins config :)

practicalli-johnny avatar Dec 12 '22 17:12 practicalli-johnny

Cool so is working now? We can create a session in the readme with the lessons we learned in this issue :)

rafaeldelboni avatar Dec 12 '22 17:12 rafaeldelboni

Success (sort of). Adding the packages.json file guided Luasnip to the location of the actual snippets...

image

So in summary

  1. Create a .config/nvim/snippets directory
  2. Add a packages.json file that defines the language(s) that should include the snippets and the location of the snippets .json file
  3. Add a path to the lua-snip configuration (vscode.lazy_load {:paths ["./snippets"]})

The only down-side of adding a path is that friendly snippets are not available. So any other snippets also need to be added to the path (or copied locally)

Alternatively I could contribute the snippets to friendly snippets and see if they are merged - this would be a much slower process than desired. I could fork friendly snippets in the mean time and add my custom snippets there... then use the fork as the plugin.

practicalli-johnny avatar Dec 12 '22 18:12 practicalli-johnny