echasnovski / mini.nvim

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

mini.pairs: python f-strings #627

Closed barrett-ruth closed 8 months ago

barrett-ruth commented 8 months ago

Contributing guidelines

Module(s)

mini.pairs

Description

Support auto pairs for quotes ("'", '"') when typing python f-strings.

Neovim version

0.10.1

Steps to reproduce

  1. nvim -nu minimal.lua
  2. Edit python file
  3. Type the following characters: print(f'

Expected behavior

The quote is autopaired after the f-string.

Actual behavior

No auto-pairing is done.

echasnovski commented 8 months ago

In 'mini.pairs' the neighborhood pattern is responsible to decide when inserted character expands into full pair. By default, ' has it such that single ' is inserted after any English letter (to be usable inside plain English text) and \. This part won't change.

The most straightforward approach to solve this issue is to update neighborhood pattern for ' only in Python files. For that, create an 'after/ftplugin/python.lua' file inside your config directory and put there these lines:

if MiniPairs ~= nil then
  MiniPairs.map_buf(0, 'i', "'", { action = 'closeopen', pair = "''", neigh_pattern = '[^\\].' })
end

This will create only a buffer-local mapping which will insert single ' only after \. The downside is that it will insert double '' after any letter also.

Closing as not planned.

barrett-ruth commented 8 months ago

Thank you for the quick response. I'm assuming I'm able to alter the regex to ignore solely f?

barrett-ruth commented 8 months ago

For anyone looking, the above solution didn't work or wasn't satisfactory, so I added the following luasnip to my python files:

    s('f', fmt('print(f{}{}{})', { c(1, { t("'"), t('"') }), i(2), rep(1) })),