L3MON4D3 / LuaSnip

Snippet Engine for Neovim written in Lua.
Apache License 2.0
3.43k stars 242 forks source link

B #813

Open ghost opened 1 year ago

hwayne commented 1 year ago

Same problem. One thing I just found, though, is that the wiki adds a little bit of extra stuff: https://github.com/L3MON4D3/LuaSnip/wiki/Cool-Snippets

no-vici commented 1 year ago

People are depending on you to explain how to use your product to them. If the docs are this confusing, then how can you expect them to install and use it?

I find your comment/suggestion quite offensive. The documentation for LuaSnip is great, mostly everything is explained thoroughly with great accompanying examples. The best thing I like about LuaSnip is the support from its author behind it, he has answered most of the users' questions, even dumpiest ones like mine. I played with my settings for a few days and did not find a solution, shortly after rasing the flag, I had the answer. He even told me to ask question earlier instead of blindly swimming in the mist. I am very grateful to his work and generosity for giving all the hard-work away with helpful answers.

LuaSnip is not a product, it's a great gift, just read the doc thoroughly, I'm sure you will find out.

  1. Keymaps can be set in Lua.
    local map = vim.keymap.set
    -- A few keymaps for choiceNode of LuaSnip.
    map({ "i", "s" }, "<A-n>", "<Plug>luasnip-next-choice")
    map({ "i", "s" }, "<A-p>", "<Plug>luasnip-prev-choice")
  2. I mentioned above, you need to read the doc, bellow is an example:
-- add nvim keybinds
snippet(
    { trig = "nkey", name = "nvim keybinds", dscr = "add a nvim keybinds" },
    fmta([[map("<>", "<>", "<>", "<>")]], { i(1, "n"), i(2, "keybind"), i(3, "command"), i(4, "options") }
)
  1. I don't how to address this answer, as far as I know that LuaSnip is compatible with friendly-snippets used with VSCode. We are talking about neovim here though. I have converted some of the snippets from friendly-snippets into LuaSnip formats, if you fancy, I can share.

I am very sure that LuaSnip has helped developers and firms around the world save millions of dollars.

evesdropper commented 1 year ago

Adding to the above response, I'd recommend checking out resources for new users in the readme before looking at the documentation - it can be pretty dense, especially if you want to pick up writing snippets and don't really know where to start.

Additionally, VSCode/Snipmate formats are just two different types of ways to define a snippet, and the documentation does not teach you how to write in that format but rather how to load them if you already have snippets defined in such formats.

For what it's worth, if you are planning to write snippets in Lua, there's quite a bit of a learning curve, so don't worry if you don't understand how to write snippets right away.

no-vici commented 1 year ago

Additionally, VSCode/Snipmate formats are just two different types of ways to define a snippet, and the documentation does not teach you how to write in that format but rather how to load them if you already have snippets defined in such formats.

Thank you for making the above bit clearer.

It took me a few weeks to get my head around LuaSnip, but that was an enjoyable learning journey, still wouldn't dare to dig deeper into the codebase. I will bite my way into that slowly ;)

polirritmico commented 1 year ago

Hello, first of all, thank you very much for this plugin. I don't want to be rude like the OP, but I agree that the mappings section is very vague and could be improved. Specifically, it would be greatly appreciated if the examples were in Lua, especially for those of us who are not familiar with somewhat intricate configurations. After reading, rereading, and reading the documentation again, IMHO, it doesn't clarify much for the lost ones like me. After a couple of hours, I managed to achieve this result:

local ls = require("luasnip")
local my_opts = {expr = true, silent = true}
vim.keymap.set({"i", "s"}, "<c-j>", function()
    if ls.expand_or_jumpable() then
        ls.expand_or_jump()
    end
end, my_opts)
vim.keymap.set({ "i", "s" }, "<c-k>", function()
    if ls.jumpable(-1) then
        ls.jump(-1)
    end
end, my_opts)
vim.keymap.set({"i", "s"}, "<c-l>", function()
    if ls.choice_active() then
        return "<Plug>luasnip-next-choice"
    end
end, my_opts)
vim.keymap.set({"i", "s"}, "<c-h>", function()
    if ls.choice_active() then
        return "<Plug>luasnip-prev-choice"
    end
end, my_opts)

I'm not sure if it's the correct procedure, but first of all, these are the most complicated mappings I have configured so far among all the plugins I have installed by a lot. And second, I couldn't find the answer in the documentation. In fact, the help confused me a little: :h luasnip on line 726 reads as follows:

vim.api.nvim_set_keymap("i", "<C-n>", "<Plug>luasnip-next-choice", {})

But that generates this error when we press the combination outside of a choice_node:

E5108: Error executing lua: .../luasnip/init.lua:398: No active choiceNode
stack traceback:
        [C]: in function 'assert'
        .../luasnip/init.lua:398: in function 'change_choice'

I also can't find a function in the API for <Plug>luasnip-next-choice or something that helps in that regard without more programing.

In the README, the complete command is provided:

imap <silent><expr> <C-E> luasnip#choice_active() ? '<Plug>luasnip-next-choice' : '<C-E>'
smap <silent><expr> <C-E> luasnip#choice_active() ? '<Plug>luasnip-next-choice' : '<C-E>'

But it's not in Lua. If there is a reason for this, it would be great to point it out.

Anyway, I think adding logic to a mapping configuration is too much overhead for someone who has just discovered the plugin. Beyond the misinformation or the technical errors in this comment, consider it as constructive feedback from a beginner.

Regards.

L3MON4D3 commented 1 year ago

Have you considered watching/reading one of the introductory guides? IMO they do a pretty good job of giving quick overviews of luasnip. The reason for the keybindings not being in lua is that the vimscript-api is perfectly sufficient (I think a bit more readable, even), just putting those commands into a vim.cmd-block will work just fine. But of course I do understand that having everything in lua is also nice, I'll put some examples up. (the mapping-section used to be more comprehensive, but I cut it down because it dominated the readme, or would have to be collapsed, which means it won't show up when searching for it)

polirritmico commented 1 year ago

I already read watch/read them. Yea, they are great. I was having trouble only with the mappings. Thanks for the update. That's exactly what I was looking for in the first place!

After that i discover that my problems where mainly by using the ctrl key in the mappings cause it conflicts with some legacy standards (, , etc) setted by my terminal. But that is outside the scope.

Anyway, thank you very much for the plugin and your help