Hylark settings
This commit is contained in:
@@ -4,34 +4,43 @@ local t = ls.text_node
|
||||
local i = ls.insert_node
|
||||
local f = ls.function_node
|
||||
|
||||
local function get_psr4_root(path)
|
||||
-- local dir = vim.fs.dirname(path)
|
||||
-- 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
|
||||
local handle = io.popen [[php -r "echo array_keys(json_decode(file_get_contents('composer.json'), true)['autoload']['psr-4'])[0];"]]
|
||||
local ns_root = handle and handle:read '*a' or ''
|
||||
if handle then
|
||||
handle:close()
|
||||
end
|
||||
ns_root = ns_root:gsub('\\$', '') -- Remove trailing backslash
|
||||
return ns_root
|
||||
end
|
||||
|
||||
local function psr_namespace(args, snip)
|
||||
local function psr_namespace(_, snip)
|
||||
local path = snip.env.TM_FILENAME_FULL or ''
|
||||
local composer_root = get_psr4_root()
|
||||
-- Find the directory mapped by composer.json
|
||||
local root_dir = composer_root:gsub('\\', '/')
|
||||
local ns = path:match(root_dir .. '/(.*)/[^/]+%.php$')
|
||||
if ns then
|
||||
return composer_root .. '\\' .. ns:gsub('/', '\\')
|
||||
else
|
||||
return composer_root ~= '' and composer_root or 'App'
|
||||
-- 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user