smjonas / snippet-converter.nvim

Bundle snippets from multiple sources and convert them to your format of choice.
Mozilla Public License 2.0
174 stars 4 forks source link

Need help for removing some snippets in friendly-snippets #6

Closed doubleChu closed 2 years ago

doubleChu commented 2 years ago

There are some license related snippets in friendly-snippets, like GPL3, LGPL2, they appear when I enter some digits and that is very annoying. I searched for ways to disable these snippets instead of modifying them myself. Now I think this plugin is just what I want for this situation but sadly I cannot get it to work. Here is my config function for the plugin:

    config = function()
      -- require("luasnip.loaders.from_vscode").load("opts")
      local template = {
        name = "Remove-License-Snippets",
        sources = {
          vscode_luasnip = { vim.fn.stdpath "data" .. "/site/pack/packer/opt/friendly-snippets" },
        },
        output = {
          vscode_luasnip = { vim.fn.stdpath "data" .. "/site/pack/packer/opt/friendly-new-snippets" },
        },
        transform_snippets = function(snippet, helper)
          if
            snippet.path:find(vim.fn.stdpath "data" .. "site/pack/packer/opt/friendly-snippets/snippets/global.json")
            and snippet.trigger.match "License"
          then
            return false
          end
        end,
      }
      require("snippet_converter").setup {
        templates = { template },
      }
    end,

Luasnip is lazy-loaded in Packer, so require("luasnip.loaders.from_vscode").load("opts") seems making snippet-converter plugin work incorrectly if I didn't comment it. I also want to know if there are any examples for disabling snippets in friendly-snippets?

smjonas commented 2 years ago

Hey, thanks for checking out my plugin! There was actually a bug that I have now fixed regarding LuaSnip snippets. I have improved your example a little bit (I also check the snippet descriptions for the word "license" / "License"):

local template = {
  name = "Remove-License-Snippets",
  sources = {
    vscode_luasnip = {
      "./friendly-snippets",
    },
  },
  output = {
    vscode_luasnip = {
      vim.fn.stdpath("data") .. "/site/pack/packer/opt/friendly-new-snippets",
    },
  },
  transform_snippets = function(snippet, _)
    if
      snippet.path:find("global%.json$") and snippet.trigger:find("[Ll]icense")
      or (snippet.description and snippet.description:find("[Ll]icense"))
    then
      return false
    end
  end,
}

I tested it with friendy-snippets and it works now :) Let me know if your issue is now fixed.

I also want to know if there are any examples for disabling snippets in friendly-snippets?

Currently there aren't any apart from your example. Do you think this or a similar example should be added to the documentation?

Please consider giving the plugin a :star: if I could help you :D

doubleChu commented 2 years ago

Thank you for replying! I just tried your config and this works great! :) For anyone who uses neovim config based on NvChad, which lazy loads LuaSnip, I figure out a way to make it work, Here is the config function for this plugin:

    config = function()
      require("luasnip.loaders.from_vscode").load(opts)
      local template = {
        name = "Remove-License-Snippets",
        sources = {
          vscode_luasnip = {"./friendly-snippets"}
        },
        output = {
          vscode_luasnip = { vim.fn.stdpath("data") .. "/site/pack/packer/opt/friendly-snippets/snippets", }
        },
        transform_snippets = function(snippet, _)
         if
           snippet.path:find("global%.json$") and snippet.trigger:find("[Ll]icense")
           or (snippet.description and snippet.description:find("[Ll]icense"))
         then
           return false
         end
        end
      }
      require("snippet_converter").setup {
        templates = {template}
      }
    end,

After doing these settings, open neovim and there would be some errors because LuaSnip is not loaded, just type i to get into insert mode, and LuaSnip would get loaded. Then do PackerSync and ConvertSnippets, your friendly-snippets json files will be replaced by the files created by this plugin. Comment out require("luasnip.loaders.from_vscode").load(opts) so next time you open neovim there will be no errors. If friendly-snippets is updated, this may not work, All you need to do is uncommenting the line above, and do it once again. I think this is useful as you don't need to delele some source code manually every time.

Currently there aren't any apart from your example. Do you think this or a similar example should be added to the documentation?

I found this plugin from Reddit through some "disable part of friendly-snnipets" keywords, so a example of how to do this would be very helpful to those who come here for this in my opinion.


Edit: Just found you don't need the require statement if you have done something like this in your LuaSnip config before, delete this line if you can convert snippets after LuaSnip is loaded, this can make the process smoother.