diff options
| author | Elvis Claros Castro <elvis@claros.ar> | 2024-12-09 10:44:18 -0300 |
|---|---|---|
| committer | Elvis Claros Castro <elvis@claros.ar> | 2024-12-09 10:44:18 -0300 |
| commit | e00fc002a6441fa3741d1f705337efbbd8850744 (patch) | |
| tree | f6891a9119ee065db574aae73395af973100a008 /lua/config/composer.lua | |
| parent | 546d06a2314d0b4b5360346243bcfc0bda3fff30 (diff) | |
| download | nvim-conf-e00fc002a6441fa3741d1f705337efbbd8850744.tar.gz nvim-conf-e00fc002a6441fa3741d1f705337efbbd8850744.zip | |
Cambios varios.
Diffstat (limited to 'lua/config/composer.lua')
| -rw-r--r-- | lua/config/composer.lua | 104 |
1 files changed, 104 insertions, 0 deletions
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, +}) + |