Snippets
This commit is contained in:
@@ -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) }
|
||||
)
|
||||
),
|
||||
})
|
||||
),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user