LuaSnip icon indicating copy to clipboard operation
LuaSnip copied to clipboard

Importing custom vscode snippets stopped working

Open danjenson opened this issue 4 years ago • 32 comments

Running :lua require("luasnip.loaders.from_vscode").load({ paths = { "~/.config/snippets" } })

No longer works. This snippet directory has always worked, and previously I had adjusted the runtime path to include it, but I prefer this method (less messy). However, it seems to have stopped loading the snippets, even when called directly from a file that would use them, i.e. t.tex and all my snippets are for tex. For reference

package.json

{
  "name": "custom-snippets",
  "contributes": {
    "snippets": [
      {
        "language": [
          "tex",
          "markdown"
        ],
        "path": "tex-math.json"
      },
      {
        "language": [
          "tex",
          "markdown"
        ],
        "path": "tex-stat.json"
      },
      {
        "language": [
          "markdown"
        ],
        "path": "markdown.json"
      }
    ]
  }
}

And one of these as an example, tex-stat.json:

{
  "probability": {
    "prefix": "pr",
    "body": "\\Pr($1)$0"
  },
  "expectation": {
    "prefix": "E",
    "body": "\\mathbb{E}($1)$0"
  },
  "measurable events": {
    "prefix": "me",
    "body": "\\mathcal{L}"
  },
  "independent": {
    "prefix": "indep",
    "body": "\\perp"
  },
  "event space": {
    "prefix": "es",
    "body": "\\Omega"
  },
  "null set": {
    "prefix": ["null", "ns"],
    "body": "\\varnothing"
  }
}

These are all top level of the ~/.config/snippets directory.

danjenson avatar Feb 05 '22 22:02 danjenson

Weird, it works fine for me (with friendly-snippets)... Could you clone friendly-snippets (not necessarily to rtp) and see whether they load?

L3MON4D3 avatar Feb 06 '22 07:02 L3MON4D3

I already load friendly snippets, and it works just fine

danjenson avatar Feb 06 '22 07:02 danjenson

Here is part of my config where I load luasnip:

	{
		"L3MON4D3/LuaSnip",
		wants = "friendly-snippets",
		after = "nvim-cmp",
		config = function()
			local luasnip = require("luasnip")
			luasnip.config.set_config({
				history = true,
				updateevents = "TextChanged,TextChangedI",
			})
			require("luasnip.loaders.from_vscode").lazy_load({ paths = { "~/.config/snippets" } })
		end,
	},

danjenson avatar Feb 06 '22 07:02 danjenson

I already load friendly snippets, and it works just fine

By directly specifying paths or just via rtp?

L3MON4D3 avatar Feb 06 '22 07:02 L3MON4D3

...I'm actually not sure what the difference is..I believe the above config says it wants friendly-snippets, so I'm not sure what packer machinery is involved, but I assume it loads it? I'm not referencing friendly-snippets anywhere else fwiw

danjenson avatar Feb 06 '22 07:02 danjenson

Ohh, IIUC wants only specifies that some plugin depends on another one, the snippets have to be added to luasnip via eg. load. Are the snippets from friendly-snippets present in the output of :LuaSnipListAvailable?

L3MON4D3 avatar Feb 06 '22 07:02 L3MON4D3

they appear to be? that generates quite a long list; how can I confirm the source of the snippets?

danjenson avatar Feb 06 '22 07:02 danjenson

Not :sweat_smile: The best way to debug that, right now, would be to just add some snippet to the different sources and check whether it's expandable.

L3MON4D3 avatar Feb 06 '22 08:02 L3MON4D3

That aside, running :lua require("luasnip.loaders.from_vscode").load({ paths = { "~/.config/snippets" } }) from a buffer for a tex file should load the snippets from that directory that are for tex, no? Isn't the simplest way just to call that command manually and see if they show up?

danjenson avatar Feb 06 '22 08:02 danjenson

Almost, the filetypes to load can be passed to load():

load({
	paths = {"..."},
	include = {"ft1", "ft2"},
	exclude = {"notft3", "notft4"}
})

L3MON4D3 avatar Feb 06 '22 08:02 L3MON4D3

So, yeah, remove the other load calls, load tex-snippets and check for presence :+1:

L3MON4D3 avatar Feb 06 '22 08:02 L3MON4D3

yea, this isn't working when I call directly from a buffer in t.tex: :lua require("luasnip.loaders.from_vscode").load({ paths = { "~/.config/snippets" }, include={"tex"} })

danjenson avatar Feb 06 '22 08:02 danjenson

Hmmmmmm, how about loading from friendly-snippets that way? eg. :lua require("luasnip.loaders.from_vscode").load({ paths = { "~/.local/share/nvim/site/pack/packer/start/friendly-snippets/" }, include={"tex"} }) (Not sure if your path for friendly-snippets is exactly the same as mine, but something like that should work)

L3MON4D3 avatar Feb 06 '22 08:02 L3MON4D3

Looks like there is an issue on NvChad detailing this too and it might be removed because it's not working? https://github.com/NvChad/NvChad/issues/719 Can you call load multiple times?

danjenson avatar Feb 06 '22 17:02 danjenson

Generally yes, but that could be prone to race-conditions as we utilize the async-file-loading from luv and append snippets to ls.snippets in the callbacks, which could lead to some snippets missing from the final tables.

Not sure how all that plays out actually, I'll be able to look into it some more in the next few days.

L3MON4D3 avatar Feb 06 '22 20:02 L3MON4D3

There definitely seems to be some non-determinism going on here. Sometimes it works, sometimes it doesn't. Below are two tex files in the same directory:

image

You can see that the one on the left is letting me select my custom al snippet, while it doesn't even appear on the right.

danjenson avatar Feb 07 '22 01:02 danjenson

Oh, could you verify snippet-availability using :LuaSnipListAvailable? That way there's fewer moving pieces (-cmp_luasnip) where the error could be introduced

L3MON4D3 avatar Feb 07 '22 06:02 L3MON4D3

Could you try if the deterministic_load-branch fixes your issues?

L3MON4D3 avatar Feb 07 '22 20:02 L3MON4D3

I'm in the middle of midterms but will try after; how do I specify a branch in packer?

danjenson avatar Feb 08 '22 02:02 danjenson

Thank you :+1: You can specify the branch in the use-call, eg. use({"L3MON4D3/Luasnip", branch="deterministic_load"}). Not sure if that plays nice with NvChad though.. (are you using NvChad?)

L3MON4D3 avatar Feb 08 '22 12:02 L3MON4D3

I'm having the same issue and am unable to load these snippets anymore in my JS/TS files: https://github.com/dsznajder/vscode-react-javascript-snippets

No config changes on my end just stopped working recently. Tried the deterministic load branch but no luck here.

monsonjeremy avatar Feb 09 '22 19:02 monsonjeremy

Could you post the generated snippets? Running the install-function from that repo results in an empty table for me.

L3MON4D3 avatar Feb 09 '22 20:02 L3MON4D3

Same issue here, I'm the one who created the issue on NvChad https://github.com/NvChad/NvChad/issues/719 I'm using Packer, and now I'm not using the preinstallated LuaSnip from NvChad, because I disabled it and tried to do it myself from scratch.

My installation:

 use {
        "L3MON4D3/LuaSnip",
        event = "InsertEnter",
        after = "nvim-cmp",
        config = function()
          local luasnip = require("luasnip")
          luasnip.config.set_config({
                  history = true,
                  updateevents = "TextChanged,TextChangedI",
          })
          require("luasnip/loaders/from_vscode").load({ paths = { "/Users/alex/.config/nvim/lua/custom/my_snippets/" }})
        end,
    }

When I open the file that I want to edit and I enter INSERT mode, no snippets are shown and if i run LuaSnipListAvailablethere is an empty map. The strange thing is that if I run:

:lua require("luasnip/loaders/from_vscode").load({ paths = { "/Users/alex/.config/nvim/lua/custom/my_snippets/" }})

From the opened file, the snippets are loaded.

nank1ro avatar Feb 11 '22 10:02 nank1ro

Could you use the other branch and call print(vim.inspect(luasnip.available())) (prints the same as :LuaSnipListAvailable) after the load?

L3MON4D3 avatar Feb 11 '22 12:02 L3MON4D3

The map is still empty

nank1ro avatar Feb 11 '22 14:02 nank1ro

Weird.. Could you post a minimal config that reproduces the issue?

L3MON4D3 avatar Feb 11 '22 14:02 L3MON4D3

Here it is my .dotfiles https://github.com/nank1ro/.dotfiles

If you want a miminal reproduction, you can follow this: Copy this folder https://github.com/nank1ro/.dotfiles/tree/main/nvim/lua/custom/my_snippets somewhere. To install I use Packer:

use {
        "L3MON4D3/LuaSnip",
        branch = "deterministic_load",
        event = "InsertEnter",
        after = "nvim-cmp",
        config = function()
          local luasnip = require("luasnip")
          luasnip.config.set_config({
                  history = true,
                  updateevents = "TextChanged,TextChangedI",
          })
          require("luasnip/loaders/from_vscode").load({ paths = { "/Users/alex/.config/nvim/lua/custom/my_snippets/" }})

        end
    }

Replace /Users/alex/.config/nvim/lua/custom/my_snippets/ with your path.

Then I open a Flutter project, If you haven't flutter installed you can see https://docs.flutter.dev/get-started/install

Then run

flutter create myapp
cd myapp
nvim lib/main.dart

Now enter Insert mode and start writing stw and no snippets are shown. stw is a shortcut for a snippet

nank1ro avatar Feb 14 '22 10:02 nank1ro

I cannot reproduce the issue, the stw-snippet loads correctly for me :/ Please try creating a file that, if sourced via nvim -u somefile, reproduces the issue (the snippet-directory can be downloaded separately, that's no problem). This should be a good starting point.

L3MON4D3 avatar Feb 14 '22 22:02 L3MON4D3

Something that solved my problem was that the file type seems to change in nvim before and after a save. nvim t.tex and then running :set filetype? returns plaintex. However, after saving and reopening the file, this returns tex, and all my snippets were defined only for tex -- adding plaintex to the snippets worked...although it's still weird that filetype inference changes..

danjenson avatar Feb 17 '22 20:02 danjenson

I cannot reproduce the issue, the stw-snippet loads correctly for me :/ Please try creating a file that, if sourced via nvim -u somefile, reproduces the issue (the snippet-directory can be downloaded separately, that's no problem). This should be a good starting point.

Finally it started working, after upgrading NvChad all seems fine. I don't know what was the problem, but now I can see my custom snippets.

nank1ro avatar Feb 21 '22 10:02 nank1ro