-
-
Notifications
You must be signed in to change notification settings - Fork 119
Custom rules
Stanislav Asunkin edited this page Jul 23, 2021
·
29 revisions
This page contains custom rules, that can be added to your neovim config using add_rules
method.
Before | Insert | After |
---|---|---|
(|) |
space |
( | ) |
( | ) |
) |
( )| |
local npairs = require'nvim-autopairs'
local Rule = require'nvim-autopairs.rule'
npairs.add_rules {
Rule(' ', ' ')
:with_pair(function (opts)
local pair = opts.line:sub(opts.col, opts.col + 1)
return vim.tbl_contains({ '()', '[]', '{}' }, pair)
end),
Rule('( ', ' )')
:with_pair(function() return false end)
:with_move(function(opts)
return opts.prev_char:match('.%)') ~= nil
end)
:use_key(')'),
Rule('{ ', ' }')
:with_pair(function() return false end)
:with_move(function(opts)
return opts.prev_char:match('.%}') ~= nil
end)
:use_key('}'),
Rule('[ ', ' ]')
:with_pair(function() return false end)
:with_move(function(opts)
return opts.prev_char:match('.%]') ~= nil
end)
:use_key(']')
}
local npairs = require'nvim-autopairs'
local Rule = require'nvim-autopairs.rule'
local cond = require'nvim-autopairs.conds'
npairs.add_rules {
Rule(' ', ' ')
:with_pair(function(opts)
local pair = opts.line:sub(opts.col, opts.col + 1)
return vim.tbl_contains({ '()', '{}', '[]' }, pair)
end)
:with_move(cond.none())
:with_cr(cond.none())
:with_del(function(opts)
local col = vim.api.nvim_win_get_cursor(0)[2]
local context = opts.line:sub(col - 1, col + 2)
return vim.tbl_contains({ '( )', '{ }', '[ ]' }, context)
end),
Rule('', ' )')
:with_pair(cond.none())
:with_move(function(opts) return opts.char == ')' end)
:with_cr(cond.none())
:with_del(cond.none())
:use_key(')'),
Rule('', ' }')
:with_pair(cond.none())
:with_move(function(opts) return opts.char == '}' end)
:with_cr(cond.none())
:with_del(cond.none())
:use_key('}'),
Rule('', ' ]')
:with_pair(cond.none())
:with_move(function(opts) return opts.char == ']' end)
:with_cr(cond.none())
:with_del(cond.none())
:use_key(']'),
}
npairs.clear_rules()
npairs.add_rules {
Rule('{', '}')
:end_wise(function() return true end),
Rule('(', ')')
:end_wise(function() return true end),
Rule('[', ']')
:end_wise(function() return true end),
}