From 535b2236a134a4e36979ae27159c3f46e808ca6c Mon Sep 17 00:00:00 2001 From: Ariel D'Alessandro Date: Sat, 12 Feb 2022 21:07:15 +0100 Subject: improve snippets --- after/plugin/luasnip.lua | 127 +++++++++++++++++++++++++++++++++++++++++++++ lua/alpha/snips/ft/go.lua | 3 ++ lua/alpha/snips/ft/lua.lua | 46 ++++++++++++++++ lua/alpha/snips/ft/php.lua | 110 +++++++++++++++++++++++++++++++++++++++ plugin/keymaps.vim | 21 ++++---- 5 files changed, 297 insertions(+), 10 deletions(-) create mode 100644 lua/alpha/snips/ft/go.lua create mode 100644 lua/alpha/snips/ft/lua.lua create mode 100644 lua/alpha/snips/ft/php.lua diff --git a/after/plugin/luasnip.lua b/after/plugin/luasnip.lua index 9a1c3db..1d6495a 100644 --- a/after/plugin/luasnip.lua +++ b/after/plugin/luasnip.lua @@ -2,6 +2,133 @@ if vim.g.snippets ~= "luasnip" then return end +local ls = require "luasnip" +local types = require "luasnip.util.types" + +ls.config.set_config { + -- This tells LuaSnip to remember to keep around the last snippet. + -- You can jump back into even if you move outside of the selection + history = true, + + -- This one is cool cause if you have dynamic snippets, it updatesas you type! + updateevent = "TextChanged,TextChangedI", + + -- Autosnippets: + enable_autosnippets = true, + + ext_opts = { + [types.choiceNode] = { + active = { + virt_text = { { "<-", "Error" } }, + }, + }, + }, +} + +-- create snippet +-- s(context, nodes, condition, ...) +local snippet = ls.s + +-- TODO: Write about this. +-- Useful for dynamic nodes and choice nodes +local snippet_from_nodes = ls.sn + +-- This is the simplest node. +-- Creates a new text node. Places cursor after node by default. +-- t { "this will be inserted" } +-- +-- Multiple lines are by passing a table of strings. +-- t { "line 1", "line 2" } +local t = ls.text_node + +-- Insert Node +-- Creates a location for the cursor to jump to. +-- Possible options to jump to are 1 - N +-- If you use 0, that's the final place to jump to. +-- +-- To create placeholder text, pass it as the second argument +-- i(2, "this is placeholder text") +local i = ls.insert_node + +-- Function Node +-- Takes a function that returns text +local f = ls.function_node + +-- This a choice snippet. You can move through with (in my config) +-- c(1, { t {"hello"}, t {"world"}, }), +-- +-- The first argument is the jump position +-- The second argument is a table of possible nodes. +-- Note, one thing that's nice is you don't have to include +-- the jump position for nodes that normally require one (can be nil) +local c = ls.choice_node + +local d = ls.dynamic_node + +-- TODO: Document what I've learned about lambda +local l = require("luasnip.extras").lambda + +local events = require "luasnip.util.events" + +local shortcut = function(val) + if type(val) == "string" then + return { t { val }, i(0) } + end + + if type(val) == "table" then + for k, v in ipairs(val) do + if type(v) == "string" then + val[k] = t { v } + end + end + end + + return val +end + +local make = function(tbl) + local result = {} + for k, v in pairs(tbl) do + table.insert(result, (snippet({ trig = k, desc = v.desc }, shortcut(v)))) + end + + return result +end + + + +local snippets = {} + +for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/alpha/snips/ft/*.lua", true)) do + local ft = vim.fn.fnamemodify(ft_path, ":t:r") + snippets[ft] = make(loadfile(ft_path)()) +end + +ls.snippets = snippets + +-- is my expansion key +-- this will expand the current item or jump to the next item within the snippet. +vim.keymap.set({ "i", "s" }, "", function() + if ls.expand_or_jumpable() then + ls.expand_or_jump() + end +end, { silent = true }) + +-- is my jump backwards key. +-- this always moves to the previous item within the snippet +vim.keymap.set({ "i", "s" }, "", function() + if ls.jumpable(-1) then + ls.jump(-1) + end +end, { silent = true }) + +-- is selecting within a list of options. +-- This is useful for choice nodes (introduced in the forthcoming episode 2) +vim.keymap.set("i", "", function() + if ls.choice_active() then + ls.change_choice(1) + end +end) -- shorcut to source my luasnips file again, which will reload my snippets vim.keymap.set("n", "s", "source ~/.config/nvim/after/plugin/luasnip.lua") diff --git a/lua/alpha/snips/ft/go.lua b/lua/alpha/snips/ft/go.lua new file mode 100644 index 0000000..5044d6f --- /dev/null +++ b/lua/alpha/snips/ft/go.lua @@ -0,0 +1,3 @@ +local M = {} + +return M diff --git a/lua/alpha/snips/ft/lua.lua b/lua/alpha/snips/ft/lua.lua new file mode 100644 index 0000000..539751e --- /dev/null +++ b/lua/alpha/snips/ft/lua.lua @@ -0,0 +1,46 @@ +local ls = require "luasnip" + +-- local snippet = ls.s +local snippet_from_nodes = ls.sn +local c = ls.choice_node +local d = ls.dynamic_node +local i = ls.insert_node +local t = ls.text_node + +local fmt = require("luasnip.extras.fmt").fmt + +local require_var = function(args, _) + local text = args[1][1] or "" + local split = vim.split(text, ".", { plain = true }) + + local options = {} + for len = 0, #split - 1 do + table.insert(options, t(table.concat(vim.list_slice(split, #split - len, #split), "_"))) + end + + return snippet_from_nodes(nil, { + c(1, options), + }) +end + +local newline = function(text) + return t { "", text } +end + +return { + req = fmt([[local {} = require("{}")]], { + d(2, require_var, { 1 }), + i(1), + }), + lf = { + desc = "table function", + "local ", + i(1), + " = function(", + i(2), + ")", + newline " ", + i(0), + newline "end", + }, +} diff --git a/lua/alpha/snips/ft/php.lua b/lua/alpha/snips/ft/php.lua new file mode 100644 index 0000000..c2210a9 --- /dev/null +++ b/lua/alpha/snips/ft/php.lua @@ -0,0 +1,110 @@ +local ls = require("luasnip") + +local snippet_from_nodes = ls.sn +local c = ls.choice_node +local t = ls.text_node +local d = ls.dynamic_node +local f = ls.function_node +local i = ls.insert_node +local fmt = require("luasnip.extras.fmt").fmt + + +function string.starts(String,Start) + return string.sub(String,1,string.len(Start))==Start +end + +local newline = function(text) + return t { "", text } +end + +local visibility = function(_, _, _, inital) + local possibles = {'private', 'public', 'protected'} + + local options = {} + + for _, value in pairs(possibles) do + if value == inital then + table.insert(options, 1, t(value)) + else + table.insert(options, t(value)) + end + end + + return snippet_from_nodes(nil, { + c(1, options) + }) +end + +local promoted_property +promoted_property = function() + return snippet_from_nodes(nil, { + c(1, { + t(""), + snippet_from_nodes(nil, { + newline "\t", + d(1, visibility, {}, "private"), + i(2, '$var'), + t ",", + d(3, promoted_property, {}) + }), + }), + }) +end + +local namespace = function () + local dir = vim.fn.expand('%:h') + local autoloads = vim.call('composer#query', 'autoload.psr-4') + if autoloads == nil then + return (dir:gsub("^%l", string.upper)) + end + + local globalNamespace + for key, value in pairs(autoloads) do + if string.starts(dir, value:sub(1, -2)) then + globalNamespace = key:sub(1, -2); + dir = dir:sub(#key + 1) + break + end + end + dir = dir:gsub("/", "\\") + + return string.format("%s\\%s",globalNamespace, dir ) +end + +local class_name = function () + local filename = vim.fn.expand('%:t:r') + return filename +end +local M = { + v = fmt( +[[ +/** + * @var {} + */ +{} ${}; +]], { + i(1, "type"), + d(2, visibility, { 1 }, "private"), + i(3, "var") + }), + class = fmt( +[[ + >u vnoremap < >gv -nnoremap :wincmd h -nnoremap :wincmd j -nnoremap :wincmd k -nnoremap :wincmd l -nnoremap :wincmd q +nnoremap wh :wincmd h +nnoremap wj :wincmd j +nnoremap wk :wincmd k +nnoremap wl :wincmd l +nnoremap wq :wincmd q + " Make windows to be basically the same size nnoremap = = -nnoremap wv :vsp -nnoremap wh :sp +nnoremap wsv :vsp +nnoremap wsh :sp -nnoremap :cnext -nnoremap :cprev +nnoremap j :cnext +nnoremap k :cprev nnoremap i :lnext nnoremap o :lprev -nnoremap bd :bd +nnoremap bd :bd! nnoremap dp ":saveas " . expand('%') -- cgit v1.2.3