From 90227af4977504ae2d4294fe76a63117dfd18498 Mon Sep 17 00:00:00 2001 From: Daniel Wennberg Date: Thu, 23 Oct 2025 02:14:32 -0700 Subject: [PATCH] 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 --- lua/snacks/image/doc.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/snacks/image/doc.lua b/lua/snacks/image/doc.lua index b18b693e..5bbba7ca 100644 --- a/lua/snacks/image/doc.lua +++ b/lua/snacks/image/doc.lua @@ -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