130 lines
4.0 KiB
Lua
130 lines
4.0 KiB
Lua
local ls = require 'luasnip'
|
|
local s = ls.snippet
|
|
local t = ls.text_node
|
|
local i = ls.insert_node
|
|
local f = ls.function_node
|
|
|
|
local function psr_namespace(_, snip)
|
|
-- local path = snip.env.TM_FILENAME_FULL or ''
|
|
-- -- Get the directory of the path
|
|
-- local dir = vim.fs.dirname(path)
|
|
-- -- Loop through parent directories to find composer.json
|
|
-- while dir ~= '/' and dir ~= nil do
|
|
-- local composer_json_path = dir .. '/composer.json'
|
|
-- if vim.fn.filereadable(composer_json_path) == 1 then
|
|
-- break
|
|
-- end
|
|
-- dir = vim.fs.dirname(dir)
|
|
-- end
|
|
-- -- If no composer.json found, return empty string
|
|
-- if dir == '/' or dir == nil then
|
|
-- return ''
|
|
-- end
|
|
--
|
|
-- -- Decode composer.json and get PSR-4 autoload mappings
|
|
-- local composer = vim.json.decode(vim.iter(vim.fn.readfile(dir .. '/composer.json')):join '')
|
|
-- local psr4 = composer['autoload'] and composer['autoload']['psr-4']
|
|
--
|
|
-- -- If no PSR-4 mappings, return empty string
|
|
-- if not psr4 then
|
|
-- return ''
|
|
-- end
|
|
--
|
|
-- -- Get the relative path from the composer.json directory
|
|
-- local relative_path = path:sub(#dir + 2)
|
|
-- -- Loop through PSR-4 mappings
|
|
-- for namespace, map in pairs(psr4) do
|
|
-- -- Check if the relative path matches the mapping
|
|
-- if relative_path:match('^' .. map:gsub('/', '%%/')) then
|
|
-- -- Extract the suffix of the path after the mapping, removing the filename
|
|
-- local suffix = relative_path:sub(#map + 2):match('^(.*)/[^/]+%.php$') or ''
|
|
-- local trimmed = namespace:gsub('\\$', '')
|
|
-- return trimmed .. (suffix ~= '' and ('\\' .. suffix:gsub('/', '\\')) or '')
|
|
-- end
|
|
-- end
|
|
end
|
|
|
|
local function class_name(args, snip)
|
|
local filename = snip.env.TM_FILENAME or ''
|
|
return filename:match '([^%.]+)' or 'ClassName'
|
|
end
|
|
|
|
return {
|
|
s('du', { t 'dump(', i(0), t ');' }),
|
|
s('dt', { t '\\PhpStan\\dumpType(', i(0), t ');' }),
|
|
s('ql', {
|
|
t '\\Illuminate\\Support\\Facades\\DB::listen(function (\\Illuminate\\Database\\Events\\QueryExecuted $e) {',
|
|
t { '', ' dump($e->sql, $e->bindings);' },
|
|
t { '', '});' },
|
|
}),
|
|
s('test', {
|
|
t 'test(',
|
|
i(1),
|
|
t ', function () {',
|
|
t { '', ' ' },
|
|
i(0),
|
|
t { '', ');' },
|
|
}),
|
|
s('/**', {
|
|
t '/**',
|
|
t { '', ' * ' },
|
|
i(0),
|
|
t { '', ' */' },
|
|
}),
|
|
s('@p', { t '@property ', i(1), t ' $', i(0) }),
|
|
s('@pb', { t '@property bool $', i(0) }),
|
|
s('@ps', { t '@property string $', i(0) }),
|
|
s('@pi', { t '@property int $', i(0) }),
|
|
s('@pc', { t '@property \\Illuminate\\Support\\Collection<int, ', i(1), t '> $', i(0) }),
|
|
s('@pd', { t '@property \\Illuminate\\Support\\Carbon $', i(0) }),
|
|
s('@pp', {
|
|
t '/**',
|
|
t { '', ' * @property int $id' },
|
|
t { '', ' * ' },
|
|
i(1),
|
|
t { '', ' * @property \\Illuminate\\Support\\Carbon $created_at' },
|
|
t { '', ' * @property \\Illuminate\\Support\\Carbon $updated_at' },
|
|
t { '', ' *', ' * Relationships' },
|
|
t { '', ' * ' },
|
|
i(0),
|
|
t { '', ' */' },
|
|
}),
|
|
s('php', {
|
|
t '<?php',
|
|
t { '', '' },
|
|
t 'namespace ',
|
|
f(psr_namespace, {}),
|
|
t ';',
|
|
t { '', '', '/**', ' * Class ' },
|
|
f(class_name, {}),
|
|
t { '', ' */', 'class ' },
|
|
f(class_name, {}),
|
|
i(1),
|
|
t { '', '{', ' public function __construct()', ' {', ' ' },
|
|
i(0, '// TODO: Implement constructor'),
|
|
t { '', ' }', '}' },
|
|
}),
|
|
s('pub', {
|
|
t 'public function ',
|
|
i(1),
|
|
t '(',
|
|
i(2),
|
|
t ')',
|
|
t { '', '{' },
|
|
t { '', ' ' },
|
|
i(0),
|
|
t { '', '}' },
|
|
}),
|
|
s('pro', {
|
|
t 'protected function ',
|
|
i(1),
|
|
t '(',
|
|
i(2),
|
|
t ')',
|
|
t { '', '{' },
|
|
t { '', ' ' },
|
|
i(0),
|
|
t { '', '}' },
|
|
}),
|
|
}
|