echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.74k stars 179 forks source link

Customizing mini.pairs behavior to prevent unwanted parenthesis closure #837

Closed mguellsegarra closed 4 months ago

mguellsegarra commented 4 months ago

Background

First, I would like to express my appreciation for your work on the mini.nvim plugin — it's been a fantastic tool in my development setup. I have a question regarding a specific use case with mini.pairs that I hope you can help me with.

Query

I'm attempting to achieve more context-sensitive behavior with the auto-pair functionality, similar to how VSCode, IntelliJ, and other editors handle auto-pairing, especially in scenarios involving JavaScript arrow functions. Specifically, I'm looking to prevent the insertion of an unwanted closing parenthesis in situations like typing an opening parenthesis before () =>. The desired outcome is for it to result in (() => rather than ()() =>.

Actual default behaviour

Before Character inserted After
\|() => { } ( (\|)() => { }

Desired behaviour

Before Character inserted After
\|() => { } ( (\|() => { }

I've experimented with various neigh_pattern configurations and other adjustments to address this issue, but so far, I haven't managed to achieve the desired behavior. I'm at a bit of a loss here. Could anyone offer some guidance or assistance?

Thank you all!

echasnovski commented 4 months ago

Sure, this can be achieved by a proper neighborhood pattern. The exact pattern depends on how much characters you want to prevent ( to stop inserting the whole pair. Here is one example:

require('mini.pairs').setup({
  mappings = {
    ['('] = { action = 'open', pair = '()', neigh_pattern = '[^\\][^(]' },
    ['['] = { action = 'open', pair = '[]', neigh_pattern = '[^\\][^[]' },
    ['{'] = { action = 'open', pair = '{}', neigh_pattern = '[^\\][^{]' },
  },
})

For "(" this actually reads as "insert whole pair if left character is not \ and if right character is not (". Similar to other opening characters.

Another approach is to take a more broad route of #835. It would be "insert whole pair if left character is not \ and if right character is whitespace". So it will not insert whole pair if directly before any non-whitespace character.

Hope it helps.

mguellsegarra commented 4 months ago

What a coincidence that just a few hours ago issue #835 was opened, addressing the same topic. Thank you once again, Evgeni, for all your work and for your quick response. I definitely love this community.