185 lines
5.9 KiB
Lua
185 lines
5.9 KiB
Lua
local helpers = require 'helpers'
|
|
local map = helpers.map
|
|
|
|
local M = {}
|
|
|
|
local function command_with_dir(dir, cmd)
|
|
if dir then
|
|
return '!cd ' .. dir .. ' && ' .. cmd
|
|
end
|
|
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*$'
|
|
local extensionSuffixes = {
|
|
php = { 'Controller', 'Repository', 'StoreRequest', 'UpdateRequest', 'Request', 'Resource', 'Test', 'Observer', 'Policy', 'Seeder', 'Factory' },
|
|
['[jt]s'] = { 'Service' },
|
|
}
|
|
for suffix_type, _ in pairs(extensionSuffixes) do
|
|
if ext:match(suffix_type) then
|
|
local suffixes = extensionSuffixes[suffix_type]
|
|
for _, suffix in ipairs(suffixes) do
|
|
local scope = base_name:match('^(%w+)' .. suffix .. '$')
|
|
if scope then
|
|
return scope
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return base_name
|
|
end
|
|
|
|
local function navigate_using_suffix(suffix)
|
|
local scope = get_scope_from_file(vim.fn.expand '%:t')
|
|
local file_name = scope .. suffix
|
|
local file_path = vim.system({ 'git', 'ls-files', file_name, '**/' .. file_name }):wait().stdout
|
|
if file_path ~= '' then
|
|
vim.cmd('edit ' .. file_path)
|
|
else
|
|
vim.notify('File ' .. file_name .. ' not found in git ls-files', vim.log.levels.ERROR)
|
|
end
|
|
end
|
|
|
|
local function create_navigation_maps(maps)
|
|
for key, suffix_and_name in pairs(maps) do
|
|
map('<Leader>n' .. key, function()
|
|
navigate_using_suffix(suffix_and_name[1])
|
|
end, { desc = 'Navigate to relevant ' .. suffix_and_name[2] })
|
|
end
|
|
end
|
|
|
|
local function create_bookmark_maps(maps)
|
|
for key, bookmark in pairs(maps) do
|
|
helpers.create_bookmark(key, bookmark)
|
|
end
|
|
end
|
|
|
|
local function laravel_bookmarks_with_dir(dir)
|
|
create_bookmark_maps {
|
|
['e'] = dir .. '.env',
|
|
['l'] = dir .. 'storage/logs/laravel.log',
|
|
['w'] = dir .. 'routes/web.php',
|
|
['a'] = dir .. 'routes/api.php',
|
|
['m'] = dir .. 'database/migrations',
|
|
|
|
['dc'] = dir .. 'app/Core/',
|
|
['dd'] = dir .. 'app/Data/',
|
|
['dE'] = dir .. 'app/Enums/',
|
|
['de'] = dir .. 'app/Events/',
|
|
['dh'] = dir .. 'app/Http/',
|
|
['dj'] = dir .. 'app/Jobs/',
|
|
['dl'] = dir .. 'app/Listeners/',
|
|
['dM'] = dir .. 'app/Mail/',
|
|
['dm'] = dir .. 'app/Models/',
|
|
['dn'] = dir .. 'app/Notifications/',
|
|
['do'] = dir .. 'app/Observers/',
|
|
['dp'] = dir .. 'app/Providers/',
|
|
|
|
['pa'] = dir .. 'app/Providers/AppServiceProvider.php',
|
|
['pe'] = dir .. 'app/Providers/EventServiceProvider.php',
|
|
|
|
['cA'] = dir .. 'config/app.php',
|
|
['ca'] = dir .. 'config/auth.php',
|
|
['cb'] = dir .. 'config/broadcasting.php',
|
|
['cd'] = dir .. 'config/database.php',
|
|
['cf'] = dir .. 'config/filesystems.php',
|
|
['ch'] = dir .. 'config/filesystems.php',
|
|
['cl'] = dir .. 'config/logging.php',
|
|
['cm'] = dir .. 'config/mail.php',
|
|
['cq'] = dir .. 'config/queue.php',
|
|
['cS'] = dir .. 'config/services.php',
|
|
['cs'] = dir .. 'config/session.php',
|
|
}
|
|
end
|
|
|
|
local function laravel_utils(dir)
|
|
map('<Leader>D', function()
|
|
helpers.open_term {
|
|
cmd = dir .. 'vendor/bin/var-dump-server',
|
|
buf_opts = {
|
|
bufhidden = 'hide',
|
|
modifiable = false,
|
|
},
|
|
}
|
|
end, { desc = 'Open dump-server window' })
|
|
end
|
|
|
|
local function laravel_keymaps(dir)
|
|
map('sl ', command_with_dir(dir, 'vendor/bin/sail '), { desc = 'Run sail command' }, 'c')
|
|
map('art ', command_with_dir(dir, 'php artisan '), { desc = 'Run artisan command' }, 'c')
|
|
map('sart ', command_with_dir(dir, 'vendor/bin/sail artisan '), { desc = 'Run artisan command with sail' }, 'c')
|
|
map('cmp ', command_with_dir(dir, 'composer '), { desc = 'Run composer script' }, 'c')
|
|
map('<Leader>pm', ':' .. command_with_dir(dir, 'vendor/bin/sail artisan migrate<CR>'))
|
|
map('<Leader>pr', ':' .. command_with_dir(dir, 'vendor/bin/sail artisan migrate:rollback<CR>'))
|
|
map('<Leader>pM', ':' .. command_with_dir(dir, 'vendor/bin/sail artisan make:'))
|
|
create_navigation_maps {
|
|
['m'] = { '.php', 'model' },
|
|
['c'] = { 'Controller.php', 'controller' },
|
|
['p'] = { 'Policy.php', 'policy' },
|
|
['R'] = { 'Resource.php', 'resource' },
|
|
['r'] = { 'Request.php', 'request' },
|
|
['t'] = { 'Test.php', 'test file' },
|
|
}
|
|
if dir == nil then
|
|
dir = ''
|
|
end
|
|
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('<Leader>m' .. key, function()
|
|
make_laravel_file(dir, name)
|
|
end, { desc = 'Make and navigate to relevant ' .. name })
|
|
end
|
|
end
|
|
|
|
M.laravel_keymaps = laravel_keymaps
|
|
M.laravel_makes = laravel_makes
|
|
M.laravel_utils = laravel_utils
|
|
M.laravel_bookmarks = laravel_bookmarks_with_dir
|
|
|
|
return M
|
|
|