From e00fc002a6441fa3741d1f705337efbbd8850744 Mon Sep 17 00:00:00 2001 From: Elvis Claros Castro Date: Mon, 9 Dec 2024 10:44:18 -0300 Subject: Cambios varios. --- lua/config/composer.lua | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 lua/config/composer.lua (limited to 'lua/config/composer.lua') diff --git a/lua/config/composer.lua b/lua/config/composer.lua new file mode 100644 index 0000000..2405416 --- /dev/null +++ b/lua/config/composer.lua @@ -0,0 +1,104 @@ +vim.treesitter.query.set( + "json", + "composer_dependencies", + [[ + (pair + key: (string + (string_content) @key (#match? @key "require|require-dev") + ) + value: (object + (pair + key: (string (string_content) @depen) + ) + ) + ) + ]] +) +local group = vim.api.nvim_create_augroup("composer-info", {}) +vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost" }, { + pattern = "composer.json", + group = group, + callback = function(event) + local parser = vim.treesitter.get_parser(event.buf, "json") + local tree = parser:parse()[1] + if tree == nil then + return + end + local query = vim.treesitter.query.get("json", "composer_dependencies") + if not query then + return + end + + local dependencies = {} + + for id, node in query:iter_captures(tree:root(), event.buf) do + if query.captures[id] == "depen" then + table.insert(dependencies, { + name = vim.treesitter.get_node_text(node, event.buf), + line = node:start(), + }) + end + end + + if #dependencies == 0 then + return + end + + local ns = vim.api.nvim_create_namespace("composer-deps") + vim.api.nvim_buf_clear_namespace(event.buf, ns, 0, -1) + + vim.system( + { "composer", "info", "-f", "json" }, + {}, + vim.schedule_wrap(function(out) + if out.code ~= 0 then + return + end + + local data = vim.json.decode(out.stdout, {}) + if type(data.installed) ~= "table" then + return + end + + for _, dep in ipairs(dependencies) do + local info = vim.iter(data.installed):find(function(inst) + return dep.name == inst.name + end) + if info then + vim.api.nvim_buf_set_extmark(event.buf, ns, dep.line, 0, { + virt_text = { { string.format('<- %s', info.version), "comment" } }, + virt_text_pos = "eol", + }) + end + end + end) + ) + vim.system( + { "composer", "outdated", "-f", "json" }, + {}, + vim.schedule_wrap(function(out) + if out.code ~= 0 then + return + end + + local data = vim.json.decode(out.stdout, {}) + if type(data.installed) ~= "table" then + return + end + + for _, dep in ipairs(dependencies) do + local info = vim.iter(data.installed):find(function(inst) + return dep.name == inst.name + end) + if info then + vim.api.nvim_buf_set_extmark(event.buf, ns, dep.line - 1, 0, { + virt_text = { { string.format('^ %s (new version)', info.latest), "error" } }, + virt_text_pos = "eol", + }) + end + end + end) + ) + end, +}) + -- cgit v1.2.3