Files
nvim/lua/projects.lua
T

80 lines
2.4 KiB
Lua
Raw Normal View History

2025-11-07 14:27:25 +00:00
-- Project specific settings
local M = {}
-- Helper to create mappings
2025-11-07 22:10:53 +00:00
local helpers = require 'helpers'
local map = helpers.map
2025-11-07 14:27:25 +00:00
2026-07-09 16:43:59 +01:00
local function relevant_directory_from_cwd(cwd)
local basename = vim.fn.fnamemodify(cwd, ':t')
local nested_folder_names = { 'server', 'frontend', 'frontend2', 'client', 'backend' }
if vim.tbl_contains(nested_folder_names, basename) then
local parent = vim.fn.fnamemodify(cwd, ':h')
return relevant_directory_from_cwd(parent)
end
return cwd
end
2025-11-07 14:27:25 +00:00
-- Get the last folder name from a path
2026-07-09 16:43:59 +01:00
local function project_name_from_dir(dir)
return vim.fn.fnamemodify(dir, ':t')
2025-11-07 14:27:25 +00:00
end
local last_applied = nil
function M.apply(cwd)
2026-07-09 16:43:59 +01:00
local dir = relevant_directory_from_cwd(cwd or vim.fn.getcwd())
local name = project_name_from_dir(dir)
2025-11-07 14:27:25 +00:00
if last_applied == name then
return
end
last_applied = name
2026-07-09 16:43:59 +01:00
-- Check if project lua file exists in nvim config path
if vim.fn.filereadable(vim.fn.stdpath 'config' .. '/lua/projects/' .. name .. '.lua') == 1 then
local setup = require('projects.' .. name)
helpers.edit_cf('w', '/lua/projects/' .. name .. '.lua')
2025-11-09 23:03:51 +00:00
setup(dir)
2025-11-07 14:27:25 +00:00
vim.notify(('Project config loaded: %s'):format(name), vim.log.levels.INFO, { title = 'projects.lua' })
2026-07-09 16:43:59 +01:00
-- Source the project file to apply any new settings
vim.api.nvim_create_autocmd({ 'BufWritePost' }, {
pattern = { vim.fn.stdpath 'config' .. '/lua/projects/' .. name .. '.lua' },
callback = function()
package.loaded['projects.' .. name] = nil
local setup = require('projects.' .. name)
setup(dir)
vim.notify(('Project config reloaded: %s'):format(name), vim.log.levels.INFO, { title = 'projects.lua' })
end,
})
2025-11-07 14:27:25 +00:00
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, {})