Quicker and project specific settings

This commit is contained in:
2025-11-07 14:27:25 +00:00
parent 5016dd4ee8
commit f8f6d8e776
4 changed files with 115 additions and 2 deletions

80
lua/projects.lua Normal file
View File

@@ -0,0 +1,80 @@
-- Project specific settings
local M = {}
-- Helper to create mappings
local map = require('helpers').map
-- Get the last folder name from a path
local function project_name_from_cwd(cwd)
return vim.fn.fnamemodify(cwd, ':t')
end
-- Define per-project configuration here.
-- Keys are folder names (last segment of your cwd).
local PROJECTS = {
-- Example: a repo folder
['runcats'] = function()
map(
'<leader>pl',
':cexpr system("cd server && vendor/bin/phpstan analyse --no-progress --error-format=raw --memory-limit=2G -vv") <CR>',
{ desc = 'Run lint' }
)
map('s ', '!cd server && ', { desc = 'Run command in server directory' }, 'c')
map('c ', '!cd client && ', { desc = 'Run command in client directory' }, 'c')
map('sl ', '!cd server && sail ', { desc = 'Run sail command' }, 'c')
map('art ', '!cd server && php artisan ', { desc = 'Run artisan command' }, 'c')
map('sart ', '!cd server && sail artisan ', { desc = 'Run artisan command with sail' }, 'c')
map('cmp ', '!cd server && composer ', { desc = 'Run composer script' }, 'c')
map('yrn ', '!cd client && yarn ', { desc = 'Run yarn script' }, 'c')
map('ts ', '!cd server && php artisan typescript:transform --format', { desc = 'Compile typescript' }, 'c')
end,
default = function() end,
}
local last_applied = nil
function M.apply(cwd)
local dir = cwd or vim.fn.getcwd()
local name = project_name_from_cwd(dir)
if last_applied == name then
return
end
last_applied = name
local setup = PROJECTS[name] or PROJECTS.default
if type(setup) == 'function' then
setup()
vim.notify(('Project config loaded: %s'):format(name), vim.log.levels.INFO, { title = 'projects.lua' })
end
end
-- Apply on startup and when the working directory changes
do
local grp = vim.api.nvim_create_augroup('ProjectConfig', { clear = true })
vim.api.nvim_create_autocmd('VimEnter', {
group = grp,
callback = function()
M.apply()
end,
})
vim.api.nvim_create_autocmd('DirChanged', {
group = grp,
callback = function(args)
-- args.file is the new cwd for DirChanged
M.apply(args and args.file or nil)
end,
})
end
-- Optional command to manually re-apply
vim.api.nvim_create_user_command('ProjectReload', function()
last_applied = nil
M.apply()
end, {})
-- Keep your helper line
require('helpers').edit_cf('w', '/lua/projects.lua')