Snippets
This commit is contained in:
@@ -35,6 +35,33 @@ vim.keymap.set('n', '<C-l>', '<C-w><C-l>', { desc = 'Move focus to the right win
|
|||||||
vim.keymap.set('n', '<C-j>', '<C-w><C-j>', { desc = 'Move focus to the lower window' })
|
vim.keymap.set('n', '<C-j>', '<C-w><C-j>', { desc = 'Move focus to the lower window' })
|
||||||
vim.keymap.set('n', '<C-k>', '<C-w><C-k>', { desc = 'Move focus to the upper window' })
|
vim.keymap.set('n', '<C-k>', '<C-w><C-k>', { desc = 'Move focus to the upper window' })
|
||||||
|
|
||||||
|
vim.keymap.set({ 'i' }, '<C-J>', function()
|
||||||
|
local ls = require 'luasnip'
|
||||||
|
if ls.choice_active() then
|
||||||
|
ls.change_choice(1)
|
||||||
|
end
|
||||||
|
end, { silent = true })
|
||||||
|
vim.keymap.set({ 'i' }, '<C-k>', function()
|
||||||
|
local ls = require 'luasnip'
|
||||||
|
if ls.choice_active() then
|
||||||
|
ls.change_choice(-1)
|
||||||
|
end
|
||||||
|
end, { silent = true })
|
||||||
|
vim.keymap.set({ 'i', 's' }, '<C-L>', function()
|
||||||
|
local ls = require 'luasnip'
|
||||||
|
if ls.expandable() then
|
||||||
|
ls.expand()
|
||||||
|
elseif ls.in_snippet() then
|
||||||
|
ls.jump(1)
|
||||||
|
end
|
||||||
|
end, { silent = true })
|
||||||
|
vim.keymap.set({ 'i', 's' }, '<C-H>', function()
|
||||||
|
local ls = require 'luasnip'
|
||||||
|
if ls.in_snippet() then
|
||||||
|
ls.jump(-1)
|
||||||
|
end
|
||||||
|
end, { silent = true })
|
||||||
|
|
||||||
vim.keymap.set('n', '<C-S-h>', '<C-w>H', { desc = 'Move window to the left' })
|
vim.keymap.set('n', '<C-S-h>', '<C-w>H', { desc = 'Move window to the left' })
|
||||||
vim.keymap.set('n', '<C-S-l>', '<C-w>L', { desc = 'Move window to the right' })
|
vim.keymap.set('n', '<C-S-l>', '<C-w>L', { desc = 'Move window to the right' })
|
||||||
vim.keymap.set('n', '<C-S-j>', '<C-w>J', { desc = 'Move window to the lower' })
|
vim.keymap.set('n', '<C-S-j>', '<C-w>J', { desc = 'Move window to the lower' })
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
-- AI code completion
|
-- AI code completion
|
||||||
|
|
||||||
if (vim.fn.getenv('COPILOT_API_KEY') ~= vim.NIL) then
|
if vim.fn.getenv 'COPILOT_API_KEY' ~= vim.NIL then
|
||||||
return {
|
return {
|
||||||
'zbirenbaum/copilot.lua',
|
'zbirenbaum/copilot.lua',
|
||||||
event = 'InsertEnter',
|
event = 'InsertEnter',
|
||||||
@@ -10,7 +10,7 @@ if (vim.fn.getenv('COPILOT_API_KEY') ~= vim.NIL) then
|
|||||||
enabled = true,
|
enabled = true,
|
||||||
auto_trigger = true,
|
auto_trigger = true,
|
||||||
keymap = {
|
keymap = {
|
||||||
accept = '<Tab>',
|
accept = '<A-a>',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
filetypes = {
|
filetypes = {
|
||||||
@@ -48,8 +48,9 @@ return {
|
|||||||
provider = 'openai_compatible',
|
provider = 'openai_compatible',
|
||||||
request_timeout = 3,
|
request_timeout = 3,
|
||||||
throttle = 500, -- Increase to reduce costs and avoid rate limits
|
throttle = 500, -- Increase to reduce costs and avoid rate limits
|
||||||
debounce = 300, -- Increase to reduce costs and avoid rate limits
|
debounce = 400, -- Increase to reduce costs and avoid rate limits
|
||||||
n_completions = 1,
|
n_completions = 1,
|
||||||
|
before_cursor_filter_length = 10,
|
||||||
provider_options = {
|
provider_options = {
|
||||||
openai_compatible = {
|
openai_compatible = {
|
||||||
api_key = 'COMPLETION_OPENAI_API_KEY',
|
api_key = 'COMPLETION_OPENAI_API_KEY',
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ return {
|
|||||||
event = 'VimEnter',
|
event = 'VimEnter',
|
||||||
version = '1.*',
|
version = '1.*',
|
||||||
dependencies = {
|
dependencies = {
|
||||||
'L3MON4D3/LuaSnip',
|
-- 'L3MON4D3/LuaSnip',
|
||||||
'folke/lazydev.nvim',
|
'folke/lazydev.nvim',
|
||||||
},
|
},
|
||||||
--- @module 'blink.cmp'
|
--- @module 'blink.cmp'
|
||||||
@@ -61,7 +61,7 @@ return {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
snippets = { preset = 'luasnip' },
|
-- snippets = { preset = 'luasnip' },
|
||||||
|
|
||||||
-- Blink.cmp includes an optional, recommended rust fuzzy matcher,
|
-- Blink.cmp includes an optional, recommended rust fuzzy matcher,
|
||||||
-- which automatically downloads a prebuilt binary when enabled.
|
-- which automatically downloads a prebuilt binary when enabled.
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ return {
|
|||||||
require('luasnip.loaders.from_lua').load {
|
require('luasnip.loaders.from_lua').load {
|
||||||
paths = { snippets_dir },
|
paths = { snippets_dir },
|
||||||
}
|
}
|
||||||
|
ls.setup {
|
||||||
|
update_events = 'TextChanged,TextChangedI',
|
||||||
|
enable_autosnippets = true,
|
||||||
|
}
|
||||||
end,
|
end,
|
||||||
opts = {},
|
opts = {},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,56 @@
|
|||||||
local ls = require 'luasnip'
|
local ls = require 'luasnip'
|
||||||
local s = ls.snippet
|
local s = ls.snippet
|
||||||
|
local sn = ls.snippet_node
|
||||||
local t = ls.text_node
|
local t = ls.text_node
|
||||||
|
local c = ls.choice_node
|
||||||
local i = ls.insert_node
|
local i = ls.insert_node
|
||||||
local f = ls.function_node
|
local f = ls.function_node
|
||||||
|
local d = ls.dynamic_node
|
||||||
|
local fmt = require('luasnip.extras.fmt').fmt
|
||||||
|
local rep = require('luasnip.extras').rep
|
||||||
|
local line_begin = require('luasnip.extras.conditions').line_begin
|
||||||
|
local extend_decorator = require 'luasnip.util.extend_decorator'
|
||||||
|
local fmta = extend_decorator.apply(fmt, { delimiters = '#~' })
|
||||||
|
|
||||||
|
local function line_begin(line_to_cursor, matched_trigger)
|
||||||
|
-- +1 because `string.sub("abcd", 1, -2)` -> abc
|
||||||
|
return line_to_cursor:sub(1, -(#matched_trigger + 1)):match '^%s*$'
|
||||||
|
end
|
||||||
|
|
||||||
|
local function empty_line(_, matched_trigger)
|
||||||
|
return vim.api.nvim_get_current_line():match('^%s*' .. vim.pesc(matched_trigger) .. '$')
|
||||||
|
end
|
||||||
|
|
||||||
|
local function file_begin(line_to_cursor, matched_trigger)
|
||||||
|
local line_number = vim.fn.line '.'
|
||||||
|
return line_number == 1 and line_begin(line_to_cursor, matched_trigger)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function tr(trigger, description)
|
||||||
|
return {
|
||||||
|
trig = trigger,
|
||||||
|
desc = description,
|
||||||
|
snippetType = 'autosnippet',
|
||||||
|
condition = empty_line,
|
||||||
|
}
|
||||||
|
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) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
local function psr_namespace(_, snip)
|
local function psr_namespace(_, snip)
|
||||||
-- local path = snip.env.TM_FILENAME_FULL or ''
|
-- local path = snip.env.TM_FILENAME_FULL or ''
|
||||||
@@ -49,81 +97,274 @@ local function class_name(args, snip)
|
|||||||
return filename:match '([^%.]+)' or 'ClassName'
|
return filename:match '([^%.]+)' or 'ClassName'
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
local function snippet_aliases(triggers, description, snippet)
|
||||||
s('du', { t 'dump(', i(0), t ');' }),
|
local snips = {}
|
||||||
s('dt', { t '\\PhpStan\\dumpType(', i(0), t ');' }),
|
for key, trigger in ipairs(triggers) do
|
||||||
s('ql', {
|
snips[key] = s(tr(trigger, description), snippet())
|
||||||
t '\\Illuminate\\Support\\Facades\\DB::listen(function (\\Illuminate\\Database\\Events\\QueryExecuted $e) {',
|
end
|
||||||
t { '', ' dump($e->sql, $e->bindings);' },
|
return snips
|
||||||
t { '', '});' },
|
end
|
||||||
}),
|
|
||||||
s('test', {
|
local function flatten(tbl)
|
||||||
t 'test(',
|
local result = {}
|
||||||
i(1),
|
local index = 1
|
||||||
t ', function () {',
|
for _, val in ipairs(tbl) do
|
||||||
t { '', ' ' },
|
if type(val) == 'table' and vim.tbl_islist(val) then
|
||||||
i(0),
|
for _, val2 in ipairs(val) do
|
||||||
t { '', ');' },
|
result[index] = val2
|
||||||
}),
|
index = index + 1
|
||||||
s('/**', {
|
end
|
||||||
t '/**',
|
else
|
||||||
t { '', ' * ' },
|
result[index] = val
|
||||||
i(0),
|
index = index + 1
|
||||||
t { '', ' */' },
|
end
|
||||||
}),
|
end
|
||||||
s('@p', { t '@property ', i(1), t ' $', i(0) }),
|
|
||||||
s('@pb', { t '@property bool $', i(0) }),
|
return result;
|
||||||
s('@ps', { t '@property string $', i(0) }),
|
end
|
||||||
s('@pi', { t '@property int $', i(0) }),
|
|
||||||
s('@pc', { t '@property \\Illuminate\\Support\\Collection<int, ', i(1), t '> $', i(0) }),
|
return flatten {
|
||||||
s('@pd', { t '@property \\Illuminate\\Support\\Carbon $', i(0) }),
|
---------------
|
||||||
s('@pp', {
|
-- DEBUGGING --
|
||||||
t '/**',
|
---------------
|
||||||
t { '', ' * @property int $id' },
|
s(tr('du ', 'Dump a variable to the dump server'), fmta('dump(#~);', { i(0) })),
|
||||||
t { '', ' * ' },
|
s(tr('ray', 'ray'), fmta('ray(#~);', { i(0) })),
|
||||||
i(1),
|
s(tr('dt ', 'Dump PHPStan type definition'), fmta('\\PhpStan\\dumpType(#~);', { i(0) })),
|
||||||
t { '', ' * @property \\Illuminate\\Support\\Carbon $created_at' },
|
s(
|
||||||
t { '', ' * @property \\Illuminate\\Support\\Carbon $updated_at' },
|
tr('ql ', 'Log all queries'),
|
||||||
t { '', ' *', ' * Relationships' },
|
fmta(
|
||||||
t { '', ' * ' },
|
[[
|
||||||
i(0),
|
\Illuminate\Support\Facades\DB::listen(function (\Illuminate\Database\Events\QueryExecuted $e) {
|
||||||
t { '', ' */' },
|
dump($e->sql, $e->bindings);
|
||||||
}),
|
});
|
||||||
s('php', {
|
#~
|
||||||
t '<?php',
|
]],
|
||||||
t { '', '' },
|
{ i(0) }
|
||||||
t 'namespace ',
|
)
|
||||||
f(psr_namespace, {}),
|
),
|
||||||
t ';',
|
--------------
|
||||||
t { '', '', '/**', ' * Class ' },
|
-- COMMENTS --
|
||||||
f(class_name, {}),
|
--------------
|
||||||
t { '', ' */', 'class ' },
|
s(
|
||||||
f(class_name, {}),
|
tr('/**', 'Docblock comment'),
|
||||||
i(1),
|
fmta(
|
||||||
t { '', '{', ' public function __construct()', ' {', ' ' },
|
[[
|
||||||
i(0, '// TODO: Implement constructor'),
|
/**
|
||||||
t { '', ' }', '}' },
|
* #~
|
||||||
}),
|
*/
|
||||||
s('pub', {
|
]],
|
||||||
t 'public function ',
|
{ i(0) }
|
||||||
i(1),
|
)
|
||||||
t '(',
|
),
|
||||||
i(2),
|
s(tr('@v', '@var docblock'), fmta('/** @var #~ $#~ */', { i(1), i(0) })),
|
||||||
t ')',
|
s(tr('* @pr', 'Class property docblock'), fmta('* @property #~ $#~', { i(1), i(0) })),
|
||||||
t { '', '{' },
|
s(tr('* @pb', 'Class boolean property docblock'), fmta('* @property bool $#~', { i(0) })),
|
||||||
t { '', ' ' },
|
s(tr('* @pi', 'Class int property docblock'), fmta('* @property int $#~', { i(0) })),
|
||||||
i(0),
|
s(tr('* @ps', 'Class string property docblock'), fmta('* @property string $#~', { i(0) })),
|
||||||
t { '', '}' },
|
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('pro', {
|
s(
|
||||||
t 'protected function ',
|
tr('@pm', 'Model magic properties docblock'),
|
||||||
i(1),
|
fmta(
|
||||||
t '(',
|
[[
|
||||||
i(2),
|
/**
|
||||||
t ')',
|
* @property int $id
|
||||||
t { '', '{' },
|
* #~
|
||||||
t { '', ' ' },
|
* @property \Illuminate\Support\Carbon $created_at
|
||||||
i(0),
|
* @property \Illuminate\Support\Carbon $updated_at
|
||||||
t { '', '}' },
|
*
|
||||||
}),
|
* Relationships
|
||||||
|
* #~
|
||||||
|
*/
|
||||||
|
]],
|
||||||
|
{ i(1), i(0) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
------------
|
||||||
|
-- SYNTAX --
|
||||||
|
------------
|
||||||
|
s(
|
||||||
|
tr('if ', 'foreach block'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
if (#~) {
|
||||||
|
#~
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ i(1), i(0) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
s(
|
||||||
|
tr('fe ', 'foreach block'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
foreach (#~ as #~) {
|
||||||
|
#~
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ i(1), i(2), 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),
|
||||||
|
})
|
||||||
|
end),
|
||||||
|
snippet_aliases({ 'af ', 'array_filter' }, 'array_filter function', function()
|
||||||
|
return fmta('array_filter(#~, #~);', {
|
||||||
|
i(1),
|
||||||
|
fn_choice(0),
|
||||||
|
})
|
||||||
|
end),
|
||||||
|
s(
|
||||||
|
tr('php', 'php class'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace #~
|
||||||
|
|
||||||
|
class #~
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
#~
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ f(psr_namespace, {}), f(class_name, {}), i(0) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
s(
|
||||||
|
tr('met', 'public class method'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
public function #~(#~)
|
||||||
|
{
|
||||||
|
#~
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ i(1), i(2), i(0) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
s(
|
||||||
|
tr('pmet', 'protected class method'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
protected function #~(#~)
|
||||||
|
{
|
||||||
|
#~
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ i(1), i(2), i(0) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
s(
|
||||||
|
tr('smet', 'public static class method'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
public static function #~(#~)
|
||||||
|
{
|
||||||
|
#~
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ i(1), i(2), i(0) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
s(
|
||||||
|
tr('spmet', 'protected static class method'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
protected static function #~(#~)
|
||||||
|
{
|
||||||
|
#~
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ i(1), i(2), i(0) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
-------------
|
||||||
|
-- PROJECT --
|
||||||
|
-------------
|
||||||
|
s(
|
||||||
|
tr('test', 'Create a test function'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
test(#~ function () {
|
||||||
|
#~
|
||||||
|
})
|
||||||
|
]],
|
||||||
|
{ i(1), i(0) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
s(tr('nn ', 'Assert not null'), fmta('Assert::notNull(#~)', { i(0) })),
|
||||||
|
-------------
|
||||||
|
-- LARAVEL --
|
||||||
|
-------------
|
||||||
|
s(
|
||||||
|
tr('bt', 'belongsTo Laravel relationship method'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\#~, $this>
|
||||||
|
*/
|
||||||
|
public function #~(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(#~::class);
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ rep(1), i(0), i(1) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
s(
|
||||||
|
tr('hm', 'hasMany Laravel relationship method'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\#~, $this>
|
||||||
|
*/
|
||||||
|
public function #~(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasOne(#~::class);
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ rep(1), i(0), i(1) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
s(
|
||||||
|
tr('ho', 'hasOne Laravel relationship method'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne<\App\Models\#~, $this>
|
||||||
|
*/
|
||||||
|
public function #~(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(#~::class);
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ rep(1), i(0), i(1) }
|
||||||
|
)
|
||||||
|
),
|
||||||
|
s(
|
||||||
|
tr('bm', 'belongsToMany Laravel relationship method'),
|
||||||
|
fmta(
|
||||||
|
[[
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\App\Models\#~, $this>
|
||||||
|
*/
|
||||||
|
public function #~(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(#~::class, #~);
|
||||||
|
}
|
||||||
|
]],
|
||||||
|
{ rep(1), i(2), i(1), i(0) }
|
||||||
|
)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user