snippets.lua (3158B)
1 local function system(command) 2 local stdout = vim.system(command, { text = true }):wait().stdout 3 if not stdout then 4 return "" 5 end 6 return stdout:gsub('\n', '') 7 end 8 9 -- borrowed from LuaSnip, thanks... 10 local _comments_cache = {} 11 local function buffer_comment_chars() 12 local commentstring = vim.bo.commentstring 13 if _comments_cache[commentstring] then 14 return _comments_cache[commentstring] 15 end 16 local comments = { '//', '/*', '*/' } 17 local placeholder = '%s' 18 local index_placeholder = commentstring:find(vim.pesc(placeholder)) 19 if index_placeholder then 20 index_placeholder = index_placeholder - 1 21 if index_placeholder + #placeholder == #commentstring then 22 comments[1] = vim.trim(commentstring:sub(1, - #placeholder - 1)) 23 else 24 comments[2] = vim.trim(commentstring:sub(1, index_placeholder)) 25 comments[3] = vim.trim(commentstring:sub(index_placeholder + #placeholder + 1, -1)) 26 end 27 end 28 _comments_cache[commentstring] = comments 29 return comments 30 end 31 local function cmt() 32 return buffer_comment_chars()[1] 33 end 34 35 local global = { 36 { 37 prefix = 'todo', 38 body = function() 39 return cmt() .. ' TODO(' .. system { 'git', 'config', '--get', 'user.email' } .. ') ' .. os.date('%d.%m.%y') .. ': $0' 40 end, 41 }, 42 { prefix = 'bang', body = '#!/usr/bin/env $0' }, 43 { 44 prefix = 'date', 45 body = function() 46 return os.date('%d.%m.%Y') 47 end, 48 }, 49 { 50 prefix = 'time', 51 body = function() 52 return os.date('%H.%M.%S') 53 end, 54 }, 55 } 56 57 local filetypes = { 58 lua = { 59 { prefix = 'f', body = 'function($1) $0 end' }, 60 { prefix = 'F', body = 'function() $0 end' }, 61 { prefix = 'r', body = 'require(\'$1\')' }, 62 { prefix = 'l', body = 'local $1 = $0' }, 63 { prefix = 'lr', body = 'local $1 = require(\'$1\')' }, 64 }, 65 dart = { 66 -- { prefix = 'forin', body = { 'for (final $1 in $2) {', '$0', '}' } }, 67 -- { prefix = 'fori', body = { 'for (var i = $1, i < $2; i++) {', '$0', '}' } }, 68 { 69 prefix = 'c', 70 body = { 71 'import \'package:ngdart/angular.dart\';', 72 '', 73 '@Component(', 74 'selector: \'$1\'', 75 'template: \'$2\'', 76 'changeDetection: ChangeDetectionStrategy.OnPush,', 77 ')', 78 'class $3 {', 79 ' $0', 80 '}', 81 }, 82 }, 83 { prefix = 'i', body = { '@Input($1)', '$0' } }, 84 { prefix = 'o', body = { '@Output($1)', '$0' } }, 85 { prefix = 'vc', body = { '@ViewChild($1)', '$0' } }, 86 { prefix = 'vch', body = { '@ViewChildren($1)', '$0' } }, 87 }, 88 html = { 89 { prefix = 'if', body = '*ngIf="$1"' }, 90 { prefix = 'for', body = '*ngFor="let $1 of $0"' }, 91 { prefix = 'fori', body = '*ngFor="let $1 of $0"; let i=index' }, 92 { prefix = 'i', body = '[$1]="$0"' }, 93 { prefix = 'o', body = '($1)="$0"' }, 94 { prefix = 'b', body = '[($1)]="$0"' }, 95 }, 96 scss = { { prefix = 'v', body = 'var(--$0)' } }, 97 } 98 99 -- Add global to every filetype 100 for ft in pairs(filetypes) do 101 for _, snippet in ipairs(global) do 102 table.insert(filetypes[ft], snippet) 103 end 104 end 105 106 return setmetatable(filetypes, { 107 __index = function(_, _) 108 return global 109 end, 110 })