fix(image): skip \usepackage in comments and body (#2325)

## Description

Here are some small and hopefully uncontroversial tweaks to package
extraction from LaTeX preambles:

* Don't consider anything in comments
* Make sure that the extracted names are actually the arguments of
`\usepackage` and not some other macro on the same line
* Stop looking for packages at `\begin{document}`, where the preamble
ends and the body of the document begins (you can't load packages after
this, so any `\usepackage` beyond this point is content, not code. Also
saves a huge amount of work in large documents.)

Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
Daniel Wennberg 2025-10-23 02:14:32 -07:00 committed by GitHub
parent 5ef4c0b421
commit 90227af497
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -128,12 +128,15 @@ function M.get_packages(buf)
return M._cache(buf, "packages", function()
local ret = {} ---@type string[]
for _, line in ipairs(vim.api.nvim_buf_get_lines(buf, 0, -1, false)) do
line = line:match("(.-)%%") or line
if line:find("\\usepackage", 1, true) then
for _, p in ipairs(vim.split(line:match("{(.-)}") or "", ",%s*")) do
for _, p in ipairs(vim.split(line:match("\\usepackage.-{(.-)}") or "", ",%s*")) do
if not vim.tbl_contains(ret, p) then
ret[#ret + 1] = p
end
end
elseif line:find("\\begin{document}", 1, true) then
break
end
end
return ret