How to invoke the expansion of LuaSnip snippets programmatically in other .lua files #970
-
ProblemI have the following snippet defined in the s( -- figure environment
{
trig="\\fg",
dscr="Automatically expand '\\fg' to figure environment",
snippetType="autosnippet"
},
fmt(
[[
\begin{figure}
\centering
\includegraphics[width=<>\textwidth]{<>}
\caption{<>}
\label{fig:<>}
\end{figure}
]],
{
i(2), i(1), i(3), i(4)
},
{ delimiters = "<>" }
)
), The ScenarioThe (simplified) scenario is as follows:
\begin{figure}
\centering
\includegraphics[width=<>\textwidth]{some_text_that_was_under_cursor}
\caption{<>}
\label{fig:<>}
\end{figure} Step 4 is where I am having issues and need help. I have made three attempts at invoking the LaTeX figure environment creation snippet, however, none have produced the desired results. In each of the following examples, " Attempted SolutionsAttempt 1 via Neovim's "feed keys" API using typing mode -- Get text under cursor
local text_under_cursor = get_text_under_cursor()
-- Clear current line
vim.api.nvim_set_current_line("") -- clear current line
-- Invoke LuaSnip figure environment creation snippet
vim.api.nvim_feedkeys("i\\fg", "t", true)
-- Fill in figure include path
vim.api.nvim_feedkeys("i" .. text_under_cursor, "t", true) This clears the current line and replaces it with Attempt 2 via Neovim's "feed keys" API using execute mode Modifying the above code slightly by changing the mode to -- Get text under cursor
local text_under_cursor = get_text_under_cursor()
-- Clear current line
vim.api.nvim_set_current_line("") -- clear current line
-- Invoke LuaSnip figure environment creation snippet
vim.api.nvim_feedkeys("i\\fg", "x!", true)
-- Fill in figure include path
vim.api.nvim_feedkeys("a" .. text_under_cursor, "t", true) Attempt 3 via LuaSnip's "snip expand" API -- Get text under cursor
local text_under_cursor = get_text_under_cursor()
-- Clear current line
vim.api.nvim_set_current_line("") -- clear current line
-- Invoke LuaSnip figure environment creation snippet
local luasnip = require("luasnip")
local snips = luasnip.get_snippets("tex")
luasnip.snip_expand(snips[1]) -- This doesn't actually find the snippet I'm looking for should expand something
-- Fill in figure include path
vim.api.nvim_feedkeys(text_under_cursor, "t", true) This results in an error " Additional InfoI am using the latest version of LuaSnip at the time of writing (commit e81cbe6) loaded via lazy.nvim. My
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Attempt 3 sounds promising! |
Beta Was this translation helpful? Give feedback.
@L3MON4D3 Your idea worked perfectly. I added the
named-snippets.lua
file in.config/nvim/lua
(the same directory astexshortcuts.lua
and then added the following code to the latter file:Thank you very much for your help. The work you do on LuaSnip is appreciated.