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

Support for regex-based triggers (LuaSnip) #1373

Open
helins opened this issue Mar 2, 2025 · 9 comments
Open

Support for regex-based triggers (LuaSnip) #1373

helins opened this issue Mar 2, 2025 · 9 comments
Labels
feature New feature or request

Comments

@helins
Copy link
Contributor

helins commented Mar 2, 2025

Feature Description

I have some LuaSnip snippets triggered by regexes.
E.g. Insert a nice full date when I type something like 20250302.

Those triggers:

  • Are never suggested with auto_show
  • Do appear when the menu is open manually on an empty cursor
    • Displayed by their pattern (trig) rather than their name
    • Inserting them inserts their trig
      • For context
      • Insertion would not make sense
      • They should be matched against typed keywords
Image
@helins helins added the feature New feature or request label Mar 2, 2025
@Saghen
Copy link
Owner

Saghen commented Mar 2, 2025

This won't be supported in the core but you could detect these yourself and call require('blink.cmp').show()

@Saghen Saghen closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2025
@helins
Copy link
Contributor Author

helins commented Mar 2, 2025

I don't think that's it.
Let me clarify my explanation, suppose those scenarios:

  • Empty cursor in insert mode
    • Show the menu
    • Can see the regex indeed
    • Accepting it inserts the actual regex
      • Useless and confusing
      • Should definitely not be part of core behavior
  • Type something that matches the regex
    • Show the menu
    • Regex not appearing
    • Therefore cannot be accepted via Blink.cmp

@Saghen Saghen reopened this Mar 2, 2025
@Saghen
Copy link
Owner

Saghen commented Mar 2, 2025

Aha, sorry I read this too quickly and missed that they were being filtered out. Related #1375

@helins
Copy link
Contributor Author

helins commented Mar 3, 2025

Alright, after updating to that super fresh PR and playing around, the problem is simple (if my understanding is correct).

  1. By default, regex specifics like %d are not considered valid for a keyword, therefore cannot be searched. E.g. my example above with only %d is unsearchable as such.

  2. Arguably, searching by raw regex is not user friendly. Users should remember what to input, not how the actual regex for the match looks like. LuaSnip makes a distinction between trig (the pattern, what is being currently displayed in the menu) and name (which can be human-friendly). I would argue that the menu should display names rather than trigs. That is really the only thing I would change.

Any opinions, @soifou (thanks by the way)?

@soifou
Copy link
Collaborator

soifou commented Mar 3, 2025

I think it totally makes sense! Could you share your "nice full date" regex snippet if you don't mind? I'd like to toy around with a reasonable set of snippets to make sure it works as expected before making a new PR.

@helins
Copy link
Contributor Author

helins commented Mar 3, 2025

A bit tricky since written in Fennel and calling into my private lib, but to give you an idea:

(LS/s {:trig    "(%d%d%d%d)(%d%d)(%d%d)"
       :name    "date_link"
       :doctrig "20240312"
       :desc    "Inserts Markdown link for the given date"
       :regTrig true}
      (LS/f (fn [_args {: captures}]
              ($/zet/link.date (numeric-capture captures
                                                1)
                               (numeric-capture captures
                                                2)
                               (numeric-capture captures
                                                3)))
            {}))

It captures the year + month + day and calls a function for generating a nice Markdown link to a local "daily note".

So to recap the above thinking, the idea is that it would be more intuitive to search for that snippet using its name. This behavior would apply to all snippets with regTrig set to true).

A another problem with the current pattern-based approach is that the completion menu disappears as digits are typed. This makes sense if you know that those patterns are not incremental. But from a UI perspective, it does not feel intuitive that the menu disappears as you type something that is actually valid. Sadly, that cannot be solved without a signifcant headache. Therefore, after sleeping on it, I keep returning to this idea of using name.

soifou added a commit to soifou/blink.cmp that referenced this issue Mar 3, 2025
This addresses an issue where snippets composed only by lua regex
patterns make them barely searchable in the completion menu.

Luasnip provides a `name` property that, when available, display the
snippet's `name` instead of the raw regex `trigger` pattern, making
regex triggers discoverable and user-friendly.

Related to Saghen#1373

Co-authored-by: helins <[email protected]>
Saghen pushed a commit that referenced this issue Mar 3, 2025
)

This addresses an issue where snippets composed only by lua regex
patterns make them barely searchable in the completion menu.

Luasnip provides a `name` property that, when available, display the
snippet's `name` instead of the raw regex `trigger` pattern, making
regex triggers discoverable and user-friendly.

Related to #1373

Co-authored-by: helins <[email protected]>
@soifou
Copy link
Collaborator

soifou commented Mar 3, 2025

Thank you for the detail answer!

Did some tests today and think the name over regex approach work very well. Could you try the latest revision and tell us if it resolves mainly your issue?

@helins
Copy link
Contributor Author

helins commented Mar 4, 2025

Thanks for the prompt implementation!
I've just tried it and it is much better IMO.

Maybe one more thing, more controversial. When accepting a selection, instead of inserting trig as a cue, what about prefering doctrig (when available)? Once again for the sake of human readability.

In the example above, (%d%d%d%d)(%d%d)(%d%d) is not much of a reminder. But 20250303 would be.

soifou added a commit to soifou/blink.cmp that referenced this issue Mar 8, 2025
Improves readability of expanded luasnip regex trigger by using
`docTrig` as a placeholder (when available) instead of the less
intuitive regex pattern.

For example, expanding a snippet with the trigger `(%d%d%d%d)(%d%d)(%d%d)`
would insert `20250303` rather than the regex itself.

Fix Saghen#1373
@soifou
Copy link
Collaborator

soifou commented Mar 8, 2025

@helins could you try my fork using the branch feat/luasnip-regex-doctrig with the following option and let me know if it works for you? I'll create a PR if everything looks good.

sources = {
    providers = {
        snippets = {
            --- @type blink.cmp.LuasnipSourceOptions
            opts = {
                prefer_doc_trig = true
            },
        },
    },
},

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

No branches or pull requests

3 participants