quangnguyen30192 / cmp-nvim-ultisnips

nvim-cmp source for ultisnips
Apache License 2.0
144 stars 19 forks source link

Feat: add support for custom context snippets #72

Closed smjonas closed 2 years ago

smjonas commented 2 years ago

For example the snippet frac

global !p
def math():
    return vim.eval('vimtex#syntax#in_mathzone()') == '1'
endglobal

context "math()"
snippet frac "Fraction"
\\frac{$1}{$2}$0
endsnippet

will now be shown by cmp when the user is in math mode in a .tex file.

smjonas commented 2 years ago

@quangnguyen30192 @jasonkena could one of you quickly test this branch? I tested it better myself this time and it seems to be working fine but I just want to be sure before merging :)

quangnguyen30192 commented 2 years ago

How do I test it 😅 could you provide me the steps?

smjonas commented 2 years ago

Yes sure :sweat_smile: One way to test it is to install gillescastel/latex-snippets https://github.com/gillescastel/latex-snippets and lervag/vimtex because it uses custom context snippets. Then in a math environment (that is inbetween two $...$) type sq, the sqrt snippet should be shown. Once outside the $ the snippet should not be shown anymore. Previously it was not shown at all.

Or you could simply create the following snippet file (e.g. to test it for a Lua file :UltiSnipsEdit lua) then type frac inside $$:

global !p
def math():
    return vim.eval('vimtex#syntax#in_mathzone()') == '1'
endglobal

context "math()"
snippet frac "Fraction"
\\frac{$1}{$2}$0
endsnippet
quangnguyen30192 commented 2 years ago

Thanks, tested, LGTM

jasonkena commented 2 years ago

@smjonas thank you for your work! Unfortunately, the expansions also work outside the math contexts, the "iA" (auto-trigger) flags on the snippets do not work, and complex pattern matching fails e.g.

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

Thank you for the feedback, I will check it!

Edit: @jasonkena I could not reproduce your first point, the snippet you provided won't expand in non-math contexts for me. I'll see if I can do something about your second point - by not working you simply mean the snippet is not shown, right? (at least, that's how it behaves for me; this is also the case on the latest commit on the main branch). Or is it now not expanded at all when typing ihat? For me it still expands in the correct context.

So I think this could be improved in a follow-up PR, I'd suggest to merge this first because it does not seem to break any existing functionality. Of course, let me know if that's not the case!

jasonkena commented 2 years ago

@smjonas hmm... it's still broken for me. Could you please send a minimal vimrc/luarc?