coq_nvim icon indicating copy to clipboard operation
coq_nvim copied to clipboard

Auto pair plugin

Open SamuelTJackson opened this issue 4 years ago • 14 comments

Hey, is there an auto pair plugin that works with coq? I tried:

windwp/nvim-autopairs
steelsojka/pears.nvim
jiangmiao/auto-pairs

So I get the following behavior?

Before: (pressed Enter at |)
{|}
Expected:
{
    |
}

SamuelTJackson avatar Aug 19 '21 16:08 SamuelTJackson

idk how they are implemented, but I don't think coq will support third party auto pairs. for both performance and maintenance point of view, i would much rather to put this in the coq itself.

I havnt used any autopair plugins so I don't actually know whats a good way to do them, ill need to think about it.

ms-jpq avatar Aug 19 '21 17:08 ms-jpq

Using windwp/nvim-autopairs:

local remap = vim.api.nvim_set_keymap
local npairs = require('nvim-autopairs')

npairs.setup({ map_bs = false })

vim.g.coq_settings = { keymap = { recommended = false } }

-- these mappings are coq recommended mappings unrelated to nvim-autopairs
remap('i', '<esc>', [[pumvisible() ? "<c-e><esc>" : "<esc>"]], { expr = true, noremap = true })
remap('i', '<c-c>', [[pumvisible() ? "<c-e><c-c>" : "<c-c>"]], { expr = true, noremap = true })
remap('i', '<tab>', [[pumvisible() ? "<c-n>" : "<tab>"]], { expr = true, noremap = true })
remap('i', '<s-tab>', [[pumvisible() ? "<c-p>" : "<bs>"]], { expr = true, noremap = true })

-- skip it, if you use another global object
_G.MUtils= {}

MUtils.CR = function()
  if vim.fn.pumvisible() ~= 0 then
    if vim.fn.complete_info({ 'selected' }).selected ~= -1 then
      return npairs.esc('<c-y>')
    else
      return npairs.esc('<c-e>') .. npairs.autopairs_cr()
    end
  else
    return npairs.autopairs_cr()
  end
end
remap('i', '<cr>', 'v:lua.MUtils.CR()', { expr = true, noremap = true })

MUtils.BS = function()
  if vim.fn.pumvisible() ~= 0 and vim.fn.complete_info({ 'mode' }).mode == 'eval' then
    return npairs.esc('<c-e>') .. npairs.autopairs_bs()
  else
    return npairs.autopairs_bs()
  end
end
remap('i', '<bs>', 'v:lua.MUtils.BS()', { expr = true, noremap = true })

zeertzjq avatar Aug 19 '21 22:08 zeertzjq

Thanks @zeertzjq ! A lot of people will run into this so it might be worth adding to the docs

lynndylanhurley avatar Aug 20 '21 17:08 lynndylanhurley

@zeertzjq how to escape completion and still in insert mode. I found it annoying whenever I presss esc, it goes to normal mode.

bangedorrunt avatar Aug 22 '21 04:08 bangedorrunt

@zeertzjq how to escape completion and still in insert mode. I found it annoying whenever I presss esc, it goes to normal mode.

Use CTRL-E, or map <Esc> to pumvisible() ? "\<C-e>" : "\<Esc>" instead.

zeertzjq avatar Aug 22 '21 04:08 zeertzjq

@zeertzjq that fixed it!

bangedorrunt avatar Aug 22 '21 05:08 bangedorrunt

autopair plugins

Looking forward to coq_nvim support autopair

dppo avatar Aug 26 '21 10:08 dppo

@SamuelTJackson
Auto-pair is a syntax feature of every language and thus should be provided by treesitter or the lsp. If you want something scaling, it must also be supported by ctags.

Personally I think supporting ctags and treesitter makes more sense, since the lsp is not needed for creating brackets, unless you want to move them (but I cant think of many useful cases on doing that).

matu3ba avatar Aug 31 '21 11:08 matu3ba

@zeertzjq This conflicts with COQ's own configuration. It seems to be because nvim-autopairs's utility functions do not replace keycodes. Also, if I may, I'd like to suggest writing an auto-pair script using NeoVim's new vim.keymap API, which accepts Lua functions directly.

Here's how i did it. Just call the function to setup the auto-pairing.

_G.PairIT = function()
  local autopairs = {
    ['('] = ')',
    ['['] = ']',
    ['{'] = '}',
    ['<'] = '>',
    [ [=[']=] ] = [=[']=],
    [ [=["]=] ] = [=["]=],
  }
  local set_pairs = vim.keymap.set
  for k, v in pairs(autopairs) do
    set_pairs('i', k, function()
      return k .. v .. '<Left>'
    end, { expr = true, noremap = true })
  end
end

kaiuri avatar Mar 26 '22 14:03 kaiuri

@matu3ba the way tree-sitter-cli works doesn't leave a lot of room for parsing pairs as separate entities, it's possible, but most parsers don't go that far. Ideally NeoVim should switch from Tree-sitter to Syntect, which is the one used by Bat. It's also Rust built and encompasses all of SublimeText's grammars, almost as fast as bare regex parsing, and as so explicitly defines pairs as entities allowing for easy pair completion and deletion, through built-in features or external. It's my personal opinion that by being too modular, NeoVim setups tend to get a little bloated overtime, but that is unrelated.

@SamuelTJackson Auto-pair is a syntax feature of every language and thus should be provided by treesitter or the lsp. If you want something scaling, it must also be supported by ctags.

Personally I think supporting ctags and treesitter makes more sense, since the lsp is not needed for creating brackets, unless you want to move them (but I cant think of many useful cases on doing that).

kaiuri avatar Mar 26 '22 15:03 kaiuri

@zeertzjq This conflicts with COQ's own configuration. It seems to be because nvim-autopairs's utility functions do not replace keycodes. Also, if I may, I'd like to suggest writing an auto-pair script using NeoVim's new vim.keymap API, which accepts Lua functions directly.

Here's how i did it. Just call the function to setup the auto-pairing.

_G.PairIT = function()
  local autopairs = {
    ['('] = ')',
    ['['] = ']',
    ['{'] = '}',
    ['<'] = '>',
    [ [=[']=] ] = [=[']=],
    [ [=["]=] ] = [=["]=],
  }
  local set_pairs = vim.keymap.set
  for k, v in pairs(autopairs) do
    set_pairs('i', k, function()
      return k .. v .. '<Left>'
    end, { expr = true, noremap = true })
  end
end

How would I actually incorporate this? I've tried the one by @zeertzjq but it doesn't work. Also tried the one on the nvim-autoparis README also doesn't work. Nvim noobie here btw, hope to learn much.

Edit: Got the indentation working now, still interested how to incorporate your code.

copypasteonly avatar Nov 14 '22 04:11 copypasteonly

@copypasteonly I'd need to see where you want to incorporate it.

kaiuri avatar Nov 15 '22 15:11 kaiuri

@zeertzjq This conflicts with COQ's own configuration. It seems to be because nvim-autopairs's utility functions do not replace keycodes. Also, if I may, I'd like to suggest writing an auto-pair script using NeoVim's new vim.keymap API, which accepts Lua functions directly.

Here's how i did it. Just call the function to setup the auto-pairing.

_G.PairIT = function()
  local autopairs = {
    ['('] = ')',
    ['['] = ']',
    ['{'] = '}',
    ['<'] = '>',
    [ [=[']=] ] = [=[']=],
    [ [=["]=] ] = [=["]=],
  }
  local set_pairs = vim.keymap.set
  for k, v in pairs(autopairs) do
    set_pairs('i', k, function()
      return k .. v .. '<Left>'
    end, { expr = true, noremap = true })
  end
end

Doesn't work great, it pairs but it doesn't insert a newline when pressing <cr> and also you can't exit ending pair by pressing the same ending pair character. You've been mentioning your work to others but doesn't work really great (I've seen you mentioning the script on multiple issues). Just answer with an actual solution instead of promoting your script.

issadarkthing avatar Oct 05 '23 09:10 issadarkthing

@issadarkthing, the features you've mentioned require a more involved solution, e.g.

kaiuri avatar Oct 05 '23 19:10 kaiuri