-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Detecting VimTeX Environment with Lua #2501
Comments
I'm not sure, as I still don't have much Lua experience. But something like this? local function env(name)
local is_inside = vim.fn["vimtex#env#is_inside"](name)
-- for debugging, check the output:
print(vim.inspect(is_inside))
return is_inside ~= nil
end
local function tikz()
return env("tikzpicture")
end |
Hmm, doesn't quite seem to work (snippet works in non tikz and tikz environments), but I also am not too sure about how to view the |
Ah, I believe this might work: local function env(name)
local lnum_start = vim.fn["vimtex#env#is_inside"](name)[1]
return lnum_start ~= 0
end |
@evesdropper to make sure we're on the same page, are using LuaSnip's
I had the same behavior (expansion in tikz and non-tikz) when using the above-suggested -- File: nvim/LuaSnip/tex.lua
-- Loaded using the LuaSnip Lua loader (`:help luasnip-lua`)
local tex = {}
tex.in_tikz = function()
local is_inside = vim.fn['vimtex#env#is_inside']("tikzpicture")
-- perhaps using both tests is redundant and only the is_inside[1] is needed?
return (is_inside[1] > 0 and is_inside[2] > 0)
end
-- Not needed in the context of this question but perhaps useful
tex.in_mathzone = function() return vim.fn['vimtex#syntax#in_mathzone']() == 1 end
tex.in_text = function() return not tex.in_mathzone() end
-- Return snippets
return
{
s({trig="tikztest"},
{
t("Works!"),
},
{condition = tex.in_tikz}
),
} So yeah, I've hard-coded the tikz environment check (which is kind of yucky) instead of using a more flexible
Edit: so as far as I can make out the parameters passed to the I'm new to Lua myself though so I may well be missing something. |
s('tikztest', {t('this works only in tikz')},
{ condition=tikz }) -- defined my func slightly different Haha, I guess we are all in the same boat - I have only used Lua for mostly new init.lua I made last month and snippets. I think hardcoding is a bit inconvenient, but then again there aren't too many environments where specific snippets are needed, at least for me just tikz and maybe itemize/enumerate. But wouldn't it be possible to use a general function, then Also looked at the snippet; for me it still displays on non tikz envs as well as tikz envs: |
Good idea, I certainly think that would work (although I haven't tested it myself)
Huh, that's interesting. Here's the result on my end using the same snippet I posted earlier: (I used autotrigger to make the effect more obvious, but the behavior is the same with manually triggered snippets) I'm not using a snippet previewer (is that nvim-cmp or something similar?) though, so I can't test your behavior. Is there a chance the snippet simply appears in the display but doesn't expand? I'm probably out of ideas otherwise, though am curious why the tikz conditional expansion would work for me and not for you. |
Yeah, it's nvim-cmp. So when I tried with autosnippets it seems to be fine, just it's still appearing even when it doesn't satisfy the condition. It's a bit strange that it still displays something though - I tried with a math snippet a little later and it also had the same effects. Maybe this is more of an issue I should bring up to those over at LuaSnip. I have been misled the whole time 😆 |
It seems to me this particular issue is resolved, and what remains is more related to LuaSnip. I'll update the docs with a short description of the |
Description
More of a question than an issue, but I couldn't really find a clear answer through other searches previously and I don't really know where else I can ask. I moved to LuaSnip a while earlier and I want to be able to create environment-dependent snippets. There's a way to do this on Python, but the Lua "translation" doesn't seem to work - snippets just activate regardless of environment.
Original Python:
Rough Lua Translation:
If someone has more information on the syntax command (if I'm using the right one - I did look at the API reference and it seemed to be outdated but still worked on UltiSnips last I checked) and figuring out what it returns so this can be manipulated for Lua, it would be helpful for things like bullet points in enumerate/itemize or TikZ specific snippets in, that would be really helpful. Thanks!
VimtexInfo
The text was updated successfully, but these errors were encountered: