diff --git a/lua/keymap.lua b/lua/keymap.lua index 493a299..6cd28aa 100644 --- a/lua/keymap.lua +++ b/lua/keymap.lua @@ -26,6 +26,15 @@ vim.keymap.set('v', 'p', '"zdP', { desc = 'Paste over selection without yanking -- or just use to exit terminal mode vim.keymap.set('t', '', '', { desc = 'Exit terminal mode' }) +vim.keymap.set('n', 'c', function() + local node = require('nvim-treesitter.ts_utils').get_node_at_cursor() + if node then + print(node:type()) + else + print 'No node found at cursor' + end +end, { desc = 'Log node' }) + -- Keybinds to make split navigation easier. -- Use CTRL+ to switch between windows -- @@ -37,12 +46,14 @@ 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/autopairs.lua b/lua/plugins/autopairs.lua index d181937..00db21b 100644 --- a/lua/plugins/autopairs.lua +++ b/lua/plugins/autopairs.lua @@ -2,7 +2,7 @@ -- https://github.com/windwp/nvim-autopairs return { - "windwp/nvim-autopairs", - event = "InsertEnter", - opts = {}, + 'windwp/nvim-autopairs', + event = 'InsertEnter', + opts = {}, } diff --git a/lua/plugins/blink.lua b/lua/plugins/blink.lua index 0bc256f..4c6db38 100644 --- a/lua/plugins/blink.lua +++ b/lua/plugins/blink.lua @@ -33,6 +33,7 @@ return { -- -- See :h blink-cmp-config-keymap for defining your own keymap preset = 'enter', + [''] = false, -- For more advanced Luasnip keymaps (e.g. selecting choice nodes, expansion) see: -- https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file#keymaps diff --git a/lua/projects.lua b/lua/projects.lua index 12930fd..3606f22 100644 --- a/lua/projects.lua +++ b/lua/projects.lua @@ -124,6 +124,9 @@ local function laravel_keymaps(dir) ['r'] = { 'Request.php', 'request' }, ['t'] = { 'Test.php', 'test file' }, } + if dir == nil then + dir = '' + end laravel_bookmarks_with_dir(dir) end diff --git a/lua/snippets/php.lua b/lua/snippets/php.lua index f86d98a..459edc8 100644 --- a/lua/snippets/php.lua +++ b/lua/snippets/php.lua @@ -1,6 +1,7 @@ local ls = require 'luasnip' local s = ls.snippet local sn = ls.snippet_node +local fn = ls.function_node local t = ls.text_node local c = ls.choice_node local i = ls.insert_node @@ -26,30 +27,42 @@ local function file_begin(line_to_cursor, matched_trigger) return line_number == 1 and line_begin(line_to_cursor, matched_trigger) end -local function tr(trigger, description) +local function in_string() + local node_type = vim.treesitter.get_node():type() + return node_type == 'string_content' or node_type == 'string' +end + +local function in_comment() + local node_type = vim.treesitter.get_node():type() + return node_type == 'comment' +end + +local function not_in_string_or_comment() + return (not in_string()) and (not in_comment()) +end + +local function tr(trigger, description, condition) + condition = condition or empty_line return { trig = trigger, desc = description, snippetType = 'autosnippet', - condition = empty_line, + condition = condition, } end -local function fn_choice(index) - return c(index, { - sn(nil, fmta('fn(#~) => #~', { i(1), i(0) })), - sn( - nil, - fmta( - [[ - function (#~) { - #~ - } - ]], - { i(1), i(0) } - ) - ), - }) +local function ctr(trigger, description) + return tr(trigger, description, in_comment) +end +local function atr(trigger, description) + return { + trig = trigger, + desc = description, + snippetType = 'autosnippet', + regTrig = true, + wordTrig = false, + condition = not_in_string_or_comment, + } end local function psr_namespace(_, snip) @@ -105,6 +118,15 @@ local function snippet_aliases(triggers, description, snippet) return snips end +local function fmtc(snipp, nodes) + return fmta( + '#~' .. snipp, + vim.list_extend({ fn(function(args, snip) + return snip.captures[1] + end) }, nodes) + ) +end + local function flatten(tbl) local result = {} local index = 1 @@ -120,7 +142,7 @@ local function flatten(tbl) end end - return result; + return result end return flatten { @@ -128,7 +150,9 @@ return flatten { -- DEBUGGING -- --------------- s(tr('du ', 'Dump a variable to the dump server'), fmta('dump(#~);', { i(0) })), - s(tr('ray', 'ray'), fmta('ray(#~);', { i(0) })), + s(atr('([^%w])du ', 'Dump a variable to the dump server'), fmtc('dump(#~)', { i(0) })), + s(tr('r ', 'ray'), fmta('ray(#~);', { i(0) })), + s(atr('([^%w])r ', 'ray'), fmtc('ray(#~)', { i(0) })), s(tr('dt ', 'Dump PHPStan type definition'), fmta('\\PhpStan\\dumpType(#~);', { i(0) })), s( tr('ql ', 'Log all queries'), @@ -145,24 +169,30 @@ return flatten { -------------- -- COMMENTS -- -------------- - s( - tr('/**', 'Docblock comment'), - fmta( - [[ - /** - * #~ - */ - ]], - { i(0) } - ) - ), + s(tr('/**', 'Docblock comment'), { + c(1, { + sn( + nil, + fmta( + [[ + /** + * #~ + */ + ]], + { i(1) } + ) + ), + sn(nil, fmta('/** #~ */', { i(1) })), + }), + }), s(tr('@v', '@var docblock'), fmta('/** @var #~ $#~ */', { i(1), i(0) })), - s(tr('* @pr', 'Class property docblock'), fmta('* @property #~ $#~', { i(1), i(0) })), - s(tr('* @pb', 'Class boolean property docblock'), fmta('* @property bool $#~', { i(0) })), - s(tr('* @pi', 'Class int property docblock'), fmta('* @property int $#~', { i(0) })), - s(tr('* @ps', 'Class string property docblock'), fmta('* @property string $#~', { i(0) })), - s(tr('* @pc', 'Class collection property docblock'), fmta('* @property \\Illuminate\\Database\\Eloquent\\Collection<#~> $#~', { i(1), i(0) })), - s(tr('* @pd', 'Class date property docblock'), fmta('* @property \\Illuminate\\Support\\Carbon $#~', { i(0) })), + s(ctr('@v', '@var docblock'), fmta('@var #~ $#~', { i(1), i(0) })), + s(ctr('* @pr', 'Class property docblock'), fmta('* @property #~ $#~', { i(1), i(0) })), + s(ctr('* @pb', 'Class boolean property docblock'), fmta('* @property bool $#~', { i(0) })), + s(ctr('* @pi', 'Class int property docblock'), fmta('* @property int $#~', { i(0) })), + s(ctr('* @ps', 'Class string property docblock'), fmta('* @property string $#~', { i(0) })), + s(ctr('* @pc', 'Class collection property docblock'), fmta('* @property \\Illuminate\\Database\\Eloquent\\Collection<#~> $#~', { i(1), i(0) })), + s(ctr('* @pd', 'Class date property docblock'), fmta('* @property \\Illuminate\\Support\\Carbon $#~', { i(0) })), s( tr('@pm', 'Model magic properties docblock'), fmta( @@ -184,7 +214,7 @@ return flatten { -- SYNTAX -- ------------ s( - tr('if ', 'foreach block'), + tr('if ', 'if block'), fmta( [[ if (#~) { @@ -205,21 +235,137 @@ return flatten { { i(1), i(2), i(0) } ) ), + s(tr('foreach', 'foreach block'), fmta('fe', {})), + s( + tr('for ', 'for block'), + fmta( + [[ + for (#~; #~; #~) { + #~ + } + ]], + { i(1, '$i'), i(2), i(3, '$i++'), i(0) } + ) + ), s(tr('return ', 'Add semicolon after return'), fmta('return #~;', { i(0) })), - s(tr('fn ', 'Shorthand function block'), fmta('fn(#~) => #~', { i(1), i(0) })), - s(tr('$ ', 'Expand $this->'), fmta('$this->#~', { i(0) })), - snippet_aliases({ 'am ', 'array_map' }, 'array_map function', function() - return fmta('array_map(#~, #~);', { - fn_choice(0), - i(1), + s(atr(' use ', 'Add use to function'), fmta(' use (#~)', { i(0) })), + s(tr('rt ', 'return alias'), fmta('return #~;', { i(0) })), + s( + atr('([^%w])fn ', 'Shorthand function block'), + c(0, { + sn(nil, fmtc('fn (#~) => #~', { i(1), i(2) })), + sn( + nil, + fmtc( + [[ + function (#~) { + #~ + } + ]], + { i(1), i(2) } + ) + ), }) - end), - snippet_aliases({ 'af ', 'array_filter' }, 'array_filter function', function() - return fmta('array_filter(#~, #~);', { - i(1), - fn_choice(0), + ), + s( + atr('([^%w])fun ', 'Shorthand function block'), + c(0, { + sn( + nil, + fmtc( + [[ + function (#~) { + #~ + } + ]], + { i(1), i(2) } + ) + ), + sn( + nil, + fmtc( + [[ + function (#~) use (#~) { + #~ + } + ]], + { i(1), i(2), i(3) } + ) + ), }) - end), + ), + s( + tr('con', 'Constructor function block'), + c(0, { + sn( + nil, + fmta( + [[ + public function __construct(#~) + { + #~ + } + ]], + { i(1), i(2) } + ) + ), + sn( + nil, + fmta( + [[ + public function __construct( + #~ + ) { + #~ + } + ]], + { i(1), i(2) } + ) + ), + }) + ), + s(atr('([^%w])function', 'Shorthand function block'), fmtc('fun', {})), + s(atr('([^%w])s%$', 'string type parameter'), fmtc('string $#~', { i(0, 'var') })), + s(atr('([^%w])i%$', 'int type parameter'), fmtc('int $#~', { i(0, 'var') })), + s(atr('([^%w])b%$', 'bool type parameter'), fmtc('bool $#~', { i(0, 'var') })), + s(atr('([^%w])a%$', 'array type parameter'), fmtc('array $#~', { i(0, 'var') })), + s(atr('$ ', 'Expand $this->'), fmta('$this->#~', { i(0) })), + s( + atr('([^%w])am ', 'array_map function'), + c(0, { + sn(nil, fmtc('array_map(fn (#~) => #~, #~)', { i(2), i(3), i(1) })), + sn( + nil, + fmtc( + [[ + array_map(function (#~) { + #~ + }, #~) + ]], + { i(2), i(0), i(1) } + ) + ), + }) + ), + s(atr('([^%w])array_map', 'array_map function'), fmtc('am', {})), + s( + atr('([^%w])af ', 'array_filter function'), + c(0, { + sn(nil, fmtc('array_filter(#~, fn (#~) => #~)', { i(1), i(2), i(3) })), + sn( + nil, + fmtc( + [[ + array_filter(#~, function (#~) { + #~ + }) + ]], + { i(1), i(2), i(0) } + ) + ), + }) + ), + s(atr('([^%w])array_filter', 'array_filter function'), fmtc('af', {})), s( tr('php', 'php class'), fmta( @@ -367,4 +513,92 @@ return flatten { { rep(1), i(2), i(1), i(0) } ) ), + s( + atr('->wr', 'Eloquent where method'), + c(0, { + sn(nil, fmta("->where('#~', #~)", { i(1), i(0) })), + sn(nil, fmta('->where(fn ($query) => #~)', { i(0) })), + sn( + nil, + fmta( + [[ + ->where(function ($query) { + #~ + }) + ]], + { i(0) } + ) + ), + sn( + nil, + fmta( + [[ + ->where(function ($query) use (#~) { + #~ + }) + ]], + { i(1), i(0) } + ) + ), + }) + ), + s(atr('->wi', 'Eloquent where in method'), fmta("->whereIn('#~', #~)", { i(1), i(0) })), + s(atr('->wn', 'Eloquent where not in method'), fmta("->whereNotIn('#~', #~)", { i(1), i(0) })), + s( + atr('->wh', 'Eloquent where has method'), + c(0, { + sn(nil, fmta("->whereHas('#~')", { i(1) })), + sn(nil, fmta("->whereHas('#~', fn ($query) => #~)", { i(1), i(0) })), + sn( + nil, + fmta( + [[ + ->whereHas('#~', function ($query) { + #~ + }) + ]], + { i(1), i(0) } + ) + ), + sn( + nil, + fmta( + [[ + ->whereHas('#~', function ($query) use (#~) { + #~ + }) + ]], + { i(1), i(2), i(0) } + ) + ), + }) + ), + s( + atr('->we', 'Eloquent where exists method'), + c(0, { + sn(nil, fmta('->whereExists(fn ($query) => #~)', { i(0) })), + sn( + nil, + fmta( + [[ + ->whereExists(function ($query) { + #~ + }) + ]], + { i(0) } + ) + ), + sn( + nil, + fmta( + [[ + ->whereExists(function ($query) use (#~) { + #~ + }) + ]], + { i(1), i(0) } + ) + ), + }) + ), }