mirror of
https://github.com/foo-dogsquared/wiki.git
synced 2025-04-30 00:19:16 +00:00
1 line
16 KiB
JSON
1 line
16 KiB
JSON
{"pageProps":{"metadata":{"date":"2022-04-21 15:33:29 +08:00","date_modified":"2022-04-21 16:41:42 +08:00","language":"en","source":""},"title":"Neovim plugin: LuaSnip","hast":{"type":"root","children":[{"type":"element","tagName":"nav","properties":{"className":"toc"},"children":[{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-1"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"example-snippets-in-lua"},"children":[{"type":"text","value":"Example snippets in Lua"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/editor.neovim.plugins.luasnip#example-snippets-in-lua"},"children":[{"type":"text","value":"Example snippets in Lua"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"simple-word-trigger"},"children":[{"type":"text","value":"Simple word trigger"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/editor.neovim.plugins.luasnip#simple-word-trigger"},"children":[{"type":"text","value":"Simple word trigger"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"patterned-trigger"},"children":[{"type":"text","value":"Patterned trigger"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/editor.neovim.plugins.luasnip#patterned-trigger"},"children":[{"type":"text","value":"Patterned trigger"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"choosing-between-asciidoctor-admonition-blocks"},"children":[{"type":"text","value":"Choosing between Asciidoctor admonition blocks"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/editor.neovim.plugins.luasnip#choosing-between-asciidoctor-admonition-blocks"},"children":[{"type":"text","value":"Choosing between Asciidoctor admonition blocks"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"dynamic-asciidoctor-header-with-optional-automatic-toc"},"children":[{"type":"text","value":"Dynamic Asciidoctor header with optional automatic TOC"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/editor.neovim.plugins.luasnip#dynamic-asciidoctor-header-with-optional-automatic-toc"},"children":[{"type":"text","value":"Dynamic Asciidoctor header with optional automatic TOC"}]}]}]}]}]}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"manual from "},{"type":"element","tagName":"a","properties":{"href":"/editor.neovim.help-system"},"children":[{"type":"text","value":"Neovim help system"}]},{"type":"text","value":": "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"luasnip.txt"}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"snippets are made of nodes\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"there are different type of nodes;\n these can be found on the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"luasnip-snippets"}]},{"type":"text","value":" on the manual\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"text nodes"}]},{"type":"text","value":" are just static text\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"insert nodes"}]},{"type":"text","value":" are points to be edited;\n can be given default text for the snippet\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"function nodes"}]},{"type":"text","value":" generate text based from other nodes;\n this is used mostly to generate dynamic output from the snippet\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"choice nodes"}]},{"type":"text","value":" contains multiple nodes to be chosen for a given jump position\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"snippet nodes"}]},{"type":"text","value":" contains other node to be inserted in the snippet\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dynamic nodes"}]},{"type":"text","value":" are similar to function nodes except they return snippet nodes\n"}]}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"LuaSnip can support several types of snippets other than its own\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"SnipMate snippets\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Visual Studio Code snippets\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lua snippets, obviously\n"}]}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"snippets are not automatically loaded unless you have invoked the appropriate function for a certain type;\n you will not load Visual Studio Code-style snippets unless you have run "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"lazy_load"}]},{"type":"text","value":" function from "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"luasnip.loaders.from_vscode"}]},{"type":"text","value":";\n for more details, each supported format has a specific section for loading them from the manual (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"luasnip-vscode-snippets-loader"}]},{"type":"text","value":")\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"adding snippets is as simple as adding them in "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"require(\"luasnip\").snippets.FILETYPE"}]},{"type":"text","value":";\n though, you have to know how to add a snippet through LuaSnip module\n"}]}]}]},{"type":"element","tagName":"h1","properties":{"id":"example-snippets-in-lua"},"children":[{"type":"text","value":"Example snippets in Lua"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We'll explore different examples from the simplest to the more complex snippets made with Lua.\nThe following code blocks will assume the environment with the default value from "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"luasnip.config.snip_env"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The plugin source code also has a comprehensive set of examples in the "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Examples/"}]},{"type":"text","value":" folder.\nIt also has a "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/L3MON4D3/LuaSnip/wiki"},"children":[{"type":"text","value":"wiki"}]},{"type":"text","value":" containing multiple example snippets showcasing the various nodes.\n"}]},{"type":"element","tagName":"h2","properties":{"id":"simple-word-trigger"},"children":[{"type":"text","value":"Simple word trigger"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The simplest snippet possible.\nThis will simply replace "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"hello"}]},{"type":"text","value":" with "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"world"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-lua"]},"children":[{"type":"text","value":"s(\"hello\", t \"world\")\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exploring other nodes, we could replace the text node with a function node.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-lua"]},"children":[{"type":"text","value":"s(\"hello\", f(function ()\n return \"world\"\nend))\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"patterned-trigger"},"children":[{"type":"text","value":"Patterned trigger"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A simple snippet that will answer you back when triggered.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This snippet makes use of:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pattern triggers (see "},{"type":"element","tagName":"a","properties":{"href":"roam:Lua patterns"},"children":[{"type":"text","value":"roam:Lua patterns"}]},{"type":"text","value":").\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Snippet captures similar to UltiSnips' capture.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Usage of a function node.\n"}]}]}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-lua"]},"children":[{"type":"text","value":"s(\n { trig = \"hello (%a+)\", regTrig = true },\n f(function (_, snip)\n return string.format(\"hey there, %s\", snip.captures[1])\n end)\n)\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"choosing-between-asciidoctor-admonition-blocks"},"children":[{"type":"text","value":"Choosing between Asciidoctor admonition blocks"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can easily create a snippet for multiple choices.\nIn Asciidoctor, we have "},{"type":"element","tagName":"a","properties":{"href":"https://docs.asciidoctor.org/asciidoc/latest/blocks/admonitions/"},"children":[{"type":"text","value":"admonition blocks"}]},{"type":"text","value":" that are formatted similarly.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's one way to define such snippet...\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-lua"]},"children":[{"type":"text","value":"s(\n \"admo\",\n fmt([[\n {}: {}\n ]], {\n c(1, {\n t \"NOTE\",\n t \"TIP\",\n t \"IMPORTANT\",\n t \"CAUTION\",\n t \"WARNING\",\n }),\n i(0),\n })\n)\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"dynamic-asciidoctor-header-with-optional-automatic-toc"},"children":[{"type":"text","value":"Dynamic Asciidoctor header with optional automatic TOC"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is a snippet for Asciidoc documents for creating headers.\nAs an example, "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"h6"}]},{"type":"text","value":" should print the appropriate header level.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It should follow some restrictions for this snippet.\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It can only have a maximum level of 6.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As a bonus, we could set the table of contents on if the header level is 1.\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This snippet makes use of dynamic node to change the node between a choice node for the first heading level and a blank text node for the rest.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-lua"]},"children":[{"type":"text","value":"function min_asciidoc_header_level(level)\n math.min(level, 6)\nend\n\ns(\n { trig = \"h(%d)\", regTrig = true },\n fmt([[\n {} {}\n {}\n {}\n ]], {\n f(function(_, snip)\n local level = min_asciidoc_header(snip.captures[1])\n return string.rep(\"=\", level)\n end),\n i(1, \"CHAPTER\"),\n d(2, function(_, snip)\n local nodes = {}\n table.insert(nodes, t \"\")\n\n local level = min_asciidoc_header(snip.captures[1])\n\n if level == 1 then\n table.insert(nodes, t \":toc:\")\n end\n\n local parent = c(1, nodes)\n if level > 1 then\n parent = t \"\"\n end\n\n return sn(nil, parent)\n end, {}),\n i(0),\n })\n)\n"}]}]}]},"backlinks":[]},"__N_SSG":true} |