commit ff178863fe9b5b66177d31ed6cea5f420c5184b2
parent 40103eb12854b45ed207510caa8dbd424e93abdd
Author: Tomas Nemec <owl@gtms.dev>
Date: Tue, 9 Jan 2024 06:29:06 +0100
update
Diffstat:
3 files changed, 179 insertions(+), 0 deletions(-)
diff --git a/after/plugin/cmp.lua b/after/plugin/cmp.lua
@@ -56,6 +56,9 @@ local function close()
end
end
+local snippet_source = require 'tms.p.cmp.snippet_source'
+cmp.register_source('snippets', snippet_source)
+
cmp.setup({
window = {
--
@@ -101,6 +104,7 @@ cmp.setup({
-- }, { { name = 'buffer' } }),
sources = cmp.config.sources({
+ { name = 'snippets' },
{ name = 'nvim_lsp' },
{ name = 'nvim_lsp_signature_help' },
{ name = 'nvim_lua' },
diff --git a/lua/tms/p/cmp/snippet_source.lua b/lua/tms/p/cmp/snippet_source.lua
@@ -0,0 +1,64 @@
+local M = {}
+
+local cache = {}
+
+function M:is_available()
+ return true
+end
+
+function M:get_debug_name()
+ return 'snippets'
+end
+
+function M:complete(_, callback)
+ local snippets = require'tms.snippets'[vim.bo.filetype] or {}
+
+ local response = {}
+
+ for key in pairs(snippets) do
+ local snippet = snippets[key]
+ local body = snippet.body
+ if type(body) == 'function' then
+ body = body()
+ end
+ if type(body) == 'table' then
+ body = table.concat(body, '\n')
+ end
+
+ local prefix = snippets[key].prefix
+ if type(prefix) == 'table' then
+ for _, p in ipairs(prefix) do
+ table.insert(response, {
+ label = p,
+ kind = require'cmp'.lsp.CompletionItemKind.Snippet,
+ insertText = p,
+ data = { prefix = p, body = body },
+ })
+ end
+ else
+ table.insert(response, {
+ label = prefix,
+ kind = require'cmp'.lsp.CompletionItemKind.Snippet,
+ insertText = prefix,
+ data = { prefix = prefix, body = body },
+ })
+ end
+ end
+ callback(response)
+end
+
+function M:resolve(completion_item, callback)
+ completion_item.documentation = { kind = require'cmp'.lsp.MarkupKind.Markdown, value = completion_item.data.body }
+ callback(completion_item)
+end
+
+function M:execute(completion_item, callback)
+ callback(completion_item)
+ local cursor = vim.api.nvim_win_get_cursor(0)
+ cursor[1] = cursor[1] - 1
+
+ vim.api.nvim_buf_set_text(0, cursor[1], cursor[2] - #completion_item.data.prefix, cursor[1], cursor[2], { '' })
+ vim.snippet.expand(completion_item.data.body)
+end
+
+return M
diff --git a/lua/tms/snippets.lua b/lua/tms/snippets.lua
@@ -0,0 +1,111 @@
+local function shell(command)
+ local file = io.popen(command, 'r')
+ local res = {}
+ for line in file:lines() do
+ table.insert(res, line)
+ end
+ return res[1]
+end
+
+-- borrowed from LuaSnip, thanks...
+local _comments_cache = {}
+local function buffer_comment_chars()
+ local commentstring = vim.bo.commentstring
+ if _comments_cache[commentstring] then
+ return _comments_cache[commentstring]
+ end
+ local comments = { '//', '/*', '*/' }
+ local placeholder = '%s'
+ local index_placeholder = commentstring:find(vim.pesc(placeholder))
+ if index_placeholder then
+ index_placeholder = index_placeholder - 1
+ if index_placeholder + #placeholder == #commentstring then
+ comments[1] = vim.trim(commentstring:sub(1, -#placeholder - 1))
+ else
+ comments[2] = vim.trim(commentstring:sub(1, index_placeholder))
+ comments[3] = vim.trim(commentstring:sub(index_placeholder + #placeholder + 1, -1))
+ end
+ end
+ _comments_cache[commentstring] = comments
+ return comments
+end
+local function cmt()
+ return buffer_comment_chars()[1]
+end
+
+local global = {
+ {
+ prefix = 'todo',
+ body = function()
+ return cmt() .. ' TODO(' .. shell('id -un') .. ') ' .. os.date('%d.%m.%y') .. ': $0'
+ end,
+ },
+ { prefix = 'bang', body = '#!/usr/bin/env $0' },
+ {
+ prefix = 'date',
+ body = function()
+ return os.date('%d.%m.%Y')
+ end,
+ },
+ {
+ prefix = 'time',
+ body = function()
+ return os.date('%H.%M.%S')
+ end,
+ },
+}
+
+local filetypes = {
+ lua = {
+ { prefix = 'f', body = 'function($1) $0 end' },
+ { prefix = 'F', body = 'function() $0 end' },
+ { prefix = 'r', body = 'require(\'$1\')' },
+ { prefix = 'l', body = 'local $1 = $0' },
+ { prefix = 'lr', body = 'local $1 = require(\'$1\')' },
+ },
+ dart = {
+ -- { prefix = 'forin', body = { 'for (final $1 in $2) {', '$0', '}' } },
+ -- { prefix = 'fori', body = { 'for (var i = $1, i < $2; i++) {', '$0', '}' } },
+ {
+ prefix = 'c',
+ body = {
+ 'import \'package:ngdart/angular.dart\';',
+ '',
+ '@Component(',
+ 'selector: \'$1\'',
+ 'template: \'$2\'',
+ 'changeDetection: ChangeDetectionStrategy.OnPush,',
+ ')',
+ 'class $3 {',
+ ' $0',
+ '}',
+ },
+ },
+ { prefix = 'i', body = { '@Input($1)', '$0' } },
+ { prefix = 'o', body = { '@Output($1)', '$0' } },
+ { prefix = 'vc', body = { '@ViewChild($1)', '$0' } },
+ { prefix = 'vch', body = { '@ViewChildren($1)', '$0' } },
+ },
+ html = {
+ { prefix = 'if', body = '*ngIf="$1"' },
+ { prefix = 'for', body = '*ngFor="let $1 of $0"' },
+ { prefix = 'fori', body = '*ngFor="let $1 of $0"; let i=index' },
+ { prefix = 'i', body = '[$1]="$0"' },
+ { prefix = 'o', body = '($1)="$0"' },
+ { prefix = 'b', body = '[($1)]="$0"' },
+ },
+ scss = { { prefix = 'v', body = 'var(--$0)' } },
+}
+
+-- Add global to every filetype
+for ft in pairs(filetypes) do
+ for _, snippet in ipairs(global) do
+ table.insert(filetypes[ft], snippet)
+ end
+end
+
+return setmetatable(filetypes, {
+ __index = function(_, _)
+ return global
+ end,
+})