diff options
| -rw-r--r-- | .config/nvim/after/ftplugin/php.lua | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/.config/nvim/after/ftplugin/php.lua b/.config/nvim/after/ftplugin/php.lua index 31d85aa..4935a63 100644 --- a/.config/nvim/after/ftplugin/php.lua +++ b/.config/nvim/after/ftplugin/php.lua @@ -22,29 +22,7 @@ vimrc.after_ftplugin('php', function(conf) return obj end - -- Generate namespace declaration based on the current file name and autoload settings. - local function generate_namespace_declaration() - local current_dir = vim.fs.dirname(vim.api.nvim_buf_get_name(0)) - local path_to_composer_json = find_composer_json(current_dir) - if not path_to_composer_json then - return nil -- failed to locate composer.json - end - local composer_json = load_json(path_to_composer_json) - if not composer_json then - return nil -- failed to load composer.json - end - local psr4 = vim.tbl_get(composer_json, 'autoload', 'psr-4') - if not psr4 then - return nil -- autoload.psr-4 section is absent - end - if vim.tbl_count(psr4) ~= 1 then - return nil -- psr-4 section is ambiguous - end - local psr4_namespace, psr4_dir - for k, v in pairs(psr4) do - psr4_namespace = k - psr4_dir = v - end + local function normalize_psr4(psr4_namespace, psr4_dir) if type(psr4_dir) == 'table' then if #psr4_dir == 1 then psr4_dir = psr4_dir[1] @@ -61,13 +39,36 @@ vimrc.after_ftplugin('php', function(conf) if psr4_dir:sub(-1, -1) == '/' then psr4_dir = psr4_dir:sub(0, -2) end - local namespace_root_dir = vim.fs.dirname(path_to_composer_json) .. '/' .. psr4_dir - if not vim.startswith(current_dir, namespace_root_dir) then - return nil + return psr4_namespace, psr4_dir + end + + -- Generate namespace declaration based on the current file name and autoload settings. + local function generate_namespace_declaration() + local current_dir = vim.fs.dirname(vim.api.nvim_buf_get_name(0)) + local path_to_composer_json = find_composer_json(current_dir) + if not path_to_composer_json then + return nil -- failed to locate composer.json + end + local composer_json = load_json(path_to_composer_json) + if not composer_json then + return nil -- failed to load composer.json + end + local psr4 = vim.tbl_get(composer_json, 'autoload', 'psr-4') + if not psr4 then + return nil -- autoload.psr-4 section is absent + end + for psr4_namespace, psr4_dir in pairs(psr4) do + psr4_namespace, psr4_dir = normalize_psr4(psr4_namespace, psr4_dir) + if psr4_namespace and psr4_dir then + local namespace_root_dir = vim.fs.dirname(path_to_composer_json) .. '/' .. psr4_dir + if vim.startswith(current_dir, namespace_root_dir) then + local current_path_suffix = current_dir:sub(#namespace_root_dir + 1) + local namespace = psr4_namespace .. current_path_suffix:gsub('/', '\\') + return ("namespace %s;"):format(namespace) + end + end end - local current_path_suffix = current_dir:sub(#namespace_root_dir + 1) - local namespace = psr4_namespace .. current_path_suffix:gsub('/', '\\') - return ("namespace %s;"):format(namespace) + return nil -- failed to detect namespace end local function generate_template() |
