Snippets
Created by
Sergio Araújo
last modified
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | -- Filename: snippets.lua
-- Last Change: Sun, 29 Oct 2023 - 13:11
local ls = require "luasnip"
local fmt = require("luasnip.extras.fmt").fmt
local fmta = require("luasnip.extras.fmt").fmta
local rep = require("luasnip.extras").rep
-- some shorthands...
local snip = ls.snippet
local node = ls.snippet_node
local text = ls.text_node
local insert = ls.insert_node
local func = ls.function_node
local choice = ls.choice_node
local dynamicn = ls.dynamic_node
ls.config.set_config {
history = true,
-- treesitter-hl has 100, use something higher (default is 200).
ext_base_prio = 200,
-- minimal increase in priority.
ext_prio_increase = 1,
enable_autosnippets = false,
store_selection_keys = "<Tab>",
}
vim.keymap.set({ "i", "s" }, "<C-l>", function()
if ls.choice_active() then
ls.change_choice(1)
end
end)
vim.keymap.set({ "i", "s" }, "<C-h>", function()
if ls.choice_active() then
ls.change_choice(-1)
end
end)
local function get_line_iter(str)
if str:sub(-1) ~= "\n" then
str = str .. "\n"
end
return str:gmatch "(.-)\n"
end
local function box_trim_lines(str)
local new_str = ""
for line in get_line_iter(str) do
line = line:gsub("^%s+", "")
line = string.gsub(line, "%s+$", "")
new_str = new_str .. "\n" .. line
end
return new_str
end
-- local return_filename = function()
-- return vim.fn.fnamemodify(vim.fn.expand('%'),':p')
-- end
local date = function()
return { os.date "%Y-%m-%d" }
end
local fulldate = function()
return { os.date "%a, %b %Y/%m/%d - %H:%M:%S" }
end
-- lua print(os.date("%a, %b %Y/%m/%d - %H:%M:%S"))
local get_visual = function(args, parent)
if (#parent.snippet.env.SELECT_RAW > 0) then
return sn(nil, i(1, parent.snippet.env.SELECT_RAW))
else -- If SELECT_RAW is empty, return a blank insert node
return sn(nil, i(1))
end
end
local filename = function()
return { vim.fn.expand "%:p" }
end
local clipboard = function()
return { vim.fn.getreg('+')}
end
-- Make sure to not pass an invalid command, as io.popen() may write over nvim-text.
local function bash(_, _, command)
local file = io.popen(command, "r")
local res = {}
for line in file:lines() do
table.insert(res, line)
end
return res
end
local function get_port_snip(args)
if #args < 1 and not args[1][1] then
return node(nil, text "hello world")
end
local type = args[1][1]
local indent = " "
if type == "NodePort" or type == "LoadBalancer" then
return node(
nil,
fmt(
box_trim_lines [[
- port: {}
{}targetPort: {}
{}nodePort: {}
]],
{
insert(1, "30000"),
indent,
insert(2, "80"),
indent,
insert(3, "30000"),
}
)
)
end
if type == "ClusterIP" then
return node(
nil,
fmt(
[[
- port: {}
{}targetPort: {}
]],
{
insert(1, "30000"),
indent,
insert(2, "80"),
}
)
)
end
end
ls.add_snippets(nil, {
all = {
snip({
trig = "date",
namr = "Date",
dscr = "Date in the form of YYYY-MM-DD",
}, {
func(date, {}),
}),
snip({trig="slas", snippetType="autosnippet", dscr = "Insert Sérgio's name"},
{
text("Sérgio Araújo ", { space = true }),
}
),
snip({
trig = "hello",
namr = "hello_choice",
dscr = "A simple choice node snippet",
}, {
text "Hello ",
text "",
choice(1, { text("foo"), text("bar")}),
insert(0),
}
),
snip({
trig = "pwd",
namr = "PWD",
dscr = "Path to current working directory",
}, {
func(bash, {}, { user_args = { "pwd" } }),
}),
snip({
trig = "filename",
namr = "Filename",
dscr = "Absolute path to file",
}, {
func(filename, {}),
}),
snip({
trig = "signature",
namr = "Signature",
dscr = "Name and Surname",
}, {
text "Sergei Bulavintsev",
insert(0),
}),
},
sh = {
snip("shebang", {
text { "#!/bin/sh", "" },
insert(0),
}),
},
python = {
snip("shebang", {
text { "#!/usr/bin/env python", "" },
insert(0),
}),
},
lua = {
snip("shebang", {
text { "#!/usr/bin/lua", "", "" },
insert(0),
}),
snip("stemplate", {
text {
"local ls = require 'luasnip'",
"ls = ls,",
"fmt = require('luasnip.extras.fmt').fmt,",
"rep = require('luasnip.extras').rep,",
"local helpers = require('core.luasnip-helper-funcs')",
"local get_visual = helpers.get_visual",
"",
"-- some shorthands...",
"s = ls.snippet,",
"sn = ls.snippet_node,",
"n = ls.snippet_node,",
"t = ls.text_node,",
"i = ls.insert_node,",
"f = ls.function_node,",
"c = ls.choice_node,",
"d = ls.dynamic_node,",'',
},
insert(0),
}),
snip("req", {
text "require('",
insert(1, "Module-name"),
text "')",
insert(0),
}),
snip("func", {
text "function(",
insert(1, "Arguments"),
text { ")", "\t" },
insert(2),
text { "", "end", "" },
insert(0),
}),
snip("forp", {
text "for ",
insert(1, "k"),
text ", ",
insert(2, "v"),
text " in pairs(",
insert(3, "table"),
text { ") do", "\t" },
insert(4),
text { "", "end", "" },
insert(0),
}),
snip("fori", {
text "for ",
insert(1, "k"),
text ", ",
insert(2, "v"),
text " in ipairs(",
insert(3, "table"),
text { ") do", "\t" },
insert(4),
text { "", "end", "" },
insert(0),
}),
snip("if", {
text "if ",
insert(1),
text { " then", "\t" },
insert(2),
text { "", "end", "" },
insert(0),
}),
snip("M", {
text { "local M = {}", "", "" },
insert(0),
text { "", "", "return M" },
}),
},
markdown = {
-- Select link, press C-s, enter link to receive snippet
snip({
trig = "link",
namr = "markdown_link [selection]",
dscr = "Create markdown link [txt](url)",
}, {
text "[",
insert(1),
text "](",
func(function(_, snip)
return snip.env.TM_SELECTED_TEXT[1] or {}
end, {}),
text ")",
insert(0),
}),
snip({ trig ="t", namr="check list", desc = "Create item of a checklist"},
fmt("- [{}] {}",
{ choice(2, { text " ", text "-", text "x" }),
insert(1, "task"), })),
snip({
trig = "code", namr = "markdown_code_fenced",
desc = "code backsticks",
priority=1000,
},
{
text "``` ",
insert(1, "Language"),
text { "", "" },
dynamicn(2, get_visual ),
text { "", "```", "" },
insert(0),
}),
snip({
trig = "codewrap",
namr = "markdown_code_wrap",
dscr = "Create markdown code block from existing text",
}, {
text "``` ",
insert(1, "Language"),
text { "", "" },
func(function(_, snip)
local tmp = {}
tmp = snip.env.TM_SELECTED_TEXT
tmp[0] = nil
return tmp or {}
end, {}),
text { "", "```", "" },
insert(0),
}),
snip({
trig = "codeempty",
namr = "markdown_code_empty",
dscr = "Create empty markdown code block",
}, {
text "``` ",
insert(1, "Language"),
text { "", "" },
insert(2, "Content"),
text { "", "```", "" },
insert(0),
}),
snip({
trig = "meta",
namr = "Metadata",
dscr = "Yaml metadata format for markdown",
}, {
text { "---", "title: " },
insert(1, "note_title"),
text { "", "author: " },
insert(2, "author"),
text { "", "date: " },
func(date, {}),
text { "", "cathegories: [" },
insert(3, ""),
text { "]", "lastmod: " },
func(date, {}),
text { "", "tags: [" },
insert(4),
text { "]", "comments: true", "---", "" },
insert(0),
}),
snip({
trig = "_skel",
namr = "File_skeleton",
dscr = "File header (dynamic)",
}, {
text { "---", "File:", },
func(filename, {}),
text { "", "Last Change: " },
func(fulldate, {}),
text { "", "tags: [" },
insert(1),
text { "]", "---", "" },
insert(0),
}),
},
go = {
snip("test", {
text "func ",
insert(1, "Name"),
text "(t *testing.T)",
text { " {", "" },
text "\t",
insert(0),
text { "", "}" },
}),
snip("typei", {
text "type ",
insert(1, "Name"),
text { " interface {", "" },
text "\t",
insert(0),
text { "", "}" },
}),
snip("types", {
text "type ",
insert(1, "Name"),
text { " struct {", "" },
text "\t",
insert(0),
text { "", "}" },
}),
snip("func", {
text "func ",
insert(1, "Name"),
text "(",
insert(2),
text ")",
insert(3),
text { " {", "" },
text "\t",
insert(0),
text { "", "}" },
}),
snip("if", {
text "if ",
insert(1, "true"),
text { " {", "" },
text "\t",
insert(0),
text { "", "}" },
}),
snip("fori", {
text "for ",
insert(1, "i := 0"),
text ";",
insert(2, "i < 10"),
text ";",
insert(3, "i++"),
text { " {", "" },
text "\t",
insert(0),
text { "", "}" },
}),
snip("forr", {
text "for ",
insert(1, "k, v"),
text " := range ",
insert(2, "expr"),
text { " {", "" },
text "\t",
insert(0),
text { "", "}" },
}),
},
yaml = {
snip(
{
trig = "pod",
namr = "k8s Pod",
dscr = "Kubernetes Pod definition",
},
fmt(
[[
apiVersion: v1
kind: Pod
metadata:
name: {}
labels:
{}: {}
spec:
containers:
- name: {}
image: {}:{}
ports:
- containerPort: {}
]],
{
insert(1, "nginx"),
insert(2, "run"),
insert(3, "nginx"),
insert(4, "nginx"),
insert(5, "nginx"),
insert(6, "latest"),
insert(7, "80"),
}
)
),
snip(
{
trig = "deploy",
namr = "k8s Deployment",
dscr = "Kubernetes Deployment definition",
},
fmt(
[[
apiVersion: apps/v1
kind: Deployment
metadata:
name: {}
labels:
{}
spec:
replicas: {}
selector:
matchLabels:
{}
template:
metadata:
labels:
{}
spec:
containers:
- name: {}
image: {}:{}
ports:
- containerPort: {}
]],
{
insert(1, "name"),
insert(2, "label"),
insert(3, "1"),
insert(4, "label"),
rep(4),
insert(5, "container_name"),
insert(6, "image"),
insert(7, "1.0"),
insert(8, "80"),
}
)
),
snip(
{
trig = "service",
namr = "k8s Service",
dscr = "Kubernetes Service definition",
},
fmt(
[[
apiVersion: v1
kind: Service
metadata:
name: {}
labels:
{}
spec:
selector:
{}
type: {}
ports:
{}
]],
{
insert(1, "name"),
insert(2),
insert(3),
choice(4, {
text "ClusterIP",
text "NodePort",
text "LoadBalancer",
}),
dynamicn(5, get_port_snip, { 4 }),
}
)
),
},
})
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.