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

Feat: add support for custom context snippets #72

Merged
merged 4 commits into from
Apr 15, 2022

Conversation

smjonas
Copy link
Collaborator

@smjonas smjonas commented Mar 19, 2022

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 smjonas requested review from quangnguyen30192 and removed request for quangnguyen30192 March 19, 2022 13:40
@smjonas smjonas marked this pull request as draft March 20, 2022 09:27
@smjonas smjonas force-pushed the custom_context_snippets branch from 54801c2 to c4e5f45 Compare April 12, 2022 12:48
@smjonas smjonas force-pushed the custom_context_snippets branch from bbf066c to bca1438 Compare April 12, 2022 13:54
@smjonas
Copy link
Collaborator Author

smjonas commented Apr 12, 2022

@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 :)

@smjonas smjonas marked this pull request as ready for review April 12, 2022 13:57
@quangnguyen30192
Copy link
Owner

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

@smjonas
Copy link
Collaborator Author

smjonas commented Apr 12, 2022

Yes sure 😅 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
Copy link
Owner

Thanks, tested, LGTM

@jasonkena
Copy link

jasonkena commented Apr 12, 2022

@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
Copy link
Collaborator Author

smjonas commented Apr 12, 2022

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!

@smjonas smjonas merged commit c6ace8c into quangnguyen30192:main Apr 15, 2022
@smjonas smjonas deleted the custom_context_snippets branch April 15, 2022 08:48
@jasonkena
Copy link

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

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

Successfully merging this pull request may close these issues.

3 participants