1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<!DOCTYPE html>
<html lang="fr" xml:lang="fr">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Roch Delannay | Les snippets dans LazyVim</title>
<meta name="author" content="Roch Delannay" />
<meta name="description" content="Blog | Carnet de recherche de Roch Delannay" />
<meta name="dcterms.date" content="2023-12-22" />
<meta name="generator" content="Pandoc, GNU make" />
<!-- pandoc - https://pandoc.org/ -->
<!-- gnu make - https://www.gnu.org/software/make/ -->
<link rel="stylesheet" href="/css/styles.css" /> </head>
<body>
<header class="main-header">
<nav class="navbar">
<ul>
<li><a href="/">Accueil</a></li>
<li><a href="/pages/publications.html">Publications</a></li>
<li><a href="/pages/cours.html">Enseignements</a></li>
<li><a href="/pages/glossaire.html">Glossaire</a></li>
<li class="right"><a href="/index-cache.html"></a></li>
</ul>
</nav></header> <div class="container">
<header class="header-bloc">
<h1>Les snippets dans LazyVim</h1>
<!-- -->
<time>2023-12-22</time>
<!-- <p>Roch Delannay</p>
-->
</header>
<div class="content">
<p>Aujourd’hui je viens de tester l’utilisation des snippets dans LazyVim (il y aura un prochain billet sur la configuration de mon environnement d’écriture.).</p>
<p>Pour faire des snippets rien de plus simple ! Il suffit de charger le plugin LuaSnip (merci au fil de <a href="https://github.com/LazyVim/LazyVim/discussions/54">discussion</a>) :</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode lua"><code class="sourceCode lua"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a> <span class="op">{</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> <span class="st">"L3MON4D3/LuaSnip"</span><span class="op">,</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> <span class="va">config</span> <span class="op">=</span> <span class="kw">function</span><span class="op">()</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">require</span><span class="op">(</span><span class="st">"luasnip.loaders.from_lua"</span><span class="op">).</span>load<span class="op">({</span> <span class="va">paths</span> <span class="op">=</span> <span class="st">"~/.config/nvim/snippets"</span> <span class="op">})</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">end</span><span class="op">,</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> <span class="op">},</span></span></code></pre></div>
<p>puis d’ajouter les snippets en LUA dans le dossier donné dans le chemin ci-dessus.</p>
<p>Pour mon premier snippet, j’ai créé un noeud texte pour générer le YAML des posts de ce blog.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode lua"><code class="sourceCode lua"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">local</span> <span class="va">ls</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">"luasnip"</span><span class="op">)</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="kw">local</span> <span class="va">s</span> <span class="op">=</span> <span class="va">ls</span><span class="op">.</span><span class="va">snippet</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="kw">local</span> <span class="va">t</span> <span class="op">=</span> <span class="va">ls</span><span class="op">.</span><span class="va">text_node</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="cf">return</span> <span class="op">{</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> <span class="co">-- Example of a multiline text node</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> s<span class="op">({</span><span class="va">trig</span> <span class="op">=</span> <span class="st">"@post"</span><span class="op">,</span> <span class="va">dscr</span> <span class="op">=</span> <span class="st">"return yaml for posts on my blog."</span><span class="op">},</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> <span class="op">{</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a> t<span class="op">({</span><span class="st">"---"</span><span class="op">,</span> <span class="st">"title: ''"</span><span class="op">,</span> <span class="st">"date:"</span><span class="op">,</span> <span class="st">"---"</span><span class="op">})</span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a> <span class="op">),</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span></code></pre></div>
<p>C’est aussi simple que ça ! Il me suffit d’appeler le déclencheur <code>@post</code> pour voir apparaître mes 3 lignes de YAML :-)</p>
<figure>
<img src="/images/snippets.gif" alt="Un petit exemple" />
<figcaption aria-hidden="true">Un petit exemple</figcaption>
</figure>
</div>
</div>
<footer>
<div>
<p>CC BY 4.0 Roch Delannay</p>
<p>Créé avec Pandoc et Make</p>
<a href="/pages/colophon.html">Colophon</a>
</div>
</footer> </body>
</html>
|