GithubHelp home page GithubHelp logo

windwp / nvim-autopairs Goto Github PK

View Code? Open in Web Editor NEW
2.9K 13.0 110.0 852 KB

autopairs for neovim written in lua

License: MIT License

Lua 99.17% Makefile 0.22% Ruby 0.02% JavaScript 0.02% Rust 0.02% Vim Script 0.55%
neovim nvim lua autopairs vim

nvim-autopairs's Introduction

nvim-autopairs

A super powerful autopair plugin for Neovim that supports multiple characters.

Requires neovim 0.7

Installation

Install the plugin with your preferred package manager:

{
    'windwp/nvim-autopairs',
    event = "InsertEnter",
    config = true
    -- use opts = {} for passing setup options
    -- this is equalent to setup({}) function
}
Plug 'windwp/nvim-autopairs'

lua << EOF
require("nvim-autopairs").setup {}
EOF
use {
    "windwp/nvim-autopairs",
    event = "InsertEnter",
    config = function()
        require("nvim-autopairs").setup {}
    end
}

Default values

{
    disable_filetype = { "TelescopePrompt", "spectre_panel" }
    disable_in_macro = true  -- disable when recording or executing a macro
    disable_in_visualblock = false -- disable when insert after visual block mode
    disable_in_replace_mode = true
    ignored_next_char = [=[[%w%%%'%[%"%.%`%$]]=]
    enable_moveright = true
    enable_afterquote = true  -- add bracket pairs after quote
    enable_check_bracket_line = true  --- check bracket in same line
    enable_bracket_in_quote = true --
    enable_abbr = false -- trigger abbreviation
    break_undo = true -- switch for basic rule break undo sequence
    check_ts = false
    map_cr = true
    map_bs = true  -- map the <BS> key
    map_c_h = false  -- Map the <C-h> key to delete a pair
    map_c_w = false -- map <c-w> to delete a pair if possible
}

Override default values

require('nvim-autopairs').setup({
  disable_filetype = { "TelescopePrompt" , "vim" },
})

Mapping <CR>

Before        Input         After
------------------------------------
{|}           <CR>          {
                                |
                            }
------------------------------------
nvim-cmp

You need to add mapping `CR` on nvim-cmp setup. Check readme.md on nvim-cmp repo.

-- If you want insert `(` after select function or method item
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
local cmp = require('cmp')
cmp.event:on(
  'confirm_done',
  cmp_autopairs.on_confirm_done()
)

You can customize the kind of completion to add ( or any character.

local handlers = require('nvim-autopairs.completion.handlers')

cmp.event:on(
  'confirm_done',
  cmp_autopairs.on_confirm_done({
    filetypes = {
      -- "*" is a alias to all filetypes
      ["*"] = {
        ["("] = {
          kind = {
            cmp.lsp.CompletionItemKind.Function,
            cmp.lsp.CompletionItemKind.Method,
          },
          handler = handlers["*"]
        }
      },
      lua = {
        ["("] = {
          kind = {
            cmp.lsp.CompletionItemKind.Function,
            cmp.lsp.CompletionItemKind.Method
          },
          ---@param char string
          ---@param item table item completion
          ---@param bufnr number buffer number
          ---@param rules table
          ---@param commit_character table<string>
          handler = function(char, item, bufnr, rules, commit_character)
            -- Your handler function. Inspect with print(vim.inspect{char, item, bufnr, rules, commit_character})
          end
        }
      },
      -- Disable for tex
      tex = false
    }
  })
)

Don't use nil to disable a filetype. If a filetype is nil then * is used as fallback.

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

npairs.setup({ map_bs = false, map_cr = 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 })
without completion plugin
-- add option map_cr
npairs.setup({ map_cr = true })

another completion plugin

If you have a problem with indent after you press <CR> please check the settings of treesitter indent or install a plugin that has indent support for your filetype.

Rule

nvim-autopairs uses rules with conditions to check pairs.

local Rule = require('nvim-autopairs.rule')
local npairs = require('nvim-autopairs')

npairs.add_rule(Rule("$$","$$","tex"))

-- you can use some built-in conditions

local cond = require('nvim-autopairs.conds')
print(vim.inspect(cond))

npairs.add_rules({
  Rule("$", "$",{"tex", "latex"})
    -- don't add a pair if the next character is %
    :with_pair(cond.not_after_regex("%%"))
    -- don't add a pair if  the previous character is xxx
    :with_pair(cond.not_before_regex("xxx", 3))
    -- don't move right when repeat character
    :with_move(cond.none())
    -- don't delete if the next character is xx
    :with_del(cond.not_after_regex("xx"))
    -- disable adding a newline when you press <cr>
    :with_cr(cond.none())
  },
  -- disable for .vim files, but it work for another filetypes
  Rule("a","a","-vim")
)

npairs.add_rules({
  Rule("$$","$$","tex")
    :with_pair(function(opts)
        print(vim.inspect(opts))
        if opts.line=="aa $$" then
        -- don't add pair on that line
          return false
        end
    end)
   }
)

-- you can use regex
-- press u1234 => u1234number
npairs.add_rules({
    Rule("u%d%d%d%d$", "number", "lua")
      :use_regex(true)
})



-- press x1234 => x12341234
npairs.add_rules({
    Rule("x%d%d%d%d$", "number", "lua")
      :use_regex(true)
      :replace_endpair(function(opts)
          -- print(vim.inspect(opts))
          return opts.prev_char:sub(#opts.prev_char - 3,#opts.prev_char)
      end)
})


-- you can do anything with regex +special key
-- example press tab to uppercase text:
-- press b1234s<tab> => B1234S1234S

npairs.add_rules({
  Rule("b%d%d%d%d%w$", "", "vim")
    :use_regex(true,"<tab>")
    :replace_endpair(function(opts)
          return
              opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char)
              .."<esc>viwU"
    end)
})

-- you can exclude filetypes
npairs.add_rule(
  Rule("$$","$$")
    :with_pair(cond.not_filetypes({"lua"}))
)
--- check ./lua/nvim-autopairs/rules/basic.lua

Rules API

Treesitter

You can use treesitter to check for a pair.

local npairs = require("nvim-autopairs")
local Rule = require('nvim-autopairs.rule')

npairs.setup({
    check_ts = true,
    ts_config = {
        lua = {'string'},-- it will not add a pair on that treesitter node
        javascript = {'template_string'},
        java = false,-- don't check treesitter on java
    }
})

local ts_conds = require('nvim-autopairs.ts-conds')


-- press % => %% only while inside a comment or string
npairs.add_rules({
  Rule("%", "%", "lua")
    :with_pair(ts_conds.is_ts_node({'string','comment'})),
  Rule("$", "$", "lua")
    :with_pair(ts_conds.is_not_ts_node({'function'}))
})

Don't add pairs if it already has a close pair in the same line

if next character is a close pair and it doesn't have an open pair in same line, then it will not add a close pair

Before        Input         After
------------------------------------
(  |))         (            (  (|))

require('nvim-autopairs').setup({
  enable_check_bracket_line = false
})

Don't add pairs if the next char is alphanumeric

You can customize how nvim-autopairs will behave if it encounters a specific character

require('nvim-autopairs').setup({
  ignored_next_char = "[%w%.]" -- will ignore alphanumeric and `.` symbol
})
Before        Input         After
------------------------------------
|foobar        (            (|foobar
|.foobar       (            (|.foobar

Plugin Integration

  require('nvim-autopairs').disable()
  require('nvim-autopairs').enable()
  require('nvim-autopairs').remove_rule('(') -- remove rule (
  require('nvim-autopairs').clear_rules() -- clear all rules
  require('nvim-autopairs').get_rules('"')
  • Sample
-- remove add single quote on filetype scheme or lisp
require("nvim-autopairs").get_rules("'")[1].not_filetypes = { "scheme", "lisp" }
require("nvim-autopairs").get_rules("'")[1]:with_pair(cond.not_after_text("["))

FastWrap

Before        Input                    After         Note
-----------------------------------------------------------------
(|foobar      <M-e> then press $       (|foobar)
(|)(foobar)   <M-e> then press q       (|(foobar))
(|foo bar     <M-e> then press qh      (|foo) bar
(|foo bar     <M-e> then press qH      (foo|) bar
(|foo bar     <M-e> then press qH      (foo)| bar    if cursor_pos_before = false
-- put this to setup function and press <a-e> to use fast_wrap
npairs.setup({
    fast_wrap = {},
})

-- change default fast_wrap
npairs.setup({
    fast_wrap = {
      map = '<M-e>',
      chars = { '{', '[', '(', '"', "'" },
      pattern = [=[[%'%"%>%]%)%}%,]]=],
      end_key = '$',
      before_key = 'h',
      after_key = 'l',
      cursor_pos_before = true,
      keys = 'qwertyuiopzxcvbnmasdfghjkl',
      manual_position = true,
      highlight = 'Search',
      highlight_grey='Comment'
    },
})

autotag html and tsx

autotag

Endwise

endwise

Custom rules

rules

Sponsors

Thanks to everyone who sponsors my projects and makes continued development maintenance possible!

nvim-autopairs's People

Contributors

adlrwbr avatar aspeddro avatar aym3nj avatar barrett-ruth avatar bew avatar breuerfelix avatar brokenbyte avatar chmanie avatar davawen avatar dmbfm avatar dsully avatar echasnovski avatar f1rstlady avatar felix-clark avatar github-actions[bot] avatar halftan avatar hexium310 avatar megalithic avatar n3wborn avatar nopsqi avatar ribru17 avatar sadiksaifi avatar sam-programs avatar shatur avatar tani avatar terr avatar unrealapex avatar windwp avatar yardnsm avatar zeertzjq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nvim-autopairs's Issues

Plugin failed to load with minium vimrc

A minium vimrc

call plug#begin('~/.vim/plugged')
Plug 'windwp/nvim-autopairs'
call plug#end()

lua <<EOF
-- require('nvim-autopairs').setup(). -- commented out because I want to load it when first time in insert mode
EOF

But if I run lua require('nvim-autopairs').setup() manually after nvim loaded, the plugin does not work correctly, there are no errors, but if I type (, it does not auto pair.
If I remove comments in this line: -- require('nvim-autopairs').setup() it works fine.
It also can not manually loaded with

packadd nvim-autopairs
lua require('nvim-autopairs').setup()

There are no errors, simply does not do the job

With packer, it also has some trouble to load if I use lazy loading and specify either keys or event to lazyloading the plugin.

Must refresh the buffer for multi-character pairs to function correctly

When trying to use multi-character pairs, such as ``` in markdown, out of the box the rules don't work at all, but when calling :lua require'nvim-autopairs'.on_attach() or simply :edit , then the the rules start to work correctly. This, however, isn't ideal, and I'm not really sure how it can be solved.

do not insert pairs when cursor is in the middle of a text

Hi there! I would love to be able to disable the insertion of matching pair when I'm in the middle of a word. For example:

require"some_text"|.foobar() -- | is the cursor, do not insert matching pair here

require"some_text" |.foobar() -- | is the cursor, do not insert matching pair here

require"some_text".foobar| -- | is the cursor, insert matching pair here

(|) -- | is the cursor, insert matching pair here, also applies to all matches like [] and {}

Basically, I don't want it to insert a matching pair if the right side of the cursor is not empty or it's not a symbol like what I've defined in the setup. ((){}[]). Thanks in advance!

edit: this seems to work already, but it still inserts a pair if the next character is a .

Alternative to closetag?

Reading the readme to me is not clear if support html as example.
Right now I use https://github.com/docunext/closetag.vim but I am not sure if this plugin can be an alternative (of course this one support also other things).

Maybe a language list of supported so I don't need to check all the rules available if are good for the one I use it?

Functionality to disable default pair in certain situations

I've read both documentation and some plugin code, but couldn't find the way to disable a default pair in certain situation.

For example, my use case is to disable adding second " inside '.vim' files when it is intended to be a comment string (for example, when there is only white space to the left of the first "). I even tried "more simple" approach to completely disable "-" pair in '.vim' files, but couldn't figure out a way to do that.

My first impression was to use this: npairs.add_rules({Rule('"', '', 'vim')}). It didn't work, although it works if I try to add new pair (for example, with npairs.add_rules({Rule('^', '^', 'vim')})).

Parentheses inserted two times with the latest update

Not sure if my config in at fault here but 43020b1 introduced weird behavior for me (with the latest config example applied)

my autopairs config looks like this right now:

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

npairs.setup({
  ignored_next_char = "[%w%.]",
  check_ts = true,
  ts_config = {
      lua = {'string'},-- it will not add pair on that treesitter node
      javascript = {'string'}
  }
})

require('nvim-treesitter.configs').setup {
    autopairs = {enable = true}
}

local ts_conds = require('nvim-autopairs.ts-conds')

-- press % => %% is only inside comment or string
npairs.add_rules({
  Rule("%", "%", "lua")
    :with_pair(ts_conds.is_ts_node({'string','comment'})),
  Rule("$", "$", "lua")
    :with_pair(ts_conds.is_not_ts_node({'function'}))
})

require("nvim-autopairs.completion.compe").setup({
  map_cr = true, --  map <CR> on insert mode
  map_complete = true -- it will auto insert `(` after select function or method item
})

while compe's looks like this:

local remap = vim.api.nvim_set_keymap

vim.opt.completeopt = { "menuone" , "noselect" } -- "noinsert"

require('compe').setup {
  enabled = true;
  autocomplete = true;
  debug = false;
  min_length = 1;
  preselect = 'enable';
  throttle_time = 80;
  source_timeout = 200;
  incomplete_delay = 400;
  max_abbr_width = 100;
  max_kind_width = 100;
  max_menu_width = 100;
  documentation = true;

  source = {
    path = true;
    buffer = true;
    calc = true;
    nvim_lsp = true;
    nvim_lua = true;
    vsnip = true;
  };
}

local t = function(str)
  return vim.api.nvim_replace_termcodes(str, true, true, true)
end

local check_back_space = function()
    local col = vim.fn.col('.') - 1
    if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
        return true
    else
        return false
    end
end

-- Use (s-)tab to:
--- move to prev/next item in completion menuone
--- jump to prev/next snippet's placeholder
_G.tab_complete = function()
  if vim.fn.pumvisible() == 1 then
    return t "<C-n>"
  elseif vim.fn.call("vsnip#available", {1}) == 1 then
    return t "<Plug>(vsnip-expand-or-jump)"
  elseif check_back_space() then
    return t "<Tab>"
  else
    return vim.fn['compe#complete']()
  end
end

_G.s_tab_complete = function()
  if vim.fn.pumvisible() == 1 then
    return t "<C-p>"
  elseif vim.fn.call("vsnip#jumpable", {-1}) == 1 then
    return t "<Plug>(vsnip-jump-prev)"
  else
    -- If <S-Tab> is not working in your terminal, change it to <C-h>
    return t "<S-Tab>"
  end
end

-- remap('i' , '<CR>','compe#confirm("<CR>")', {expr = true , noremap = true})

remap('i' , '<Tab>','v:lua.tab_complete()', {expr = true})
remap('s' , '<Tab>','v:lua.tab_complete()', {expr = true})
remap('i' , '<S-Tab>','v:lua.s_tab_complete()', {expr = true})
remap('s' , '<S-Tab>','v:lua.s_tab_complete()', {expr = true})

Now, after confirming a functon in compe's popup I have this kind of result:

foo((|))

In other words two pairs of parentheses are being inserted.

Did I understand the change incorrectly?

indent with autopairs

Have can I achieve indent with autopairs same to webstorm's one?

Peek.2021-06-11.15-32.mp4

Auto-closing quotes with common prefix

I do not consider this to be extra feature but rather improvement on base feature.

Currently this configuration:

require('nvim-autopairs').setup({
    pairs_map = {
        ['`'] = '`',
        ['```'] = '```',
    }
})

Does not work as expected (expected behavior: `| -> `|` , ```| -> ```|```).

auto brackets with autocomplete selection

I am using autopairs for the past one month when I moved by init.vim to init.lua. Its a great plugin. I am trying to move from Vvs-code to nvim
I am using it with nvim-compe for autocomplete. Is it possible to enable the autopairs automatically when I say select a function from my auto complete with the cursor in the middle of the brackets and not triggering of the auto brackets when say I have selected a module from my autocomplete.

I am attaching a gif to show what I mean.
● demo py - pivot - Visual Studio Code - Insiders 2021-06-21 15-09-37

feat: toggle to deactivate+activate bracket creation

Hey, nice plugin so far.

'b3nj5m1n/kommentary'
-- insert I
-- insert {
{}'b3nj5m1n/kommentary'
-- Thats bad, I wanted to set the bracket after I am done with configuring the plugin options

Can you make a toggle to deactivate the behavior?
Or how do you work with this plugin?
Being forced to delete the bracket is very unfortunate for usability.

enable break line rule for all filetypes

Is there a way to enable break line rule for all filetypes without having to list the filetypes manually? I'd love to be able to set something like break_line_filetype = { "*" } and it enables the rule for all filetypes. Thanks in advance!

Not working as intended with `|foobar` examples

Did this with minimal config, only calling autopairs with:
require('nvim-autopairs').setup()

Before Input Expected Actual
|foobar ( (|)foobar (|foobar
|.foobar ( (|).foobar (|.foobar
|+foobar ( (|)+foobar (|+foobar

Neovim version:
NVIM v0.5.0-dev+1367-g27c616d68 Build type: RelWithDebInfo LuaJIT 2.1.0-beta3

nvim-autopairs version:
b581620 (current)

Any typed text when pum is visible is deleted when typing an auto-paired character

If a user has a typed word, and the completion menu is also opened, pressing a paired character , such as '(' or '{' will delete the user's text, including the character, and instead insert the closing character from the pair.

For example, if the user has typed Upload| and the pum is visible, pressing ( would result in ). Instead, it should have resulted in Upload(|)

Skip Jumping to Next Bracket

Hello @windwp ,

Initially, thank you for maintaining so many beautiful plugins for neovim.

Is there a way to disable the jump mode with a flag that I am missing even though many of the community likes it?

Current behavior:

Before                  Input         After
------------------------------------
((someword|)         )               ((someword)|

The one I wish for:

Before                   Input         After
------------------------------------
((someword |)         )               ((someword)|)

paste broken since last two updates

I made simple mapping, so I could use ctrl+v in insert mode:

-- <C-r> - id doing paste from default copy register in insert mode
vim.api.nvim_set_keymap("i", "<C-v>", '<C-r>+', { noremap = true } ) --and disable 2MMB so no accidental paste..
vim.api.nvim_set_keymap("c", "<C-v>", '<C-r>+', { noremap = true } ) --and disable 2MMB so no accidental paste..
  

It worked ok some time ago but now this code:

config=function() require("nv-minimap")

becomes - after paste in insert mode with ctrl+v :

config=function) requirenv-minimap()()"""") 

The 'funny' thing is pasting in inset mode with my mapping work if there is no space char before cursor position. So if I paste inside word then everything works ok. For now I switched to this version: b8272f5 and problem is not preset (was introduced in latter commits)

add support pairs with more than 2 character

With this configuration:

require('nvim-autopairs').setup({
    pairs_map = {
        ["'"] = "'",
        ['"'] = '"',
        ['('] = ')',
        ['['] = ']',
        ['{'] = '}',
        ['`'] = '`',
        -- python strings (working)
        ["b'"] = "'",
        ["f'"] = "'",
        ["r'"] = "'",
        -- python strings (error)
        ['b"'] = '"',
        ['f"'] = '"',
        ['r"'] = '"',
    }
})

I get error when typing f".
E116: Invalid arguments for function MPairs.autopairs("f"",""")

Btw, is there any way to configure extra autoclose rules based on filetype?

attempt to call field 'autopairs_cr' (a nil value)

The sample mapping with nvim-compe gives the error attempt to call field 'autopairs_cr' (a nil value) when CR is pressed and it types a zero instead.

2021-Apr-18

This is the config (taken from the README)

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

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

vim.g.completion_confirm_key = ""
MUtils.completion_confirm=function()
  if vim.fn.pumvisible() ~= 0  then
    if vim.fn.complete_info()["selected"] ~= -1 then
      return vim.fn["compe#confirm"](npairs.esc("<c-r>"))
    else
      return npairs.esc("<cr>")
    end
  else
    return npairs.autopairs_cr()
  end
end


remap('i' , '<CR>','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true})

Jump Over to Next Line if Closing Brackets Is On Next Line

What happens:

Before        Input         After
------------------------------------
{                             {
    |           }                 }|
}                             }
------------------------------------

What I Want to Happen:

Before        Input         After
------------------------------------
{                             {
    |           }                 
}                             }|
------------------------------------

Is there any way to achieve this?

Help with configuration

Hello,

I'm having an issue that is quite annoying and I don't know how to solve it. This is what happens:

update(set|))
// input: (
update(set(|))
// input: )
update(set()|)
// input: backspace
update(set(|)
//input: backspace
update(set|

autopairs is constantly eating my closing brackets, and I don't know why, and I don't know how to configure it so it doesn't do that. Note that the count of the parens is not always supposed to make sense, in the example I included a closing parens that pairs with an opening parens earlier in a previous line (as maybe that's relevant with how the plugin counts).

What I'd want is that when I open a parens, it also inserts the closing parens, especially if the next parens in the line is already paired with an opening one.

Here's an example of a good behavior I'd like

update(set|))
// input: (
update(set(|)))

and

update(set()|))
// input: backspace
update(set|))

or

update(set()|))
// input: backspace
update(set(|))
// input: backspace
update(set|))

Let me know if that doesn't make sense, or if this maybe isn't the plugin to do this with

Help with tab?

So this doesn't really have anything to do with autopairs really but I am using nvim-autopairs's esc function to try and write a mapping for tab and shift-tab with completion-nvim and vsnip and was hoping you could help me and/or are maybe using something similar for your personal neovim setup.

This is what I am using. Whe

MUtils.tab=function()
    if vim.fn.pumvisible() ~= 0  then
        return npairs.esc("<C-n>")
    else
        if vim.fn["vsnip#available"](1) ~= 0 then
            return npairs.esc("<Plug>(vsnip-expand-or-jump)")
        else
            return npairs.esc("<Tab>")
        end
    end
end

MUtils.s_tab=function()
    if vim.fn.pumvisible() ~= 0  then
        return npairs.esc("<C-p>")
    else
        if vim.fn["vsnip#jumpable"](-1) ~= 0 then
            return npairs.esc("<Plug>(vsnip-jump-prev)")
        else
            return npairs.esc("<C-h>")
        end
    end
end

The plugin requires tree-sitter

If Treesitter is not installed, then the plugin gives an error:

изображение

Is it possible to turn this dependency into optional? I have a minimal configuration that allows me to use Neovim to edit text fields in browser (using Firenvim) and do not need Treesitter for it.

Print Message "You need to install treesitter"

Hey, thank you so much for the great work on this plugin. It's fast and works quite seamlessly. I just had one question: I seem to be getting a print message whenever i open up nvim saying "you need to install treesitter". Treesitter is of course already installed (i use packer.nvim). Even after running :PackerSync and reloading, I still get this message.

I checked the code and I see where the error is coming from and I am confused why this line is not returning ok for me. Any ideas?

This is the file where i configure nvim-autopairs:

local npairs = require('nvim-autopairs')
local Rule = require('nvim-autopairs.rule')

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

vim.g.completion_confirm_key = ""
MUtils.completion_confirm=function()
  if vim.fn.pumvisible() ~= 0  then
    if vim.fn.complete_info()["selected"] ~= -1 then
      return vim.fn["compe#confirm"](npairs.esc("<cr>"))
    else
      return npairs.esc("<cr>")
    end
  else
    return npairs.autopairs_cr()
  end
end


require("nvim-autopairs.completion.compe").setup({
  map_cr = true, --  map <CR> on insert mode
  map_complete = true -- it will auto insert `(` after select function or method item
})

npairs.setup({
    check_ts = true,
    ts_config = {
        lua = {'string'},-- it will not add pair on that treesitter node
        javascript = {'template_string'},
        java = false,-- don't check treesitter on java
    }
})

dot command doesn't repeat the pairs

When I am in insert mode, and I write something which has some pairs: (test)

And then press the . command to repeat. It will only repeat what is inside the pair: test without the parenthesis.

Is this the desired behaviour? Have I messed up something in my config? Thanks in advance.

Endwise rules incorrect indents

There seems to be an issue with indent-levels for endwise-rules:

Example rules:

npairs.clear_rules()
npairs.add_rules({
  endwise('then$', 'end', 'lua', nil)
})

Before:
image

After:
image

It seems to consistently indent the matching end one level too little.

I can't get sample Rule to work

Hello, thanks for your plugin.

I'm trying to define a rule for a specific filetype but the sample code doesn't work

npairs.add_rules({
  Rule("$", "$",{"tex", "latex"})
    -- don't add a pair if  the previous character is xxx
    :with_pair(cond.not_before_regex_check("xxx", 3))
     }
)

The only way I can get it to work is to leave off the filetype.

Here is my full configuration

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

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

vim.g.completion_confirm_key = ""
MUtils.completion_confirm=function()
  if vim.fn.pumvisible() ~= 0  then
    if vim.fn.complete_info()["selected"] ~= -1 then
      return vim.fn["compe#confirm"](npairs.esc("<cr>"))
    else
      return npairs.esc("<cr>")
    end
  else
    return npairs.autopairs_cr()
  end
end


remap('i' , '<CR>','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true})


-- you can use some builtin condition
local cond = require('nvim-autopairs.conds')
require('nvim-autopairs').clear_rules()  -- <=== I clear the default rules on purpose.  This is the only way my defined rule works


npairs.add_rules({
  Rule("(", ")")
    :with_pair(cond.not_before_regex_check("\\", 1))  -- <== I only want this rule to apply on latex files; adding {"tex","latex","plaintex"}  to the argument list had no effect.  It just made the rule not apply.
  }
)

Quotes within other pairs are not properly skipped

When pressing any of the keys ',`," within any other pairs, a single symbol is added rather than skipping the closing symbol

For example, starting at this position where the cursor is |
("|")

Expected behavior:
(""|)

Actual behavior:
(""|")

This is with default configuration settings

Pairs associated to more than a single character

First of all, thanks for creating this nifty plugin.

I am trying to set up the pair ['``'] = "''". These is how LaTeX understands double quotation. The pairing partially works, but instead of getting |'' as it should, I get '|'. The cursor enters between the second single quotes. This may be due to the previous default pairing ["'"] = "'", but I would hope that autopairs is smarter than that.

On the same topic, for LaTeX purposes I would also like to setup paring like \( is paired to \) - this is math mode. I tried ['\\('] = '\\)', it is does not seem to work. The single space does not work either. Any ideas there?

Adding spacing rules?

Do you think it is suitable to add the following rules to basic.lua?

Rule(' ', ' '):with_pair(function (opts)
  local pair = opts.line:sub(opts.col, opts.col+2)
  return vim.tbl_contains({'()', '[]', '{}'}, pair)
end)

So we have the following

before insert after
(|) ( |) ( | )
{|} { |} { | }
[|] [ |] [ | ]

`disable_filetypes` not working

I just noticed that the pairs are appearing inside the telescope prompt even if they are disabled. I am using the plugin with default options.

2021-05-15.14-41-22.mp4

[Bug] Since latest update (30 mins ago) a bunch of text appears when Nvim autopairs is enabled.

I'm experiencing the bug with both my config and the minimal config:

Peek.2021-06-01.21-22.mp4

Minimal config:

require('nvim-autopairs').setup()

My config:

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

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

vim.g.completion_confirm_key = ""
MUtils.completion_confirm=function()
  if vim.fn.pumvisible() ~= 0  then
    if vim.fn.complete_info()["selected"] ~= -1 then
      return vim.fn["compe#confirm"](npairs.esc("<cr>"))
    else
      return npairs.esc("<cr>")
    end
  else
    return npairs.autopairs_cr()
  end
end


remap('i' , '<CR>','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true})

npairs.setup({
	disable_filetype = { "TelescopePrompt" },
	ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]],"%s+", ""),
	enable_moveright = true,
	enable_afterquote = true,  -- add bracket pairs after quote
	enable_check_bracket_line = true,  --- check bracket in same line
	check_ts = true,
	ts_config = {
		lua = {'string'},-- it will not add pair on that treesitter node
		javascript = {'template_string'},
		java = false,-- don't check treesitter on java
	}
})

require('nvim-treesitter.configs').setup {
    autopairs = {enable = true}
}

local ts_conds = require('nvim-autopairs.ts-conds')


-- press % => %% is only inside comment or string
npairs.add_rules({
  Rule("%", "%", "lua")
    :with_pair(ts_conds.is_ts_node({'string','comment'})),
  Rule("$", "$", "lua")
    :with_pair(ts_conds.is_not_ts_node({'function'}))
})

Using both configs I get [kind of] the same output. Why? I don't really know why, maybe you added some sort of verbosity?

Hope this gets solved soon. Thanks!

Edit: indeed latest update is broken(?), using this commit:

use {"windwp/nvim-autopairs", opt = true, commit = "b5816204bd2f92f1c64dff132fbd67a1530c1751"}

Works as expected

does not work with nvim-compe

thanks for this awesome plugin!
i am using nvim-compe for various reasons.

the provided example in the readme lacks of one "end" statement. correction:

MUtils.completion_confirm = function()
  if vim.fn.pumvisible() ~= 0  then
    if vim.fn.complete_info()["selected"] ~= -1 then
      vim.fn["compe#confirm"]()
      return npairs.esc("<c-y>")
    else
      vim.defer_fn(function()
        vim.fn["compe#confirm"]()
      end, 20)
      return npairs.esc("<c-n>")
    end
  else
    return npairs.check_break_line_char()
  end
end

even though i fixed syntax error, the plugin does not work with compe and i am not able to fix that :S
i setup auto-pairs after i setup compe.
what i type {<enter> what i get:

{
    <cursor>

the closing bracket is missing :S

any help appreciated !

/edit
i am ... damn! after switching to compe i forgot the setup() function for autopairs when copy pasting...
it is working now as expected and i will open a PR for the syntax fix :)

break line rule only works for curly braces

Hi there, I think it would be better if break line rule works for all of matching pairs. Currently it only works for {}.

const obj = {
  |
} // this works
const arr = [
|] // this doesn't

note: | is cursor

Newline inside pairs

It would be very nice if there was an option to let the carriage return space things out properly for stuff like functions, etc. For instance, pressing enter in

int main() {|}

would yield

int main() {
    |
}

where | is the location of the cursor. Thanks for your work on this plugin!

Nested block issue where closing pair is not generated.

My apologies for the bad title, but unsure how to accurately name this situation. However, when you open a block, hit new line and attempt to open another where the closing for the first is directly after your cursor the closing pair isn't generated.

An example

val x = {
|}

At the | if I was to enter { I'd expect the following:

val x = {
{|}}`

However, I'm instead left with the following:

val x = {
{|}`

Note that this doesn't happen if there is a space between the cursor and the closing pair.

val x = {
| }

The example above would correctly produce:

val x = {
{|} }

consistent variable names?

Is there any reason to name the first 2 option as camelCase and the rest of them snake_case? I think it would be better if it has the same naming convention.

Single `'` not skipped when in another pair.

As a follow-up to #3, I believe it has only been fixed for " and not for '. For example, given then following:

api.nvim_set_var('test_thing|')

Expectation

api.nvim_set_var('test_thing'|)

Reality

api.nvim_set_var('test_thing'|')

How to get indentation too?

I am newbie to this plugin. So, without nvim-autopairs,it works like this.

int functionname() {
    |

with , nvim-autopairs , i get this,

int functionname() {
|}

I wanted this.

int functionname() {
    |
}

Please help me. I want closing brace to be on next line

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.