Skip to content

Instantly share code, notes, and snippets.

@adalessa
Created September 22, 2024 07:38
Show Gist options
  • Save adalessa/ce9b3d4de8f7bc2b0252dbb1c8354145 to your computer and use it in GitHub Desktop.
Save adalessa/ce9b3d4de8f7bc2b0252dbb1c8354145 to your computer and use it in GitHub Desktop.
Plugin para mostrar version de composer
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, {})
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, {})
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,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment