From 9bd219f05338e6ad43604f06eb39142a354d3ce8 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 9 Dec 2025 10:35:51 +0000 Subject: [PATCH] Snippets --- lua/keymap.lua | 2 - lua/plugins/ai-completion.lua | 2 +- lua/projects.lua | 49 +++++++++++++++++++++ lua/snippets/php.lua | 83 ++++++++++++++++++----------------- lua/snippets/snip_utils.lua | 15 ++++--- 5 files changed, 103 insertions(+), 48 deletions(-) diff --git a/lua/keymap.lua b/lua/keymap.lua index 6cd28aa..d4ca057 100644 --- a/lua/keymap.lua +++ b/lua/keymap.lua @@ -46,14 +46,12 @@ vim.keymap.set('n', '', '', { desc = 'Move focus to the upper win vim.keymap.set({ 'i' }, '', function() local ls = require 'luasnip' - print('jj') if ls.choice_active() then ls.change_choice(1) end end, { silent = true }) vim.keymap.set({ 'i' }, '', function() local ls = require 'luasnip' - print('kk') if ls.choice_active() then ls.change_choice(-1) end diff --git a/lua/plugins/ai-completion.lua b/lua/plugins/ai-completion.lua index ab621a9..5d24be3 100644 --- a/lua/plugins/ai-completion.lua +++ b/lua/plugins/ai-completion.lua @@ -31,7 +31,7 @@ return { require('minuet').setup { virtualtext = { auto_trigger_ft = { '*' }, - auto_trigger_ignore_ft = { 'help', 'TelescopePrompt', 'codecompanion' }, + auto_trigger_ignore_ft = { 'help', 'TelescopePrompt', 'codecompanion', 'snacks_input' }, keymap = { accept = '', accept_line = '', diff --git a/lua/projects.lua b/lua/projects.lua index 3606f22..517b9af 100644 --- a/lua/projects.lua +++ b/lua/projects.lua @@ -18,6 +18,26 @@ local function command_with_dir(dir, cmd) return '!' .. cmd end +local function make_laravel_file(dir, cmd) + local cwd = vim.fn.getcwd() + if dir then + vim.fn.chdir(dir) + end + vim.ui.input({ prompt = 'Make: ' .. cmd }, function(input) + if input == nil then + vim.fn.chdir(cwd) + return + end + + local output = vim.system({ 'vendor/bin/sail', 'artisan', 'make:' .. cmd, input }):wait().stdout + local new_file = output:match '%[([%w%./]+)%]' + if new_file ~= nil then + vim.cmd('edit ' .. new_file) + end + vim.fn.chdir(cwd) + end) +end + local function get_scope_from_file(filename) local ext = filename:match '%.(%w+)$' local base_name = filename:match '^(%w+)%.?%w*$' @@ -130,6 +150,33 @@ local function laravel_keymaps(dir) laravel_bookmarks_with_dir(dir) end +local function laravel_makes(dir) + for key, name in pairs { + c = 'controller', + d = 'data', + e = 'event', + f = 'factory', + j = 'job', + l = 'listener', + ma = 'mail', + mi = 'migration', + mo = 'model', + mw = 'middleware', + n = 'notification', + o = 'observer', + pi = 'model --pivot', + po = 'policy', + pr = 'provider', + t = 'test --pest', + v = 'view', + x = 'exception', + } do + map('m' .. key, function() + make_laravel_file(dir, name) + end, { desc = 'Make and navigate to relevant ' .. name }) + end +end + -- Define per-project configuration here. -- Keys are folder names (last segment of your cwd). local PROJECTS = { @@ -170,6 +217,7 @@ local PROJECTS = { map('s ', '!cd server && ', { desc = 'Run command in server directory' }, 'c') map('c ', '!cd client && ', { desc = 'Run command in client directory' }, 'c') laravel_keymaps 'server' + laravel_makes 'server' map('yrn ', '!cd client && yarn ', { desc = 'Run yarn script' }, 'c') map('pt', ':!cd server && php artisan typescript:transform --format', { desc = 'Compile typescript' }) require('conform').formatters.pint = { @@ -191,6 +239,7 @@ local PROJECTS = { helpers.open_term { cmd = 'lazysql pgsql://homestead:password@localhost:5432/homestead' } end, { desc = 'Open database manager' }) laravel_keymaps() + laravel_makes() map('yrn ', '!cd frontend && yarn ', { desc = 'Run yarn script' }, 'c') map('pm', ':vendor/bin/sail composer migrate') end, diff --git a/lua/snippets/php.lua b/lua/snippets/php.lua index 0c680d4..c2bad7a 100644 --- a/lua/snippets/php.lua +++ b/lua/snippets/php.lua @@ -2,6 +2,7 @@ local ls = require 'luasnip' local s = ls.snippet local sn = ls.snippet_node local fn = ls.function_node +local ms = ls.multi_snippet local t = ls.text_node local c = ls.choice_node local i = ls.insert_node @@ -19,7 +20,7 @@ local atr = utils.atr local ctr = utils.ctr local bs = utils.bs -local function psr_namespace(_, snip) +local function psr_namespace() local path = vim.fn.expand '%' -- Get the directory of the path local dir = vim.fs.dirname(path) @@ -169,26 +170,24 @@ return { s(etr('return ', 'Add semicolon after return'), fmta('return #~;', { i(0) })), s(atr(' use ', 'Add use to function'), fmta(' use (#~)', { i(0) })), s(etr('rt ', 'return alias'), fmta('return #~;', { i(0) })), - bs( - atr('fn ', 'Shorthand function block'), - c(0, { + bs(etr('fn ', 'Shorthand function block'), { + c(1, { sn(nil, fmta('fn (#~) => #~', { i(1), i(2) })), sn( nil, fmta( [[ - function (#~) { - #~ - } - ]], + function (#~) { + #~ + } + ]], { i(1), i(2) } ) ), - }) - ), - bs( - atr('fun ', 'Shorthand function block'), - c(0, { + }), + }), + bs(etr('fun ', 'Shorthand function block'), { + c(1, { sn( nil, fmta( @@ -211,11 +210,11 @@ return { { i(1), i(2), i(3) } ) ), - }) - ), + }), + }), s( etr('con', 'Constructor function block'), - c(0, { + c(1, { sn( nil, fmta( @@ -250,41 +249,45 @@ return { bs(atr('a%$', 'array type parameter'), fmta('array $#~', { i(0, 'var') })), s(atr('$ ', 'Expand $this->'), fmta('$this->#~', { i(0) })), bs( - atr('am ', 'array_map function'), - c(0, { - sn(nil, fmta('array_map(fn (#~) => #~, #~)', { i(2), i(3), i(1) })), - sn( - nil, - fmta( - [[ + etr('am ', 'array_map function'), + { + c(1, { + sn(nil, fmta('array_map(fn (#~) => #~, #~)', { i(2), i(3), i(1) })), + sn( + nil, + fmta( + [[ array_map(function (#~) { #~ }, #~) ]], - { i(2), i(0), i(1) } - ) - ), - }) + { i(2), i(0), i(1) } + ) + ), + }), + } ), - bs(atr('array_map', 'array_map function'), fmta('am', {})), + bs(etr('array_map', 'array_map function'), fmta('am', {})), bs( - atr('af ', 'array_filter function'), - c(0, { - sn(nil, fmta('array_filter(#~, fn (#~) => #~)', { i(1), i(2), i(3) })), - sn( - nil, - fmta( - [[ + etr('af ', 'array_filter function'), + { + c(1, { + sn(nil, fmta('array_filter(#~, fn (#~) => #~)', { i(1), i(2), i(3) })), + sn( + nil, + fmta( + [[ array_filter(#~, function (#~) { #~ }) ]], - { i(1), i(2), i(0) } - ) - ), - }) + { i(1), i(2), i(0) } + ) + ), + }), + } ), - bs(atr('([^%w])array_filter', 'array_filter function'), fmta('af', {})), + bs(etr('array_filter', 'array_filter function'), fmta('af', {})), s( etr('php', 'php class'), fmta( diff --git a/lua/snippets/snip_utils.lua b/lua/snippets/snip_utils.lua index abefef5..0965a48 100644 --- a/lua/snippets/snip_utils.lua +++ b/lua/snippets/snip_utils.lua @@ -2,6 +2,7 @@ local ls = require 'luasnip' local s = ls.snippet local sn = ls.snippet_node local fn = ls.function_node +local ms = ls.multi_snippet local t = ls.text_node local c = ls.choice_node local i = ls.insert_node @@ -114,15 +115,19 @@ end ---@param options? any ---@return table utils.bs = function(trigger, nodes, options) + local btrigger if type(trigger) == 'string' then - trigger = utils.atr('([^%w])' .. trigger) + btrigger = utils.atr('([^%w_-])' .. trigger) else - trigger.trig = '([^%w])' .. trigger.trig + btrigger = vim.tbl_extend('keep', utils.atr('([^%w_-])' .. trigger.trig), trigger) end - return s( - trigger, + return ms( + { + trigger, + btrigger, + }, vim.list_extend({ fn(function(args, snip) - return snip.captures[1] + return snip.captures[1] or '' end) }, nodes), options )