ejmastnak / dotfiles

ejmastnak's dotfiles
91 stars 13 forks source link

Questions: use ultisnips with luasnips in neovim #2

Closed ertwro closed 1 year ago

ertwro commented 1 year ago

Hi. First of all thanks for your guide on using snippets with neovim. I’m not a programmer am a physician and I’m slowly migrating my snippets to lua but the reason I started was to do what Mr. Castell was doing when writing math. Is there a way to use his ultisnips with neovim? Migrating the more than 100 math snippets will take a while and time is not something I have a lot of. Thanks for everything.

ejmastnak commented 1 year ago

Hi! Please clarify a few things to make sure we understand each other:

ertwro commented 1 year ago

Ok. Yes, I'm using Neovim 0.8 I used to have Vim and used UltiSnips. Now I'm full time on Neovim. I'm trying to switch to Luasnip since is what Neovim uses. The format I would like is the native Luasnip. So far I've tried a couple things like a converter that outputs in the JSON format (which works fine for most simple snippets) or writing new entries myself in Lua based on the examples you provided in your guide. Where am stuck is in translating complex snippets that use python code to Lua. E.g.

priority 1000
context "math()"
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

And I've tried turning it into something like this, which is most likely complete nonesense.

s(
    { trig = "([^.*])%/", dscr="() frac", wordTrig = false, regTrig = true, snippetType = "autosnippet" },
    fmta(
      f(function(_, snip)
        !p local stripped = snip.captures[1,-2)]
    local depth = 0 local i = #stripped 
    while true do
      if stripped:sub(i,i) == ')' 
    then 
      depth = depth + 1 
    end     
    if stripped:sub(i,i) == '(' 
      then depth = depth - 1 end    
    if depth == 0 
      then break 
    end;
    i=i-1 
    end 
      end),
      d(1, get_visual),
      i(1, 'snip.rv = stripped:sub(1,i) .. "\\frac{" .. stripped:sub(i+2,-2) .. "}" {$1}$0)',
    }),
    { condition = tex.in_mathzone }
}
ejmastnak commented 1 year ago

Okay I understand now, thanks for taking the time to clarify things! Two comments:

  1. You probably know this, but it should be perfectly fine to use UltiSnips with Neovim. I personally used UltiSnips with Neovim for about 6 months without problems (have you tried Ultisnips/Neovim and run into problems? Maybe you had a different experience...)
  2. I don't know of an automated way to translate complex UltiSnips snippets to Lua. Gilles Castel's snippets are complex, with a lot of custom logic, and I think, if you were translating them to Luasnip's native Lua syntax, you would have to do so by hand on a case-by-case basis.

I wish I could be more helpful, but I'd suggest either using UltiSnips with Neovim (if you want to keep using Castel's snippets) or, if you are want to use Luasnip, to write snippets by hand in Lua (probably start with something simpler than Castel's).

ertwro commented 1 year ago

Thank You very Much. Your're right I need to start simple I've been following Tj DevRies series and others to learn. Fortunately I found someone who translated the hardest of the functions in Castel's snippets. I'm sharing it with you so maybe you can find them useful too. "iurimateus/luasnip-latex-snippets.nvim"

I was very sad to know of Mr. Castel's passing. He was so Young. Is a great loss.

ejmastnak commented 1 year ago

Thanks for the updates and sorry for the late reply! Closing this issue, as it seems we have reached a resolution.