From 0fb3cfeef93bed613f574aa20992d2c728dc343e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 18 May 2024 14:56:00 +0200 Subject: [PATCH] moar nvim: snippets and other things --- nvim/lazy-lock.json | 36 ++++---- nvim/lua/config/autocmd.lua | 1 + nvim/lua/config/cmp.lua | 124 ++++++++++++++------------- nvim/lua/config/icons.lua | 10 ++- nvim/lua/config/keymaps.lua | 19 +++-- nvim/lua/config/lsp.lua | 1 + nvim/lua/config/options.lua | 6 +- nvim/lua/plugins/core.lua | 15 +++- nvim/lua/plugins/lsp.lua | 112 +++++++++++++------------ nvim/lua/setup.lua | 1 - nvim/lua/utils/cmd.lua | 45 ++++++++++ nvim/lua/utils/lsp.lua | 22 +++++ nvim/snippets/all.lua | 9 ++ nvim/snippets/php.lua | 163 ++++++++++++++++++++++++++++++++++++ 14 files changed, 426 insertions(+), 138 deletions(-) create mode 100644 nvim/lua/utils/cmd.lua create mode 100644 nvim/lua/utils/lsp.lua create mode 100644 nvim/snippets/all.lua create mode 100644 nvim/snippets/php.lua diff --git a/nvim/lazy-lock.json b/nvim/lazy-lock.json index 23ed47c..5bb94c7 100644 --- a/nvim/lazy-lock.json +++ b/nvim/lazy-lock.json @@ -1,29 +1,31 @@ { - "LuaSnip": { "branch": "master", "commit": "b152822e1a4bafb6bdf11a16cc26525cbd95ee00" }, - "catppuccin": { "branch": "main", "commit": "6af91673ceccbd6e073b66c439e7fce239f72d24" }, + "LuaSnip": { "branch": "master", "commit": "03c607c2bd4db0238d8f93a6393ffbd931466390" }, + "catppuccin": { "branch": "main", "commit": "d97387aea8264f484bb5d5e74f2182a06c83e0d8" }, "cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" }, - "cmp-nvim-lsp": { "branch": "main", "commit": "5af77f54de1b16c34b23cba810150689a3a90312" }, + "cmp-nvim-lsp": { "branch": "main", "commit": "39e2eda76828d88b773cc27a3f61d2ad782c922d" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, - "conform.nvim": { "branch": "master", "commit": "12b3995537f52ba2810a9857e8ca256881febbda" }, - "fd": { "branch": "master", "commit": "8acd7722f09ff45ef51335751160e0a8dcc096dc" }, - "gitsigns.nvim": { "branch": "main", "commit": "bc933d24a669608968ff4791b14d2d9554813a65" }, - "indent-blankline.nvim": { "branch": "master", "commit": "3d08501caef2329aba5121b753e903904088f7e6" }, - "lazy.nvim": { "branch": "main", "commit": "d3974346b6cef2116c8e7b08423256a834cb7cbc" }, - "lsp-zero.nvim": { "branch": "v3.x", "commit": "e00dcf8a6019a88139626c1668dccdc295abd324" }, + "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" }, + "conform.nvim": { "branch": "master", "commit": "00f9d91391b04b1935e2f15948bd96cc111e7d3a" }, + "fd": { "branch": "master", "commit": "29936f0fbae1e52984ab582b2b2c98685d6ad638" }, + "gitsigns.nvim": { "branch": "main", "commit": "805610a9393fa231f2c2b49cb521bfa413fadb3d" }, + "indent-blankline.nvim": { "branch": "master", "commit": "171d4d5a1560ccb556e94aa6df7e969068384049" }, + "lazy.nvim": { "branch": "main", "commit": "05240b41548c4245a04d34ee54f789e824129991" }, + "lsp-zero.nvim": { "branch": "v3.x", "commit": "f12d50716e8e59ea9f5cf484eac6968c33a95917" }, "lspkind-nvim": { "branch": "master", "commit": "1735dd5a5054c1fb7feaf8e8658dbab925f4f0cf" }, "lualine-lsp-progress": { "branch": "master", "commit": "56842d097245a08d77912edf5f2a69ba29f275d7" }, "lualine.nvim": { "branch": "master", "commit": "0a5a66803c7407767b799067986b4dc3036e1983" }, - "nvim-autopairs": { "branch": "master", "commit": "14e97371b2aab6ee70054c1070a123dfaa3e217e" }, - "nvim-cmp": { "branch": "main", "commit": "8f3c541407e691af6163e2447f3af1bd6e17f9a3" }, - "nvim-lspconfig": { "branch": "master", "commit": "97762065bf7e1ac617d0a8710eb7ec2d656287a9" }, - "nvim-treesitter": { "branch": "master", "commit": "268611e3ece8463bfb5b09044dcd1b76a28ffbb6" }, - "nvim-treesitter-textobjects": { "branch": "master", "commit": "23b820146956b3b681c19e10d3a8bc0cbd9a1d4c" }, + "nvim-autopairs": { "branch": "master", "commit": "b0b79e42a28f09719a7da9534c3731fa37319d9b" }, + "nvim-cmp": { "branch": "main", "commit": "5260e5e8ecadaf13e6b82cf867a909f54e15fd07" }, + "nvim-lspconfig": { "branch": "master", "commit": "a284b14b3a9c4851f900286cd7eb68e3a8f90b1c" }, + "nvim-treesitter": { "branch": "master", "commit": "00a8cfd562dc805eb4d732fbca35e23640dbf7c8" }, + "nvim-treesitter-textobjects": { "branch": "master", "commit": "84cc9ed772f1fee2f47c1e076f518829583d8347" }, "nvim-ts-autotag": { "branch": "main", "commit": "531f48334c422222aebc888fd36e7d109cb354cd" }, - "nvim-web-devicons": { "branch": "master", "commit": "475fbcfcb6ee7c35aa33a6b6207ebd4032791d87" }, - "oil.nvim": { "branch": "master", "commit": "bcfc0a2e01def5019aa14fac2fc6de20dedb6d3d" }, + "nvim-web-devicons": { "branch": "master", "commit": "e37bb1feee9e7320c76050a55443fa843b4b6f83" }, + "oil.nvim": { "branch": "master", "commit": "06a19f77f1a1da37b675635e6f9c5b5d50bcaacd" }, "playground": { "branch": "master", "commit": "ba48c6a62a280eefb7c85725b0915e021a1a0749" }, "plenary.nvim": { "branch": "master", "commit": "08e301982b9a057110ede7a735dd1b5285eb341f" }, "telescope-ui-select.nvim": { "branch": "master", "commit": "6e51d7da30bd139a6950adf2a47fda6df9fa06d2" }, - "telescope.nvim": { "branch": "master", "commit": "fac83a556e7b710dc31433dec727361ca062dbe9" }, + "telescope.nvim": { "branch": "master", "commit": "0c12735d5aff6a48ffd8111bf144dc2ff44e5975" }, + "trouble.nvim": { "branch": "main", "commit": "b9cf677f20bb2faa2dacfa870b084e568dca9572" }, "which-key.nvim": { "branch": "main", "commit": "4433e5ec9a507e5097571ed55c02ea9658fb268a" } } \ No newline at end of file diff --git a/nvim/lua/config/autocmd.lua b/nvim/lua/config/autocmd.lua index 9e0ef74..35de7d9 100644 --- a/nvim/lua/config/autocmd.lua +++ b/nvim/lua/config/autocmd.lua @@ -1,4 +1,5 @@ local autocmd = vim.api.nvim_create_autocmd +local augroup = vim.api.nvim_create_augroup -- Highlight on yank autocmd('TextYankPost', { diff --git a/nvim/lua/config/cmp.lua b/nvim/lua/config/cmp.lua index d7f0375..d21cfb9 100644 --- a/nvim/lua/config/cmp.lua +++ b/nvim/lua/config/cmp.lua @@ -1,61 +1,73 @@ return function() - local cmp = require('cmp') - local lspkind = require('utils.lspkind') - local icons = require('config.icons') + local cmp = require("cmp") + local utils = require("utils.cmd") + local luasnip = require("luasnip") + local lspkind = require("utils.lspkind") + local icons = require("config.icons") - local selectPrev = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }) - local selectNext = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }) + local selectPrev = utils.selectPrev({ behavior = cmp.SelectBehavior.Insert }) + local selectNext = utils.selectNext({ behavior = cmp.SelectBehavior.Insert }) - local windowstyle = { - border = vim.g.float_border or 'none', - winhighlight = 'Normal:Pmenu,FloatBorder:FloatBorder,CursorLine:PmenuSel,Search:None', - } + local windowstyle = { + border = vim.g.float_border or "none", + winhighlight = "Normal:Pmenu,FloatBorder:FloatBorder,CursorLine:PmenuSel,Search:None", + } - return { - preselect = false, - completion = { - completeopt = "menuone,longest,noinsert,preview" - }, - view = { - entries = { name = 'custom', selection_order = 'near_cursor' }, - }, - window = { - documentation = windowstyle, - completion = windowstyle - }, - mapping = { - [""] = selectPrev, - [""] = selectPrev, - [""] = selectNext, - [""] = selectNext, - [""] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm({ select = true }), - }, - snippet = { - expand = function(args) - require('luasnip').lsp_expand(args.body) - end, - }, - formatting = { - fields = { "abbr", "kind", "menu" }, - format = lspkind.format({ - mode = 'symbol', - preset = 'codicons', - symbol_map = icons.symbols, - maxwidth = 40, - ellipsis_char = "...", - }), - }, - sources = { - { name = 'nvim_lsp' }, - { name = 'buffer' }, - { name = 'path' }, - { name = 'luasnip' } - }, - experimental = { - ghost_text = { - hl_group = "NonText", - }, - }, - } + return { + preselect = false, + completion = { + completeopt = "menuone,longest,noinsert,preview", + }, + view = { + entries = { name = "custom", selection_order = "near_cursor" }, + }, + window = { + documentation = windowstyle, + completion = windowstyle, + }, + mapping = { + [""] = selectPrev, + [""] = selectPrev, + [""] = selectNext, + [""] = selectNext, + [""] = cmp.mapping.abort(), + [""] = utils.confirm({ select = true }), + }, + snippet = { + expand = function(args) + require("luasnip").lsp_expand(args.body) + end, + }, + formatting = { + fields = { "abbr", "kind", "menu" }, + format = lspkind.format({ + mode = "symbol", + preset = "codicons", + symbol_map = icons.symbols, + maxwidth = 40, + ellipsis_char = "...", + }), + }, + sources = { + { name = "nvim_lsp" }, + { name = "luasnip" }, + { name = "buffer" }, + { name = "path" }, + { + name = "spell", + option = { + keep_all_entries = false, + enable_in_context = function() + return true + end, + preselect_correct_word = true, + }, + }, + }, + experimental = { + ghost_text = { + hl_group = "NonText", + }, + }, + } end diff --git a/nvim/lua/config/icons.lua b/nvim/lua/config/icons.lua index 2083cf5..08231fb 100644 --- a/nvim/lua/config/icons.lua +++ b/nvim/lua/config/icons.lua @@ -51,6 +51,14 @@ return { staged = "✓", conflict = "", }, + todo = { + default = '', + warn = '', + perf = '', + bug = '', + hack = '', + test = '󰄉', + }, diagnostics = { error = '', warn = '', @@ -66,7 +74,7 @@ return { unknown = '', }, symbols = { - Text = "", + Text = "TXT", Method = "", Function = "󰊕", Constructor = "", diff --git a/nvim/lua/config/keymaps.lua b/nvim/lua/config/keymaps.lua index b0de05d..3d14d54 100644 --- a/nvim/lua/config/keymaps.lua +++ b/nvim/lua/config/keymaps.lua @@ -46,7 +46,7 @@ return { { { "n", "v" }, "p", [["+p]], { desc = "Paste from system clipboard register" } }, -- File operations - -- { "n", "Fe", "Ex", { silent = true, desc = "Open Netrw" } }, + { "n", "gf", "e ", { silent = true, desc = "Open (or create) file under cursor" } }, { "n", "Fc", ":CreateFile ", { silent = true, desc = "Create new file" } }, { "n", "Fx", "!chmod +x %", { silent = true, desc = "Set execute flag on current file" } }, @@ -71,11 +71,15 @@ return { { "n", "rw", [[:%s/\<\>//gI]], { desc = "search+replace word under cursor" } }, - { { "x" }, "fhx", [[:s/\(\x\{2\}\)/0x\1, /g]], { desc = "Format hex" } }, - { { "x" }, "fha", [[:s/0x\(\x\{1\}\X\)/0x0\1/g]], { desc = "Format each hex number to be 2 characters long" } }, - { { "x" }, "fhn", [[:s/\(\(0x\x\{1,2\}, \)\{8\}\)/\1\r/g]], { desc = "Format hex with 8 bytes per row" } }, + -- Hex formatting + { "x", "fhx", [[:s/\(\x\{2\}\)/0x\1, /g]], { desc = "Format hex" } }, + { "x", "fha", [[:s/0x\(\x\{1\}\X\)/0x0\1/g]], { desc = "Format each hex number to be 2 characters long" } }, + { "x", "fhn", [[:s/\(\(0x\x\{1,2\}, \)\{8\}\)/\1\r/g]], { desc = "Format hex with 8 bytes per row" } }, -- {{"x"}, "hb", [[:'<,'>s/\(\x\{2\}\)/0x\1, /g]], { desc = "Format a hex string to byte array"}}, + -- Case formatting + { "x", "fcsc", [[:s/\%V\([a-z]\+\)_\?/\u\1/g]], { desc = "Convert text from snake_case to CamelCase" } }, + -- diagnostics { "n", "do", vim.diagnostic.open_float, { desc = "Open diagnostics" } }, { "n", "dn", vim.diagnostic.get_next, { desc = "Goto next" } }, @@ -85,14 +89,17 @@ return { { "n", "m", ":terminal tmuxsi", { desc = "Open tmux manager" } }, - { "n", "Q", "" }, + { "n", "Q", "" }, -- NOPE }, whichkey = { - mode = { "n", "v" }, + mode = { "n", "v", "x" }, ["r"] = { name = "+Rename" }, ["b"] = { name = "+Buffers" }, ["f"] = { name = "+Format" }, + ["fc"] = { name = "+Case" }, + ["fcs"] = { name = "+snake_case" }, + ["fh"] = { name = "+Hex" }, ["F"] = { name = "+Files" }, ["d"] = { name = "+Diagnostics" }, ["s"] = { name = "+Search" }, diff --git a/nvim/lua/config/lsp.lua b/nvim/lua/config/lsp.lua index 1f7ded7..17155a5 100644 --- a/nvim/lua/config/lsp.lua +++ b/nvim/lua/config/lsp.lua @@ -1,6 +1,7 @@ vim.g.lsp_zero_ui_float_border = vim.g.float_border or 'none' return { + document_highlight = true, servers = { -- PHP phpactor = { diff --git a/nvim/lua/config/options.lua b/nvim/lua/config/options.lua index 339f3b6..c06c4fb 100644 --- a/nvim/lua/config/options.lua +++ b/nvim/lua/config/options.lua @@ -35,7 +35,7 @@ set.wrap = false -- Disable line wrap set.pumheight = 20 set.laststatus = 3 set.splitkeep = "screen" -set.scrolloff = 20 +set.scrolloff = 10 -- Configure how new splits should be opened set.splitright = true @@ -67,6 +67,10 @@ set.incsearch = true set.ignorecase = true set.smartcase = true +-- Spell stuff, because i cant english +set.spell = true +set.spelllang = { 'en_us' } + -- line numbers set.number = true set.relativenumber = true diff --git a/nvim/lua/plugins/core.lua b/nvim/lua/plugins/core.lua index bcc94ec..12de16a 100644 --- a/nvim/lua/plugins/core.lua +++ b/nvim/lua/plugins/core.lua @@ -166,5 +166,18 @@ return { keys = keys.oil or {}, opts = require('config.oil'), dependencies = { "nvim-tree/nvim-web-devicons" }, - } + }, + { + "folke/trouble.nvim", + dependencies = { "nvim-tree/nvim-web-devicons" }, + opts = {}, + }, + { + 'L3MON4D3/LuaSnip', + opts = {}, + config = function (_, opts) + require("luasnip.loaders.from_lua").load({paths = "~/.config/nvim/snippets"}) + require("luasnip").setup(opts) + end + }, } diff --git a/nvim/lua/plugins/lsp.lua b/nvim/lua/plugins/lsp.lua index 42cf47b..6d628b3 100644 --- a/nvim/lua/plugins/lsp.lua +++ b/nvim/lua/plugins/lsp.lua @@ -1,64 +1,66 @@ return { - 'VonHeikemen/lsp-zero.nvim', - branch = 'v3.x', - dependencies = { - 'neovim/nvim-lspconfig', - 'hrsh7th/cmp-nvim-lsp', + "VonHeikemen/lsp-zero.nvim", + branch = "v3.x", + dependencies = { + "neovim/nvim-lspconfig", + "hrsh7th/cmp-nvim-lsp", - -- completion - { - 'hrsh7th/nvim-cmp', - version = false, - event = "InsertEnter", - dependencies = { - 'hrsh7th/cmp-buffer', - 'hrsh7th/cmp-path', - 'hrsh7th/cmp-nvim-lsp', - 'L3MON4D3/LuaSnip', - 'onsails/lspkind-nvim', - }, - opts = require('config.cmp'), - config = function(_, opts) - local cmp = require('cmp') - cmp.setup(opts) + -- completion + { + "hrsh7th/nvim-cmp", + version = false, + event = "InsertEnter", + dependencies = { + "hrsh7th/cmp-buffer", -- autocomplete from buffer + "hrsh7th/cmp-path", -- autocomplete from filesystem + "hrsh7th/cmp-nvim-lsp", -- autocomplete from lsp + "saadparwaiz1/cmp_luasnip", -- autocomplete from snippet engine + "L3MON4D3/LuaSnip", -- snippet engine + "onsails/lspkind-nvim", -- icons for kind + }, + opts = require("config.cmp"), + config = function(_, opts) + local cmp = require("cmp") + cmp.setup(opts) - -- insert () on function completion using autopairs - cmp.event:on( - 'confirm_done', - require('nvim-autopairs.completion.cmp').on_confirm_done() - ) - end - }, - }, - opts = require('config.lsp'), - config = function(_, opts) - local lsp_zero = require('lsp-zero') - local lsp_config = require('lspconfig') - local augroup = vim.api.nvim_create_augroup("Lsp", {}) - local map = require('utils.keys') + -- insert () on function completion using autopairs + cmp.event:on("confirm_done", require("nvim-autopairs.completion.cmp").on_confirm_done()) + end, + }, + }, + opts = require("config.lsp"), + config = function(_, opts) + local lsp_zero = require("lsp-zero") + local lsp_config = require("lspconfig") + local augroup = vim.api.nvim_create_augroup("Lsp", {}) + local map = require("utils.keys") - lsp_zero.on_attach(function(_, bufnr) - for _, v in ipairs(require('config.keymaps').lsp or {}) do - map(v, { buffer = bufnr }) - end - end) + lsp_zero.on_attach(function(_, bufnr) + for _, v in ipairs(require("config.keymaps").lsp or {}) do + map(v, { buffer = bufnr }) + end + end) - for name, server_opt in pairs(opts.servers) do - local on_attach = function(_, bufnr) - vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) + for name, server_opt in pairs(opts.servers) do + local on_attach = function(client, bufnr) + vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) - if server_opt.on_save then - vim.api.nvim_create_autocmd("BufWritePre", { - group = augroup, - buffer = bufnr, - callback = server_opt.on_save, - }) - end - end + if server_opt.on_save then + vim.api.nvim_create_autocmd("BufWritePre", { + group = augroup, + buffer = bufnr, + callback = server_opt.on_save, + }) + end - server_opt = vim.tbl_deep_extend("force", {on_attach = on_attach}, server_opt or {}) + if (opts.document_highlight or false) and client.supports_method('textDocument/documentHighlight')then + require("utils.lsp").document_highlight(bufnr) + end + end - lsp_config[name].setup(server_opt) - end - end, + server_opt = vim.tbl_deep_extend("force", { on_attach = on_attach }, server_opt or {}) + + lsp_config[name].setup(server_opt) + end + end, } diff --git a/nvim/lua/setup.lua b/nvim/lua/setup.lua index 4223cef..b0e40a2 100644 --- a/nvim/lua/setup.lua +++ b/nvim/lua/setup.lua @@ -3,7 +3,6 @@ local M = {} M.keymaps = function(opts) opts = type(opts) == 'string' and require(opts) or opts vim.g.mapleader = opts.leader - for _, v in ipairs(opts.global) do vim.keymap.set(unpack(v)) end diff --git a/nvim/lua/utils/cmd.lua b/nvim/lua/utils/cmd.lua new file mode 100644 index 0000000..0e5cd0a --- /dev/null +++ b/nvim/lua/utils/cmd.lua @@ -0,0 +1,45 @@ + +local luasnip = require('luasnip') +local cmp = require('cmp') + +local M = {} + +function M.selectNext(opts) + return cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item(opts or {}) + elseif luasnip.locally_jumpable(1) then + luasnip.jump(1) + else + fallback() + end + end, {"i", "s"}) +end + +function M.selectPrev(opts) + return cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item(opts or {}) + elseif luasnip.locally_jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, {"i", "s"}) +end + +function M.confirm(opts) + return cmp.mapping(function(fallback) + if cmp.visible() then + if luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + else + cmp.confirm(opts or {}) + end + else + fallback() + end + end) +end + +return M diff --git a/nvim/lua/utils/lsp.lua b/nvim/lua/utils/lsp.lua new file mode 100644 index 0000000..0d91a39 --- /dev/null +++ b/nvim/lua/utils/lsp.lua @@ -0,0 +1,22 @@ + +local M = {} + +function M.document_highlight(bufnr) + local group = vim.api.nvim_create_augroup('lsp_document_highlight', { clear = false }) + vim.api.nvim_clear_autocmds({ + buffer = bufnr, + group = group, + }) + vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, { + group = group, + buffer = bufnr, + callback = vim.lsp.buf.document_highlight, + }) + vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, { + group = group, + buffer = bufnr, + callback = vim.lsp.buf.clear_references, + }) +end + +return M diff --git a/nvim/snippets/all.lua b/nvim/snippets/all.lua new file mode 100644 index 0000000..a7f555b --- /dev/null +++ b/nvim/snippets/all.lua @@ -0,0 +1,9 @@ + + +return { + s("ternary", { + -- equivalent to "${1:cond} ? ${2:then} : ${3:else}" + i(1, "cond"), t(" ? "), i(2, "then"), t(" : "), i(3, "else") + }) +} + diff --git a/nvim/snippets/php.lua b/nvim/snippets/php.lua new file mode 100644 index 0000000..f3c071e --- /dev/null +++ b/nvim/snippets/php.lua @@ -0,0 +1,163 @@ +local q = require('vim.treesitter.query') + +local ls = require('luasnip') +local s = ls.snippet +-- local sn = ls.snippet_node +-- local isn = ls.indent_snippet_node +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node +local c = ls.choice_node +-- local d = ls.dynamic_node +-- local r = ls.restore_node +local events = require('luasnip.util.events') +local ai = require('luasnip.nodes.absolute_indexer') +local fmt = require('luasnip.extras.fmt').fmt +local m = require('luasnip.extras').m +local lambda = require('luasnip.extras').l +local rep = require('luasnip.extras').rep +local postfix = require('luasnip.extras.postfix').postfix + +local visibility_modifiers = { + t('public'), + t('private'), + t('protected'), +} + +local query_constructor_args = vim.treesitter.query.parse( + 'php', + [[ + (method_declaration + name: (name) @method_name (#eq? @method_name "__construct") + parameters: + (formal_parameters + (simple_parameter name: (variable_name (name) @arg_name )) + )) + ]] +) + +return { + -- Class + s( + { + trig = 'cl', + }, + fmt( + [[ + /** + * {} {} + * + */ + + {} + + class {} {{ + + {} + + {} function __construct({}) {{ + {} + + }} + + }} + ]], + { + i(1, 'Class description'), + i(2, 'Classname'), + f(function(args, snip, user_arg) + -- TODO: generate namespace using parent directories + return 'namespace ' .. args[1][1] .. ';' + end, { 2 }), + rep(2), + f(function(args, snip, user_arg) + local parser = vim.treesitter.get_parser(0, 'php') + local syntax_tree = parser:parse() + local root = syntax_tree[1]:root() + + local text = '' + local fields = { '' } + + for _, match, _ in query_constructor_args:iter_matches(root, 0) do + for id, node in pairs(match) do + local name = query_constructor_args.captures[id] + if name == 'arg_name' then + text = q.get_node_text(node, 0) + fields[#fields + 1] = 'private $_' .. text .. ';' + end + end + end + return fields + end, 4), + c(3, visibility_modifiers), + i(4, '/* Constructor parameters */'), + f(function() + local fields = { '' } + local parser = vim.treesitter.get_parser(0, 'php') + local syntax_tree = parser:parse() + local root = syntax_tree[1]:root() + + local text = '' + local fields = { '' } + + for _, match, _ in query_constructor_args:iter_matches(root, 0) do + for id, node in pairs(match) do + local name = query_constructor_args.captures[id] + if name == 'arg_name' then + text = q.get_node_text(node, 0) + fields[#fields + 1] = '$this->_' .. text .. '= $' .. text .. ';' + end + end + end + return fields + end, { 4 }), + } + ) + ), + s( + { + trig = 'am', + }, + fmt( + [[ + Cacca + ]], + {} + ) + ), + s( + { + trig = 'af', + }, + fmt( + [[ + Cacca + ]], + {} + ) + ), + -- Getter Setter + s( + { + trig = 'getset' + }, + fmt([[ + public function get{}() : {} {{ + return {}; + }} + public function set{}(${} {}) {{ + $this->{} = ${}; + return $this; + }} + ]], { + i(1, 'Name'), + i(2, 'Type'), + rep(1), -- getter return type + rep(1), -- getter return value + rep(1), -- setter arg name + rep(2), -- setter arg type + rep(2), -- setter property + rep(1), -- setter value + }) + ) +}, {}