Skip to content
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] How to translate these Snippets from UltiSnip to LuaSnip #1299

Open
Toenns opened this issue Feb 16, 2025 · 1 comment
Open

[Question] How to translate these Snippets from UltiSnip to LuaSnip #1299

Toenns opened this issue Feb 16, 2025 · 1 comment

Comments

@Toenns
Copy link

Toenns commented Feb 16, 2025

Heyo!!

I am about to transfer an UltiSnip snippet to a LuaSnip snippet, also with some python code inside but i don't really understand how to tranlate them. The Snippet is written for LaTeX.
I will give the snippets numbers (*), *=1,2,3,...,13 so that, if you know how to translate a snippet, that you could please give the number and translate it or explain me if and how it would be equivalent implemented in LuaSnip. The more I understand it the better. xD

Many thanks in advance :)

When its finished, i will upload it to my GitHub if you are interested. The OG Version if from Gilles Castels GitHub who explains his snippets on his Website quite well.
The Doc of UltiSnips

(1)
snippet "([a-zA-Z])bar" "bar" riA
\overline{`!p snip.rv=match.group(1)`}
endsnippet

(2)
snippet "([a-zA-Z])hat" "hat" riA
\hat{`!p snip.rv=match.group(1)`}
endsnippet

(3)
snippet '(?<!i)sts' "text subscript" irA
_\text{$1} $0
endsnippet

(4)
snippet '(?<!\\)(arcsin|arccos|arctan|arccot|arccsc|arcsec|pi|zeta|int)' "ln" rwA
\\`!p snip.rv = match.group(1)`
endsnippet

(5)
snippet '(?<!\\)(sin|cos|arccot|cot|csc|ln|log|exp|star|perp)' "ln" rwA
\\`!p snip.rv = match.group(1)`
endsnippet

(6)
snippet nn "Tikz node" w
\node[$5] (${1/[^0-9a-zA-Z]//g}${2}) ${3:at (${4:0,0}) }{$${1}$};
$0
endsnippet

(7)
snippet rij "mrij" i
(${1:x}_${2:n})_{${3:$2}\\in${4:\\N}}$0
endsnippet

(8)
snippet 'math(.*)math' "math" wr
`!p
import subprocess
code = match.group(1)
code = 'ToString[' + code + ', TeXForm]'
snip.rv = subprocess.check_output(['wolframscript', '-code', code])
`
endsnippet

(9)
snippet 'sympy(.*)sympy' "sympy" wr
`!p
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing()
snip.rv = eval('latex(' + match.group(1).replace('\\', '').replace('^', '**').replace('{', '(').replace('}', ')') + ')')
`
endsnippet

(10)
snippet '([A-Za-z])_(\d\d)' "auto subscript2" wrA
`!p snip.rv = match.group(1)`_{`!p snip.rv = match.group(2)`}
endsnippet

(11)
snippet '([A-Za-z])(\d)' "auto subscript" wrA
`!p snip.rv = match.group(1)`_`!p snip.rv = match.group(2)`
endsnippet

(12)
snippet '^.*\)/' "() frac" wrA
`!p
stripped = match.string[:-1]
depth = 0
i = len(stripped) - 1
while True:
	if stripped[i] == ')': depth += 1
	if stripped[i] == '(': depth -= 1
	if depth == 0: break;
	i-=1
snip.rv = stripped[0:i] + "\\frac{" + stripped[i+1:-1] + "}"
`{$1}$0
endsnippet

(13)
snippet '((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/' "symbol frac" wrA
\\frac{`!p snip.rv = match.group(1)`}{$1}$0
endsnippet

@evesdropper
Copy link
Contributor

There's a repo that already does this: https://github.com/iurimateus/luasnip-latex-snippets.nvim

I can also take some that I have translated myself, e.g. (12) and (13) the fraction snippet. Not going to go into a lot of detail as there are guides online, including some I've written. The key is that the snip.rv value is replaced by a dynamic node that generates the text based on the Lua equivalent of the Python function in the first one takes the capture and returns a list of nodes. As for the second one, LuaSnip's equivalent of match.group(n) is snip.captures[n], and you can write a function node as I have done to return the capture.

Note also both of these use more complex regex than Lua patterns; thus I've updated the value regTrig = true for regex trigger and trigEngine = ecma for ecma regex, which is similar to Python's regex.

-- condition from another file
function M.in_math()
    return vim.api.nvim_eval("vimtex#syntax#in_mathzone()") == 1
end

-- fractions (parentheses case)
local generate_fraction = function (_, snip)
    local stripped = snip.captures[1]
    local depth = 0
    local j = #stripped
    while true do
        local c = stripped:sub(j, j)
        if c == "(" then
            depth = depth + 1
        elseif c == ")" then
            depth = depth - 1
        end
        if depth == 0 then
            break
        end
        j = j - 1
    end
    return sn(nil,
        fmta([[
        <>\frac{<>}{<>}
        ]],
        { t(stripped:sub(1, j-1)), t(stripped:sub(j+1, -2)), i(1)}))
end

return {
    -- (12) in LuaSnip
    autosnippet({ trig='(^.*\\))/', name='fraction', dscr='auto fraction 2', trigEngine="ecma" },
    { d(1, generate_fraction) },
    { condition=tex.in_math, show_condition=tex.in_math })
    -- (13) in LuaSnip
    autosnippet({ trig="((\\d+)|(\\d*)(\\\\)?([A-Za-z]+)((\\^|_)(\\{\\d+\\}|\\d))*)\\/", name='fraction', dscr='auto fraction 1', trigEngine="ecma"},
    fmta([[
    \frac{<>}{<>}<>
    ]],
    { f(function (_, snip)
        return snip.captures[1]
    end), i(1), i(0) }),
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants